1 # Licensed under the Apache License, Version 2.0 (the "License");
2 # you may not use this file except in compliance with the License.
3 # You may obtain 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,
9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
18 from lxml import etree
21 def tosca_translator(file_name):
22 if os.path.exists(file_name):
24 file_format = var.split('.')
25 if len(file_format) == 2:
26 if file_format[1] == 'yaml' or file_format[1] == 'yang':
27 print("Its a yaml file")
28 os.system('pyang -f yin '+var + ' -o ' + file_format[0]+'.xml')
29 elif file_format[1] == 'xml':
30 print ("Its a "+file_format[1]+" file")
33 print ("File format not supported exiting script.")
36 print ("File format not supported exiting script.")
38 tree = etree.parse(file_format[0]+'.xml')
42 xslt_root = etree.parse('tosca_transformer.xslt')
44 transform = etree.XSLT(xslt_root)
46 result_tree = transform(doc)
47 output_file = file_format[0]+'_tosca.yaml'
49 f = open(output_file, "w")
50 f.write(str(result_tree))
52 print ("TOSCA file generated with name "+output_file)
54 print ("File does not exist, exiting the script")
56 if __name__ == '__main__':
57 parser = argparse.ArgumentParser(add_help=True)
58 parser.add_argument('-n', '--filename', dest='filename', required=True,
59 help='Please enter the YANG file name: ')
60 args = parser.parse_args()
61 file_name = str(args.filename)
62 tosca_translator(file_name)