From 766c2551527574936d3210188c9b95b3f8ea726a Mon Sep 17 00:00:00 2001 From: Elias Dorneles Date: Tue, 11 Aug 2015 15:20:33 -0300 Subject: [PATCH] upgrade parsel and add shim for deprecated selectorlist methods --- requirements.txt | 2 +- scrapy/selector/unified.py | 17 ++++++++++++++++- tests/test_selector.py | 22 ++++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index afefb9d333b..368e9340b58 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,4 +7,4 @@ queuelib six>=1.5.2 PyDispatcher>=2.0.5 service_identity -parsel>=0.9.3 +parsel>=0.9.5 diff --git a/scrapy/selector/unified.py b/scrapy/selector/unified.py index 25cf6c98d8d..dddf80c0650 100644 --- a/scrapy/selector/unified.py +++ b/scrapy/selector/unified.py @@ -3,7 +3,7 @@ """ import warnings -from parsel import Selector as ParselSelector, SelectorList +from parsel import Selector as ParselSelector, SelectorList as ParselSelectorList from scrapy.utils.trackref import object_ref from scrapy.utils.python import to_bytes from scrapy.http import HtmlResponse, XmlResponse @@ -26,9 +26,24 @@ def _response_from_text(text, st): body=to_bytes(text, 'utf-8')) +class SelectorList(ParselSelectorList, object_ref): + @deprecated(use_instead='.extract()') + def extract_unquoted(self): + return [x.extract_unquoted() for x in self] + + @deprecated(use_instead='.xpath()') + def x(self, xpath): + return self.select(xpath) + + @deprecated(use_instead='.xpath()') + def select(self, xpath): + return self.xpath(xpath) + + class Selector(ParselSelector, object_ref): __slots__ = ['response'] + selectorlist_cls = SelectorList def __init__(self, response=None, text=None, type=None, root=None, _root=None, **kwargs): st = _st(response, type or self._default_type) diff --git a/tests/test_selector.py b/tests/test_selector.py index 19b807a3f25..141455b6648 100644 --- a/tests/test_selector.py +++ b/tests/test_selector.py @@ -101,6 +101,28 @@ def test_weakref_slots(self): assert not hasattr(x, '__dict__'), "%s does not use __slots__" % \ x.__class__.__name__ + def test_deprecated_selector_methods(self): + sel = Selector(TextResponse(url="http://example.com", body=b'

some text

')) + + with warnings.catch_warnings(record=True) as w: + sel.select('//p') + self.assertSubstring('Use .xpath() instead', str(w[-1].message)) + + with warnings.catch_warnings(record=True) as w: + sel.extract_unquoted() + self.assertSubstring('Use .extract() instead', str(w[-1].message)) + + def test_deprecated_selectorlist_methods(self): + sel = Selector(TextResponse(url="http://example.com", body=b'

some text

')) + + with warnings.catch_warnings(record=True) as w: + sel.xpath('//p').select('.') + self.assertSubstring('Use .xpath() instead', str(w[-1].message)) + + with warnings.catch_warnings(record=True) as w: + sel.xpath('//p').extract_unquoted() + self.assertSubstring('Use .extract() instead', str(w[-1].message)) + class DeprecatedXpathSelectorTest(unittest.TestCase):