Skip to content

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.py
index 4ca4305d0..0acbbe364 100644
--- a/scrapy/spider/models.py
+++ b/scrapy/spider/models.py
@@ -4,8 +4,6 @@ Base class for Scrapy spiders
See documentation in docs/topics/spiders.rst
"""
-import warnings
-
from scrapy import log
from scrapy.http import Request
from scrapy.utils.misc import arg_to_iter
@@ -18,38 +16,18 @@ class BaseSpider(object_ref):
class.
"""
- # XXX: class attributes kept for backwards compatibility
name = 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_domains
def log(self, message, level=log.DEBUG):
"""Log the given messages at the given log level. Always use this
diff --git a/scrapy/tests/test_spider.py b/scrapy/tests/test_spider.py
index 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 sys
import warnings
from 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')