def tree_preprocess(dom_tree): element = XML(dom_tree) for e in list(element[0]): element.append(e) del element[0] tempfile = TemporaryFile(suffix='.xml') ElementTree(element).write(tempfile) tempfile.seek(0) return tempfile
def _handle_sync(begin, lines:list): newtag = XML("<null/>").makeelement("Sync", {}) diff = len(lines) #Will be used if no endsync tag is found for j in range(begin+1, len(lines)): if lines[j].replace(" ",'') == "endsync:": diff = j - begin+1 break if lines[j].lstrip()[0] == "#": continue sm_tagline = lines[j].lstrip().split(" ") attr, prop = sm_tagline[0].split(".") value = sm_tagline[-1] chtag = newtag.makeelement(attr, {prop:value}) newtag.append(chtag) return newtag, diff
def generate_full_xml(vorsatz, fields, nutzdaten_ticket="default_nutzdaten_ticket", empfaenger="9198", th_fields=_TEST_TH_FIELDS): """Generates the full XML for the given `vorsatz` and `fields`. In a first step the <Nutzdaten> part is generated before the ERiC library is called for generating the proper <TransferHeader>. """ # Generate the content bits ET.register_namespace('', "http://www.elster.de/elsterxml/schema/v11") nutzdaten = generate_xml_nutzdaten(vorsatz, fields, nutzdaten_ticket, empfaenger) # Load into ELSTER base XML base_xml = XML(_BASE_XML) datenteil_xml = Element('DatenTeil') datenteil_xml.append(nutzdaten) base_xml.append(datenteil_xml) # Generate TransferHeader using ERiC xml_string = _pretty(base_xml, remove_decl=False) eric = EricApi(debug=False) try: eric.initialise() xml_string_with_th = eric.create_th( xml_string, datenart=th_fields.datenart, testmerker=th_fields.testmerker, herstellerId=th_fields.herstellerId, datenLieferant=th_fields.datenLieferant) finally: eric.shutdown() return xml_string_with_th.decode()
def convert(string, out=None): root = XML("<StickMan></StickMan>") lines = string.splitlines() lines = [l for l in lines if l.replace(" ","")] last_speed = 1 i = 0 while i<len(lines): line = lines[i].lstrip() if not line.replace(" ",""): i+=i if line[0] == "#": i+=1 continue if ":" in line: parts = list(map(lambda x:x.replace(" ",""),line.split(":"))) if parts[0] == "sync": newtag, diff = _handle_sync(i, lines) root.append(newtag) i+=diff elif parts[0] == "speed": newtag = root.makeelement("speed", {"speed":parts[1]}) root.append(newtag) i+=1 elif parts[0] == "flip": newtag = root.makeelement("flip", {}) root.append(newtag) i+=1 elif parts[0] == "loop": newtag = root.makeelement("Loop", {"n":parts[1]}) k = i+1 while k<len(lines): if lines[k].replace(" ","") == "endloop:": i+=k+1 break if lines[k].replace(" ","") == "sync:": elem, diff= _handle_sync(k, lines) newtag.append(elem) k+=diff else: sp = lines[k].lstrip().split(" ") attr, prop = sp[0].split(".") value = sp[-1] ch = root.makeelement(attr, {prop:value}) newtag.append(ch) root.append(newtag) else: spl = lines[i].lstrip().split(" ") attr, prop = spl[0].split(".") value = spl[-1] ch = root.makeelement(attr, {prop:value}) root.append(ch) i+=1 xml_output = tostring(root, "unicode") HEADER = f''' <!--Autogenerated by animToXML.py in minified form. For now there is no option for specifying whether to output in minified form or not but this might change in the future-->''' if out: import os if os.path.exists(out): res = input(f"Do you want to overwrite file '{out}'?[y/n]\n") if res.lower() == "y": open(out, "w").write(HEADER+xml_output) elif res.lower() == "n": print("Failed to write to output file!") else: open(out, "w").write(xml_output) return xml_output