SEP-012: bugfix backward compatibility of Spider.domain_name and Spider.extra_domain_names

--HG--
extra : rebase_source : 66f779cddc6854092951078d443dbf9113f7576a
This commit is contained in:
Daniel Grana 2010-04-05 12:09:43 -03:00
parent 77a4d9aba9
commit 70ac6642d5
2 changed files with 21 additions and 9 deletions

View File

@ -34,13 +34,14 @@ class BaseSpider(object_ref):
self.__dict__.update(kwargs)
# XXX: SEP-12 backward compatibility (remove for 0.10)
if hasattr(self, 'domain_name'):
warnings.warn("Spider.domain_name attribute is deprecated, use Spider.name instead", \
warnings.warn("Spider.domain_name attribute is deprecated, use Spider.name instead and Spider.allowed_domains", \
DeprecationWarning, stacklevel=4)
self.name = self.domain_name
if hasattr(self, 'extra_domain_names'):
warnings.warn("Spider.extra_domain_names attribute is deprecated - user Spider.allowed_domains instead", \
DeprecationWarning, stacklevel=4)
self.allowed_domains = [self.name] + list(self.extra_domain_names)
self.allowed_domains = [self.name]
if hasattr(self, 'extra_domain_names'):
warnings.warn("Spider.extra_domain_names attribute is deprecated - user Spider.allowed_domains instead", \
DeprecationWarning, stacklevel=4)
self.allowed_domains += list(self.extra_domain_names)
if name is not None:
self.name = name
@ -50,13 +51,13 @@ class BaseSpider(object_ref):
self.start_urls = []
if not self.allowed_domains:
self.allowed_domains = []
if not getattr(self, 'domain_name', None):
self.domain_name = self.name
if not getattr(self, 'extra_domain_names', None):
self.extra_domain_names = self.allowed_domains
if not self.name:
self.name = 'default'
# XXX: SEP-12 forward compatibility (remove for 0.10)
self.domain_name = self.name
self.extra_domain_names = self.allowed_domains
def log(self, message, level=log.DEBUG):
"""Log the given messages at the given log level. Always use this
method to send log messages from your spider

View File

@ -11,6 +11,7 @@ from scrapy.contrib.spiders.crawl import CrawlSpider
from scrapy.contrib.spiders.feed import XMLFeedSpider, CSVFeedSpider
from scrapy.contrib.dupefilter import RequestFingerprintDupeFilter, NullDupeFilter
class BaseSpiderTest(unittest.TestCase):
spider_class = BaseSpider
@ -20,6 +21,12 @@ class BaseSpiderTest(unittest.TestCase):
domain_name = 'example.com'
extra_domain_names = ('example.org', 'example.net')
class OldSpiderWithoutExtradomains(spider_class):
domain_name = 'example.com'
class NewSpider(spider_class):
name = 'example.com'
@ -48,6 +55,10 @@ class BaseSpiderTest(unittest.TestCase):
self.assert_('example.org' in spider.allowed_domains, spider.allowed_domains)
self.assert_('example.net' in spider.allowed_domains, spider.allowed_domains)
spider = self.OldSpiderWithoutExtradomains()
self.assertEqual(spider.name, 'example.com')
self.assert_('example.com' in spider.allowed_domains, spider.allowed_domains)
spider = self.NewSpider()
self.assertEqual(spider.domain_name, 'example.com')
self.assert_('example.org' in spider.extra_domain_names, spider.extra_domain_names)