Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

rename XPathSelectorList as SelectorList #176

Daniel Grañab38ac27eee70
diff --git a/scrapy/selector/__init__.py b/scrapy/selector/__init__.py
index 86ece8f0e..2b855d147 100644
--- a/scrapy/selector/__init__.py
+++ b/scrapy/selector/__init__.py
@@ -7,11 +7,10 @@ variable.
Two backends are currently available: lxml (default) and libxml2.
"""
-
import os
-backend = os.environ.get('SCRAPY_SELECTORS_BACKEND')
+backend = os.environ.get('SCRAPY_SELECTORS_BACKEND')
if backend == 'libxml2':
from scrapy.selector.libxml2sel import *
elif backend == 'lxml':
@@ -26,3 +25,15 @@ else:
from scrapy.selector.lxmlsel import *
from scrapy.selector.csssel import *
+from scrapy.selector.list import SelectorList
+
+
+class XPathSelectorList(SelectorList):
+
+ def __init__(self, *a, **kw):
+ import warnings
+ from scrapy.exceptions import ScrapyDeprecationWarning
+ warnings.warn('XPathSelectorList is deprecated, use '
+ 'scrapy.selector.SelectorList instead',
+ category=ScrapyDeprecationWarning, stacklevel=1)
+ super(XPathSelectorList, self).__init__(*a, **kw)
diff --git a/scrapy/selector/csssel.py b/scrapy/selector/csssel.py
index 9fce75877..13da8df95 100644
--- a/scrapy/selector/csssel.py
+++ b/scrapy/selector/csssel.py
@@ -1,8 +1,10 @@
from cssselect import GenericTranslator, HTMLTranslator
from scrapy.utils.python import flatten
-from scrapy.selector import HtmlXPathSelector, XmlXPathSelector, XPathSelectorList
+from scrapy.selector import HtmlXPathSelector, XmlXPathSelector
+from .list import SelectorList
-class CSSSelectorList(XPathSelectorList):
+
+class CSSSelectorList(SelectorList):
def xpath(self, xpath):
return self.__class__(flatten([x.xpath(xpath) for x in self]))
@@ -12,6 +14,7 @@ class CSSSelectorList(XPathSelectorList):
def text(self, all=False):
return self.__class__(flatten([x.text(all) for x in self]))
+
class CSSSelectorMixin(object):
def select(self, css):
return CSSSelectorList(super(CSSSelectorMixin, self).select(self.translator.css_to_xpath(css)))
@@ -25,8 +28,10 @@ class CSSSelectorMixin(object):
def get(self, attr):
return self.xpath('@' + attr)
+
class XmlCSSSelector(CSSSelectorMixin, XmlXPathSelector):
translator = GenericTranslator()
+
class HtmlCSSSelector(CSSSelectorMixin, HtmlXPathSelector):
translator = HTMLTranslator()
diff --git a/scrapy/selector/libxml2sel.py b/scrapy/selector/libxml2sel.py
index da546ade2..2f5d72ea1 100644
--- a/scrapy/selector/libxml2sel.py
+++ b/scrapy/selector/libxml2sel.py
@@ -12,10 +12,11 @@ from scrapy.utils.misc import extract_regex
from scrapy.utils.trackref import object_ref
from scrapy.utils.decorator import deprecated
from .libxml2document import Libxml2Document, xmlDoc_from_html, xmlDoc_from_xml
-from .list import XPathSelectorList
+from .list import SelectorList
+
+
+__all__ = ['HtmlXPathSelector', 'XmlXPathSelector', 'XPathSelector']
-__all__ = ['HtmlXPathSelector', 'XmlXPathSelector', 'XPathSelector', \
- 'XPathSelectorList']
class XPathSelector(object_ref):
@@ -44,13 +45,13 @@ class XPathSelector(object_ref):
except libxml2.xpathError:
raise ValueError("Invalid XPath: %s" % xpath)
if hasattr(xpath_result, '__iter__'):
- return XPathSelectorList([self.__class__(node=node, parent=self, \
+ return SelectorList([self.__class__(node=node, parent=self, \
expr=xpath) for node in xpath_result])
else:
- return XPathSelectorList([self.__class__(node=xpath_result, \
+ return SelectorList([self.__class__(node=xpath_result, \
parent=self, expr=xpath)])
else:
- return XPathSelectorList([])
+ return SelectorList([])
def re(self, regex):
return extract_regex(regex, self.extract())
@@ -62,7 +63,7 @@ class XPathSelector(object_ref):
if isinstance(self.xmlNode, libxml2.xmlDoc):
data = self.xmlNode.getRootElement().serialize('utf-8')
text = unicode(data, 'utf-8', errors='ignore') if data else u''
- elif isinstance(self.xmlNode, libxml2.xmlAttr):
+ elif isinstance(self.xmlNode, libxml2.xmlAttr):
# serialization doesn't work sometimes for xmlAttr types
text = unicode(self.xmlNode.content, 'utf-8', errors='ignore')
else:
diff --git a/scrapy/selector/list.py b/scrapy/selector/list.py
index a6977fd99..ea634db1f 100644
--- a/scrapy/selector/list.py
+++ b/scrapy/selector/list.py
@@ -1,7 +1,8 @@
from scrapy.utils.python import flatten
from scrapy.utils.decorator import deprecated
-class XPathSelectorList(list):
+
+class SelectorList(list):
def __getslice__(self, i, j):
return self.__class__(list.__getslice__(self, i, j))
@@ -18,6 +19,6 @@ class XPathSelectorList(list):
def extract_unquoted(self):
return [x.extract_unquoted() for x in self]
- @deprecated(use_instead='XPathSelectorList.select')
+ @deprecated(use_instead='SelectorList.select')
def x(self, xpath):
return self.select(xpath)
diff --git a/scrapy/selector/lxmlsel.py b/scrapy/selector/lxmlsel.py
index 5bf9fd1dc..18b3e5044 100644
--- a/scrapy/selector/lxmlsel.py
+++ b/scrapy/selector/lxmlsel.py
@@ -10,11 +10,10 @@ from scrapy.utils.python import unicode_to_str
from scrapy.utils.decorator import deprecated
from scrapy.http import TextResponse
from .lxmldocument import LxmlDocument
-from .list import XPathSelectorList
+from .list import SelectorList
-__all__ = ['HtmlXPathSelector', 'XmlXPathSelector', 'XPathSelector', \
- 'XPathSelectorList']
+__all__ = ['HtmlXPathSelector', 'XmlXPathSelector', 'XPathSelector']
class XPathSelector(object_ref):
@@ -25,8 +24,8 @@ class XPathSelector(object_ref):
def __init__(self, response=None, text=None, namespaces=None, _root=None, _expr=None):
if text is not None:
- response = TextResponse(url='about:blank', \
- body=unicode_to_str(text, 'utf-8'), encoding='utf-8')
+ response = TextResponse(url='about:blank', encoding='utf-8',
+ body=unicode_to_str(text, 'utf-8'))
if response is not None:
_root = LxmlDocument(response, self._parser)
@@ -39,7 +38,7 @@ class XPathSelector(object_ref):
try:
xpathev = self._root.xpath
except AttributeError:
- return XPathSelectorList([])
+ return SelectorList([])
try:
result = xpathev(xpath, namespaces=self.namespaces)
@@ -51,7 +50,7 @@ class XPathSelector(object_ref):
result = [self.__class__(_root=x, _expr=xpath, namespaces=self.namespaces)
for x in result]
- return XPathSelectorList(result)
+ return SelectorList(result)
def re(self, regex):
return extract_regex(regex, self.extract())
@@ -88,10 +87,8 @@ class XPathSelector(object_ref):
def __str__(self):
data = repr(self.extract()[:40])
return "<%s xpath=%r data=%s>" % (type(self).__name__, self._expr, data)
-
__repr__ = __str__
-
@deprecated(use_instead='XPathSelector.extract')
def extract_unquoted(self):
return self.extract()