netsted template validate type error
[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 os
15 import sys
16
17 from toscaparser.tosca_template import ToscaTemplate
18 from toscaparser.utils.gettextutils import _
19 import toscaparser.utils.urlutils
20
21 """
22 CLI entry point to show how TOSCA Parser can be used programmatically
23
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.
27
28 It can be used as,
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>
32
33 e.g.
34 #tosca-parser
35  --template-file=toscaparser/tests/data/tosca_helloworld.yaml
36 #tosca-parser
37  --template-file=toscaparser/tests/data/CSAR/csar_hello_world.zip
38 """
39
40
41 class ParserShell(object):
42
43     def _validate(self, args):
44         if len(args) < 1:
45             msg = _('The program requires a template or a CSAR file as an '
46                     'argument. Please refer to the usage documentation.')
47             raise ValueError(msg)
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.')
51             raise ValueError(msg)
52
53     def main(self, args):
54         self._validate(args)
55         path = args[0].split('--template-file=')[1]
56         if os.path.isfile(path):
57             self.parse(path)
58         elif toscaparser.utils.urlutils.UrlUtils.validate_url(path):
59             self.parse(path, False)
60         else:
61             raise ValueError(_('"%(path)s" is not a valid file.')
62                              % {'path': path})
63
64     def parse(self, path, a_file=True):
65         output = None
66         tosca = ToscaTemplate(path, None, a_file)
67
68         version = tosca.version
69         if tosca.version:
70             print ("\nversion: " + version)
71
72         if hasattr(tosca, 'description'):
73             description = tosca.description
74             if description:
75                 print ("\ndescription: " + description)
76
77         if hasattr(tosca, 'inputs'):
78             inputs = tosca.inputs
79             if inputs:
80                 print ("\ninputs:")
81                 for input in inputs:
82                     print ("\t" + input.name)
83
84         if hasattr(tosca, 'nodetemplates'):
85             nodetemplates = tosca.nodetemplates
86             if nodetemplates:
87                 print ("\nnodetemplates:")
88                 for node in nodetemplates:
89                     print ("\t" + node.name)
90
91         if hasattr(tosca, 'outputs'):
92             outputs = tosca.outputs
93             if outputs:
94                 print ("\noutputs:")
95                 for output in outputs:
96                     print ("\t" + output.name)
97
98
99 def main(args=None):
100     if args is None:
101         args = sys.argv[1:]
102     ParserShell().main(args)
103
104
105 if __name__ == '__main__':
106     main()