我们从Python开源项目中,提取了以下43个代码示例,用于说明如何使用lxml.etree.ElementBase()。
def insert_errors( el, errors, form_id=None, form_index=None, error_class="error", error_creator=default_error_creator, ): el = _find_form(el, form_id=form_id, form_index=form_index) for name, error in errors.items(): if error is None: continue for error_el, message in _find_elements_for_name(el, name, error): assert isinstance(message, (basestring, type(None), ElementBase)), ( "Bad message: %r" % message) _insert_error(error_el, message, error_class, error_creator)
def convert_to_tree(xml_node: etree.ElementBase, parent=None, **kwargs) -> XMLAwareCursor: cursor_args = { 'node_type': xml_node.tag, 'marker': truncate_char_field(xml_node, 'marker'), 'text': content_text(xml_node), 'title': truncate_char_field(xml_node, 'title'), **kwargs, } if 'emblem' in xml_node.attrib: cursor_args['type_emblem'] = xml_node.attrib['emblem'] if parent: cursor = parent.add_child(**cursor_args) else: cursor = XMLAwareCursor.new_tree(**cursor_args) cursor.xml_node = xml_node for xml_child in xml_node.xpath('./*[not(self::content)]'): convert_to_tree(xml_child, cursor, **kwargs) return cursor
def __call__(self, el, is_block, message): error_el = el.makeelement(self.error_container_tag) if self.error_message_class: error_el.set('class', self.error_message_class) if is_block and self.error_block_class: error_el.set('class', error_el.get('class', '')+' '+self.error_block_class) if message is None or message == '': message = self.default_message if isinstance(message, ElementBase): error_el.append(message) else: assert isinstance(message, basestring), ( "Bad message; should be a string or element: %r" % message) error_el.text = message or self.default_message if is_block and self.block_inside: if self.insert_before: error_el.tail = el.text el.text = None el.insert(0, error_el) else: el.append(error_el) else: parent = el.getparent() pos = parent.index(el) if self.insert_before: parent.insert(pos, error_el) else: error_el.tail = el.tail el.tail = None parent.insert(pos+1, error_el)
def __init__(self, element: etree.ElementBase, file_name, globals=None): self.__data_container = DataContainer() self.interpreter = UJMLPythonInterpreter(globals or {}) """ Instance of :class:`urban_journey.UJMLPythonInterpreter` used as the embedded python interpreter to run the python code in the ujml file. """ self.channel_register = ChannelRegister() """Instance of :class:`urban_journey.ChannelRegister` used as the main channel register.""" self.__configure_interpreter() self.__file_name = os.path.abspath(file_name) self.node_dict_by_id = {} """A dictionary containing all already read nodes by id.""" super().__init__(element, None) self.pyqt_app = None """If PyQt4 is enabled, it contains the :class:`PyQt4.QtGui.QApplication` instance. """ if self.pyqt: self.pyqt_enable() self.__semaphore = Semaphore(0) self.ujml_module = UjmlModule(self.channel_register) """Module that triggers the default ujml channels.""" self.__check_version() self.update_children() self.__exc_info = None self.connections = {} self.check_list = CheckInSemaphore()
def __init__(self, element: etree.ElementBase, root): super().__init__(element, root) if self.src is None: source = self.element.text else: with open(self.src) as f: source = f.read() if self.async: # Not sure how this would be useful because of the GIL, but whatever. This loop runs on a separate thread. loop = get_event_loop() loop.call_soon(self.exec, source) else: self.exec(source, is_global=True)
def __init__(self, element: etree.ElementBase, root): super().__init__(element, root) self.root.data.add_data_node(self)
def __init__(self, element: etree.ElementBase, root, register=True): element.node = self self.element = element # :class:`etree.ElementBase` object representing this node in the xlm code. self.parents = [] #: List of paret objects. self.__children = None self.__file_name = None self.__root = root self.__register = register if register: self.root.register_node(self)
def GenerateAnyInfos(infos): def GetTextElement(tree): if infos["namespace"][0] == "##any": return tree.xpath("p")[0] return tree.xpath("ns:p", namespaces={"ns": infos["namespace"][0]})[0] def ExtractAny(tree): return GetTextElement(tree).text def GenerateAny(tree, value): GetTextElement(tree).text = etree.CDATA(value) def InitialAny(): if infos["namespace"][0] == "##any": element_name = "p" else: element_name = "{%s}p" % infos["namespace"][0] p = etree.Element(element_name) p.text = etree.CDATA("") return p return { "type": COMPLEXTYPE, "extract": ExtractAny, "generate": GenerateAny, "initial": InitialAny, "check": lambda x: isinstance(x, (StringType, UnicodeType, etree.ElementBase)) }
def standardize_content(xml: etree.ElementBase): """For ease of use, we don't require all text be wrapped in a "content" tag. However, that idiom _is_ helpful when parsing, so let's add them here.""" xpath_selector = ('not(self::content) ' # not a content element 'and not(ancestor::content)' # not a child of a content 'and not(./content) ') # doesn't contain a content for element in xml.xpath(f'.//*[{xpath_selector}]'): if element.text: content_xml = etree.SubElement(element, 'content') content_xml.text = element.text element.text = None
def clean_content(xml: etree.ElementBase): """Remove beginning and trailing whitespace from <content> tags.""" for content_xml in xml.xpath('.//content'): content_xml.text = (content_xml.text or '').lstrip() last_child = content_xml.xpath('./*[last()]') if last_child: last_child = last_child[0] last_child.tail = (last_child.tail or '').rstrip() else: content_xml.text = content_xml.text.rstrip()
def warn_about_mismatches(policy: Policy, xml: etree.ElementBase): """When the elements in the document don't match their fields in the XML, we warn the user.""" pairs = [ (policy.omb_policy_id, cleanup_string(xml, 'policyNum')), (policy.title, cleanup_string(xml, 'subject')), (policy.issuance, parse_published(xml)), ] for db_val, xml_val in pairs: if db_val != xml_val: logger.warning( 'Mismatch between XML and database. DB says %s, XML %s', repr(db_val), repr(xml_val), )
def cleanup_string(xml: etree.ElementBase, tag_name: str): return (xml.findtext(f".//{tag_name}") or '').strip()
def parse_published(xml: etree.ElementBase): published_txt = (xml.findtext('.//published') or '').strip() try: return dateutil_parser.parse(published_txt).date() except ValueError: logger.warning('Invalid XML published date: %s', published_txt)
def anchor(cursor: XMLAwareCursor, xml_span: etree.ElementBase, start: int) -> Optional[ExternalLink]: text = ''.join(xml_span.itertext()) href = xml_span.attrib.get('href', text) return ExternalLink(doc_node=cursor.model, start=start, end=start + len(text), href=href)
def requirement(cursor: XMLAwareCursor, xml_span: etree.ElementBase, start: int) -> Optional[InlineRequirement]: text = ''.join(xml_span.itertext()) req_id = xml_span.attrib.get('id', '') req = Requirement.objects.filter(req_id=req_id).first() if req: return InlineRequirement( doc_node=cursor.model, start=start, end=start + len(text), requirement=req ) else: logger.warning("Can't find requirement: %s", repr(req_id))
def cite(cursor: XMLAwareCursor, xml_span: etree.ElementBase, start: int) -> Optional[ExternalLink]: text = ''.join(xml_span.itertext()) return Cite(doc_node=cursor.model, start=start, end=start + len(text))
def len_of_child_text(xml_node: etree.ElementBase): return sum(len(txt) for txt in xml_node.itertext())
def derive(self, xml: etree.ElementBase): intro_text = xml.text or '' self.len_so_far += len(intro_text) for child_xml in xml: handler = annotation_mapping[child_xml.tag] annotation = handler(self.cursor, child_xml, self.len_so_far) if annotation: yield annotation yield from self.derive(child_xml) self.len_so_far += len(child_xml.tail or '')
def truncate_char_field(xml_node: etree.ElementBase, field_name: str) -> str: value = xml_node.attrib.get(field_name, '') field = DocNode._meta.get_field(field_name) if len(value) > field.max_length: logger.warn(f'Truncating "{value}" to {field.max_length} characters') value = value[:field.max_length] return value
def content_text(xml_node: etree.ElementBase): """Fetch just the text from a node. We'll assume it's been preprocessed to have a single <content/> child.""" content_node = xml_node.find('./content') if content_node is None: return '' else: return ''.join(content_node.itertext())
def __init__(self, file_location, iter_elem, parser=None): """ Basic XML parser & iterator :param file_location: XML encoded file :param iter_elem: element to use as record iterator :param parser: a custom etree.XMLParser (required for custom etree.ElementBase subclasses) """ super(Reader, self).__init__() if parser is not None: self.iterator = parse(file_location, parser=parser).iter(iter_elem) else: self.iterator = parse(file_location).iter(iter_elem)
def create_child(self, element: etree.ElementBase): """ Finds NodeBase child class for a child element. There is no need to use this function. If this lookup behaviour has to be customized, override child_lookup(..) instead. """ if element.tag is etree.Comment: return if element.tag is etree.PI: return # If this is a ref element. Find the element it references and add it as a child. if element.tag == "ref": node_id = element.get("id") child = self.find_node_by_id(node_id) if child is None: raise IdNotFoundError(self.file_name, element.sourceline, node_id) else: # Check if this is a data Data element if isinstance(inspect.getattr_static(self, element.tag, None), Data): from urban_journey.ujml.nodes.data import data klass = data else: # Check if parent element knows what type it is. klass = self.child_lookup(element) # Update the node_register if it's empty. if len(node_register) == 0: update_plugins() # Look for node class in the register. if klass is None: if element.tag in node_register: klass = node_register[element.tag] else: # Node type was not found. raise UnknownElementError(self.file_name, element.sourceline, element.tag) child = klass(element, self.root) if not hasattr(child, "element"): self.raise_exception(MissingSuperInitError, self.tag, element.tag) # Add child self.add_child(child)
def remove_elements(*elements: etree.ElementBase, keep_children=False, preserve_text=False, preserve_tail=False) -> None: """ Removes the given elements from its tree. Unless ``keep_children`` is passed as ``True``, its children vanish with it into void. If ``preserve_text`` is ``True``, the text and tail of a deleted element will be preserved either in its left sibling's tail or its parent's text. """ for element in elements: if preserve_text and element.text: previous = element.getprevious() if previous is None: parent = element.getparent() if parent.text is None: parent.text = '' parent.text += element.text else: if previous.tail is None: previous.tail = element.text else: previous.tail += element.text if preserve_tail and element.tail: if keep_children and len(element): if element[-1].tail: element[-1].tail += element.tail else: element[-1].tail = element.tail else: previous = element.getprevious() if previous is None: parent = element.getparent() if parent.text is None: parent.text = '' parent.text += element.tail else: if len(element): if element[-1].tail is None: element[-1].tail = element.tail else: element[-1].tail += element.tail else: if previous.tail is None: previous.tail = '' previous.tail += element.tail if keep_children: for child in element: element.addprevious(child) element.getparent().remove(element)