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
17 from toscaparser.tosca_template import ToscaTemplate
18 from toscaparser.utils.gettextutils import _
19 import toscaparser.utils.urlutils
22 CLI entry point to show how TOSCA Parser can be used programmatically
24 This is a basic command line utility showing the entry point in the
25 TOSCA Parser and how to iterate over parsed template. It can be extended
26 or modified to fit an individual need.
29 #tosca-parser --template-file=<path to the YAML template>
30 #tosca-parser --template-file=<path to the CSAR zip file>
31 #tosca-parser --template-file=<URL to the template or CSAR>
35 --template-file=toscaparser/tests/data/tosca_helloworld.yaml
37 --template-file=toscaparser/tests/data/CSAR/csar_hello_world.zip
41 class ParserShell(object):
43 def _validate(self, args):
45 msg = _('The program requires a template or a CSAR file as an '
46 'argument. Please refer to the usage documentation.')
48 if "--template-file=" not in args[0]:
49 msg = _('The program expects "--template-file" as the first '
50 'argument. Please refer to the usage documentation.')
55 path = args[0].split('--template-file=')[1]
56 if os.path.isfile(path):
58 elif toscaparser.utils.urlutils.UrlUtils.validate_url(path):
59 self.parse(path, False)
61 raise ValueError(_('"%(path)s" is not a valid file.')
64 def parse(self, path, a_file=True):
66 tosca = ToscaTemplate(path, None, a_file)
68 version = tosca.version
70 print ("\nversion: " + version)
72 if hasattr(tosca, 'description'):
73 description = tosca.description
75 print ("\ndescription: " + description)
77 if hasattr(tosca, 'inputs'):
82 print ("\t" + input.name)
84 if hasattr(tosca, 'nodetemplates'):
85 nodetemplates = tosca.nodetemplates
87 print ("\nnodetemplates:")
88 for node in nodetemplates:
89 print ("\t" + node.name)
91 if hasattr(tosca, 'outputs'):
92 outputs = tosca.outputs
95 for output in outputs:
96 print ("\t" + output.name)
102 ParserShell().main(args)
105 if __name__ == '__main__':