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
5 # http://www.apache.org/licenses/LICENSE-2.0
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
18 from toscaparser.tosca_template import ToscaTemplate
19 from toscaparser.utils.gettextutils import _
20 import toscaparser.utils.urlutils
23 CLI entry point to show how TOSCA Parser can be used programmatically
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.
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>
36 --template-file=toscaparser/tests/data/tosca_helloworld.yaml
38 --template-file=toscaparser/tests/data/CSAR/csar_hello_world.zip
42 class ParserShell(object):
44 def get_parser(self, argv):
45 parser = argparse.ArgumentParser(prog="tosca-parser")
47 parser.add_argument('--template-file',
50 help=_('YAML template or CSAR file to parse.'))
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):
60 elif toscaparser.utils.urlutils.UrlUtils.validate_url(path):
61 self.parse(path, False)
63 raise ValueError(_('"%(path)s" is not a valid file.')
66 def parse(self, path, a_file=True):
68 tosca = ToscaTemplate(path, None, a_file)
70 version = tosca.version
72 print("\nversion: " + version)
74 if hasattr(tosca, 'description'):
75 description = tosca.description
77 print("\ndescription: " + description)
79 if hasattr(tosca, 'inputs'):
84 print("\t" + input.name)
86 if hasattr(tosca, 'nodetemplates'):
87 nodetemplates = tosca.nodetemplates
89 print("\nnodetemplates:")
90 for node in nodetemplates:
91 print("\t" + node.name)
93 # example of retrieving policy object
94 '''if hasattr(tosca, 'policies'):
95 policies = tosca.policies
98 for policy in policies:
99 print("\t" + policy.name)
102 for trigger in policy.triggers:
103 print("\ttrigger name:" + trigger.name)'''
105 if hasattr(tosca, 'outputs'):
106 outputs = tosca.outputs
109 for output in outputs:
110 print("\t" + output.name)
116 ParserShell().main(args)
119 if __name__ == '__main__':