Skip to content

Commit

Permalink
Merge pull request #109 from nctl144/lru_cache
Browse files Browse the repository at this point in the history
[MRG+1] add lru_cache to css2xpath
  • Loading branch information
kmike authored May 31, 2018
2 parents f6103c8 + 0bedb8a commit 5323b83
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
17 changes: 14 additions & 3 deletions parsel/csstranslator.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import six

if six.PY2:
from functools32 import lru_cache
else:
from functools import lru_cache

from cssselect import GenericTranslator as OriginalGenericTranslator
from cssselect import HTMLTranslator as OriginalHTMLTranslator
from cssselect.xpath import XPathExpr as OriginalXPathExpr
from cssselect.xpath import _unicode_safe_getattr, ExpressionError
from cssselect.parser import FunctionalPseudoElement
from cssselect.parser import parse, FunctionalPseudoElement


class XPathExpr(OriginalXPathExpr):
Expand Down Expand Up @@ -91,11 +98,15 @@ def xpath_text_simple_pseudo_element(self, xpath):


class GenericTranslator(TranslatorMixin, OriginalGenericTranslator):
pass
@lru_cache(maxsize=256)
def css_to_xpath(self, css, prefix='descendant-or-self::'):
return super(GenericTranslator, self).css_to_xpath(css, prefix)


class HTMLTranslator(TranslatorMixin, OriginalHTMLTranslator):
pass
@lru_cache(maxsize=256)
def css_to_xpath(self, css, prefix='descendant-or-self::'):
return super(HTMLTranslator, self).css_to_xpath(css, prefix)


_translator = HTMLTranslator()
Expand Down
35 changes: 28 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys

from setuptools import setup
from pkg_resources import parse_version
from setuptools import setup, __version__ as setuptools_version


with open('README.rst') as readme_file:
Expand All @@ -14,6 +16,29 @@
test_requirements = [
]

def has_environment_marker_platform_impl_support():
"""Code extracted from 'pytest/setup.py'
https://github.com/pytest-dev/pytest/blob/7538680c/setup.py#L31
The first known release to support environment marker with range operators
it is 18.5, see:
https://setuptools.readthedocs.io/en/latest/history.html#id235
"""
return parse_version(setuptools_version) >= parse_version('18.5')

install_requires = [
'w3lib>=1.8.0',
'lxml>=2.3',
'six>=1.5.2',
'cssselect>=0.9'
]
extras_require = {}

if not has_environment_marker_platform_impl_support():
if sys.version_info[0:2] < (3, 0):
install_requires.append("functools32")
else:
extras_require[":python_version<'3.0'"] = ["functools32"]

setup(
name='parsel',
version='1.4.0',
Expand All @@ -28,12 +53,8 @@
package_dir={'parsel':
'parsel'},
include_package_data=True,
install_requires=[
'w3lib>=1.8.0',
'lxml>=2.3',
'six>=1.5.2',
'cssselect>=0.9',
],
install_requires=install_requires,
extras_require=extras_require,
license="BSD",
zip_safe=False,
keywords='parsel',
Expand Down

0 comments on commit 5323b83

Please sign in to comment.