mirror of https://github.com/scrapy/scrapy.git
fixed default adaptor inheritance, added test
--HG-- extra : convert_revision : svn%3Ab85faa78-f9eb-468e-a121-7cced6da292c%40978
This commit is contained in:
parent
67ab30a5ba
commit
774e7f623a
|
|
@ -18,8 +18,9 @@ def IDENTITY(v):
|
|||
|
||||
class ItemAdaptorMeta(type):
|
||||
def __new__(meta, class_name, bases, attrs):
|
||||
da = attrs.get('default_adaptor') or IDENTITY
|
||||
attrs['default_adaptor'] = staticmethod(adaptize(da))
|
||||
da = attrs.get('default_adaptor')
|
||||
if da:
|
||||
attrs['default_adaptor'] = staticmethod(adaptize(da))
|
||||
|
||||
cls = type.__new__(meta, class_name, bases, attrs)
|
||||
|
||||
|
|
@ -46,6 +47,7 @@ class ItemAdaptor(object):
|
|||
|
||||
item_class = None
|
||||
field_adaptors = {}
|
||||
default_adaptor = IDENTITY
|
||||
|
||||
def __init__(self, response=None, item=None):
|
||||
self.item_instance = item if item else self.item_class()
|
||||
|
|
|
|||
|
|
@ -24,6 +24,9 @@ class TestAdaptor(BaseAdaptor):
|
|||
class DefaultedAdaptor(BaseAdaptor):
|
||||
default_adaptor = lambda v: v.title()
|
||||
|
||||
class InheritDefaultAdaptor(DefaultedAdaptor):
|
||||
pass
|
||||
|
||||
|
||||
class ItemAdaptorTest(unittest.TestCase):
|
||||
|
||||
|
|
@ -39,6 +42,11 @@ class ItemAdaptorTest(unittest.TestCase):
|
|||
dta.name = 'marta'
|
||||
assert dta.name == 'Marta'
|
||||
|
||||
def test_inheritdefaultadaptor(self):
|
||||
ida = InheritDefaultAdaptor()
|
||||
ida.name = 'marta'
|
||||
assert ida.name == 'Marta'
|
||||
|
||||
def test_inheritance(self):
|
||||
class ChildTestAdaptor(TestAdaptor):
|
||||
url = lambda v: v.lower()
|
||||
|
|
|
|||
Loading…
Reference in New Issue