Python lxml.etree._Element() Examples

The following are 30 code examples of lxml.etree._Element(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module lxml.etree , or try the search function .
Example #1
Source File: field.py    From ruia with Apache License 2.0 6 votes vote down vote up
def extract(self, html_etree: etree._Element, is_source: bool = False):
        elements = self._get_elements(html_etree=html_etree)

        if is_source:
            return elements if self.many else elements[0]

        if elements:
            results = [self._parse_element(element) for element in elements]
        elif self.default is None:
            raise NothingMatchedError(
                f"Extract `{self.css_select or self.xpath_select}` error, "
                "please check selector or set parameter named `default`"
            )
        else:
            results = self.default if type(self.default) == list else [self.default]

        return results if self.many else results[0] 
Example #2
Source File: basics.py    From costar_plan with Apache License 2.0 6 votes vote down vote up
def to_yaml(obj):
    """ Simplify yaml representation for pretty printing """
    # Is there a better way to do this by adding a representation with yaml.Dumper?
    # Ordered dict: http://pyyaml.org/ticket/29#comment:11
    if obj is None or isstring(obj):
        out = str(obj)
    elif type(obj) in [int, float, bool]:
        return obj
    elif hasattr(obj, 'to_yaml'):
        out = obj.to_yaml()
    elif isinstance(obj, etree._Element):
    	out = etree.tostring(obj, pretty_print = True)
    elif type(obj) == dict:
        out = {}
        for (var, value) in obj.items():
            out[str(var)] = to_yaml(value)
    elif hasattr(obj, 'tolist'):
        # For numpy objects
        out = to_yaml(obj.tolist())
    elif isinstance(obj, collections.Iterable):
        out = [to_yaml(item) for item in obj]
    else:
        out = str(obj)
    return out 
Example #3
Source File: translate.py    From odoo13-x64 with GNU General Public License v3.0 6 votes vote down vote up
def _extract_translatable_qweb_terms(element, callback):
    """ Helper method to walk an etree document representing
        a QWeb template, and call ``callback(term)`` for each
        translatable term that is found in the document.

        :param etree._Element element: root of etree document to extract terms from
        :param Callable callback: a callable in the form ``f(term, source_line)``,
                                  that will be called for each extracted term.
    """
    # not using elementTree.iterparse because we need to skip sub-trees in case
    # the ancestor element had a reason to be skipped
    for el in element:
        if isinstance(el, SKIPPED_ELEMENT_TYPES): continue
        if (el.tag.lower() not in SKIPPED_ELEMENTS
                and "t-js" not in el.attrib
                and not ("t-jquery" in el.attrib and "t-operation" not in el.attrib)
                and el.get("t-translation", '').strip() != "off"):
            _push(callback, el.text, el.sourceline)
            for att in ('title', 'alt', 'label', 'placeholder'):
                if att in el.attrib:
                    _push(callback, el.attrib[att], el.sourceline)
            _extract_translatable_qweb_terms(el, callback)
        _push(callback, el.tail, el.sourceline) 
Example #4
Source File: field.py    From hproxy with MIT License 6 votes vote down vote up
def extract_value(self, html, is_source=False):
        """
        Use css_select or re_select to extract a field value
        :return:
        """
        value = ''
        if self.css_select:
            value = html.cssselect(self.css_select)
        elif self.xpath_select:
            value = html.xpath(self.xpath_select)
        else:
            raise ValueError('%s field: css_select or xpath_select is expected' % self.__class__.__name__)
        if is_source:
            return value
        if isinstance(value, list) and len(value) == 1:
            if isinstance(value[0], etree._Element):
                text = ''
                for node in value[0].itertext():
                    text += node.strip()
                value = text
            if isinstance(value[0], str) or isinstance(value[0], etree._ElementUnicodeResult):
                value = ''.join(value)
        if self.default is not None:
            value = value if value else self.default
        return value 
Example #5
Source File: element.py    From dm_control with Apache License 2.0 6 votes vote down vote up
def to_xml(self, prefix_root=None, debug_context=None):
    """Generates an etree._Element corresponding to this MJCF element.

    Args:
      prefix_root: (optional) A `NameScope` object to be treated as root
        for the purpose of calculating the prefix.
        If `None` then no prefix is included.
      debug_context: (optional) A `debugging.DebugContext` object to which
        the debugging information associated with the generated XML is written.
        This is intended for internal use within PyMJCF; users should never need
        manually pass this argument.

    Returns:
      An etree._Element object.
    """
    prefix_root = prefix_root or self.namescope
    xml_element = etree.Element(self._spec.name)
    self._attributes_to_xml(xml_element, prefix_root, debug_context)
    self._children_to_xml(xml_element, prefix_root, debug_context)
    return xml_element 
Example #6
Source File: __init__.py    From pycon with MIT License 6 votes vote down vote up
def invoice_to_xml(invoice: Invoice) -> etree._Element:
    root_tag = "{%s}FatturaElettronica" % NAMESPACE_MAP["p"]
    schema_location_key = "{%s}schemaLocation" % NAMESPACE_MAP["xsi"]

    root = etree.Element(
        root_tag,
        attrib={schema_location_key: SCHEMA_LOCATION},
        nsmap=NAMESPACE_MAP,
        versione="FPR12",
    )

    header = _generate_header(invoice)
    body = _generate_body(invoice)

    tags = [*dict_to_xml(header), *dict_to_xml(body)]

    for tag in tags:
        root.append(tag)

    return root 
Example #7
Source File: syntax.py    From bfg9000 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def write(self, out):
        target = E.Target(Name='Build')
        if self.makedir:
            target.append(E.MakeDir(Directories=self.makedir))

        for line in self.commands:
            if not isinstance(line, etree._Element):  # pragma: no cover
                raise TypeError('expected an lxml element')
            target.append(line)

        self._write(out, [
            # Import the C++ properties to get $(OutDir). There might be a
            # better way to handle this.
            E.Import(Project=r'$(VCTargetsPath)\Microsoft.Cpp.default.props'),
            E.Import(Project=r'$(VCTargetsPath)\Microsoft.Cpp.props'),
            target
        ]) 
Example #8
Source File: service.py    From gdata-python3 with Apache License 2.0 6 votes vote down vote up
def Query(self, uri):
        """Performs a query and returns a resulting feed or entry.

        Args:
          uri: A string representing the URI of the feed that is to be queried.

        Returns:
          On success, a tuple in the form:
          (boolean succeeded=True, ElementTree._Element result)
          On failure, a tuple in the form:
          (boolean succeeded=False, {'status': HTTP status code from server,
                                     'reason': HTTP reason from the server,
                                     'body': HTTP body of the server's response})
        """
        result = self.Get(uri)
        return result 
Example #9
Source File: group.py    From pykeepass with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, name=None, element=None, icon=None, notes=None,
                 kp=None, expires=None, expiry_time=None):

        self._kp = kp

        if element is None:
            super(Group, self).__init__(
                element=Element('Group'),
                kp=kp,
                expires=expires,
                expiry_time=expiry_time,
                icon=icon
            )
            self._element.append(E.Name(name))
            if notes:
                self._element.append(E.Notes(notes))

        else:
            assert type(element) in [_Element, Element, ObjectifiedElement], \
                'The provided element is not an LXML Element, but {}'.format(
                    type(element)
                )
            assert element.tag == 'Group', 'The provided element is not a Group '\
                'element, but a {}'.format(element.tag)
            self._element = element 
Example #10
Source File: parser.py    From pymoca with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def enter_classDefinition(self, tree: etree._Element):
        # we don't know if variables are states
        # yet, we need to wait until equations are parsed
        self.scope_stack.append({
            'time': self.sym.sym('time'),
            'sample_times': [],
            'var': OrderedDict(),  # variables
            'states': [],  # list of which variables are states (based on der call)
            'dvar': OrderedDict(),  # derivative of variables
            'eqs': [],  # equations
            'when_eqs': [],  # when equations
            'c': {},  # conditions
            'pre_c': {},  # pre conditions
            'p': [],  # parameters and constants
            'prop': {},  # properties for variables
            'ng': [],  # gaussian
            'nu': [],  # uniform
        }) 
Example #11
Source File: basics.py    From director with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def to_yaml(obj):
    """ Simplify yaml representation for pretty printing """
    # Is there a better way to do this by adding a representation with yaml.Dumper?
    # Ordered dict: http://pyyaml.org/ticket/29#comment:11
    if obj is None or isstring(obj):
        out = str(obj)
    elif type(obj) in [int, float, bool]:
        return obj
    elif hasattr(obj, 'to_yaml'):
        out = obj.to_yaml()
    elif isinstance(obj, etree._Element):
    	out = etree.tostring(obj, pretty_print = True)
    elif type(obj) == dict:
        out = {}
        for (var, value) in list(obj.items()):
            out[str(var)] = to_yaml(value)
    elif hasattr(obj, 'tolist'):
        # For numpy objects
        out = to_yaml(obj.tolist())
    elif isinstance(obj, collections.Iterable):
        out = [to_yaml(item) for item in obj]
    else:
        out = str(obj)
    return out 
Example #12
Source File: item.py    From ruia with Apache License 2.0 6 votes vote down vote up
def _parse_html(cls, *, html_etree: etree._Element):
        if html_etree is None:
            raise ValueError("<Item: html_etree is expected>")
        item_ins = cls()
        fields_dict = getattr(item_ins, "__fields", {})
        for field_name, field_value in fields_dict.items():
            if not field_name.startswith("target_"):
                clean_method = getattr(item_ins, f"clean_{field_name}", None)
                value = field_value.extract(html_etree)
                if clean_method is not None and callable(clean_method):
                    try:
                        aws_clean_func = clean_method(value)
                        if isawaitable(aws_clean_func):
                            value = await aws_clean_func
                        else:
                            raise InvalidFuncType(
                                f"<Item: clean_method must be a coroutine function>"
                            )
                    except IgnoreThisItem:
                        item_ins.ignore_item = True

                setattr(item_ins, field_name, value)
                item_ins.results[field_name] = value
        return item_ins 
Example #13
Source File: kbinxml.py    From kbinxml with MIT License 6 votes vote down vote up
def __init__(self, input):
        if isinstance(input, etree._Element):
            self.xml_doc = input
        elif isinstance(input, etree._ElementTree):
            self.xml_doc = input.getroot()
        elif KBinXML.is_binary_xml(input):
            self.from_binary(input)
        else:
            self.from_text(input) 
Example #14
Source File: service.py    From gdata-python3 with Apache License 2.0 5 votes vote down vote up
def Post(self, data, uri, extra_headers=None, url_params=None,
             escape_params=True, redirects_remaining=4, media_source=None,
             converter=None):
        """Insert or update  data into a GData service at the given URI.

        Args:
          data: string, ElementTree._Element, atom.Entry, or gdata.GDataEntry The
                XML to be sent to the uri.
          uri: string The location (feed) to which the data should be inserted.
               Example: '/base/feeds/items'.
          extra_headers: dict (optional) HTTP headers which are to be included.
                         The client automatically sets the Content-Type,
                         Authorization, and Content-Length headers.
          url_params: dict (optional) Additional URL parameters to be included
                      in the URI. These are translated into query arguments
                      in the form '&dict_key=value&...'.
                      Example: {'max-results': '250'} becomes &max-results=250
          escape_params: boolean (optional) If false, the calling code has already
                         ensured that the query will form a valid URL (all
                         reserved characters have been escaped). If true, this
                         method will escape the query and any URL parameters
                         provided.
          media_source: MediaSource (optional) Container for the media to be sent
              along with the entry, if provided.
          converter: func (optional) A function which will be executed on the
              server's response. Often this is a function like
              GDataEntryFromString which will parse the body of the server's
              response and return a GDataEntry.

        Returns:
          If the post succeeded, this method will return a GDataFeed, GDataEntry,
          or the results of running converter on the server's result body (if
          converter was specified).
        """
        return GDataService.PostOrPut(self, 'POST', data, uri,
                                      extra_headers=extra_headers, url_params=url_params,
                                      escape_params=escape_params, redirects_remaining=redirects_remaining,
                                      media_source=media_source, converter=converter) 
Example #15
Source File: xml_utils_test.py    From python3-saml with MIT License 5 votes vote down vote up
def testToElement(self):
        """
        Tests the to_etree method of the OneLogin_Saml2_XML
        """
        xml = '<test>test1</test>'
        elem = etree.fromstring(xml)
        xml_expected = etree.tostring(elem)

        res = OneLogin_Saml2_XML.to_etree(xml)
        self.assertIsInstance(res, etree._Element)
        self.assertEqual(xml_expected, etree.tostring(res))

        res = OneLogin_Saml2_XML.to_etree(xml.encode('utf8'))
        self.assertIsInstance(res, etree._Element)
        self.assertEqual(xml_expected, etree.tostring(res))

        self.assertIsInstance(res, etree._Element)
        self.assertEqual(xml_expected, etree.tostring(res))

        res = OneLogin_Saml2_XML.to_etree(elem)
        self.assertIs(res, elem)

        with self.assertRaises(ValueError) as context:
            OneLogin_Saml2_XML.to_etree(1)
            exception = context.exception
            self.assertIn("unsupported type", str(exception)) 
Example #16
Source File: service.py    From gdata-python3 with Apache License 2.0 5 votes vote down vote up
def Put(self, data, uri, extra_headers=None, url_params=None,
            escape_params=True, redirects_remaining=3, media_source=None,
            converter=None):
        """Updates an entry at the given URI.

        Args:
          data: string, ElementTree._Element, or xml_wrapper.ElementWrapper The
                XML containing the updated data.
          uri: string A URI indicating entry to which the update will be applied.
               Example: '/base/feeds/items/ITEM-ID'
          extra_headers: dict (optional) HTTP headers which are to be included.
                         The client automatically sets the Content-Type,
                         Authorization, and Content-Length headers.
          url_params: dict (optional) Additional URL parameters to be included
                      in the URI. These are translated into query arguments
                      in the form '&dict_key=value&...'.
                      Example: {'max-results': '250'} becomes &max-results=250
          escape_params: boolean (optional) If false, the calling code has already
                         ensured that the query will form a valid URL (all
                         reserved characters have been escaped). If true, this
                         method will escape the query and any URL parameters
                         provided.
          converter: func (optional) A function which will be executed on the
              server's response. Often this is a function like
              GDataEntryFromString which will parse the body of the server's
              response and return a GDataEntry.

        Returns:
          If the put succeeded, this method will return a GDataFeed, GDataEntry,
          or the results of running converter on the server's result body (if
          converter was specified).
        """
        return GDataService.PostOrPut(self, 'PUT', data, uri,
                                      extra_headers=extra_headers, url_params=url_params,
                                      escape_params=escape_params, redirects_remaining=redirects_remaining,
                                      media_source=media_source, converter=converter) 
Example #17
Source File: __init__.py    From gdata-python3 with Apache License 2.0 5 votes vote down vote up
def _BecomeChildElement(self, element_tree):
        """Converts this object into an etree element and adds it as a child node.

        Adds self to the ElementTree. This method is required to avoid verbose XML
        which constantly redefines the namespace.

        Args:
          element_tree: ElementTree._Element The element to which this object's XML
              will be added.
        """
        new_element = ElementTree.Element('tag__')  # uh, uhm... empty tag name - sorry google, this is bogus? (c)https://github.com/lqc
        element_tree.append(new_element)
        self._TransferToElementTree(new_element) 
Example #18
Source File: service.py    From gdata-python3 with Apache License 2.0 5 votes vote down vote up
def Put(self, data, uri, extra_headers=None, url_params=None,
            escape_params=True, content_type='application/atom+xml'):
        """Updates an entry at the given URI.

        Args:
          data: string, ElementTree._Element, or xml_wrapper.ElementWrapper The
                XML containing the updated data.
          uri: string A URI indicating entry to which the update will be applied.
               Example: '/base/feeds/items/ITEM-ID'
          extra_headers: dict (optional) HTTP headers which are to be included.
                         The client automatically sets the Content-Type,
                         Authorization, and Content-Length headers.
          url_params: dict (optional) Additional URL parameters to be included
                      in the URI. These are translated into query arguments
                      in the form '&dict_key=value&...'.
                      Example: {'max-results': '250'} becomes &max-results=250
          escape_params: boolean (optional) If false, the calling code has already
                         ensured that the query will form a valid URL (all
                         reserved characters have been escaped). If true, this
                         method will escape the query and any URL parameters
                         provided.

        Returns:
          httplib.HTTPResponse Server's response to the PUT request.
        """
        if extra_headers is None:
            extra_headers = {}
        if content_type:
            extra_headers['Content-Type'] = content_type
        return self.request('PUT', uri, data=data, headers=extra_headers,
                            url_params=url_params) 
Example #19
Source File: texmath.py    From acl-anthology with Apache License 2.0 5 votes vote down vote up
def to_html(self, element):
        """Converts a TeX math expression to HTML markup."""
        if isinstance(element, etree._Element):
            return self.etree_to_html(element)
        elif isinstance(element, str):
            value = self.etree_to_html(
                etree.fromstring("<span>{}</span>".format(element))
            )
            return etree.tostring(value)
        raise NotImplementedError(
            "Cannot convert elements of type {}".format(type(element))
        ) 
Example #20
Source File: test_magic_envelope.py    From federation with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_payload_extracted_on_init(self, diaspora_public_payload):
        env = MagicEnvelope(payload=diaspora_public_payload)
        assert isinstance(env.doc, _Element)
        assert env.author_handle == "foobar@example.com"
        assert env.message == b"<status_message><foo>bar</foo></status_message>" 
Example #21
Source File: texmath.py    From acl-anthology with Apache License 2.0 5 votes vote down vote up
def _parse(self, everything, trg):
        """Parses a list of TeX constituents into an lxml.etree._Element.

        Arguments:
            everything: An iterator over TeX constituents as provided by TexSoup
            trg: The lxml.etree._Element to parse the expression into

        The approach of iterating over the TeX constituents roughly follows
        <https://github.com/alvinwan/TexSoup/blob/master/examples/list_everything.py>.
        """
        sxscript = False  # Tracks whether we're in a subscript/superscript
        for code in everything:
            if isinstance(code, TexSoup.TexCmd):
                # code is a TeX command
                self._parse_command(code, trg)
            elif isinstance(code, TexSoup.TokenWithPosition):
                # code is text
                sxscript = self._parse_text(code, trg)
            elif isinstance(code, TexSoup.Arg):
                # If in subscript/superscript, wrap the entire element in respective tag
                if sxscript:
                    my_trg = etree.Element(sxscript)
                    self._parse(TexSoup.TexSoup(code.value).expr.everything, my_trg)
                    trg.append(my_trg)
                    sxscript = False
                # Otherwise, just parse it normally
                else:
                    self._parse(TexSoup.TexSoup(code.value).expr.everything, trg)
            else:
                log.error("TeX-math parser got unhandled element: {}".format(type(code))) 
Example #22
Source File: parser.py    From pymoca with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def walk(e: etree._Element, l: ModelListener) -> None:
    tag = e.tag
    l.call('enter_every_before', e)
    l.call('enter_' + tag, e)
    l.call('enter_every_after', e)
    for c in e.getchildren():
        walk(c, l)
    l.call('exit_every_before', e)
    l.call('exit_' + tag, e)
    l.call('exit_every_after', e) 
Example #23
Source File: parser.py    From pymoca with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def exit_else(self, tree: etree._Element):
        self.model[tree] = [self.model[c] for c in tree]


# noinspection PyProtectedMember 
Example #24
Source File: parser.py    From pymoca with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def exit_then(self, tree: etree._Element):
        self.model[tree] = [self.model[c] for c in tree] 
Example #25
Source File: parser.py    From pymoca with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def exit_cond(self, tree: etree._Element):
        assert len(tree) == 1
        self.model[tree] = self.model[tree[0]] 
Example #26
Source File: parser.py    From pymoca with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def exit_when(self, tree: etree._Element):
        assert len(tree) == 2
        cond = self.model[tree[0]]
        then = self.model[tree[1]]
        self.model[tree] = {
            'cond': self.cond(cond),
            'then': then
        }
        self.scope['when_eqs'].append(self.model[tree]) 
Example #27
Source File: parser.py    From pymoca with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def exit_false(self, tree: etree._Element):
        self.model[tree] = False 
Example #28
Source File: parser.py    From pymoca with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def exit_true(self, tree: etree._Element):
        self.model[tree] = True 
Example #29
Source File: parser.py    From pymoca with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def exit_real(self, tree: etree._Element):
        self.model[tree] = float(tree.attrib["value"]) 
Example #30
Source File: parser.py    From pymoca with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def exit_item(self, tree: etree._Element):
        assert len(tree) == 1
        self.model[tree] = {
            tree.attrib['name']: self.model[tree[0]]
        }