Scrapy, a fast high-level web crawling & scraping framework for Python.
Simplified BaseSpider code by removing backwards compatibility code
Pablo Hoffmana28cf290081f
diff --git a/scrapy/spider/models.py b/scrapy/spider/models.pyindex 4ca4305d0..0acbbe364 100644--- a/scrapy/spider/models.py+++ b/scrapy/spider/models.py@@ -4,8 +4,6 @@ Base class for Scrapy spidersSee documentation in docs/topics/spiders.rst"""-import warnings-from scrapy import logfrom scrapy.http import Requestfrom scrapy.utils.misc import arg_to_iter@@ -18,38 +16,18 @@ class BaseSpider(object_ref):class."""- # XXX: class attributes kept for backwards compatibilityname = None- start_urls = []- allowed_domains = []def __init__(self, name=None, **kwargs):- 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 and Spider.allowed_domains", \- DeprecationWarning, stacklevel=4)- self.name = self.domain_name- 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- # XXX: create instance attributes (class attributes were kept for- # backwards compatibility)- if not self.start_urls:+ elif not getattr(self, 'name', None):+ raise ValueError("%s must have a name" % type(self).__name__)+ self.__dict__.update(kwargs)+ if not hasattr(self, 'start_urls'):self.start_urls = []- if not self.allowed_domains:+ if not hasattr(self, 'allowed_domains'):self.allowed_domains = []- if not self.name:- raise ValueError("%s must have a name" % type(self).__name__)-- # XXX: SEP-12 forward compatibility (remove for 0.10)- self.domain_name = self.name- self.extra_domain_names = self.allowed_domainsdef log(self, message, level=log.DEBUG):"""Log the given messages at the given log level. Always use thisdiff --git a/scrapy/tests/test_spider.py b/scrapy/tests/test_spider.pyindex 14aa78168..4dcb7d47a 100644--- a/scrapy/tests/test_spider.py+++ b/scrapy/tests/test_spider.py@@ -1,6 +1,5 @@from __future__ import with_statement-import sysimport warningsfrom twisted.trial import unittest@@ -15,54 +14,12 @@ class BaseSpiderTest(unittest.TestCase):spider_class = BaseSpider- class OldSpider(spider_class):-- 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'- allowed_domains = ('example.org', 'example.net')--def setUp(self):warnings.simplefilter("always")def tearDown(self):warnings.resetwarnings()- def test_sep12_deprecation_warnings(self):- if sys.version_info[:2] < (2, 6):- # warnings.catch_warnings() was added in Python 2.6- raise unittest.SkipTest("This test requires Python 2.6+")- with warnings.catch_warnings(record=True) as w:- spider = self.OldSpider()- self.assertEqual(len(w), 2) # one for domain_name & one for extra_domain_names- self.assert_(issubclass(w[-1].category, DeprecationWarning))-- def test_sep12_backwards_compatibility(self):- spider = self.OldSpider()- self.assertEqual(spider.name, 'example.com')- self.assert_('example.com' in spider.allowed_domains, spider.allowed_domains)- 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)- self.assert_('example.net' in spider.extra_domain_names, spider.extra_domain_names)-def test_base_spider(self):spider = self.spider_class("example.com")self.assertEqual(spider.name, 'example.com')