Python lxml.etree 模块,XPathEvalError() 实例源码

我们从Python开源项目中,提取了以下11个代码示例,用于说明如何使用lxml.etree.XPathEvalError()

项目:aiida-fleur    作者:broeder-j    | 项目源码 | 文件源码
def eval_xpath(node, xpath, parser_info={'parser_warnings':[]}):
    """
    Tries to evalutate an xpath expression. If it fails it logs it.

    :param root node of an etree and an xpath expression (relative, or absolute)
    :returns either nodes, or attributes, or text
    """
    try:
        return_value = node.xpath(xpath)
    except etree.XPathEvalError:
        parser_info['parser_warnings'].append('There was a XpathEvalError on the xpath: {} \n'
            'Either it does not exist, or something is wrong with the expression.'.format(xpath))
        # TODO maybe raise an error again to catch in upper routine, to know where exactly
        return []
    if len(return_value) == 1:
        return return_value[0]
    else:
        return return_value
项目:aiida-fleur    作者:broeder-j    | 项目源码 | 文件源码
def eval_xpath(node, xpath):
    """
    Tries to evalutate an xpath expression. If it fails it logs it.

    :param root node of an etree and an xpath expression (relative, or absolute)
    :returns either nodes, or attributes, or text
    """
    try:
        return_value = node.xpath(xpath)
    except XPathEvalError:
        parser_info['parser_warnings'].append('There was a XpathEvalError on the xpath: {} \n'
            'Either it does not exist, or something is wrong with the expression.'.format(xpath))
        # TODO maybe raise an error again to catch in upper routine, to know where exactly
        return []
    if len(return_value) == 1:
        return return_value[0]
    else:
        return return_value
项目:maas    作者:maas    | 项目源码 | 文件源码
def try_match_xpath(xpath, doc, logger=logging):
    """See if the XPath expression matches the given XML document.

    Invalid XPath expressions are logged, and are returned as a
    non-match.

    :type xpath: Either `unicode` or `etree.XPath`
    :type doc: Either `etree._ElementTree` or `etree.XPathDocumentEvaluator`

    :rtype: bool
    """
    try:
        # Evaluating an XPath expression against a document with LXML
        # can return a list or a string, and perhaps other types.
        # Casting the return value into a boolean context appears to
        # be the most reliable way of detecting a match.
        return bool(match_xpath(xpath, doc))
    except etree.XPathEvalError as error:
        # Get a plaintext version of `xpath`.
        expr = xpath.path if is_compiled_xpath(xpath) else xpath
        logger.warning("Invalid expression '%s': %s", expr, str(error))
        return False
项目:repo-checker    作者:1dot75cm    | 项目源码 | 文件源码
def _post_xpath(self, rules):
        """????????"""
        _data = []
        log.debug("rules: %s, %s" % (rules[0], rules[1]))
        tree = html.fromstring(self.resp.content)

        for rule in rules:
            if not rule:
                _data.append("none")  # ???
                break

            try:
                log.debug("match: %s" % tree.xpath(rule)[0])
                _data.append(self._process_data(tree.xpath(rule)))
            except etree.XPathEvalError as e:
                _data.append(self._process_data(
                    [i.text_content() for i in tree.cssselect(rule)]))
            except IndexError:
                _data.append("error")  # ??????

        return _data  # (date, commit)
项目:BlogSpider    作者:hack4code    | 项目源码 | 文件源码
def remove_element_with_xpath(self, doc, removed_xpath_nodes):
        for xpath in filter(lambda _: _.strip(),
                            removed_xpath_nodes):
            try:
                nodes = doc.xpath(xpath)
            except XPathEvalError:
                logger.error('Error in pipeline content invalid xpath[%s]',
                             xpath)
            else:
                for _ in nodes:
                    _.drop_tree()
        return doc
项目:aiida-fleur    作者:broeder-j    | 项目源码 | 文件源码
def get_tag(self, xpath):
        """
        Tries to evalutate an xpath expression. If it fails it logs it.

        :param root node of an etree and an xpath expression (relative, or absolute)
        :returns ALWAYS a node list
        """
        from lxml import etree

        if 'inp.xml' in self.files:
            # read in inpxml
            inpxmlfile = self.get_file_abs_path('inp.xml')

            if self._schema_file_path: # Schema there, parse with schema
                #xmlschema_doc = etree.parse(self._schema_file_path)
                #xmlschema = etree.XMLSchema(xmlschema_doc)
                #parser = etree.XMLParser(schema=xmlschema, attribute_defaults=True)
                tree = etree.parse(inpxmlfile)#, parser)
            else: #schema not there, parse without
                print 'parsing inp.xml without XMLSchema'
                tree = etree.parse(inpxmlfile)

            root = tree.getroot()
        else:
            raise InputValidationError(
                      "No inp.xml file yet specified, to get a tag from")

        try:
            return_value = root.xpath(xpath)
        except etree.XPathEvalError:
            #print (
            raise InputValidationError(
                'There was a XpathEvalError on the xpath: {} \n Either it does '
                'not exist, or something is wrong with the expression.'
                ''.format(xpath))
            return []
        if len(return_value) == 1:
            return return_value
        else:
            return return_value
项目:aiida-fleur    作者:broeder-j    | 项目源码 | 文件源码
def eval_xpath2(node, xpath, parser_info={'parser_warnings':[]}):
    """
    Tries to evalutate an xpath expression. If it fails it logs it.

    :param root node of an etree and an xpath expression (relative, or absolute)
    :returns either nodes, or attributes, or text
    """
    try:
        return_value = node.xpath(xpath)
    except etree.XPathEvalError:
        parser_info['parser_warnings'].append('There was a XpathEvalError on the xpath: {} \n'
            'Either it does not exist, or something is wrong with the expression.'.format(xpath))
        # TODO maybe raise an error again to catch in upper routine, to know where exactly
        return []
    return return_value
项目:aiida-fleur    作者:broeder-j    | 项目源码 | 文件源码
def eval_xpath3(node, xpath, create=False, place_index=None, tag_order=None):
    """
    Tries to evalutate an xpath expression. If it fails it logs it.

    :param root node of an etree and an xpath expression (relative, or absolute)
    :returns ALWAYS a node list
    """
    #print('call eval_xpath3')
    try:
        return_value = node.xpath(xpath)
    except etree.XPathEvalError:
        message = (
            'There was a XpathEvalError on the xpath: {} \n Either it does '
            'not exist, or something is wrong with the expression.'
            ''.format(xpath))
        raise etree.XPathEvalError(message)
        return []

    if return_value == []:
        if create:
            #print node, xpath, create
            x_pieces =  [e for e in xpath.split('/') if e != ""]
            #x_pieces = xpath.split('/')
            xpathn = ''
            #for piece in x_pieces[:-1]:
            #    piece = piece + '/'
            #    xpathn = xpathn + piece
            #print x_pieces
            for piece in x_pieces[:-1]:
                xpathn = xpathn + '/'+ piece
            #print 'xpathn: {}'.format(xpathn)
            #node_c = eval_xpath3(node, xpathn, create)
            # this is REKURSIV! since create tag calls eval_xpath3
            create_tag(node, xpathn, x_pieces[-1], create=create, place_index=place_index, tag_order=tag_order)
            return_value = node.xpath(xpath)
            #print 'return:{}'.format(return_value)
            return return_value
        else:
            return return_value
    else:
        return return_value
项目:javapackages    作者:fedora-java    | 项目源码 | 文件源码
def xpath_query(self, query, boolean=False):
        if not query.startswith('/'):
            query = '//' + query
        if boolean:
            query = 'boolean({0})'.format(query)
        try:
            nsmap = dict(self.NSMAP)
            nsmap.update(self.root.nsmap)
            if None in nsmap:
                nsmap['default'] = nsmap[None]
                del nsmap[None]
            query_result = self.root.xpath(query, namespaces=nsmap)
        except etree.XPathEvalError as error:
            raise PomQueryInvalid("XPath query '{0}': {1}.".format(query,
                                                                   error))
        if not boolean:
            if len(query_result) == 0:
                raise PomQueryNoMatch(dedent("""\
                        XPath query '{0}' didn't match any node.
                        (Did you forget to specify 'pom:' namespace?)""").format(query))
            for i, element in enumerate(query_result):
                if hasattr(element, 'is_attribute') and element.is_attribute:
                    if not hasattr(element, 'attrname'):
                        element.attrname = self.root.xpath('name({q}[{i1}])'.format(q=query, i1=i + 1),
                                                           namespaces=nsmap)
        return query_result
项目:stixmarx    作者:mitre    | 项目源码 | 文件源码
def test_marking_path_parsing(self):
        """Test that parsed paths are applied correctly"""

        # paths to attempt for a global AMBER marking
        global_xpaths = [
            {
                "path": "//node() | //@*",
                "should_pass": True
            },
            {
                "path": "this is not a real xpath",
                "should_pass": False
            }
        ]
        # paths to attempt for a local RED marking
        local_xpaths = [
            {
                "path": "../../../descendant-or-self::node() | ../../../descendant-or-self::node()/@*",
                "should_pass": True
            },
            {
                "path": "this is not a real xpath",
                "should_pass": False
            }
        ]

        for global_path_dict in global_xpaths:
            for local_path_dict in local_xpaths:
                # Format our STIX XML template
                xml = STIX_XML_TEMPLATE_GLOBAL_AND_COMPONENT.format(global_path_dict["path"], local_path_dict["path"])
                xml_readable = StringIO(xml)

                # Build and parse the MarkingContainer
                try:
                    container = stixmarx.parse(xml_readable)
                except etree.XPathEvalError:
                    self.assertTrue(global_path_dict["should_pass"] is False or local_path_dict["should_pass"] is False)
                    continue

                package = container.package

                colors = [marking_spec.marking_structures[0].color for marking_spec in container.get_markings(package.indicators[0])]

                self.assertTrue(('AMBER' in colors) == global_path_dict["should_pass"])
                self.assertTrue(('RED' in colors) == local_path_dict["should_pass"])
项目:stixmarx    作者:mitre    | 项目源码 | 文件源码
def test_marking_path_parsing_for_observable(self):
        """Test that parsed paths are applied correctly to Observable"""

        # paths to attempt for a component RED marking
        observable_xpaths = [
            {
                "path": "../../../indicator:Observable[1]/descendant-or-self::node() | ../../../indicator:Observable[1]/descendant-or-self::node()/@*",
                "should_pass": True
            },
            {
                "path": "this is not a real xpath",
                "should_pass": False
            }
        ]
        # paths to attempt for a component AMBER marking
        component_xpaths = [
            {
                "path": "../../../descendant-or-self::node() | ../../../descendant-or-self::node()/@*",
                "should_pass": True
            },
            {
                "path": "this is not a real xpath",
                "should_pass": False
            }
        ]

        for observable_path_dict in observable_xpaths:
            for component_path_dict in component_xpaths:
                # Format our STIX XML template
                xml = STIX_XML_TEMPLATE_EMBEDDED_OBSERVABLE.format(
                    observable_path_dict["path"],
                    component_path_dict["path"]
                )
                xml_readable = StringIO(xml)

                # Build and parse the MarkingContainer
                try:
                    container = stixmarx.parse(xml_readable)
                except etree.XPathEvalError:
                    self.assertTrue(observable_path_dict["should_pass"] is False or component_path_dict["should_pass"] is False)
                    continue

                package = container.package

                colors = [marking_spec.marking_structures[0].color for marking_spec in container.get_markings(package.indicators[0].observable)]

                self.assertTrue(('RED' in colors) == observable_path_dict["should_pass"])
                self.assertTrue(('AMBER' in colors) == component_path_dict["should_pass"])