Coverage for backpack/config/tool.py: 25%

24 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-30 23:12 +0000

1''' The ``config.tool`` module defines a simple command-line interface (CLI) program that can be 

2used as a development tool. It creates snippets that you can copy-paste into the Panorama 

3application project files like graph.json, or package.json of the business logic package. 

4''' 

5from typing import Mapping, Any 

6import sys 

7import argparse, json, textwrap 

8from .config import ConfigBase 

9 

10def cli(name: str, config: ConfigBase) -> None: 

11 ''' Creates a simple command line interface for printing config snippets. 

12 

13 Args: 

14 name (str): The name of your Panorama app 

15 config (ConfigBase): Instance of your configuration structure, subclass of ConfigBase. 

16 ''' 

17 def render_template(template_path: str, variables: Mapping[str, Any]): 

18 try: 

19 from jinja2 import Environment, FileSystemLoader 

20 except ImportError: 

21 sys.exit('To use the render command you must install jinja2 with "pip install Jinja2"') 

22 env = Environment(loader=FileSystemLoader('/'), trim_blocks=True) 

23 env.filters['to_pretty_json'] = lambda value: json.dumps(value, indent=4) 

24 template = env.get_template(template_path) 

25 return template.render(variables) 

26 

27 parser = argparse.ArgumentParser(description=textwrap.dedent( 

28 f'''\ 

29 Configuration snippet generator for {name} application. 

30 

31 This program can generate json and markdown snippets that you can copy-paste to the 

32 metadata and package definitions of your AWS Panorama project. The snippets contain the 

33 definitions of the application parameters in the required format. The following formats 

34 are supported: 

35 

36 - nodes: generates a json snippet to be pasted in the nodeGraph.nodes field of graph.json 

37 - edges: generates a json snippet to be pasted in the nodeGraph.edges field of graph.json. 

38 Specify the code node name. 

39 - interface: generates json a snippet to be pasted in nodePackage.interfaces field of the 

40 package.json of the application code package 

41 - markdown: generates a markdown snippet that you can paste to the README of your project, 

42 or other parts of the documentation. 

43 - render: renders a Jinja2 template. Specify the template filename and the code node name. 

44 '''), 

45 formatter_class=argparse.RawDescriptionHelpFormatter 

46 ) 

47 function_map = { 

48 'nodes' : lambda config, _: ( 

49 print(json.dumps(config.get_panorama_definitions(), indent=4)) 

50 ), 

51 'edges' : lambda config, kwargs: ( 

52 print(json.dumps(config.get_panorama_edges(kwargs['code_node']), indent=4)) 

53 ), 

54 'interface': lambda config, _: ( 

55 print(json.dumps(config.get_panorama_app_interface(), indent=4)) 

56 ), 

57 'markdown': ( 

58 lambda config, _: print(config.get_panorama_markdown_doc()) 

59 ), 

60 'render': lambda config, kwargs: ( 

61 print(render_template(kwargs['template'], { 

62 'nodes': config.get_panorama_definitions(), 

63 'edges': config.get_panorama_edges(kwargs['code_node']), 

64 'interface': config.get_panorama_app_interface(), 

65 'markdown': config.get_panorama_markdown_doc() 

66 })) 

67 ) 

68 } 

69 

70 parser.add_argument('command', choices=function_map.keys(), 

71 help='Prints configuration snippets for graph.json nodes, edges, application ' 

72 'interface in package.json, or in markdown format.' 

73 ) 

74 parser.add_argument('--code-node', '-c', type=str, default='code_node', 

75 help='Code node name (used in edges snippet)' 

76 ) 

77 parser.add_argument('--template', '-t', type=str, 

78 help='Template file (used in render command)' 

79 ) 

80 

81 args = parser.parse_args() 

82 func = function_map[args.command] 

83 func(config, {'code_node': args.code_node, 'template': args.template}) 

84 

85if __name__=='__main__': 

86 cli('config_base', ConfigBase())