Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_unicode(self):
if sys.version_info[0] < 3:
css = '.a\xc1b'.decode('ISO-8859-1')
else:
css = '.a\xc1b'
xpath = GenericTranslator().css_to_xpath(css)
assert css[1:] in xpath
xpath = xpath.encode('ascii', 'xmlcharrefreplace').decode('ASCII')
assert xpath == (
"descendant-or-self::*[@class and contains("
"concat(' ', normalize-space(@class), ' '), ' aÁb ')]")
def test_unicode_escapes(self):
# \22 == '"' \20 == ' '
css_to_xpath = GenericTranslator().css_to_xpath
assert css_to_xpath(r'*[aval="\'\22\'"]') == (
'''descendant-or-self::*[@aval = concat("'",'"',"'")]''')
assert css_to_xpath(r'*[aval="\'\22 2\'"]') == (
'''descendant-or-self::*[@aval = concat("'",'"2',"'")]''')
assert css_to_xpath(r'*[aval="\'\20 \'"]') == (
'''descendant-or-self::*[@aval = "' '"]''')
assert css_to_xpath('*[aval="\'\\20\r\n \'"]') == (
'''descendant-or-self::*[@aval = "' '"]''')
def test_quoting(self):
css_to_xpath = GenericTranslator().css_to_xpath
assert css_to_xpath('*[aval="\'"]') == (
'''descendant-or-self::*[@aval = "'"]''')
assert css_to_xpath('*[aval="\'\'\'"]') == (
"""descendant-or-self::*[@aval = "'''"]""")
assert css_to_xpath('*[aval=\'"\']') == (
'''descendant-or-self::*[@aval = '"']''')
assert css_to_xpath('*[aval=\'"""\']') == (
'''descendant-or-self::*[@aval = '"""']''')
def __disable_listeners(jmx):
"""
Set ResultCollector to disabled
:param jmx: JMX
:return:
"""
sel = 'stringProp[name=filename]'
xpath = GenericTranslator().css_to_xpath(sel)
listeners = jmx.get('ResultCollector')
for listener in listeners:
file_setting = listener.xpath(xpath)
if not file_setting or not file_setting[0].text:
listener.set("enabled", "false")
def _get_request_body(self, element, request_config):
"""
Get body params from sampler
:param element:
:return: dict
"""
raw_body = self._get_bool_prop(element, 'HTTPSampler.postBodyRaw')
query = 'elementProp[name="HTTPsampler.Arguments"]>collectionProp>elementProp'
xpath = GenericTranslator().css_to_xpath(query)
if raw_body:
http_args_element = element.xpath(xpath)[0]
body = self._get_string_prop(http_args_element, 'Argument.value')
if body:
self.log.debug('Got %s for body in %s (%s)', body, element.tag, element.get("name"))
return {'body': body}
else:
return {}
else:
url = request_config.get('url', '')
method = request_config.get('method', 'get')
return self._get_params(element, xpath, url=url, method=method)
def match_selector(rule, tree):
"""Yield the ``(element, specificity)`` in ``tree`` matching ``rule``."""
selector_list = cssselect.parse(rule.selector.as_css())
translator = cssselect.GenericTranslator()
for selector in selector_list:
if not selector.pseudo_element:
specificity = selector.specificity()
for element in tree.xpath(translator.selector_to_xpath(selector)):
yield element, specificity
except ImportError:
raise ImportError(
'cssselect does not seem to be installed. '
'See http://packages.python.org/cssselect/')
SelectorSyntaxError = external_cssselect.SelectorSyntaxError
ExpressionError = external_cssselect.ExpressionError
SelectorError = external_cssselect.SelectorError
__all__ = ['SelectorSyntaxError', 'ExpressionError', 'SelectorError',
'CSSSelector']
class LxmlTranslator(external_cssselect.GenericTranslator):
"""
A custom CSS selector to XPath translator with lxml-specific extensions.
"""
def xpath_contains_function(self, xpath, function):
# Defined there, removed in later drafts:
# http://www.w3.org/TR/2001/CR-css3-selectors-20011113/#content-selectors
if function.argument_types() not in (['STRING'], ['IDENT']):
raise ExpressionError(
"Expected a single string or ident for :contains(), got %r"
% function.arguments)
value = function.arguments[0].value
return xpath.add_condition(
'contains(__lxml_internal_css:lower-case(string(.)), %s)'
% self.xpath_literal(value.lower()))
return xpath
def xpath_attr_functional_pseudo_element(self, xpath, function):
if function.argument_types() not in (['STRING'], ['IDENT']):
raise ExpressionError(
"Expected a single string or ident for ::attr(), got %r"
% function.arguments)
return ScrapyXPathExpr.from_xpath(xpath,
attribute=function.arguments[0].value)
def xpath_text_simple_pseudo_element(self, xpath):
"""Support selecting text nodes using ::text pseudo-element"""
return ScrapyXPathExpr.from_xpath(xpath, textnode=True)
class ScrapyGenericTranslator(TranslatorMixin, GenericTranslator):
pass
class ScrapyHTMLTranslator(TranslatorMixin, HTMLTranslator):
pass
class CSSSelectorMixin(object):
def select(self, css):
xpath = self._css2xpath(css)
return super(CSSSelectorMixin, self).select(xpath)
def _css2xpath(self, css):
return self.translator.css_to_xpath(css)
if not method:
raise ExpressionError("The pseudo-element ::%s is unknown" % pseudo_element)
xpath = method(xpath)
return xpath
def xpath_attr_functional_pseudo_element(self, xpath, function):
if function.argument_types() not in (['STRING'], ['IDENT']):
raise ExpressionError("Expected a single string or ident for ::attr(), got %r" % function.arguments)
return CdeXPathExpr.from_xpath(xpath, attribute=function.arguments[0].value)
def xpath_text_simple_pseudo_element(self, xpath):
"""Support selecting text nodes using ::text pseudo-element"""
return CdeXPathExpr.from_xpath(xpath, textnode=True)
class CssXmlTranslator(TranslatorMixin, GenericTranslator):
pass
class CssHTMLTranslator(TranslatorMixin, HTMLTranslator):
pass