Skip to content

Commit

Permalink
upgrade parsel and add shim for deprecated selectorlist methods
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasdorneles committed Aug 11, 2015
1 parent e50610b commit 766c255
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ queuelib
six>=1.5.2
PyDispatcher>=2.0.5
service_identity
parsel>=0.9.3
parsel>=0.9.5
17 changes: 16 additions & 1 deletion scrapy/selector/unified.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions tests/test_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'<p>some text</p>'))

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'<p>some text</p>'))

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

Expand Down

0 comments on commit 766c255

Please sign in to comment.