def track_changes_comment(self, node, attributes, added_attr): comment = '' timestamp = self.track_changes_timestamp() if added_attr: comment += 'Added attribute%s to existing %s tag: ' \ % ('s' if len(added_attr) != 1 else '', node.xpath('local-name()')) + \ ', '.join('%s=%s' % (k, v) for k, v in added_attr.iteritems()) if len(added_attr) != len(attributes): comment += '\nDid not replace attribute%s: ' % \ ('s' if len(attributes) - len(added_attr) != 1 else '') + \ ', '.join('%s=%s with %s' % (k, node.get(k), v) for k, v in attributes.iteritems() if k not in added_attr) oxy_comment_start = etree.ProcessingInstruction( 'oxy_comment_start', 'author="%s" timestamp="%s"' % (self.track_changes_author, timestamp) + ' comment="%s"' % comment) oxy_comment_end = etree.ProcessingInstruction('oxy_comment_end') parent_node = node.getparent() parent_node.insert(parent_node.index(node), oxy_comment_start) parent_node.insert(parent_node.index(node) + 1, oxy_comment_end) # shift any tail text from name node to last pi oxy_comment_end.tail = node.tail node.tail = '' # end comment processing instruction is now last node # return in case any tail text needs adjustment return oxy_comment_end
def on_finish(self) -> None: import lxml.etree as etree self.last_xml = None # index_path = os.path.join(self.output_dir, 'index.xml') output_files = sorted(self.files, key=lambda x: x.module) root = etree.Element('mypy-report-index', name=self.main_file) doc = etree.ElementTree(root) for file_info in output_files: etree.SubElement(root, 'file', file_info.attrib(), total=str(file_info.total()), name=file_info.name, module=file_info.module) xslt_path = os.path.relpath('mypy-html.xslt', '.') xml_pi = etree.ProcessingInstruction('xml', 'version="1.0" encoding="utf-8"') transform_pi = etree.ProcessingInstruction( 'xml-stylesheet', 'type="text/xsl" href="%s"' % cgi.escape(xslt_path, True)) root.addprevious(xml_pi) root.addprevious(transform_pi) self.schema.assertValid(doc) self.last_xml = doc
def _generate_globals(self): metadata = self.distribution.metadata author = metadata.author or metadata.maintainer or "UNKNOWN" version = metadata.get_version() fullname = self.distribution.get_name() variables = { 'ProductVersion': version, 'ProductUpgradeCode': self.upgrade_code, 'ProductName': string.capwords(' '.join(fullname.replace('-', '_').split('_'))), 'Author': author, } include = le.Element('Include') for name, variable in variables.items(): include.append( le.ProcessingInstruction('define', '{} = "{}"'.format(name, variable))) with open('Globals.wxs', 'wb+') as f: f.write(le.tostring(include, pretty_print=True))
def __enter__( self ): self._xml = XML.ElementTree( XML.Element( 'amazon' )) base, ext = os.path.splitext( self.filename ) xsl_path = base + '.xslt' # Dynamic name eases customization pi_xsl = XML.ProcessingInstruction( 'xml-stylesheet', 'type="text/xsl" href="' + xsl_path + '"' ) self._xml.getroot().addprevious( pi_xsl ) return self
def build_deviceinfo_file(self, input_list): """ Creates a file containing all devices info with format <property name= "<name>" value="<value>"/> :type input_list: dict :param input_list:List containing all getprop keys/values """ # Check if input list is empty, if yes, write nothing. if not input_list or not isinstance(input_list, dict): return # Prepare XML file xml_doc = etree.Element("DeviceProperties") file_path = os.path.join(self.path, "device_info.xml") # Assign style sheet to test report processing_instruction = etree.ProcessingInstruction( "xml-stylesheet", "type=\"text/xsl\" href=\"../../Core/Report/html/deviceinfo.xsl\"") # Create the <DeviceProperties> base element for key, value in input_list.items(): # Build <property name= "<name>" value="<value>"/> element = etree.SubElement(xml_doc, "property") element.set("name", clean_xml_text(key)) element.set("value", clean_xml_text(value)) # Write and close XML File (getprop.txt) with open(file_path, 'w') as f_test_report: f_test_report.write( etree.tostring(processing_instruction, pretty_print=True, xml_declaration=True)) f_test_report.write(etree.tostring(xml_doc, pretty_print=True))
def link_xslt(root: ET.Element, href: str = None): if not ET_HAVE_LXML: raise NotImplementedError("Please install the LXML package") pseudoattrs = {'type': 'text/xsl', 'href': href} value = ' '.join("{:s}='{:s}'".format(k, html.escape(v, quote=True)) for (k, v) in pseudoattrs.items()) root.addprevious(ET.ProcessingInstruction('xml-stylesheet', value))
def update_report_file(self): """ Update the xml report file """ try: temp_test_report_file = os.path.join(tempfile.gettempdir(), "Temporary_TestReport.xml") processing_instruction = etree.ProcessingInstruction( "xml-stylesheet", "type=\"text/xsl\" href=\"report.xsl\"") with open(temp_test_report_file, 'w') as f_test_report: f_test_report.write( etree.tostring(processing_instruction, pretty_print=True, xml_declaration=True)) f_test_report.write( etree.tostring(self.document, pretty_print=True)) # Copy the temporary file into the test report shutil.move(temp_test_report_file, self.filename) # copy the XSL file in the same folder ad the XML file shutil.copy(self._xsl_path, self._base) except Exception as report_exception: LOGGER_FWK.warning("Fail to update test report '%s' ! (%s)" % (str(self.filename), str(report_exception)))
def get_element(leo_node): """recursively read from leo nodes and write into an Element tree """ # comment if leo_node.h[:2] == '# ': return etree.Comment(leo_node.b) # processing instruction if leo_node.h[:2] == '? ': target = leo_node.h.split()[1] return etree.ProcessingInstruction(target, leo_node.b) # regular element ele = etree.Element(make_tag(leo_node.h.split()[0]), nsmap=NSMAP) if uAxml in leo_node.v.u and '_edit' in leo_node.v.u[uAxml]: d = leo_node.v.u[uAxml]['_edit'] for k in d: ele.set(make_tag(k), d[k]) if tail_sentinel in leo_node.b: ele.text, ele.tail = leo_node.b.split(tail_sentinel, 1) else: ele.text = leo_node.b for child in leo_node.children(): ele.append(get_element(child)) return ele
def add_pi_in_screen(xml, limit=83, target='dbsuse-fo', fontsize='8pt'): """Add processing-instruction for long texts in screens .. note:: This function modifies directly the XML tree :param xml: XML tree :type xml: :class:`lxml.etree._ElementTree` :param limit: maximum number of characters allowed :type limit: int :param target: name of processing instruction :type target: str :param fontsize: font size to use :type fontsize: str """ tree = xml.getroottree() if hasattr(xml, 'getroottree') else xml for screen in tree.iter('screen'): if screen.text is not None and \ any([len(i) > limit for i in screen.text.split("\n")]): pi = etree.ProcessingInstruction(target, 'font-size="{}"'.format(fontsize)) pi.tail = screen.text screen.text = '' screen.insert(0, pi)
def __makeSignInReq(self): """ Generate the XML document that contains the sign-in request for the Quickbooks API """ root = etree.Element("QBXML") tree = etree.ElementTree(root) root.addprevious(etree.ProcessingInstruction('qbxml', 'version="6.0"')) el_signon = etree.SubElement(root, "SignonMsgsRq") el_app_cert = etree.SubElement(el_signon, 'SignonAppCertRq') el_datetime = etree.SubElement(el_app_cert, 'ClientDateTime') el_datetime.text = self.__getXMLDatetime() el_app = etree.SubElement(el_app_cert, 'ApplicationLogin') el_app.text = self.app_name el_ticket = etree.SubElement(el_app_cert, 'ConnectionTicket') el_ticket.text = self.conn_ticket el_lang = etree.SubElement(el_app_cert, 'Language') el_lang.text = 'English' el_app_id = etree.SubElement(el_app_cert, 'AppID') el_app_id.text = self.app_name_id el_ver = etree.SubElement(el_app_cert, 'AppVer') el_ver.text = self.app_name_ver if self.debug: print etree.tostring(tree, pretty_print=True, encoding="utf-8", xml_declaration=True) return tree
def SerializeXML(self, inline_projects=False): #<Prebuild version="1.10" xmlns="http://dnpb.sourceforge.net/schemas/prebuild-1.10.xsd"> #<Solution activeConfig="Debug" name="SpaceEngineersDedi64" path="./" version="0.0.1"> root = e( 'Prebuild', { 'version': '1.10', 'xmlns': 'http://dnpb.sourceforge.net/schemas/prebuild-1.10.xsd' }) solution = etree.SubElement(root, 'Solution') # Required attributes for attrName in ['name', 'activeConfig', 'path', 'version']: self.setAttr(solution, attrName, required=True) for configuration in self.configurations: configuration.SerializeXML(solution) buildorder = self.calculateProjectOrder() for projectID in buildorder: solution.append( etree.Comment(text='Prerequisites: {}'.format(', '.join( sorted(self.project_deps[projectID]))))) if inline_projects: solution.append(self.projects[projectID].SerializeXML()) else: solution.append( etree.ProcessingInstruction( 'include', 'file="{}"'.format( os.path.join(self.projects[projectID].path, 'prebuild.xml'))))
def envelope(**kwargs): """Create OAI-PMH envelope for response.""" e_oaipmh = Element(etree.QName(NS_OAIPMH, 'OAI-PMH'), nsmap=NSMAP) e_oaipmh.set(etree.QName(NS_XSI, 'schemaLocation'), '{0} {1}'.format(NS_OAIPMH, NS_OAIPMH_XSD)) e_tree = ElementTree(element=e_oaipmh) if current_app.config['OAISERVER_XSL_URL']: e_oaipmh.addprevious( etree.ProcessingInstruction( 'xml-stylesheet', 'type="text/xsl" href="{0}"'.format( current_app.config['OAISERVER_XSL_URL']))) e_responseDate = SubElement(e_oaipmh, etree.QName(NS_OAIPMH, 'responseDate')) # date should be first possible moment e_responseDate.text = datetime_to_datestamp(datetime.utcnow()) e_request = SubElement(e_oaipmh, etree.QName(NS_OAIPMH, 'request')) for key, value in kwargs.items(): if key == 'from_' or key == 'until': value = datetime_to_datestamp(value) elif key == 'resumptionToken': value = value['token'] e_request.set(key, value) e_request.text = url_for('invenio_oaiserver.response', _external=True) return e_tree, e_oaipmh
def apply_filters_element(self, elt, profile_filter=True, assembly_filter=False, setPI = False, remove_conditional_elements = ['div', 'span']): # assert(not elt.get('class') is None) # logger.debug('condition') # logger.debug(elt.get('class')) tag = elt.xpath('string(local-name())') # logger.debug(tag) # logger.debug(self.job_criteria()) if assembly_filter: evalc = self.eval_condition(self.job_criteria(), elt.get('class')) if evalc in [True, None]: if evalc and tag in remove_conditional_elements: self.unwrap(elt) return True return False if profile_filter: for profile in self.profiles_criteria(): evalc = self.eval_condition(profile, elt.get('class')) if evalc in [True, None]: logger.debug("found " + str(profile)) return False if setPI: elt.addprevious(ET.ProcessingInstruction("filter", "[{} {}]".format(tag, elt.get('class')))) self.remove(elt) return True
def _convert_node(bs_node, parent=None, makeelement=None): res = None if isinstance(bs_node, (Tag, _PseudoTag)): attribs = dict((k, unescape(v)) for k, v in bs_node.attrs) if parent is not None: res = etree.SubElement(parent, bs_node.name, attrib=attribs) else: res = makeelement(bs_node.name, attrib=attribs) for child in bs_node: _convert_node(child, res) elif type(bs_node) is NavigableString: if parent is None: return None _append_text(parent, unescape(bs_node)) elif isinstance(bs_node, Comment): res = etree.Comment(bs_node) if parent is not None: parent.append(res) elif isinstance(bs_node, ProcessingInstruction): if bs_node.endswith('?'): # The PI is of XML style (<?as df?>) but BeautifulSoup # interpreted it as being SGML style (<?as df>). Fix. bs_node = bs_node[:-1] res = etree.ProcessingInstruction(*bs_node.split(' ', 1)) if parent is not None: parent.append(res) elif isinstance(bs_node, Declaration): pass else: # CData _append_text(parent, unescape(bs_node)) return res
def addProcessingInstruction(parent, piTarget, piText): i = 0 # find position to insert after other comments and PIs but before any element for i, child in enumerate(parent): if not isinstance(child, (etree._Comment, etree._ProcessingInstruction)): break parent.insert(i, etree.ProcessingInstruction(piTarget, piText))
def invoice_query(requestID=1, iteratorID="", querytype="", fullname="", IncludeLineItems=False, MaxReturned=100): attributes = {} if not iteratorID: attributes['iterator'] = "Start" else: attributes['iterator'] = "Continue" attributes['iteratorID'] = iteratorID attributes['requestID'] = str(requestID) root = etree.Element("QBXML") root.addprevious(etree.ProcessingInstruction("qbxml", "version=\"8.0\"")) msg = etree.SubElement(root, 'QBXMLMsgsRq', {'onError': 'stopOnError'}) irq = etree.SubElement(msg, querytype + 'QueryRq', attributes) mrt = etree.SubElement(irq, 'MaxReturned') mrt.text = str(MaxReturned) mrt = etree.SubElement(irq, 'EntityFilter') ef = etree.SubElement(mrt, 'FullNameWithChildren') ef.text = str(fullname) if querytype == 'Invoice' and IncludeLineItems: incitems = etree.SubElement(irq, 'IncludeLineItems') incitems.text = "true" includelinkedtxns = etree.SubElement(irq, 'IncludeLinkedTxns') includelinkedtxns.text = "true" tree = etree.ElementTree(root) requestxml = etree.tostring(tree, xml_declaration=True, encoding='UTF-8') return requestxml
def _get_xslt_pi(self): htmlescaped_url = htmlencode(self.xslt) quote_sanitized = htmlescaped_url.replace('"', '').replace("\\", "") return etree.tostring(etree.ProcessingInstruction( "xml-stylesheet", 'type="text/xsl" href="' + quote_sanitized + '"', ), encoding="UTF-8").decode("UTF-8")
def on_file(self, tree: MypyFile, type_map: Dict[Node, Type]) -> None: import lxml.etree as etree self.last_xml = None path = os.path.relpath(tree.path) if stats.is_special_module(path): return if path.startswith('..'): return if 'stubs' in path.split('/'): return visitor = stats.StatisticsVisitor(inferred=True, typemap=type_map, all_nodes=True) tree.accept(visitor) root = etree.Element('mypy-report-file', name=path, module=tree._fullname) doc = etree.ElementTree(root) file_info = FileInfo(path, tree._fullname) with open(path) as input_file: for lineno, line_text in enumerate(input_file, 1): status = visitor.line_map.get(lineno, stats.TYPE_EMPTY) file_info.counts[status] += 1 etree.SubElement(root, 'line', number=str(lineno), precision=stats.precision_names[status], content=line_text[:-1]) # Assumes a layout similar to what XmlReporter uses. xslt_path = os.path.relpath('mypy-html.xslt', path) xml_pi = etree.ProcessingInstruction('xml', 'version="1.0" encoding="utf-8"') transform_pi = etree.ProcessingInstruction( 'xml-stylesheet', 'type="text/xsl" href="%s"' % cgi.escape(xslt_path, True)) root.addprevious(xml_pi) root.addprevious(transform_pi) self.schema.assertValid(doc) self.last_xml = doc self.files.append(file_info)
def enable_oxygen_track_changes(node): '''Add a processing instruction to a document with an OxygenXMl option to enable the track changes mode. ''' oxy_track_changes = etree.ProcessingInstruction('oxy_options', 'track_changes="on"') # FIXME: shouldn't we be able to add this to the root node # and detect if it is already there? node.append(oxy_track_changes)
def convert_pi(bs_node, parent): if bs_node.endswith('?'): # The PI is of XML style (<?as df?>) but BeautifulSoup # interpreted it as being SGML style (<?as df>). Fix. bs_node = bs_node[:-1] res = etree.ProcessingInstruction(*bs_node.split(' ', 1)) if parent is not None: parent.append(res) return res
def addProcessingInstruction(parent, piTarget, piText, insertBeforeChildElements=True): child = etree.ProcessingInstruction(piTarget, piText) if insertBeforeChildElements: i = 0 # find position to insert after other comments and PIs but before any element for i, _otherChild in enumerate(parent): if not isinstance(_otherChild, (etree._Comment, etree._ProcessingInstruction)): break # insert before this child parent.insert(i, child) else: # can go after elements parent.append(child)
def test_integration(self): doc = xtree('testdata/test_integ_data.xml') doc.parse(False) for text in doc.tree.xpath('//text'): el = etree.Element('EL1') el.set('target', 'stuff') doc.wrap_text(text, 'stuff', el) pi = etree.ProcessingInstruction('PI1', 'Pb') doc.insert_node(text, 'text', pi, True) pi = etree.ProcessingInstruction('PI2', 'Pb') doc.insert_node(text, 'wrapped', pi) el = etree.Element('EL2') el.set('target', 'wrap') doc.wrap_text(text, 'wrapped', el) filename = 'testdata/test_data.xml' doc.serialise(filename) f = open(filename, 'r').read() r = open('testdata/r_test_integ_data.xml', 'r').read() self.assertEqual(f, r)
def test_insertPIBefore(self): doc = xtree('testdata/test_wrap.xml') doc.parse(False) for text in doc.tree.xpath('//text'): pi = etree.ProcessingInstruction('PI', 'Pb') doc.insert_node(text, 'stuff', pi, True) r = xtree('testdata/r_test_insertPIBefore.xml') r.parse(False) checker = LXMLOutputChecker() self.assertTrue( checker.compare_docs(r.tree.getroot(), doc.tree.getroot()))
def dump(f, content, xslt = None): root = etree.Element('root') for r in content: serialize(r, root) if xslt is not None: root.addprevious( etree.ProcessingInstruction( 'xml-stylesheet', 'type="text/xsl" href="' + xslt + '"', )) et = etree.ElementTree(root) et.write(f, xml_declaration=True, pretty_print=True, encoding='UTF-8')
def _publish_feed(self, doc): ''' publish feed ''' feed = etree.ElementTree(doc) xml_declaration = etree.ProcessingInstruction('xml', 'version="1.0" encoding="utf-8"') xml_declaration_string = etree.tostring(xml_declaration, encoding=unicode) feed_string = etree.tostring(feed, pretty_print=True, encoding=unicode) with codecs.open(self.atom_file, "w", encoding='utf-8') as f: f.write(xml_declaration_string) f.write('\n') f.write(feed_string) print "Feed written to file: {}".format(self.atom_file)
def return_processing_instructions(self): """ Get all processing instructions saved when loading the document :param tree: etree.ElementTree object to write PIs to """ pi_list = [] if self.processing_instructions is not None: for key in self.processing_instructions: value = self.processing_instructions.get(key) pi_item = etree.ProcessingInstruction(key, value) pi_list.append(pi_item) return pi_list
def buildReport(self): '''build the report''' root = etree.Element("pvWebMonitor") root.set("version", "1") node = etree.SubElement(root, "written_by") node.text = 'pvWebMonitor/PvWatch' node = etree.SubElement(root, "datetime") node.text = str(utils.getTime()).split('.')[0] sorted_id_list = sorted(self.xref) fields = ("name", "id", "description", "timestamp", "record_type", "counter", "units", "value", "char_value", "raw_value", "format") for mne in sorted_id_list: pv = self.xref[mne] entry = self.pvdb[pv] node = etree.SubElement(root, "pv") node.set("id", mne) node.set("name", pv) for item in fields: subnode = etree.SubElement(node, item) subnode.text = str(entry[item]) try: pi_xml = etree.ProcessingInstruction('xml', 'version="1.0"') xmlText = etree.tostring(pi_xml, pretty_print=True) except ValueError: # some instanced of lxml raise a ValueError saying that 'xml' is not allowed xmlText = '<?xml version="1.0" ?>\n' pi_xsl = etree.ProcessingInstruction( 'xml-stylesheet', 'type="text/xsl" href="pvlist.xsl"') xmlText += etree.tostring(pi_xsl, pretty_print=True) xmlText += etree.tostring(root, pretty_print=True) return xmlText
def app2xml(apps, filename): xml = F.apparatus() tree = etree.ElementTree(xml) xml.addprevious( etree.ProcessingInstruction( 'xml-model', 'href="appxnorm.rnc" type="application/relax-ng-compact-syntax"')) xml.extend(apps) with open(filename, 'wb') as outfile: outfile.write( etree.tostring(tree, pretty_print=True, encoding='utf-8', xml_declaration=True))
def start_svg(self): "Base SVG Document Creation" SVG_NAMESPACE = 'http://www.w3.org/2000/svg' SVG = '{%s}' % SVG_NAMESPACE NSMAP = { None: SVG_NAMESPACE, 'xlink': 'http://www.w3.org/1999/xlink', 'a3': 'http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/', } root_attrs = self._get_root_attributes() self.root = etree.Element(SVG + "svg", attrib=root_attrs, nsmap=NSMAP) if hasattr(self, 'style_sheet_href'): pi = etree.ProcessingInstruction( 'xml-stylesheet', 'href="%s" type="text/css"' % self.style_sheet_href) self.root.addprevious(pi) comment_strings = ( ' Created with SVG.Graph ', ' SVG.Graph by Jason R. Coombs ', ' Based on SVG::Graph by Sean E. Russel ', ' Based on Perl SVG:TT:Graph by Leo Lapworth & Stephan Morgan ', ' ' + '/' * 66, ) list(map(self.root.append, map(etree.Comment, comment_strings))) defs = etree.SubElement(self.root, 'defs') self.add_defs(defs) if not hasattr(self, 'style_sheet_href') and not self.css_inline: self.root.append( etree.Comment( ' include default stylesheet if none specified ')) style = etree.SubElement(defs, 'style', type='text/css') # TODO: the text was previously escaped in a CDATA declaration... how # to do that with etree? style.text = self.get_stylesheet().cssText self.root.append(etree.Comment('SVG Background')) etree.SubElement( self.root, 'rect', { 'width': str(self.width), 'height': str(self.height), 'x': '0', 'y': '0', 'class': 'svgBackground', }, )
def track_changes_inserted(self, new_node, old_text, dbres): # create & insert oxygen processing instructions # relative to the new node that was just inserted timestamp = self.track_changes_timestamp() # create a delete marker for the old text oxy_delete = etree.ProcessingInstruction( 'oxy_delete', 'author="%s" timestamp="%s" content="%s"' % (self.track_changes_author, timestamp, old_text)) # FIXME: escape content in surface form (quotes, etc) # Create a marker for the beginning of an insertion. # Use the description if possible, to provide enough # context for reviewers to determine if this is the correct # entity (other information doesn't seem to be useful # or is already in the document in another form) comment = dbres.description or dbres.label or \ '(label/description unavailable)' # convert quotes to single quotes to avoid breaking comment text comment = comment.replace('"', '\'') oxy_insert_start = etree.ProcessingInstruction( 'oxy_insert_start', 'author="%s" timestamp="%s"' % (self.track_changes_author, timestamp) + ' comment="%s"' % comment) # marker for the end of the insertion oxy_insert_end = etree.ProcessingInstruction('oxy_insert_end') # - add deletion, then insertion start just before new tag parent_node = new_node.getparent() parent_node.insert(parent_node.index(new_node), oxy_delete) parent_node.insert(parent_node.index(new_node), oxy_insert_start) # - insert end *after* new tag parent_node.insert(parent_node.index(new_node) + 1, oxy_insert_end) # return last processing instruction, since it may # need to have 'tail' text appended return oxy_insert_end