rename XPathSelectorList as SelectorList #176

This commit is contained in:
Daniel Graña 2013-01-09 11:09:02 -02:00
parent bc17e9d412
commit b38ac27eee
5 changed files with 37 additions and 22 deletions

View File

@ -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)

View File

@ -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()

View File

@ -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:

View File

@ -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)

View File

@ -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()