Python lxml.etree() Examples

The following are 30 code examples of lxml.etree(). 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 , or try the search function .
Example #1
Source File: session.py    From mlbstreamer with GNU General Public License v2.0 6 votes vote down vote up
def update_api_keys(self):

        logger.debug("updating MLB api keys")
        content = self.session.get(self.MLB_API_KEY_URL).text
        parser = lxml.etree.HTMLParser()
        data = lxml.etree.parse(StringIO(content), parser)

        scripts = data.xpath(".//script")
        for script in scripts:
            if script.text and "apiKey" in script.text:
                self._state.api_key = self.API_KEY_RE.search(script.text).groups()[0]
            if script.text and "clientApiKey" in script.text:
                self._state.client_api_key = self.CLIENT_API_KEY_RE.search(script.text).groups()[0]

        logger.debug("updating Okta api keys")
        content = self.session.get(self.MLB_OKTA_URL).text
        self._state.okta_client_id = self.OKTA_CLIENT_ID_RE.search(content).groups()[0]
        self.save() 
Example #2
Source File: pci.py    From qubes-core-admin with GNU Lesser General Public License v2.1 6 votes vote down vote up
def on_device_list_attached(self, vm, event, **kwargs):
        # pylint: disable=unused-argument,no-self-use
        if not vm.is_running() or isinstance(vm, qubes.vm.adminvm.AdminVM):
            return
        xml_desc = lxml.etree.fromstring(vm.libvirt_domain.XMLDesc())

        for hostdev in xml_desc.findall('devices/hostdev'):
            if hostdev.get('type') != 'pci':
                continue
            address = hostdev.find('source/address')
            bus = address.get('bus')[2:]
            device = address.get('slot')[2:]
            function = address.get('function')[2:]

            ident = '{bus}_{device}.{function}'.format(
                bus=bus,
                device=device,
                function=function,
            )
            yield (PCIDevice(vm.app.domains[0], ident), {}) 
Example #3
Source File: generic.py    From n6 with GNU Affero General Public License v3.0 6 votes vote down vote up
def rss_item_to_relevant_data(self, item):
        """
        Extract the relevant data from the given RSS item.

        Args:
            `item`:
                A single item from the RSS feed.  Such an
                item is an element of a list obtained with a
                `<lxml etree/html document>.xpath(...)` call
                (see the source code of the _process_rss()
                method).

        Returns:
            Some hashable object.  It may be, for example, a
            tuple or a string -- the exact type depends on the
            implementation provided by a particular subclass
            of BaseRSSCollector.
        """
        raise NotImplementedError 
Example #4
Source File: test_of_config.py    From ryu with Apache License 2.0 5 votes vote down vote up
def _print_ports(self, tree, ns):
        for port in tree.findall('{%s}%s/{%s}%s' % (ns, ofc_consts.RESOURCES,
                                                    ns, ofc_consts.PORT)):
            print(lxml.etree.tostring(port, pretty_print=True)) 
Example #5
Source File: test_of_config.py    From ryu with Apache License 2.0 5 votes vote down vote up
def _do_get_config(self, source):
        print('source = %s' % source)
        config_xml = self.switch.raw_get_config(source)

        tree = lxml.etree.fromstring(config_xml)
        # print(lxml.etree.tostring(tree, pretty_print=True))
        self._validate(tree) 
Example #6
Source File: utils.py    From libvirt-test-API with GNU General Public License v2.0 5 votes vote down vote up
def get_xml_value(dom, path):
    dom_xml = dom.XMLDesc(0)
    tree = lxml.etree.fromstring(dom_xml)
    return tree.xpath(path) 
Example #7
Source File: connection_allocPages.py    From libvirt-test-API with GNU General Public License v2.0 5 votes vote down vote up
def get_host_pagesize(conn):
    ret = []
    tree = lxml.etree.fromstring(conn.getCapabilities())

    set = tree.xpath("/capabilities/host/cpu/pages")
    for n in set:
        ret.append(int(n.attrib['size']))

    return ret 
Example #8
Source File: add_iothread.py    From libvirt-test-API with GNU General Public License v2.0 5 votes vote down vote up
def find_iothreadid_fromxml(vm, running, iothreadid):
    if (running == 1):
        tree = lxml.etree.fromstring(vm.XMLDesc(0))
    else:
        tree = lxml.etree.fromstring(vm.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE))

    set = tree.xpath("//iothreadids/iothread")
    for n in set:
        ids = n.attrib['id']
        if int(ids) == iothreadid:
            return True

    return False 
Example #9
Source File: info_iothread.py    From libvirt-test-API with GNU General Public License v2.0 5 votes vote down vote up
def find_iothreadid_fromxml(vm, running, iothreadid):
    if (running == 1):
        tree = lxml.etree.fromstring(vm.XMLDesc(0))
    else:
        tree = lxml.etree.fromstring(vm.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE))

    set = tree.xpath("//iothreadids/iothread")
    for n in set:
        ids = n.attrib['id']
        if int(ids) == iothreadid:
            return True

    return False 
Example #10
Source File: pin_iothread.py    From libvirt-test-API with GNU General Public License v2.0 5 votes vote down vote up
def find_iothreadpin_fromxml(vm, running, iothreadid):
    if (running == 1):
        tree = lxml.etree.fromstring(vm.XMLDesc(0))
    else:
        tree = lxml.etree.fromstring(vm.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE))

    set = tree.xpath("//cputune/iothreadpin")
    for n in set:
        ids = n.attrib["iothread"]
        cpuset_xml = n.attrib["cpuset"]
        if int(ids) == iothreadid:
            logger.info("cpuset in xml is %s" % cpuset_xml)
            return cpuset_xml

    return False 
Example #11
Source File: fsinfo.py    From libvirt-test-API with GNU General Public License v2.0 5 votes vote down vote up
def get_guest_mac(vm):
    tree = lxml.etree.fromstring(vm.XMLDesc(0))
    set = tree.xpath("/domain/devices/interface/mac")

    for n in set:
        return n.attrib['address']

    return False 
Example #12
Source File: fsinfo.py    From libvirt-test-API with GNU General Public License v2.0 5 votes vote down vote up
def check_agent_status(vm):
    """ make sure agent is okay to use """

    tree = lxml.etree.fromstring(vm.XMLDesc(0))

    set = tree.xpath("//channel[@type='unix']/target[@name='org.qemu.guest_agent.0']")
    for n in set:
        if n.attrib['state'] == 'connected':
            return True

    return False 
Example #13
Source File: del_iothread.py    From libvirt-test-API with GNU General Public License v2.0 5 votes vote down vote up
def find_iothreadid_fromxml(vm, running, iothreadid):
    if (running == 1):
        tree = lxml.etree.fromstring(vm.XMLDesc(0))
    else:
        tree = lxml.etree.fromstring(vm.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE))

    set = tree.xpath("//iothreadids/iothread")
    for n in set:
        ids = n.attrib['id']
        if int(ids) == iothreadid:
            return True

    return False 
Example #14
Source File: set_memory_period.py    From libvirt-test-API with GNU General Public License v2.0 5 votes vote down vote up
def get_period_fromxml(vm, running):
    if (running == 1):
        tree = lxml.etree.fromstring(vm.XMLDesc(0))
    else:
        tree = lxml.etree.fromstring(vm.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE))

    set = tree.xpath("//memballoon/stats")
    if len(set) == 0:
        return 0
    for n in set:
        period = n.attrib['period']
        return period 
Example #15
Source File: set_user_passwd.py    From libvirt-test-API with GNU General Public License v2.0 5 votes vote down vote up
def check_agent_status(vm):
    """ make sure agent is okay to use """

    tree = lxml.etree.fromstring(vm.XMLDesc(0))

    set = tree.xpath("//channel[@type='unix']/target[@name='org.qemu.guest_agent.0']")
    for n in set:
        if n.attrib['state'] == 'connected':
            return True

    return False 
Example #16
Source File: client.py    From BhagavadGita with GNU General Public License v3.0 5 votes vote down vote up
def get_etree():
    global _etree
    if _etree is not None:
        return _etree
    try:
        from lxml import etree as _etree
    except ImportError:
        try:
            from xml.etree import cElementTree as _etree
        except ImportError:
            try:
                from xml.etree import ElementTree as _etree
            except ImportError:
                raise TypeError('lxml or etree not found')
    return _etree 
Example #17
Source File: xml_tools_test.py    From dm_control with Apache License 2.0 5 votes vote down vote up
def test_nested(self):
    element = etree.Element('inserted')
    xml_tools.nested_element(element, depth=2)
    level_1 = element.find('inserted')
    self.assertIsNotNone(level_1)
    level_2 = level_1.find('inserted')
    self.assertIsNotNone(level_2) 
Example #18
Source File: xml_tools_test.py    From dm_control with Apache License 2.0 5 votes vote down vote up
def test_tostring(self):
    xml_str = """
    <root>
      <head>
        <content></content>
      </head>
    </root>"""
    tree = xml_tools.parse(six.StringIO(xml_str))
    self.assertEqual(b'<root>\n  <head>\n    <content/>\n  </head>\n</root>\n',
                     etree.tostring(tree, pretty_print=True)) 
Example #19
Source File: generic.py    From n6 with GNU Affero General Public License v3.0 5 votes vote down vote up
def _process_rss(self, result):
        try:
            document = lxml.etree.fromstring(result)
        except lxml.etree.XMLSyntaxError:
            document = lxml.html.fromstring(result)
        data_row_xpath = "//item"
        rows = document.xpath(data_row_xpath)
        return set(map(self.rss_item_to_relevant_data, rows)) 
Example #20
Source File: response.py    From pyspider with Apache License 2.0 5 votes vote down vote up
def doc(self):
        """Returns a PyQuery object of the response's content"""
        if hasattr(self, '_doc'):
            return self._doc
        elements = self.etree
        doc = self._doc = PyQuery(elements)
        doc.make_links_absolute(utils.text(self.url))
        return doc 
Example #21
Source File: response.py    From pyspider with Apache License 2.0 5 votes vote down vote up
def etree(self):
        """Returns a lxml object of the response's content that can be selected by xpath"""
        if not hasattr(self, '_elements'):
            try:
                parser = lxml.html.HTMLParser(encoding=self.encoding)
                self._elements = lxml.html.fromstring(self.content, parser=parser)
            except LookupError:
                # lxml would raise LookupError when encoding not supported
                # try fromstring without encoding instead.
                # on windows, unicode is not availabe as encoding for lxml
                self._elements = lxml.html.fromstring(self.content)
        if isinstance(self._elements, lxml.etree._ElementTree):
            self._elements = self._elements.getroot()
        return self._elements 
Example #22
Source File: base_handlers.py    From django-taxii-services with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def evaluate_criteria(cls, prp, content_etree, criteria):
        """
        Evaluates the criteria in a query. Note that criteria can have
        child criteria (which will cause recursion) and child criterion.

        Arguments:
            content_etree - an lxml etree to evaluate
            criteria - the criteria to evaluate against the etree

        Returns:
            True or False, indicating whether the content_etree
            matches the criteria

        """

        for child_criteria in criteria.criteria:
            value = cls.evaluate_criteria(prp, content_etree, child_criteria)
            if value is True and criteria.operator == tdq.OP_OR:
                return True
            elif value is False and criteria.operator == tdq.OP_AND:
                return False
            else:  # Don't know anything for sure yet
                pass

        for criterion in criteria.criterion:
            value = cls.evaluate_criterion(prp, content_etree, criterion)
            # TODO: Is there a way to keep this DRY?
            if value is True and criteria.operator == tdq.OP_OR:
                return True
            elif value is False and criteria.operator == tdq.OP_AND:
                return False
            else:  # Don't know anything for sure yet
                pass

        return criteria.operator == tdq.OP_AND 
Example #23
Source File: base_handlers.py    From django-taxii-services with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def evaluate_criterion(cls, prp, content_etree, criterion):
        """
        Evaluates the criterion in a query by turning the Criterion into an XPath and
        evaluating it against the content_etree

        Arguments:
            content_etree - an lxml etree to evaluate
            criterion - the criterion to evaluate against the etree

        Returns:
            True or False, indicating whether the content_etree
            matches the criterion
        """

        xpath, nsmap = cls.get_xpath(prp, criterion)
        # print xpath
        matches = content_etree.xpath(xpath, namespaces=nsmap)
        # XPath results can be a boolean (True, False) or
        # a NodeSet
        if matches in (True, False):  # The result is boolean, take it literally
            result = matches
        else:  # The result is a NodeSet. The Criterion is True iff there are >0 resulting nodes
            result = len(matches) > 0

        if criterion.negate:
            return not result

        return result 
Example #24
Source File: pci.py    From qubes-core-admin with GNU Lesser General Public License v2.1 5 votes vote down vote up
def description(self):
        if self._description is None:
            hostdev_details = \
                self.backend_domain.app.vmm.libvirt_conn.nodeDeviceLookupByName(
                    self.libvirt_name
                )
            self._description = _device_desc(lxml.etree.fromstring(
                hostdev_details.XMLDesc()))
        return self._description 
Example #25
Source File: pci.py    From qubes-core-admin with GNU Lesser General Public License v2.1 5 votes vote down vote up
def on_device_list_pci(self, vm, event):
        # pylint: disable=unused-argument,no-self-use
        # only dom0 expose PCI devices
        if vm.qid != 0:
            return

        for dev in vm.app.vmm.libvirt_conn.listAllDevices():
            if 'pci' not in dev.listCaps():
                continue

            xml_desc = lxml.etree.fromstring(dev.XMLDesc())
            libvirt_name = xml_desc.findtext('name')
            yield PCIDevice(vm, None, libvirt_name=libvirt_name) 
Example #26
Source File: test_of_config.py    From ryu with Apache License 2.0 5 votes vote down vote up
def _do_edit_config(self, config):
        tree = lxml.etree.fromstring(config)
        self._validate(tree)
        self.switch.raw_edit_config(target='running', config=config) 
Example #27
Source File: xml_serialiser.py    From acvtool with Apache License 2.0 5 votes vote down vote up
def get_xml(self):
        report = Element("report")
        report.set("name", self.app_name)
        
        groups = Utils2.get_groupped_classes(self.smalitree)
        for g in groups:
            package = SubElement(report,"package")
            package.set("name", Utils2.get_package_name(g[0].name))
            for cl in g:
                if (cl.is_coverable()):
                    self.add_xml_class(package, cl)
        return etree.tostring(report, pretty_print=True) 
Example #28
Source File: types.py    From pyang with ISC License 5 votes vote down vote up
def _prepare_documents(cls):
        if cls._schema is None:
            cls._schema = lxml.etree.fromstring(cls.SCHEMA)
            cls._avalue = lxml.etree.fromstring(cls.AVALUE)
            cls._pattern = cls._schema[0][0][0][0] 
Example #29
Source File: types.py    From pyang with ISC License 5 votes vote down vote up
def __init__(self, spec, pos, invert_match):
        self._prepare_documents()
        self.spec = spec
        self.pos = pos
        self.invert_match = invert_match

        self._pattern.set('value', spec)
        try:
            self.schema = lxml.etree.XMLSchema(etree=self._schema)
        except lxml.etree.XMLSchemaParseError as err:
            self.schema = None
            self.error = err
        else:
            self.error = None 
Example #30
Source File: test_of_config.py    From ryu with Apache License 2.0 5 votes vote down vote up
def _get_schema():
    # file_name = of_config.OF_CONFIG_1_0_XSD
    # file_name = of_config.OF_CONFIG_1_1_XSD
    file_name = of_config.OF_CONFIG_1_1_1_XSD
    return lxml.etree.XMLSchema(file=file_name)