Skip to content

Commit 0ea8c38

Browse files
committed
rename XPathSelectorList as SelectorList scrapy#176
1 parent c08991c commit 0ea8c38

File tree

5 files changed

+37
-22
lines changed

5 files changed

+37
-22
lines changed

scrapy/selector/__init__.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
Two backends are currently available: lxml (default) and libxml2.
88
99
"""
10-
1110
import os
1211

13-
backend = os.environ.get('SCRAPY_SELECTORS_BACKEND')
1412

13+
backend = os.environ.get('SCRAPY_SELECTORS_BACKEND')
1514
if backend == 'libxml2':
1615
from scrapy.selector.libxml2sel import *
1716
elif backend == 'lxml':
@@ -26,3 +25,15 @@
2625
from scrapy.selector.lxmlsel import *
2726

2827
from scrapy.selector.csssel import *
28+
from scrapy.selector.list import SelectorList
29+
30+
31+
class XPathSelectorList(SelectorList):
32+
33+
def __init__(self, *a, **kw):
34+
import warnings
35+
from scrapy.exceptions import ScrapyDeprecationWarning
36+
warnings.warn('XPathSelectorList is deprecated, use '
37+
'scrapy.selector.SelectorList instead',
38+
category=ScrapyDeprecationWarning, stacklevel=1)
39+
super(XPathSelectorList, self).__init__(*a, **kw)

scrapy/selector/csssel.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from cssselect import GenericTranslator, HTMLTranslator
22
from scrapy.utils.python import flatten
3-
from scrapy.selector import HtmlXPathSelector, XmlXPathSelector, XPathSelectorList
3+
from scrapy.selector import HtmlXPathSelector, XmlXPathSelector
4+
from .list import SelectorList
45

5-
class CSSSelectorList(XPathSelectorList):
6+
7+
class CSSSelectorList(SelectorList):
68
def xpath(self, xpath):
79
return self.__class__(flatten([x.xpath(xpath) for x in self]))
810

@@ -12,6 +14,7 @@ def get(self, attr):
1214
def text(self, all=False):
1315
return self.__class__(flatten([x.text(all) for x in self]))
1416

17+
1518
class CSSSelectorMixin(object):
1619
def select(self, css):
1720
return CSSSelectorList(super(CSSSelectorMixin, self).select(self.translator.css_to_xpath(css)))
@@ -25,8 +28,10 @@ def text(self, all=False):
2528
def get(self, attr):
2629
return self.xpath('@' + attr)
2730

31+
2832
class XmlCSSSelector(CSSSelectorMixin, XmlXPathSelector):
2933
translator = GenericTranslator()
3034

35+
3136
class HtmlCSSSelector(CSSSelectorMixin, HtmlXPathSelector):
3237
translator = HTMLTranslator()

scrapy/selector/libxml2sel.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
from scrapy.utils.trackref import object_ref
1313
from scrapy.utils.decorator import deprecated
1414
from .libxml2document import Libxml2Document, xmlDoc_from_html, xmlDoc_from_xml
15-
from .list import XPathSelectorList
15+
from .list import SelectorList
16+
17+
18+
__all__ = ['HtmlXPathSelector', 'XmlXPathSelector', 'XPathSelector']
1619

17-
__all__ = ['HtmlXPathSelector', 'XmlXPathSelector', 'XPathSelector', \
18-
'XPathSelectorList']
1920

2021
class XPathSelector(object_ref):
2122

@@ -44,13 +45,13 @@ def select(self, xpath):
4445
except libxml2.xpathError:
4546
raise ValueError("Invalid XPath: %s" % xpath)
4647
if hasattr(xpath_result, '__iter__'):
47-
return XPathSelectorList([self.__class__(node=node, parent=self, \
48+
return SelectorList([self.__class__(node=node, parent=self, \
4849
expr=xpath) for node in xpath_result])
4950
else:
50-
return XPathSelectorList([self.__class__(node=xpath_result, \
51+
return SelectorList([self.__class__(node=xpath_result, \
5152
parent=self, expr=xpath)])
5253
else:
53-
return XPathSelectorList([])
54+
return SelectorList([])
5455

5556
def re(self, regex):
5657
return extract_regex(regex, self.extract())
@@ -62,7 +63,7 @@ def extract(self):
6263
if isinstance(self.xmlNode, libxml2.xmlDoc):
6364
data = self.xmlNode.getRootElement().serialize('utf-8')
6465
text = unicode(data, 'utf-8', errors='ignore') if data else u''
65-
elif isinstance(self.xmlNode, libxml2.xmlAttr):
66+
elif isinstance(self.xmlNode, libxml2.xmlAttr):
6667
# serialization doesn't work sometimes for xmlAttr types
6768
text = unicode(self.xmlNode.content, 'utf-8', errors='ignore')
6869
else:

scrapy/selector/list.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from scrapy.utils.python import flatten
22
from scrapy.utils.decorator import deprecated
33

4-
class XPathSelectorList(list):
4+
5+
class SelectorList(list):
56

67
def __getslice__(self, i, j):
78
return self.__class__(list.__getslice__(self, i, j))
@@ -18,6 +19,6 @@ def extract(self):
1819
def extract_unquoted(self):
1920
return [x.extract_unquoted() for x in self]
2021

21-
@deprecated(use_instead='XPathSelectorList.select')
22+
@deprecated(use_instead='SelectorList.select')
2223
def x(self, xpath):
2324
return self.select(xpath)

scrapy/selector/lxmlsel.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010
from scrapy.utils.decorator import deprecated
1111
from scrapy.http import TextResponse
1212
from .lxmldocument import LxmlDocument
13-
from .list import XPathSelectorList
13+
from .list import SelectorList
1414

1515

16-
__all__ = ['HtmlXPathSelector', 'XmlXPathSelector', 'XPathSelector', \
17-
'XPathSelectorList']
16+
__all__ = ['HtmlXPathSelector', 'XmlXPathSelector', 'XPathSelector']
1817

1918

2019
class XPathSelector(object_ref):
@@ -25,8 +24,8 @@ class XPathSelector(object_ref):
2524

2625
def __init__(self, response=None, text=None, namespaces=None, _root=None, _expr=None):
2726
if text is not None:
28-
response = TextResponse(url='about:blank', \
29-
body=unicode_to_str(text, 'utf-8'), encoding='utf-8')
27+
response = TextResponse(url='about:blank', encoding='utf-8',
28+
body=unicode_to_str(text, 'utf-8'))
3029
if response is not None:
3130
_root = LxmlDocument(response, self._parser)
3231

@@ -39,7 +38,7 @@ def select(self, xpath):
3938
try:
4039
xpathev = self._root.xpath
4140
except AttributeError:
42-
return XPathSelectorList([])
41+
return SelectorList([])
4342

4443
try:
4544
result = xpathev(xpath, namespaces=self.namespaces)
@@ -51,7 +50,7 @@ def select(self, xpath):
5150

5251
result = [self.__class__(_root=x, _expr=xpath, namespaces=self.namespaces)
5352
for x in result]
54-
return XPathSelectorList(result)
53+
return SelectorList(result)
5554

5655
def re(self, regex):
5756
return extract_regex(regex, self.extract())
@@ -84,10 +83,8 @@ def __nonzero__(self):
8483
def __str__(self):
8584
data = repr(self.extract()[:40])
8685
return "<%s xpath=%r data=%s>" % (type(self).__name__, self._expr, data)
87-
8886
__repr__ = __str__
8987

90-
9188
@deprecated(use_instead='XPathSelector.extract')
9289
def extract_unquoted(self):
9390
return self.extract()

0 commit comments

Comments
 (0)