2 # Licensed under the Apache License, Version 2.0 (the "License"); you may
3 # not use this file except in compliance with the License. You may obtain
4 # a copy of the License at
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 # License for the specific language governing permissions and limitations
14 # Copyright 2016 RIFT.io Inc
24 from rift
.mano
.yang_translator
.common
.utils
import _
25 from rift
.mano
.yang_translator
.rwmano
.yang_translator
import YangTranslator
29 Test the yang translation from command line as:
31 --template-file=<path to the JSON template or tar.gz>
32 --template-type=<type of template e.g. yang>
33 --parameters="purpose=test"
34 --output_dir=<output directory>
36 Takes four user arguments,
37 1. type of translation (e.g. yang) (required)
38 2. Path to the file that needs to be translated (required)
39 3. Input parameters (optional)
40 4. Write to output files in a dir (optional), else print on screen
42 In order to use translator to only validate template,
43 without actual translation, pass --validate-only along with
44 other required arguments.
49 class TranslatorShell(object):
51 SUPPORTED_TYPES
= ['yang']
52 COPY_DIRS
= ['images']
53 SUPPORTED_INPUTS
= (TAR
, JSON
, XML
, YAML
) = ('tar', 'json', 'xml', 'yaml')
55 def _parse_args(self
, raw_args
=None):
56 parser
= argparse
.ArgumentParser(
57 description
='RIFT.io YANG translator for descriptors')
64 help="Template file to translate")
69 help="Directory to output")
72 help="Input parameters")
75 help="Create a ZIP archive",
79 help="Enable debug logging",
82 args
= parser
.parse_args(raw_args
)
84 args
= parser
.parse_args()
87 def main(self
, raw_args
=None, log
=None):
88 args
= self
._parse
_args
(raw_args
)
91 logging
.basicConfig(level
=logging
.DEBUG
)
93 logging
.basicConfig(level
=logging
.ERROR
)
94 log
= logging
.getLogger("yang-translator")
96 log
.debug(_("Args passed is {}").format(args
))
100 for f
in args
.template_file
:
101 path
= os
.path
.abspath(f
[0])
102 if not os
.path
.isfile(path
):
103 msg
= _("The path %(path)s is not a valid file.") % {
106 raise ValueError(msg
)
108 ftype
= self
._get
_file
_type
(path
)
109 if self
.ftype
is None:
111 elif self
.ftype
!= ftype
:
112 msg
= (_("All input files hould be of same type"))
114 raise ValueError(msg
)
115 self
.in_files
.append(path
)
117 self
.log
.debug(_("Input files are of type {0}").
121 self
._translate
(output_dir
=args
.output_dir
,
122 archive
=args
.archive
)
124 def _translate(self
, output_dir
=None, archive
=False):
126 self
.log
.debug(_('Loading the yang template for {0}.').
127 format(self
.in_files
))
128 translator
= YangTranslator(self
.log
, files
=self
.in_files
)
129 self
.log
.debug(_('Translating the yang template for {0}.').
130 format(self
.in_files
))
131 output
= translator
.translate()
134 translator
.write_output(output
,
135 output_dir
=output_dir
,
138 for key
in output
.keys():
139 print(_("TOSCA Template {0}:\n{1}").
140 format(key
, output
[key
]))
142 self
.log
.error(_("Did not get any translated output!!"))
145 def _get_file_type(self
, path
):
146 m
= magic
.open(magic
.MAGIC_MIME
)
149 if typ
.startswith('text/plain'):
152 # On Fedora 20, it return x-gzip, while on Ubuntu 16 it is gzip
153 elif typ
.startswith('application/x-gzip') or \
154 typ
.startswith('application/gzip') :
157 msg
= _("The file {0} is not a supported type: {1}"). \
160 raise ValueError(msg
)
163 def main(args
=None, log
=None):
164 TranslatorShell().main(raw_args
=args
, log
=log
)
167 if __name__
== '__main__':