Skip to content

Commit

Permalink
Drop support for scrapy.project.crawler (And scrapy.stats consequently)
Browse files Browse the repository at this point in the history
  • Loading branch information
curita committed Aug 12, 2014
1 parent 9cbbfd8 commit c90977c
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 66 deletions.
2 changes: 1 addition & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from scrapy import optional_features

collect_ignore = ["scrapy/stats.py"]
collect_ignore = ["scrapy/stats.py", "scrapy/project.py"]
if 'django' not in optional_features:
collect_ignore.append("tests/test_djangoitem/models.py")

Expand Down
31 changes: 0 additions & 31 deletions docs/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -280,37 +280,6 @@ I'm scraping a XML document and my XPath selector doesn't return any items

You may need to remove namespaces. See :ref:`removing-namespaces`.


I'm getting an error: "cannot import name crawler"
--------------------------------------------------

This is caused by Scrapy changes due to the singletons removal. The error is
most likely raised by a module (extension, middleware, pipeline or spider) in
your Scrapy project that imports ``crawler`` from ``scrapy.project``. For
example::

from scrapy.project import crawler

class SomeExtension(object):
def __init__(self):
self.crawler = crawler
# ...

This way to access the crawler object is deprecated, the code should be ported
to use ``from_crawler`` class method, for example::

class SomeExtension(object):

@classmethod
def from_crawler(cls, crawler):
o = cls()
o.crawler = crawler
return o

Scrapy command line tool has some backwards compatibility in place to support
the old import mechanism (with a deprecation warning), but this mechanism may
not work if you use Scrapy differently (for example, as a library).

.. _user agents: http://en.wikipedia.org/wiki/User_agent
.. _LIFO: http://en.wikipedia.org/wiki/LIFO
.. _DFO order: http://en.wikipedia.org/wiki/Depth-first_search
Expand Down
2 changes: 1 addition & 1 deletion docs/topics/shell.rst
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ Here's an example of how you would call it from your spider::
# We want to inspect one specific response.
if ".org" in response.url:
from scrapy.shell import inspect_response
inspect_response(response)
inspect_response(response, self)

# Rest of parsing code.

Expand Down
15 changes: 0 additions & 15 deletions scrapy/crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,6 @@ def __init__(self, spidercls, settings):
self.spider = None
self.engine = None

def install(self):
# TODO: remove together with scrapy.project.crawler usage
import scrapy.project
assert not hasattr(scrapy.project, 'crawler'), "crawler already installed"
scrapy.project.crawler = self

def uninstall(self):
# TODO: remove together with scrapy.project.crawler usage
import scrapy.project
assert hasattr(scrapy.project, 'crawler'), "crawler not installed"
del scrapy.project.crawler

@defer.inlineCallbacks
def crawl(self, *args, **kwargs):
assert not self.crawling, "Crawling already taking place"
Expand Down Expand Up @@ -84,9 +72,6 @@ def crawl(self, spidercls, *args, **kwargs):
crawler = self._create_logged_crawler(spidercls)
self.crawlers.add(crawler)

crawler.install()
crawler.signals.connect(crawler.uninstall, signals.engine_stopped)

d = crawler.crawl(*args, **kwargs)
self.crawl_deferreds.add(d)
return d
Expand Down
16 changes: 10 additions & 6 deletions scrapy/project.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@

"""
Obsolete module, kept for giving a meaningful error message when trying to
import.
"""
--------- WARNING: THIS MODULE IS DEPRECATED -----------

This module is deprecated. If you want to get the Scrapy crawler from your
extension, middleware or pipeline implement the `from_crawler` class method.
raise ImportError("""scrapy.project usage has become obsolete.
If you want to get the Scrapy crawler from your extension, middleware or
pipeline implement the `from_crawler` class method (or look up for extending
components that have already done it, such as spiders).
For example:
@classmethod
def from_crawler(cls, crawler):
return cls(crawler)
"""
return cls(crawler)""")
5 changes: 2 additions & 3 deletions scrapy/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,9 @@ def _is_relevant(self, value):
return isinstance(value, self.relevant_classes)


def inspect_response(response, spider=None):
def inspect_response(response, spider):
"""Open a shell to inspect the given response"""
from scrapy.project import crawler
Shell(crawler).start(response=response, spider=spider)
Shell(spider.crawler).start(response=response)


def _request_deferred(request):
Expand Down
2 changes: 1 addition & 1 deletion scrapy/spider.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ def __getattr__(self, name):
raise AttributeError(self.message)

spiders = ObsoleteClass("""
"from scrapy.spider import spiders" no longer works - use "from scrapy.project import crawler" and then access crawler.spiders attribute"
"from scrapy.spider import spiders" no longer works - use "from scrapy.spidermanager import SpiderManager" and instantiate it with your project settings"
""")

13 changes: 7 additions & 6 deletions scrapy/stats.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from scrapy.project import crawler
stats = crawler.stats

import warnings
from scrapy.exceptions import ScrapyDeprecationWarning
warnings.warn("Module `scrapy.stats` is deprecated, use `crawler.stats` attribute instead",
ScrapyDeprecationWarning, stacklevel=2)
"""
Obsolete module, kept for giving a meaningful error message when trying to
import.
"""

raise ImportError("scrapy.stats usage has become obsolete, use "
"`crawler.stats` attribute instead")
2 changes: 0 additions & 2 deletions tests/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ def run(self):
dispatcher.connect(self.record_signal, signal)

self.crawler = get_crawler(TestSpider)
self.crawler.install()
self.crawler.signals.connect(self.item_scraped, signals.item_scraped)
self.crawler.signals.connect(self.request_scheduled, signals.request_scheduled)
self.crawler.signals.connect(self.response_downloaded, signals.response_downloaded)
Expand All @@ -109,7 +108,6 @@ def stop(self):
for name, signal in vars(signals).items():
if not name.startswith('_'):
disconnect_all(signal)
self.crawler.uninstall()
self.deferred.callback(None)

def geturl(self, path):
Expand Down

0 comments on commit c90977c

Please sign in to comment.