# coding: utf-8 from lxml import etree from os import listdir from os.path import join, isdir #update_attrs = {'family': {'hidden': 'False', 'mode': 'normal'}, # 'variable': {'auto_freeze': 'False', # 'auto_save': 'False', # 'disabled': 'False', # 'hidden': 'False', # 'mandatory': 'False', # 'mode': 'normal', # 'multi': 'False', # 'remove_check': 'False', # 'remove_condition': 'False'}, # 'param': {'hidden': 'True'}, # 'separator': {'never_hidden': 'False'} # } #remove_attrs = {'param': ['optional'], # 'variable': ['redefine', 'exists']} #update_attrs = {'check': {'level': 'error'}} #remove_attrs = {'variable': ['remove_condition']} update_attrs = {'param': ['pouet']} remove_attrs = {} remove_tags = () root_dir = '../flattener_dicos' def xml_parse_document(document): for child in document: if not isinstance(child.tag, str): continue #if child.tag in remove_tags: # etree.SubElement(document, 'containers') # print list(child) # #document.remove(child) # continue if child.tag in update_attrs: if child.attrib.get('type') == None: child.attrib['type'] = 'string' #for key, value in update_attrs[child.tag].items(): # if key not in child.attrib: # child.attrib[key] = value if child.tag in remove_attrs: for key in remove_attrs[child.tag]: if key in child.attrib: del(child.attrib[key]) #print(child.tag) #print(child.attrib) #if child.text is None: # text = None #else: # text = child.text.strip() #if text: # print(text) if list(child) != []: ret = xml_parse_document(child) def edit(xmlfile): print('process {}'.format(xmlfile)) document = etree.parse(xmlfile).getroot() xml_parse_document(document) with file(xmlfile, 'w') as fh: fh.write(etree.tostring(document, pretty_print=True, encoding="UTF-8")) def main(): for xmldir in listdir(root_dir): xmldir = join(root_dir, xmldir) resultdir = join(xmldir, 'result') #if isdir(xmldir): # for dico in listdir(xmldir): # if dico.endswith('.xml'): # edit(join(xmldir, dico)) if isdir(resultdir): for dico in listdir(resultdir): if dico.endswith('.xml'): edit(join(resultdir, dico)) if __name__ == "__main__": main()