我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用lxml.etree.tostring()。
def test_351(self): """SDNTB-351 regression test unbound namespaces must be removed from attributes """ article = copy.deepcopy(self.article) article['abstract'] = "" article['body_html'] = """\ <p class="BrdtekstInnrykk">Målet i kortbane-VM som nylig ble avsluttet i den canadiske byen Windsor var personlig rekord på <st1:metricconverter productid="1500 meter" w:st="on">1500 meter</st1:metricconverter><br></p> """ expected = b"""\ <body.content><p class="lead" lede="true" /><p class="txt-ind">Målet i kortbane-VM som nylig ble avsluttet i den canadiske byen Windsor var personlig rekord på 1500 meter</p><p class="txt">footer text</p><media media-type="image" class="illustrasjonsbilde"><media-referenc e mime-type="image/jpeg" source="test_id" /><media-caption>test feature media</media-caption></media> </body.content> """.replace(b'\n', b'').replace(b' ', b'') formatter_output = self.formatter.format(article, {'name': 'Test NTBNITF'}) doc = formatter_output[0]['encoded_item'] nitf_xml = etree.fromstring(doc) body_content = nitf_xml.find("body/body.content") self.assertEqual(etree.tostring(body_content).replace(b'\n', b'').replace(b' ', b''), expected)
def test_body_none(self): article = copy.deepcopy(self.article) article['body_html'] = None formatter_output = self.formatter.format(article, {'name': 'Test NTBNITF'}) # the test will raise an exception during self.formatter.format if SDNTB-420 bug is still present # but we also check that body.content is there doc = formatter_output[0]['encoded_item'] nitf_xml = etree.fromstring(doc) expected = (""" <body.content> <p class="lead" lede="true">This is the abstract</p> <p class="txt">footer text</p> <media media-type="image" class="illustrasjonsbilde"> <media-reference mime-type="image/jpeg" source="test_id"/> <media-caption>test feature media</media-caption> </media> </body.content>""").replace('\n', '').replace(' ', '') content = etree.tostring(nitf_xml.find('body/body.content'), encoding="unicode").replace('\n', '').replace(' ', '') self.assertEqual(content, expected)
def update(self, docs, commitwithin=None): """Post list of docs to Solr, return URL and status. Opptionall tell Solr to "commitwithin" that many milliseconds.""" url = self.url + '/update' add_xml = etree.Element('add') if commitwithin is not None: add_xml.set('commitWithin', str(commitwithin)) for doc in docs: xdoc = etree.SubElement(add_xml, 'doc') for key, value in doc.iteritems(): if value: field = etree.Element('field', name=key) field.text = (value if isinstance(value, unicode) else str(value)) xdoc.append(field) request = urllib2.Request(url) request.add_header('Content-Type', 'text/xml; charset=utf-8') request.add_data(etree.tostring(add_xml, pretty_print=True)) response = urllib2.urlopen(request).read() status = etree.XML(response).findtext('lst/int') return url, status
def xpathOne(self,xpath): ''' :param xpath: <class str|xpath expression> :function: xpath match one :return: <class Parser> ''' try: labels = self._html.xpath(xpath) except Exception as e: printText("[Error]parser.py Parser xpathOne:%s %s %s"%(e,xpath,self.url),logFile=self.logFile,color=self.color,debug=self.debug) return Parser(data='',url=self.url,logFile=self.logFile,color=self.color,debug=self.debug) if len(labels) > 0: label = labels[0] return Parser(data=etree.tostring(label,encoding="unicode",method="html"),url=self.url,logFile=self.logFile,color=self.color, debug=self.debug) if isinstance(label,etree._Element) else Parser(data=label,url=self.url,logFile=self.logFile,color=self.color,debug=self.debug) else: printText("[WARING]parser.py Parser xpathOne parse None:%s %s"%(xpath,self.url),logFile=self.logFile,color=self.color,debug=self.debug) return Parser(data='',url=self.url,logFile=self.logFile,color=self.color,debug=self.debug)
def xpathAll(self,xpath): ''' :param xpath: <class str|xpath expression> :function: xpath match all :return: [<class Parser>,<class Parser>...] ''' try: labels = self._html.xpath(xpath) except Exception as e: printText("[Error]parser.py Parser xpathAll:%s %s %s"%(e,xpath,self.url),logFile=self.logFile,color=self.color,debug=self.debug) return [] if len(labels)>0: return [Parser(data=etree.tostring(label,encoding="unicode",method="html"),url=self.url,logFile=self.logFile,color=self.color, debug=self.debug) if isinstance(label,etree._Element) else Parser(data=label,url=self.url,logFile=self.logFile,color=self.color, debug=self.debug) for label in labels] else: printText("[WARING]parser.py Parser xpathAll parse None:%s %s"%(xpath,self.url),logFile=self.logFile,color=self.color,debug=self.debug) return []
def set_bing_wallpaper(): r = requests.get(URL01) if r.status_code == 200: try: parser = etree.XMLParser(recover=True) xml = etree.XML(r.content, parser) print(etree.tostring(xml)) print('===========') image = xml.find('image') urlBase = image.find('urlBase') url = 'http://www.bing.com%s_1920x1200.jpg' % (urlBase.text) if download(url) is True: set_background(comun.POTD) print('===========') except Exception as e: print(e)
def get_fonts(self): """ create font dict by parsing first fontset """ if not self.xml_file or not self.xml_folders: return False self.fonts = {} for folder in self.xml_folders: paths = [os.path.join(self.path, folder, "Font.xml"), os.path.join(self.path, folder, "font.xml")] self.font_file = utils.check_paths(paths) if not self.font_file: return False self.fonts[folder] = [] root = utils.get_root_from_file(self.font_file) for node in root.find("fontset").findall("font"): font = {"name": node.find("name").text, "size": node.find("size").text, "line": node.sourceline, "content": ET.tostring(node, pretty_print=True, encoding="unicode"), "file": self.font_file, "filename": node.find("filename").text} self.fonts[folder].append(font)
def __getitem__(self, key): if key == "line": return self.line elif key == "type": return self.tag elif key == "name": return self.name elif key == "filename": return self.filename elif key == "file": return self.file elif key == "content": return ET.tostring(self.node, pretty_print=True, encoding="unicode") elif key == "length": return self.length return super().__getitem__(key)
def clean_markup(self, markup, parser=None): """Apply ``Cleaner`` to markup string or document and return a cleaned string or document.""" result_type = type(markup) if isinstance(markup, six.string_types): doc = fromstring(markup, parser=parser) else: doc = copy.deepcopy(markup) self(doc) if issubclass(result_type, six.binary_type): return tostring(doc, encoding='utf-8') elif issubclass(result_type, six.text_type): return tostring(doc, encoding='unicode') else: return doc #: A default Cleaner instance, which kills comments, processing instructions, script tags, style tags.
def latest_content(url): ''' ?????????? Parameter -------- url:???? Return -------- string:????????? ''' try: html = lxml.html.parse(url) res = html.xpath('//div[@id=\"artibody\"]/p') if ct.PY3: sarr = [etree.tostring(node).decode('utf-8') for node in res] else: sarr = [etree.tostring(node) for node in res] sarr = ''.join(sarr).replace(' ', '')#.replace('\n\n', '\n'). html_content = lxml.html.fromstring(sarr) content = html_content.text_content() return content except Exception as er: print(str(er))
def _guba_content(url): try: html = lxml.html.parse(url) res = html.xpath('//div[@class=\"ilt_p\"]/p') if ct.PY3: sarr = [etree.tostring(node).decode('utf-8') for node in res] else: sarr = [etree.tostring(node) for node in res] sarr = ''.join(sarr).replace(' ', '')#.replace('\n\n', '\n'). html_content = lxml.html.fromstring(sarr) content = html_content.text_content() ptime = html.xpath('//div[@class=\"fl_left iltp_time\"]/span/text()')[0] rcounts = html.xpath('//div[@class=\"fl_right iltp_span\"]/span[2]/text()')[0] reg = re.compile(r'\((.*?)\)') rcounts = reg.findall(rcounts)[0] return [content, ptime, rcounts] except Exception: return ['', '', '0']
def debug_page(): headers = { 'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:31.0) Gecko/20100101 Firefox/31.0' } url = 'http://m.qfang.com/guangzhou/rent/100001468?gardenId=1109818' r = requests.get(url=url, headers=headers) #r.encoding='gbk' print r.status_code print type(r.content) print r.content #print chardet.detect(r) tree = etree.HTML(r.text,parser=etree.HTMLParser(encoding='utf-8')) #print etree.tostring(tree) return tree,r.text # ????????header??
def lxml_test(): url = "http://www.caixunzz.com" req = urllib2.Request(url=url) resp = urllib2.urlopen(req) #print resp.read() ''' parse_body=html.fromstring(resp.read()) href=parse_body.xpath('//a[@class="label"]/@href') print href #not working from above ''' tree = etree.HTML(resp.read()) href = tree.xpath('//a[@class="label"]/@href') #print href.tag for i in href: #print html.tostring(i) #print type(i) print i print type(href) #not working yet
def serialize_html_fragment(el, skip_outer=False): """ Serialize a single lxml element as HTML. The serialized form includes the elements tail. If skip_outer is true, then don't serialize the outermost tag """ assert not isinstance(el, basestring), ( "You should pass in an element, not a string like %r" % el) html = etree.tostring(el, method="html", encoding=_unicode) if skip_outer: # Get rid of the extra starting tag: html = html[html.find('>')+1:] # Get rid of the extra end tag: html = html[:html.rfind('<')] return html.strip() else: return html
def _get_config(self): """ get the config from the device and store it """ # TODO: fix this xml library conversion nonsense # get config sections # TODO: needs xml refactoring self.config_output['policies'] = ET.fromstring(letree.tostring(self.device.rpc.get_config( filter_xml=letree.XML('<configuration><security><policies></policies></security></configuration>'))))[0][0] # TODO: needs xml refactoring self.config_output['address_book'] = ET.fromstring(letree.tostring(self.device.rpc.get_config( filter_xml=letree.XML( '<configuration><security><address-book><global /></address-book></security></configuration>'))))[0][0] # TODO: needs xml refactoring self.config_output['output_junos_default'] = ET.fromstring(letree.tostring(self.device.rpc.get_config( filter_xml=letree.XML( '<configuration><groups><name>junos-defaults</name><applications></applications></groups></configuration>'))))[ 0].find('applications') # TODO: needs xml refactoring self.config_output['output_applications'] = ET.fromstring(letree.tostring(self.device.rpc.get_config( filter_xml=letree.XML('<configuration><applications></applications></configuration>'))))[0]
def sign_request(self): ''' Calculates the signature to the header of the SOAP request which can be used by the STS to verify that the SOAP message originated from a trusted service. ''' base_xml = etree.fromstring(self._xml_text) request_tree = _extract_element(base_xml, 'Body', {'SOAP-ENV': "http://schemas.xmlsoap.org/soap/envelope/"}) request = _canonicalize(etree.tostring(request_tree)) request_tree = _extract_element(base_xml, 'Timestamp', {'ns3': "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"}) timestamp = _canonicalize(etree.tostring(request_tree)) self._request_digest = _make_hash(request.encode(UTF_8)).decode(UTF_8) # pylint: disable=W0612 self._timestamp_digest = _make_hash(timestamp.encode(UTF_8)).decode(UTF_8) # pylint: disable=W0612 self._algorithm = SHA256 self._signed_info = _canonicalize(SIGNED_INFO_TEMPLATE % self.__dict__) self._signature_value = _sign(self._private_key, self._signed_info).decode(UTF_8) self._signature_text = _canonicalize(SIGNATURE_TEMPLATE % self.__dict__) self.embed_signature()
def xml_to_str(tree, encoding=None, xml_declaration=False): from xml.etree.ElementTree import ElementTree # tostring() returns bytecode unless encoding is 'unicode', and does not reliably produce an XML declaration. We # ALWAYS want bytecode so we can convert to unicode explicitly. if encoding is None: assert not xml_declaration if PY2: stream = io.BytesIO() else: stream = io.StringIO() encoding = 'unicode' else: stream = io.BytesIO() stream.write(('<?xml version="1.0" encoding="%s"?>' % encoding).encode(encoding)) ElementTree(tree).write(stream, encoding=encoding, xml_declaration=False, method='xml') return stream.getvalue()
def write_xml(input): """Writes Sublime Text snippets (Plist)""" from lxml import etree completions = input["completions"][0] data = etree.Element("snippet") content = etree.SubElement(data, "content") content.text = etree.CDATA(input["completions"][0]["contents"]) tabTrigger = etree.SubElement(data, "tabTrigger") tabTrigger.text = input["completions"][0]['trigger'] scope = etree.SubElement(data, "scope") scope.text = input["scope"] if 'description' in input['completions'][0]: description = etree.SubElement(data, "description") description.text = input["completions"][0]["description"] output = etree.tostring(data, pretty_print=True, encoding="utf-8").decode('utf-8') return output
def list_courses(self): ''' List courses available in Studio site ''' self.ensure_studio_site() url = "%s/home/" % self.BASE ret = self.ses.get(url) parser = etree.HTMLParser() xml = etree.parse(StringIO(ret.content), parser).getroot() courses = [] course_ids = [] for course in xml.findall('.//li[@class="course-item"]'): cid = course.get("data-course-key") if self.verbose: print cid # etree.tostring(course) courses.append(course) course_ids.append(cid) return {'xml': courses, 'course_ids': course_ids, }
def import_gemini_object(self, gemini_string): '''Imports the Gemini metadata into CKAN. The harvest_source_reference is an ID that the harvest_source uses for the metadata document. It is the same ID the Coupled Resources use to link dataset and service records. Some errors raise Exceptions. ''' log = logging.getLogger(__name__ + '.import') xml = etree.fromstring(gemini_string) valid, profile, errors = self._get_validator().is_valid(xml) if not valid: out = errors[0][0] + ':\n' + '\n'.join(e[0] for e in errors[1:]) log.error('Errors found for object with GUID %s:' % self.obj.guid) self._save_object_error(out,self.obj,'Import') unicode_gemini_string = etree.tostring(xml, encoding=unicode, pretty_print=True) # may raise Exception for errors package_dict = self.write_package_from_gemini_string(unicode_gemini_string)
def _transform_to_html(self, content, xslt_package=None, xslt_path=None): xslt_package = xslt_package or __name__ xslt_path = xslt_path or \ '../templates/ckanext/spatial/gemini2-html-stylesheet.xsl' # optimise -- read transform only once and compile rather # than at each request with resource_stream(xslt_package, xslt_path) as style: style_xml = etree.parse(style) transformer = etree.XSLT(style_xml) xml = etree.parse(StringIO(content.encode('utf-8'))) html = transformer(xml) response.headers['Content-Type'] = 'text/html; charset=utf-8' response.headers['Content-Length'] = len(content) result = etree.tostring(html, pretty_print=True) return result
def rpc_create_subscription(self, unused_session, rpc, *unused_params): logger.info("rpc_create-subscription") logger.debug("Session:%s", format(unused_session)) logger.debug("RPC received:%s", format(etree.tostring(rpc))) for param in unused_params: logger.debug("Param:" + etree.tostring(param)) unused_session.subscription_active = True return etree.Element("ok") # ********************************** # Setup SNMP # **********************************
def serialize(self, infile, root): """Serialize Element as XML file. Keyword arguments: infile -- a string for the path to the input file processed. root -- Element to be serialized as XML. """ ofile_name = os.path.splitext(os.path.basename(infile))[0] ofile_path = os.path.join(self.outdir, ofile_name+'.xml') xml = etree.tostring( root, encoding='utf-8', xml_declaration=True, pretty_print=True).decode('utf-8') with open(ofile_path, mode='w', encoding='utf-8') as ofile: ofile.write(xml) pass
def fixml_buy_now(self, ticker, quantity, limit): """Generates the FIXML for a buy order.""" fixml = Element("FIXML") fixml.set("xmlns", FIXML_NAMESPACE) order = SubElement(fixml, "Order") order.set("TmInForce", "0") # Day order order.set("Typ", "2") # Limit order.set("Side", "1") # Buy order.set("Px", "%.2f" % limit) # Limit price order.set("Acct", TRADEKING_ACCOUNT_NUMBER) instrmt = SubElement(order, "Instrmt") instrmt.set("SecTyp", "CS") # Common stock instrmt.set("Sym", ticker) ord_qty = SubElement(order, "OrdQty") ord_qty.set("Qty", str(quantity)) return tostring(fixml)
def fixml_sell_eod(self, ticker, quantity, limit): """Generates the FIXML for a sell order.""" fixml = Element("FIXML") fixml.set("xmlns", FIXML_NAMESPACE) order = SubElement(fixml, "Order") order.set("TmInForce", "7") # Market on close order.set("Typ", "2") # Limit order.set("Side", "2") # Sell order.set("Px", "%.2f" % limit) # Limit price order.set("Acct", TRADEKING_ACCOUNT_NUMBER) instrmt = SubElement(order, "Instrmt") instrmt.set("SecTyp", "CS") # Common stock instrmt.set("Sym", ticker) ord_qty = SubElement(order, "OrdQty") ord_qty.set("Qty", str(quantity)) return tostring(fixml)
def fixml_short_now(self, ticker, quantity, limit): """Generates the FIXML for a sell short order.""" fixml = Element("FIXML") fixml.set("xmlns", FIXML_NAMESPACE) order = SubElement(fixml, "Order") order.set("TmInForce", "0") # Day order order.set("Typ", "2") # Limit order.set("Side", "5") # Sell short order.set("Px", "%.2f" % limit) # Limit price order.set("Acct", TRADEKING_ACCOUNT_NUMBER) instrmt = SubElement(order, "Instrmt") instrmt.set("SecTyp", "CS") # Common stock instrmt.set("Sym", ticker) ord_qty = SubElement(order, "OrdQty") ord_qty.set("Qty", str(quantity)) return tostring(fixml)
def fixml_cover_eod(self, ticker, quantity, limit): """Generates the FIXML for a sell to cover order.""" fixml = Element("FIXML") fixml.set("xmlns", FIXML_NAMESPACE) order = SubElement(fixml, "Order") order.set("TmInForce", "7") # Market on close order.set("Typ", "2") # Limit order.set("Side", "1") # Buy order.set("Px", "%.2f" % limit) # Limit price order.set("AcctTyp", "5") # Cover order.set("Acct", TRADEKING_ACCOUNT_NUMBER) instrmt = SubElement(order, "Instrmt") instrmt.set("SecTyp", "CS") # Common stock instrmt.set("Sym", ticker) ord_qty = SubElement(order, "OrdQty") ord_qty.set("Qty", str(quantity)) return tostring(fixml)
def outer_html(self): """Get the html representation of the first selected element:: >>> d = PyQuery('<div><span class="red">toto</span> rocks</div>') >>> print(d('span')) <span class="red">toto</span> rocks >>> print(d('span').outer_html()) <span class="red">toto</span> >>> print(d('span').outerHtml()) <span class="red">toto</span> >>> S = PyQuery('<p>Only <b>me</b> & myself</p>') >>> print(S('b').outer_html()) <b>me</b> .. """ if not self: return None e0 = self[0] if e0.tail: e0 = deepcopy(e0) e0.tail = '' return etree.tostring(e0, encoding=unicode)
def setUp(self): super(TestViewSaving, self).setUp() self.arch = h.DIV( h.DIV( h.H3("Column 1"), h.UL( h.LI("Item 1"), h.LI("Item 2"), h.LI("Item 3"))), h.DIV( h.H3("Column 2"), h.UL( h.LI("Item 1"), h.LI(h.SPAN("My Company", attrs(model='res.company', id=1, field='name', type='char'))), h.LI(h.SPAN("+00 00 000 00 0 000", attrs(model='res.company', id=1, field='phone', type='char'))) )) ) self.view_id = self.registry('ir.ui.view').create(self.cr, self.uid, { 'name': "Test View", 'type': 'qweb', 'arch': ET.tostring(self.arch, encoding='utf-8').decode('utf-8') })
def show(self, display=True, validate=False): # apply modification if validate: tree = self.validate() else: inpxmlfile = self._original.get_file_abs_path('inp.xml') tree = etree.parse(inpxmlfile) tree = self.apply_modifications(tree, self._tasks) if display: xmltreestring = etree.tostring(tree, xml_declaration=True, pretty_print = True) print xmltreestring return tree #print self.apply_modifications(self._original.get_dict(), self._tasks)
def _extract_upnperror(self, err_xml): """ Extract the error code and error description from an error returned by the device. """ nsmap = {'s': list(err_xml.nsmap.values())[0]} fault_str = err_xml.findtext( 's:Body/s:Fault/faultstring', namespaces=nsmap) try: err = err_xml.xpath( 's:Body/s:Fault/detail/*[name()="%s"]' % fault_str, namespaces=nsmap)[0] except IndexError: msg = 'Tag with name of %r was not found in the error response.' % fault_str self._log.debug( msg + '\n' + etree.tostring(err_xml, pretty_print=True).decode('utf8')) raise SOAPProtocolError(msg) err_code = err.findtext('errorCode', namespaces=err.nsmap) err_desc = err.findtext('errorDescription', namespaces=err.nsmap) if err_code is None or err_desc is None: msg = 'Tags errorCode or errorDescription were not found in the error response.' self._log.debug( msg + '\n' + etree.tostring(err_xml, pretty_print=True).decode('utf8')) raise SOAPProtocolError(msg) return int(err_code), err_desc
def to_string(entry): return etree.tostring(entry, pretty_print=True)
def test_example(self): """ Tests the encoder based on an example. """ with open(os.path.join(os.path.dirname(__file__), 'example.om')) as f: xmlnode = etree.fromstring(f.read()) encoded = encode_xml(expected, 'om') print(etree.tostring(encoded, pretty_print=True).decode()) # and check that they are as expected self.assertTrue(elements_equal(encoded, xmlnode), "Encoding an OpenMath object")
def encode_bytes(obj, nsprefix=None): """ Encodes an OpenMath element into a string. :param obj: Object to encode as string. :type obj: OMAny :rtype: bytes """ node = encode_xml(obj, nsprefix) return etree.tostring(node)
def extract_html_macro(item, **kwargs): """ Delete from body_html all html tags except links """ if 'body_html' not in item: return None root = sd_etree.parse_html(item['body_html'], content='html') links = {} count = 0 # extract all links and add them to a dictionary with a unique # generated key for every link for a in root.findall('.//a'): links['__##link' + str(count) + '##__'] = etree.tostring(a, encoding="unicode") count = count + 1 # replace all text links with the generated keys # regenerate html back from root in order to avoid issues # on link replacements where are used text links generated from root body_html = etree.tostring(root, encoding="unicode") for link in links: body_html = body_html.replace(links[link], link) body_html = body_html.replace('<p>', '__##br##__') body_html = body_html.replace('</p>', '__##br##__') body_html = body_html.replace('<br/>', '__##br##__') # extract text from the html that don't contains any link, # it just contains link keys that are not affected by text extraction # because they are already text root = sd_etree.parse_html(body_html, content='html') body_html = etree.tostring(root, encoding="unicode", method="text") # in extracted text replace the link keys with links for link in links: body_html = body_html.replace(link, links[link]) body_html = body_html.replace('\n', '__##br##__') list_paragraph = body_html.split('__##br##__') item['body_html'] = ''.join('<p>' + p + '</p>' for p in list_paragraph if p and p.strip()) return item
def test_prefix_cleaning(self): """SDNTB-313 regression test""" article = copy.deepcopy(self.article) article['abstract'] = '' del article['associations'] article['body_html'] = "<pref:h1><other_pref:body.content><t:t/>toto</other_pref:body.content></pref:h1>" expected = (b'<body.content><p class="lead" lede="true" />toto<p class="txt">' b'footer text</p></body.content>') formatter_output = self.formatter.format(article, {'name': 'Test NTBNITF'}) doc = formatter_output[0]['encoded_item'] nitf_xml = etree.fromstring(doc) body_content = nitf_xml.find("body/body.content") self.assertEqual(b''.join(etree.tostring(body_content).split()), b''.join(expected.split()))
def commit(self, waitsearcher=False, waitflush=False): """Commit uncommitted changes to Solr immediately, without waiting.""" commit_xml = etree.Element('commit') commit_xml.set('waitFlush', str(waitflush)) commit_xml.set('waitSearcher', str(waitsearcher)) url = self.url + '/update' request = urllib2.Request(url) request.add_header('Content-Type', 'text/xml; charset=utf-8') request.add_data(etree.tostring(commit_xml, pretty_print=True)) response = urllib2.urlopen(request).read() status = etree.XML(response).findtext('lst/int') return url, status
def staged_to_string(self): """Convert the staging area to a list of strings. Returns: A list of string representing the configuration in the staging area. """ cfgs = [] for cfg in self.staged: if isinstance(cfg['config'], etree._Element): cfgs.append(etree.tostring(cfg['config'])) else: cfgs.append(cfg['config']) return cfgs
def __str__(self): pretty = etree.tostring(self._root, pretty_print=True, encoding='unicode') return pretty
def document(self): """ Return xml document as string for consumption """ # Back and forth to make sure our Template is valid XML root = etree.fromstring(self.ENVELOPE_TEMPLATE) xml = etree.tostring(root, pretty_print=True, encoding='unicode') return xml
def document(self): """ Return as string for consumption """ self._set_message_id() # Make sure to generate a fresh UUID xml = etree.tostring(self._root, pretty_print=True, encoding='unicode') return xml
def backup_osm(self): """Writes OSM data as-is.""" osm = etree.Element('osm', version='0.6', generator=TITLE) for osmel in self.osmdata.values(): el = osmel.to_xml() if osmel.osm_type != 'node': etree.SubElement(el, 'center', lat=str(osmel.lat), lon=str(osmel.lon)) osm.append(el) return ("<?xml version='1.0' encoding='utf-8'?>\n" + etree.tostring(osm, encoding='utf-8').decode('utf-8'))
def to_osc(self, josm=False): """Returns a string with osmChange or JOSM XML.""" osc = etree.Element('osm' if josm else 'osmChange', version='0.6', generator=TITLE) if josm: neg_id = -1 changeset = etree.SubElement(osc, 'changeset') ch_tags = { 'source': self.source, 'created_by': TITLE, 'type': 'import' } for k, v in ch_tags.items(): etree.SubElement(changeset, 'tag', k=k, v=v) for osmel in self.matched: if osmel.action is not None: el = osmel.to_xml() if josm: if osmel.action == 'create': el.set('id', str(neg_id)) neg_id -= 1 else: el.set('action', osmel.action) osc.append(el) else: etree.SubElement(osc, osmel.action).append(el) return ("<?xml version='1.0' encoding='utf-8'?>\n" + etree.tostring(osc, encoding='utf-8').decode('utf-8'))
def dumpReg(self, maxlen = 40, filehandle = sys.stdout): """Dump all the dictionaries constructed from the Registry object""" write('***************************************', file=filehandle) write(' ** Dumping Registry contents **', file=filehandle) write('***************************************', file=filehandle) write('// Types', file=filehandle) for name in self.typedict: tobj = self.typedict[name] write(' Type', name, '->', etree.tostring(tobj.elem)[0:maxlen], file=filehandle) write('// Groups', file=filehandle) for name in self.groupdict: gobj = self.groupdict[name] write(' Group', name, '->', etree.tostring(gobj.elem)[0:maxlen], file=filehandle) write('// Enums', file=filehandle) for name in self.enumdict: eobj = self.enumdict[name] write(' Enum', name, '->', etree.tostring(eobj.elem)[0:maxlen], file=filehandle) write('// Commands', file=filehandle) for name in self.cmddict: cobj = self.cmddict[name] write(' Command', name, '->', etree.tostring(cobj.elem)[0:maxlen], file=filehandle) write('// APIs', file=filehandle) for key in self.apidict: write(' API Version ', key, '->', etree.tostring(self.apidict[key].elem)[0:maxlen], file=filehandle) write('// Extensions', file=filehandle) for key in self.extdict: write(' Extension', key, '->', etree.tostring(self.extdict[key].elem)[0:maxlen], file=filehandle) # write('***************************************', file=filehandle) # write(' ** Dumping XML ElementTree **', file=filehandle) # write('***************************************', file=filehandle) # write(etree.tostring(self.tree.getroot(),pretty_print=True), file=filehandle) # # typename - name of type # required - boolean (to tag features as required or not)
def prettify(self, elem): """ Return a pretty-printed XML string for the Element. """ rough_string = ElementTree.tostring(elem, 'utf8') root = etree.fromstring(rough_string) return etree.tostring(root, pretty_print=True, encoding=ENCODE_METHOD).replace(" ".encode(), "\t".encode()) # minidom does not support UTF-8 '''reparsed = minidom.parseString(rough_string) return reparsed.toprettyxml(indent="\t", encoding=ENCODE_METHOD)'''
def fast_prettify(self, code): result = etree.tostring(etree.fromstring(code.encode(), parser=self.lxml_parser), pretty_print=True) return result.strip().decode()
def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False): """ Generate view dynamically using attributes stored on the product.template""" field_prefix = self._prefixes.get('field_prefix') custom_field_prefix = self._prefixes.get('custom_field_prefix') if view_type == 'form' and not view_id: view_ext_id = 'product_configurator.product_configurator_form' view_id = self.env.ref(view_ext_id).id res = super(ProductConfigurator, self).fields_view_get( view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu ) wizard_id = self.env.context.get('wizard_id') if res.get('type') != 'form' or not wizard_id: return res wiz = self.browse(wizard_id) # Get updated fields including the dynamic ones fields = self.fields_get() dynamic_fields = { k: v for k, v in fields.iteritems() if k.startswith( field_prefix) or k.startswith(custom_field_prefix) } res['fields'].update(dynamic_fields) mod_view = self.add_dynamic_fields(res, dynamic_fields, wiz) # Update result dict from super with modified view res.update({'arch': etree.tostring(mod_view)}) return res