def main(): absolute_folder = sys.argv[1] pattern = '*.iml' fileList = [] # Walk through directory for dName, sdName, fList in os.walk(absolute_folder): for fileName in fList: if fnmatch.fnmatch(fileName, pattern): # Match search string fileList.append(os.path.join(dName, fileName)) pbar = ProgressBar(widgets=['Processing :', Percentage(), ' ', Bar(), ' ', ETA()], maxval=len(fileList)).start() fcount = 0 for fileName in fileList: output_dict = generate_empty_dict() eclipse_file_path = os.path.dirname(fileName)+'/.classpath' with open(fileName, 'r') as f: intellij_data = f.read() if not intellij_data: pass intellij_dict = xmltodict.parse(intellij_data) fcount = fcount + 1 # print(intellij_dict) output_dict = addSrcType(intellij_dict, output_dict) output_dict = addCombinedRules(intellij_dict, output_dict) output_dict = addConType(intellij_dict, output_dict) # print json.dumps(intellij_dict) result = bf.etree(output_dict, root=Element('classpath')) #print tostring(result) with open(eclipse_file_path, 'w') as f: data = tostring(result, doctype='<?xml version="1.0" encoding="UTF-8"?>') data = data.replace('<classpath>','') data = data.replace('</classpath>', '') data = data.replace('<?xml version="1.0" encoding="UTF-8"?>', '<?xml version="1.0" encoding="UTF-8"?><classpath>') data = data +'</classpath>' f.write(data) # Add .project file project_path = os.path.dirname(fileName)+'/.project' xml_data = """<?xml version="1.0" encoding="UTF-8"?> <projectDescription> <name>%s</name> <comment/> <projects/> <buildSpec> <buildCommand> <name>org.eclipse.jdt.core.javabuilder</name> <arguments/> </buildCommand> </buildSpec> <natures> <nature>org.eclipse.jdt.core.javanature</nature> </natures> </projectDescription>""" root_name = os.path.splitext(os.path.basename(fileName))[0] xml_data = xml_data%(root_name) with open(project_path, 'w') as f: f.write(xml_data) pbar.update(fcount) pbar.finish()
def get_filter_xml(rpc): with open(rpc['rpc_template'], 'r') as file: data = file.read().replace('\n', '') data_dic = xmltodict.parse(data) element = bf.etree(data_dic)[0] return element
if __name__ == '__main__': base = etree.fromstring(open('TransactionGroup.xml', 'r').read()) obj = bf.data(base) # template t = obj['Project']['Groups']['GroupConfig']['Property'][0]['ItemConfig'][0] filepath = 'TransactionGroup.csv' csv_data = DictReader(open(filepath)) items = list() for row in csv_data: t['@name'] = row['name'] for i in range(len(t['Property'])): if t['Property'][i]['@name'] == 'TARGET_DATA_TYPE': if is_int(row['datatype']): dt = row['datatype'] elif row['datatype'] in datatypes.keys(): dt = datatypes[row['datatype']] else: print("Error with datatype on row: " + row['name']) dt = t['Property'][i]['$'] t['Property'][i]['$'] = dt if t['Property'][i]['@name'] == 'OPCITEMPATH': t['Property'][i]['$'] = row['opcitempath'] if t['Property'][i]['@name'] == 'TARGET_NAME': t['Property'][i]['$'] = row['target'] items.append(deepcopy(t)) obj['Project']['Groups']['GroupConfig']['Property'][0][ 'ItemConfig'] = items with open('TransactionGroupOutput.xml', 'wb') as f: f.write(etree.tostring(bf.etree(obj)[0], pretty_print=True))
def JsonParseXML(json): result = bf.etree(json, root=Element('root')) return tostring(result)
from netconf import client import xmltodict from xmljson import badgerfish as bf from lxml import etree with open('tests/testdata_wrong.xml', 'r') as file: data = file.read().replace('\n', '') jsondata = xmltodict.parse(data) etreeX = bf.etree(jsondata) print(type(etreeX[0])) newconf = etreeX[0] session = client.NetconfSSHSession("localhost", "8300", "admin", "admin") edit_config_response = session.edit_config(newconf=data) print(etree.tostring(edit_config_response, pretty_print=True)) session.close()