fix package init name for nfv-toscaparser
[parser.git] / tosca2heat / tosca-parser / toscaparser / shell.py
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         # example of retrieving policy object
94         '''if hasattr(tosca, 'policies'):
95             policies = tosca.policies
96             if policies:
97                 print("policies:")
98                 for policy in policies:
99                     print("\t" + policy.name)
100                     if policy.triggers:
101                         print("\ttriggers:")
102                         for trigger in policy.triggers:
103                             print("\ttrigger name:" + trigger.name)'''
104
105         if hasattr(tosca, 'outputs'):
106             outputs = tosca.outputs
107             if outputs:
108                 print("\noutputs:")
109                 for output in outputs:
110                     print("\t" + output.name)
111
112
113 def main(args=None):
114     if args is None:
115         args = sys.argv[1:]
116     ParserShell().main(args)
117
118
119 if __name__ == '__main__':
120     main()