Skip to content

Commit 5323b83

Browse files
authored
Merge pull request #109 from nctl144/lru_cache
[MRG+1] add lru_cache to css2xpath
2 parents f6103c8 + 0bedb8a commit 5323b83

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

parsel/csstranslator.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
import six
2+
3+
if six.PY2:
4+
from functools32 import lru_cache
5+
else:
6+
from functools import lru_cache
7+
18
from cssselect import GenericTranslator as OriginalGenericTranslator
29
from cssselect import HTMLTranslator as OriginalHTMLTranslator
310
from cssselect.xpath import XPathExpr as OriginalXPathExpr
411
from cssselect.xpath import _unicode_safe_getattr, ExpressionError
5-
from cssselect.parser import FunctionalPseudoElement
12+
from cssselect.parser import parse, FunctionalPseudoElement
613

714

815
class XPathExpr(OriginalXPathExpr):
@@ -91,11 +98,15 @@ def xpath_text_simple_pseudo_element(self, xpath):
9198

9299

93100
class GenericTranslator(TranslatorMixin, OriginalGenericTranslator):
94-
pass
101+
@lru_cache(maxsize=256)
102+
def css_to_xpath(self, css, prefix='descendant-or-self::'):
103+
return super(GenericTranslator, self).css_to_xpath(css, prefix)
95104

96105

97106
class HTMLTranslator(TranslatorMixin, OriginalHTMLTranslator):
98-
pass
107+
@lru_cache(maxsize=256)
108+
def css_to_xpath(self, css, prefix='descendant-or-self::'):
109+
return super(HTMLTranslator, self).css_to_xpath(css, prefix)
99110

100111

101112
_translator = HTMLTranslator()

setup.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4+
import sys
45

5-
from setuptools import setup
6+
from pkg_resources import parse_version
7+
from setuptools import setup, __version__ as setuptools_version
68

79

810
with open('README.rst') as readme_file:
@@ -14,6 +16,29 @@
1416
test_requirements = [
1517
]
1618

19+
def has_environment_marker_platform_impl_support():
20+
"""Code extracted from 'pytest/setup.py'
21+
https://github.com/pytest-dev/pytest/blob/7538680c/setup.py#L31
22+
The first known release to support environment marker with range operators
23+
it is 18.5, see:
24+
https://setuptools.readthedocs.io/en/latest/history.html#id235
25+
"""
26+
return parse_version(setuptools_version) >= parse_version('18.5')
27+
28+
install_requires = [
29+
'w3lib>=1.8.0',
30+
'lxml>=2.3',
31+
'six>=1.5.2',
32+
'cssselect>=0.9'
33+
]
34+
extras_require = {}
35+
36+
if not has_environment_marker_platform_impl_support():
37+
if sys.version_info[0:2] < (3, 0):
38+
install_requires.append("functools32")
39+
else:
40+
extras_require[":python_version<'3.0'"] = ["functools32"]
41+
1742
setup(
1843
name='parsel',
1944
version='1.4.0',
@@ -28,12 +53,8 @@
2853
package_dir={'parsel':
2954
'parsel'},
3055
include_package_data=True,
31-
install_requires=[
32-
'w3lib>=1.8.0',
33-
'lxml>=2.3',
34-
'six>=1.5.2',
35-
'cssselect>=0.9',
36-
],
56+
install_requires=install_requires,
57+
extras_require=extras_require,
3758
license="BSD",
3859
zip_safe=False,
3960
keywords='parsel',

0 commit comments

Comments
 (0)