Module convertConfXML2JSON
[hide private]
[frames] | no frames]

Source Code for Module convertConfXML2JSON

 1  #!/usr/bin/python 
 2  #coding:utf8 
 3  from __future__ import with_statement 
 4  import os, sys, json 
 5   
 6  __author__ = "Jérôme Kieffer" 
 7   
 8  sys.path.append(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "src")) 
 9  from XSDataCommon import XSConfiguration 
10  from EDConfiguration import bestType 
11   
12 -def convert(infile, outfile):
13 dico = {"__extend__":[]} 14 xml = XSConfiguration.parseFile(infile) 15 for other in xml.XSImportConfiguration: 16 print type(other.directory) 17 if other.directory not in [None, "None"]: 18 dico["__extend__"].append(os.path.join(other.directory, other.name)) 19 else: 20 dico["__extend__"].append(other.name) 21 22 xsPluginList = xml.getXSPluginList() 23 if xsPluginList is not None: 24 for pluginItem in xsPluginList.getXSPluginItem(): 25 plugin_conf = {} 26 plugin_name = pluginItem.name 27 paramList = pluginItem.getXSParamList() 28 if paramList: 29 for paramItem in paramList.getXSParamItem(): 30 plugin_conf[paramItem.name] = bestType(paramItem.value) 31 dico[plugin_name] = plugin_conf 32 with open(outfile, "w") as f: 33 f.write(json.dumps(dico, indent=4))
34 35 if __name__ == "__main__": 36 if len(sys.argv) > 1: 37 infiles = sys.argv[1:] 38 else: 39 print("Usage: convert a XML configuration to a JSON one") 40 sys.exit(1) 41 for infile in infiles: 42 outfile = os.path.splitext(infile)[0] + ".json" 43 if not os.path.isfile(infile): 44 print("No such input file %s" % infile) 45 continue 46 if os.path.isfile(outfile): 47 print("Output file %s exist: skipping" % outfile) 48 continue 49 print("Converting %s \t-->\t%s" % (infile, outfile)) 50 convert(infile, outfile) 51