def write_on_file(write, tree): etree.indent(write) tree.write('BPSIM_output.xml', xml_declaration=True, encoding='UTF-8', method='xml', pretty_print=True)
def convert(args, outfile, xt_state=None, xt_error=None, outext=None, line_size=None, line_style=None, opacity=None): """convert multiple gpx into a geojson, kml or gpx and ouput.""" tree = merge(args) if xt_state != '0': decimate(tree, xt_error) n_point = count_track_point(tree) if outext == '.geojson': geojson = togeojson(tree, line_size, line_style, opacity) s = json.dumps(geojson, ensure_ascii=False, separators=(',', ':')) else: if outext == '.kml': tree = tokml(tree, line_size, opacity) ET.indent(tree, space='', level=0) s = ET.tostring(tree.getroot(), encoding='unicode') if outfile is None: print(s) # FIXME: in win10, output in sjis else: with codecs.open(outfile, 'w', 'utf-8') as f: f.write(s) f.write('\n') return n_point
def convert_md_to_tei(md_file, xml_file, plain: bool, lines: bool): M = Markdown(extensions=['attr_list', TEI(), 'markdown_del_ins']) with open(md_file, 'r', encoding='utf-8') as file: md = file.read() md = preprocess_md(md) markup = M.convert(md) markup = postprocess_markup(markup) parser = et.XMLParser(remove_blank_text=True, encoding='UTF-8') xml = et.fromstring(markup, parser) xml = postprocess_xml(xml) xml = xml.getroottree() if plain: xml.write(xml_file, encoding='utf-8') elif lines: xml_str = et.tostring(xml, encoding='unicode') xml_str = xml_str.replace('\n', '') xml_str = xml_str.replace('<pb', '\n<pb') xml_str = xml_str.replace('<lb', '\n <lb') with open(xml_file, 'w', encoding='utf-8') as f: f.write(xml_str) else: et.indent(xml, ' ') xml.write(xml_file, encoding='utf-8', pretty_print=True)
def stop(self): etree.indent(self.loggermessagefile) pretty_xml = etree.tostring(self.loggermessagefile, pretty_print=True, encoding='unicode') self.fh.write(pretty_xml) self.fh.close()
def get_plex_current_users(token): uri = 'http://192.168.1.180:32400/status/sessions?X-Plex-Token={}'.format( token) try: _verbose_print('INFO: calling {} with a {} second timeout'.format( uri, _TIME_OUT)) response = requests.get(uri, timeout=_TIME_OUT) response.raise_for_status() _verbose_print('INFO: body of request\n{}'.format( response.text.strip())) _verbose_print('INFO: response.headers: {}'.format( pprint.pformat(response.headers, compact=True))) if 'Content-Type' not in response.headers or response.headers[ 'Content-Type'].find('xml') == -1: print( 'ERROR: the body of the http response was not xml as expected', file=sys.stderr) return None _verbose_print('INFO: treating response body as XML') root = etree.fromstring(response.text.strip().encode('utf-8')) etree.indent(root, level=0) _verbose_print('INFO: XML response body\n{}'.format( etree.tostring(root, pretty_print=True).decode('utf-8'))) return _find_users(root) except Exception as e: print('ERROR: Exception raised while calling or processing GET {}'. format(uri), file=sys.stderr) print('ERROR: {}'.format(e), file=sys.stderr) return None
def close(self): etree.indent(self.paramfile) pretty_xml = etree.tostring(self.paramfile, pretty_print=True, encoding='unicode') self.f.write(pretty_xml) self.f.close()
def xml_delete_tag(xmltree, xpath, occurrences=None): """ Deletes a xml tag in an xmletree. :param xmltree: an xmltree that represents inp.xml :param xpath: a path to the tag to be deleted :param occurrences: int or list of int. Which occurence of the parent nodes to create a tag. By default all nodes are used. :returns: xmltree with deleted tag """ from masci_tools.io.common_functions import is_sequence root = xmltree.getroot() nodes = eval_xpath(root, xpath, list_return=True) if occurrences is not None: if not is_sequence(occurrences): occurrences = [occurrences] try: nodes = [nodes[occ] for occ in occurrences] except IndexError as exc: raise ValueError('Wrong value for occurrences') from exc for node in nodes: parent = node.getparent() parent.remove(node) etree.indent(xmltree) return xmltree
def update_toolconf(self, ourdir, ourxml): # path is relative to tools localconf = "./local_tool_conf.xml" self.run_rsync(self.tool_conf_path, localconf) tree = ET.parse(localconf) root = tree.getroot() hasTF = False TFsection = None for e in root.findall("section"): if e.attrib["name"] == self.our_name: hasTF = True TFsection = e if not hasTF: TFsection = ET.Element("section") root.insert(0, TFsection) # at the top! our_tools = TFsection.findall("tool") conf_tools = [x.attrib["file"] for x in our_tools] for xml in ourxml: # may be > 1 if xml not in conf_tools: # new ET.SubElement(TFsection, "tool", {"file": xml}) ET.indent(tree) newconf = f"{self.tool_id}_conf" tree.write(newconf, pretty_print=True) self.run_rsync(newconf, self.tool_conf_path) if False and self.args.packages and self.args.packages > "": self.install_deps()
def generate_lrt_S(lrt_file, csv_file, logger_name): tree = etree.parse(lrt_file) root = tree.getroot() logger_name.info(f"Total entries: {len(root)}") count = 0 for row in tqdm(csv_file, desc="Adding entries", leave=False): number, tgrp, as_cluster = row[0], row[3], row[4] route_elem = etree.SubElement(root, "route") user_elem = etree.SubElement(route_elem, "user") next_elem = etree.SubElement(route_elem, "next") user_elem.set("type", "E164") next_elem.set("type", "regex") user_elem.text = number next_elem.text = f"!(^.*)$!sip:\\1;key={tgrp}@{as_cluster}Cluster!" count += 1 time.sleep(0.05) etree.indent(tree) xml_data = etree.tostring( tree, pretty_print=True, xml_declaration=True, encoding="UTF-8", standalone=True, ) with open(lrt_file, "wb") as f: f.write(xml_data) logger_name.info(f"Entries added: {count}") logger_name.info(f"New total entries: {len(root)}")
def segment_document(doc): """ For each lines and paragraphs, this function segments the text and add new <seg> elements. :param doc: a XML document :return: XML doc with segmented text :rtype: a new XLM doc """ # Only the text enclosed between <p> and <l> is segmented. paragraphs = doc.xpath('//tei:text//tei:p', namespaces=ns) lines = doc.xpath('//tei:text//tei:l', namespaces=ns) segment_elements(paragraphs) segment_elements(lines) # Used to normalize the text. if args.normalize: segs = doc.xpath('//tei:seg', namespaces=ns) normalize(segs) # Used to lemmatize the text. if args.lemmatize: # Only the original text is lemmatized. orig = doc.xpath('//tei:orig', namespaces=ns) lemmatize(orig) # Indent() inserts tail whitespace for pretty-printing an XML tree. etree.indent(doc) return doc.write("Segmented_" + str(args.file), pretty_print=True, encoding="utf-8", method="xml")
def save(self, filePath): tree = XMLElementTree(self.toXML()) indent(tree, space=" ") tree.write(filePath, xml_declaration=True, encoding="utf-8", pretty_print=True)
def _validate_expanded(self, tool: GalaxyToolXmlDocument) -> List[Diagnostic]: """Validates the document after loading all the macros referenced and expands them. Args: document (Document): [description] xml_tree (etree.ElementTree): [description] Returns: List[Diagnostic]: [description] """ try: expanded_tool_tree, _ = xml_macros.load_with_references(tool.path) expanded_xml = remove_macros(expanded_tool_tree) root = expanded_xml.getroot() etree.indent(root, space=DEFAULT_INDENTATION) content = etree.tostring(root, pretty_print=True, encoding=str) formatted_xml = etree.fromstring(content) self.xsd_schema.assertValid(formatted_xml) return [] except etree.DocumentInvalid as e: diagnostics = self._build_diagnostics_for_expanded_macros(tool, e) return diagnostics except etree.XMLSyntaxError as e: return self._build_diagnostics_for_macros_file_syntax_error( tool, e)
def save(self, filePath, setAllReferences=True): if setAllReferences: self.setAllReferences() tree = XMLElementTree(self.toXML()) indent(tree, space=" ") tree.write(filePath, xml_declaration=True, encoding="utf-8", pretty_print=True)
def to_level_3(doc): """ For each lines and paragraphs, this function segments, lemmatizes and normalizes the text. :param doc: a XML document :return: a level 3 XML document :rtype: a new XLM document """ # Only the text enclosed between <p> and <l> is segmented. paragraphs = doc.xpath('//tei:text//tei:p', namespaces=ns) lines = doc.xpath('//tei:text//tei:l', namespaces=ns) segment_elements(paragraphs) segment_elements(lines) # Used to lemmatize the text. if args.lemmatize: # If the text is only to be lemmatized, segs are lemmatized. tags = doc.xpath('//tei:seg', namespaces=ns) lemmatize(tags) # Indent() inserts tail whitespace for pretty-printing an XML tree. etree.indent(doc) # This output file is specific to the project e-ditiones, you can easily change the output with e.g. doc.write("New" + args.file, ...) return doc.write(args.file.replace("-2", "-3"), pretty_print=True, encoding="utf-8", method="xml")
def xml_replace_tag(xmltree, xpath, newelement, occurrences=None): """ replaces xml tags by another tag on an xmletree in place :param xmltree: an xmltree that represents inp.xml :param xpath: a path to the tag to be replaced :param newelement: a new tag :param occurrences: int or list of int. Which occurence of the parent nodes to create a tag. By default all nodes are used. :returns: xmltree with replaced tag """ import copy from masci_tools.io.common_functions import is_sequence root = xmltree.getroot() nodes = eval_xpath(root, xpath, list_return=True) if occurrences is not None: if not is_sequence(occurrences): occurrences = [occurrences] try: nodes = [nodes[occ] for occ in occurrences] except IndexError as exc: raise ValueError('Wrong value for occurrences') from exc for node in nodes: parent = node.getparent() index = parent.index(node) parent.remove(node) parent.insert(index, copy.deepcopy(newelement)) etree.indent(xmltree) return xmltree
def write(self, app, filename: str = "scene.xml"): # export self.app = app self.factory = Factory() # header xmlParent = ET.Element('HelioK') xmlParent.set('version', '2020.2') xmlParent.set('unitLength', self.unitLength) xmlParent.set('unitAngle', self.unitAngle) # main self.writeWorld(xmlParent, app.world) self.writeFactory(xmlParent, app.factory) xmlScene = ET.SubElement(xmlParent, 'Scene') for n in app.scene.nodes: self.writeNode(xmlScene, n) # print if ET.LXML_VERSION >= (4, 5): ET.indent(xmlParent, space="\t") xmlString = ET.tostring(xmlParent, xml_declaration=True, encoding='UTF-8') with open(filename, "wb") as f: f.write(xmlString)
def generate_lrt_B(lrt_file, csv_file, logger_name): tree = etree.parse(lrt_file) root = tree.getroot() logger_name.info(f"Total entries: {len(root)}") count = 0 for row in tqdm(csv_file, desc="Adding entries", leave=False): number = row[0] lst_carrier = CARRIERS.get(row[5]) tgrp, tcontext, fqdn = lst_carrier[0], lst_carrier[1], lst_carrier[2] route_elem = etree.SubElement(root, "route") user_elem = etree.SubElement(route_elem, "user") next_elem = etree.SubElement(route_elem, "next") user_elem.set("type", "E164") next_elem.set("type", "regex") user_elem.text = number next_elem.text = f"!(^.*)$!sip:\\1;tgrp={tgrp};trunk-context={tcontext}@{fqdn}!" count += 1 time.sleep(0.05) etree.indent(tree) xml_data = etree.tostring( tree, pretty_print=True, xml_declaration=True, encoding="UTF-8", standalone=True, ) with open(lrt_file, "wb") as f: f.write(xml_data) logger_name.info(f"Entries added: {count}") logger_name.info(f"New total entries: {len(root)}")
def updateFile(file, root): etree.indent(root, "\t") temp = file + ".temp" with open(temp, 'wb') as outfile: root.write(outfile, pretty_print=True, xml_declaration=True, encoding="utf-8") os.remove(file) os.rename(temp, file) print(f"updated {file}\n")
def write_xml(): etree.indent(ROOT, space='\t') et = etree.ElementTree(ROOT) #et.write('Package.xml',xml_declaration=True,encoding='UTF-8',standalone=True) return etree.tostring(ROOT, xml_declaration=True, encoding='UTF-8', standalone=True)
def tostring(self, xml_declaration=False): """Serialize back to XML. The XML Declaration is optional and can be controlled by `xml_declaration`""" doc = etree.ElementTree(self._element) etree.indent(doc, space=" ") return serialize_xml_for_salesforce(doc, xml_declaration=xml_declaration)
def to_string(self, root: etree.Element) -> str: """Turns an XML element into a pretty string. :param root: the XML element to turn into a string :return: the XML element as a pretty string using 4 spaces as indentation """ etree.indent(root, " " * 4) return etree.tostring(root, encoding="utf-8", method="xml", pretty_print=True).decode("utf-8")
def verify_xml(data): try: root = et.XML(data) if root != None: et.indent(root) et.ElementTree(root) return True except et.XMLSyntaxError: return False
def to_xml(self): """Returns self.root lxml.etree. Returns ------- lxml.etree """ etree.indent(self.root, space=" ") return self.root
def pretty_xml(text, encoding=None): """ Add indent to the XML text """ from lxml import etree encoding = encoding or Config.encoding root = etree.fromstring(text) etree.indent(root) return etree.tostring(root).decode(encoding)
def _reserialize(tree, indent=True): new_tree = deepcopy(tree) serialized = etree.tostring(new_tree, encoding="utf-8").decode("utf-8") unserialized = etree.fromstring(serialized, parser=etree.XMLParser(recover=True)) if indent: etree.indent(unserialized, space=" ", level=0) return unserialized
def write_xml_element(root_element: etree._Element, path: Path, indent: bool = True) -> None: if indent: etree.indent(root_element, space=INDENT) with open(path, "w", encoding=Encoding.UTF_8.value) as f: f.write( etree.tostring(root_element, encoding=str, doctype=get_xml_header(Encoding.UTF_8.value)))
def main(): global g_verbose stringsFiles = [] addMissing = False doSave = False doCompare = False doReplace = False doRemove = False doStats = False try: pairs, rest = getopt.getopt(sys.argv[1:], "acf:il:rsv%") for option, value in pairs: if option == '-a': addMissing = True elif option == '-c': doCompare = True elif option == '-i': doSave = True elif option == '-f': stringsFiles.append(value) elif option == '-l': stringsFiles.append(langFileFor(value)) elif option == '-v': g_verbose = True elif option == '-r': doRemove = True elif option == '-s': doStats = True elif option == '-%': doReplace = True else: usage() except: usage() pairs = mk_xml.getStrings(STRINGS_FILE, False) # Build list of files to work on if 0 == len(stringsFiles): for subdir, dirs, files in os.walk('res_src'): for file in [file for file in files if file == "strings.xml"]: stringsFiles.append( "%s/%s" % (subdir, file) ) parser = etree.XMLParser(remove_blank_text=True, encoding="utf-8") for path in stringsFiles: print('looking at', path) doc = etree.parse(path, parser) # checkAgainst( doc, pairs ) if doReplace: replacePcts( doc ) if addMissing: doAddMissing( doc ) if doCompare: compare( pairs, path ) if doRemove: removeNotInEnglish( doc ) # print stats after any other changes have been made if doStats: printStats( doc ) if doSave: try: etree.indent(doc, space=' ') except: print('unable to use indent(); check formatting') out = open( path, "wb" ) out.write( etree.tostring( doc, pretty_print=True, encoding="utf-8", xml_declaration=True ) )
def save_to_file(root_node, out_path): # Make a new document tree doc = etree.ElementTree(root_node) etree.indent(doc, space=" ") # outstr = etree.tostring(doc) # Save to XML file with open(out_path, 'wb') as outFile: doc.write(outFile, xml_declaration=True, encoding='utf-8', pretty_print=True)
def compare_asts(*asts): etrees = set() for ast in asts: with open(ast, "rb") as f: ast_data = f.read() ast_data = ast_data.decode('utf-8').encode('ascii') tree = etree.XML(ast_data) remove_line_numers(tree) etree.indent(tree, space="") etrees.add(etree.tostring(tree)) return len(etrees) == 1
def format_content(self, content: str, tabSize: int = 4) -> str: """Formats the given XML content.""" try: parser = etree.XMLParser(strip_cdata=False) xml = etree.fromstring(content, parser=parser) spaces = " " * tabSize etree.indent(xml, space=spaces) result = etree.tostring(xml, pretty_print=True, encoding=str) return result except etree.XMLSyntaxError: return content # Do not auto-format if there are syntax errors