c11c0ad9e75050a1fb5058c7035e962d8a5facb4
[sdc/sdc-distribution-client.git] /
1 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
2 #    not use this file except in compliance with the License. You may obtain
3 #    a copy of the License at
4 #
5 #         http://www.apache.org/licenses/LICENSE-2.0
6 #
7 #    Unless required by applicable law or agreed to in writing, software
8 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 #    License for the specific language governing permissions and limitations
11 #    under the License.
12
13
14 import argparse
15 import os
16 import sys
17
18 from toscaparser.tosca_template import ToscaTemplate
19 from toscaparser.utils.gettextutils import _
20 import toscaparser.utils.urlutils
21
22 """
23 CLI entry point to show how TOSCA Parser can be used programmatically
24
25 This is a basic command line utility showing the entry point in the
26 TOSCA Parser and how to iterate over parsed template. It can be extended
27 or modified to fit an individual need.
28
29 It can be used as,
30 #tosca-parser --template-file=<path to the YAML template>
31 #tosca-parser --template-file=<path to the CSAR zip file>
32 #tosca-parser --template-file=<URL to the template or CSAR>
33
34 e.g.
35 #tosca-parser
36  --template-file=toscaparser/tests/data/tosca_helloworld.yaml
37 #tosca-parser
38  --template-file=toscaparser/tests/data/CSAR/csar_hello_world.zip
39 """
40
41
42 class ParserShell(object):
43
44     def get_parser(self, argv):
45         parser = argparse.ArgumentParser(prog="tosca-parser")
46
47         parser.add_argument('--template-file',
48                             metavar='<filename>',
49                             required=True,
50                             help=_('YAML template or CSAR file to parse.'))
51
52         return parser
53
54     def main(self, argv):
55         parser = self.get_parser(argv)
56         (args, extra_args) = parser.parse_known_args(argv)
57         path = args.template_file
58         if os.path.isfile(path):
59             self.parse(path)
60         elif toscaparser.utils.urlutils.UrlUtils.validate_url(path):
61             self.parse(path, False)
62         else:
63             raise ValueError(_('"%(path)s" is not a valid file.')
64                              % {'path': path})
65
66     def parse(self, path, a_file=True):
67         output = None
68         tosca = ToscaTemplate(path, None, a_file)
69
70         version = tosca.version
71         if tosca.version:
72             print("\nversion: " + version)
73
74         if hasattr(tosca, 'description'):
75             description = tosca.description
76             if description:
77                 print("\ndescription: " + description)
78
79         if hasattr(tosca, 'inputs'):
80             inputs = tosca.inputs
81             if inputs:
82                 print("\ninputs:")
83                 for input in inputs:
84                     print("\t" + input.name)
85
86         if hasattr(tosca, 'nodetemplates'):
87             nodetemplates = tosca.nodetemplates
88             if nodetemplates:
89                 print("\nnodetemplates:")
90                 for node in nodetemplates:
91                     print("\t" + node.name)
92
93         if hasattr(tosca, 'outputs'):
94             outputs = tosca.outputs
95             if outputs:
96                 print("\noutputs:")
97                 for output in outputs:
98                     print("\t" + output.name)
99
100
101 def main(args=None):
102     if args is None:
103         args = sys.argv[1:]
104     ParserShell().main(args)
105
106
107 if __name__ == '__main__':
108     main()