Scrapy, a fast high-level web crawling & scraping framework for Python.
Moved module: scrapy.core.queue to scrapy.queue
Pablo Hoffmanf6c11af4c274
diff --git a/scrapy/commands/shell.py b/scrapy/commands/shell.pyindex 87119718c..bf2e88d65 100644--- a/scrapy/commands/shell.py+++ b/scrapy/commands/shell.py@@ -10,7 +10,7 @@ from scrapy.shell import Shellclass Command(ScrapyCommand):requires_project = False- default_settings = {'QUEUE_CLASS': 'scrapy.core.queue.KeepAliveExecutionQueue'}+ default_settings = {'QUEUE_CLASS': 'scrapy.queue.KeepAliveExecutionQueue'}def syntax(self):return "[url|file]"diff --git a/scrapy/conf/default_settings.py b/scrapy/conf/default_settings.pyindex 6b03ca43f..143f29632 100644--- a/scrapy/conf/default_settings.py+++ b/scrapy/conf/default_settings.py@@ -175,7 +175,7 @@ MEMUSAGE_WARNING_MB = 0NEWSPIDER_MODULE = ''-QUEUE_CLASS = 'scrapy.core.queue.ExecutionQueue'+QUEUE_CLASS = 'scrapy.queue.ExecutionQueue'RANDOMIZE_DOWNLOAD_DELAY = Truediff --git a/scrapy/contrib/queue/__init__.py b/scrapy/contrib/queue/__init__.pyindex dc3d46db8..c98f6e730 100644--- a/scrapy/contrib/queue/__init__.py+++ b/scrapy/contrib/queue/__init__.py@@ -1,4 +1,4 @@-from scrapy.core.queue import ExecutionQueue+from scrapy.queue import ExecutionQueuefrom scrapy.utils.sqlite import JsonSqlitePriorityQueuefrom scrapy.conf import settingsdiff --git a/scrapy/contrib/queue/sqs.py b/scrapy/contrib/queue/sqs.pyindex 69ce2dc7c..f3ccb816d 100644--- a/scrapy/contrib/queue/sqs.py+++ b/scrapy/contrib/queue/sqs.py@@ -4,7 +4,7 @@ from twisted.internet import threadsfrom boto.sqs.connection import SQSConnectionfrom boto.sqs import regions-from scrapy.core.queue import ExecutionQueue+from scrapy.queue import ExecutionQueuefrom scrapy.utils.py26 import jsonfrom scrapy.conf import settingsdiff --git a/scrapy/core/queue.py b/scrapy/core/queue.pyindex 17ba27272..0b6d10600 100644--- a/scrapy/core/queue.py+++ b/scrapy/core/queue.py@@ -1,86 +1,5 @@-from twisted.internet import defer+from scrapy.queue import *-from scrapy.http import Request-from scrapy.utils.misc import arg_to_iter-from scrapy import log---class ExecutionQueue(object):-- polling_delay = 5-- def __init__(self, _spiders):- self.spider_requests = []- self._spiders = _spiders-- def _append_next(self):- """Called when there are no more items left in self.spider_requests.- This method is meant to be overriden in subclasses to add new (spider,- requests) tuples to self.spider_requests. It can return a Deferred.- """- pass-- def get_next(self):- """Return a tuple (spider, requests) containing a list of Requests and- the Spider which will be used to crawl those Requests. If there aren't- any more spiders to crawl it must return (None, []).-- This method can return a deferred.- """- if self.spider_requests:- return self._get_next_now()- d = defer.maybeDeferred(self._append_next)- d.addCallback(lambda _: self._get_next_now())- return d-- def _get_next_now(self):- try:- return self.spider_requests.pop(0)- except IndexError:- return (None, [])-- def is_finished(self):- """Return True if the queue is empty and there won't be any more- spiders to crawl (this is for one-shot runs). If it returns ``False``- Scrapy will keep polling this queue for new requests to scrape- """- return not bool(self.spider_requests)-- def append_spider(self, spider):- """Append a Spider to crawl"""- requests = spider.start_requests()- self.spider_requests.append((spider, requests))-- def append_request(self, request, spider=None, **kwargs):- if spider is None:- spider = self._spiders.create_for_request(request, **kwargs)- if spider:- self.spider_requests.append((spider, [request]))-- def append_url(self, url, spider=None, **kwargs):- """Append a URL to crawl with the given spider. If the spider is not- given, a spider will be looked up based on the URL- """- if spider is None:- spider = self._spiders.create_for_request(Request(url), **kwargs)- if spider:- requests = arg_to_iter(spider.make_requests_from_url(url))- self.spider_requests.append((spider, requests))-- def append_spider_name(self, spider_name, **spider_kwargs):- """Append a spider to crawl given its name and optional arguments,- which are used to instantiate it. The SpiderManager is used to lookup- the spider- """- try:- spider = self._spiders.create(spider_name, **spider_kwargs)- except KeyError:- log.msg('Unable to find spider: %s' % spider_name, log.ERROR)- else:- self.append_spider(spider)---class KeepAliveExecutionQueue(ExecutionQueue):-- def is_finished(self):- return False+import warnings+warnings.warn("scrapy.core.queue is deprecated and will be removed in Scrapy 0.11, use scrapy.queue instead", \+ DeprecationWarning, stacklevel=2)diff --git a/scrapy/queue.py b/scrapy/queue.pynew file mode 100644index 000000000..17ba27272--- /dev/null+++ b/scrapy/queue.py@@ -0,0 +1,86 @@+from twisted.internet import defer++from scrapy.http import Request+from scrapy.utils.misc import arg_to_iter+from scrapy import log+++class ExecutionQueue(object):++ polling_delay = 5++ def __init__(self, _spiders):+ self.spider_requests = []+ self._spiders = _spiders++ def _append_next(self):+ """Called when there are no more items left in self.spider_requests.+ This method is meant to be overriden in subclasses to add new (spider,+ requests) tuples to self.spider_requests. It can return a Deferred.+ """+ pass++ def get_next(self):+ """Return a tuple (spider, requests) containing a list of Requests and+ the Spider which will be used to crawl those Requests. If there aren't+ any more spiders to crawl it must return (None, []).++ This method can return a deferred.+ """+ if self.spider_requests:+ return self._get_next_now()+ d = defer.maybeDeferred(self._append_next)+ d.addCallback(lambda _: self._get_next_now())+ return d++ def _get_next_now(self):+ try:+ return self.spider_requests.pop(0)+ except IndexError:+ return (None, [])++ def is_finished(self):+ """Return True if the queue is empty and there won't be any more+ spiders to crawl (this is for one-shot runs). If it returns ``False``+ Scrapy will keep polling this queue for new requests to scrape+ """+ return not bool(self.spider_requests)++ def append_spider(self, spider):+ """Append a Spider to crawl"""+ requests = spider.start_requests()+ self.spider_requests.append((spider, requests))++ def append_request(self, request, spider=None, **kwargs):+ if spider is None:+ spider = self._spiders.create_for_request(request, **kwargs)+ if spider:+ self.spider_requests.append((spider, [request]))++ def append_url(self, url, spider=None, **kwargs):+ """Append a URL to crawl with the given spider. If the spider is not+ given, a spider will be looked up based on the URL+ """+ if spider is None:+ spider = self._spiders.create_for_request(Request(url), **kwargs)+ if spider:+ requests = arg_to_iter(spider.make_requests_from_url(url))+ self.spider_requests.append((spider, requests))++ def append_spider_name(self, spider_name, **spider_kwargs):+ """Append a spider to crawl given its name and optional arguments,+ which are used to instantiate it. The SpiderManager is used to lookup+ the spider+ """+ try:+ spider = self._spiders.create(spider_name, **spider_kwargs)+ except KeyError:+ log.msg('Unable to find spider: %s' % spider_name, log.ERROR)+ else:+ self.append_spider(spider)+++class KeepAliveExecutionQueue(ExecutionQueue):++ def is_finished(self):+ return Falsediff --git a/scrapy/tests/test_core_queue.py b/scrapy/tests/test_core_queue.pyindex 2d41910ad..6d1b37d85 100644--- a/scrapy/tests/test_core_queue.py+++ b/scrapy/tests/test_core_queue.py@@ -1,6 +1,6 @@import unittest-from scrapy.core.queue import ExecutionQueue, KeepAliveExecutionQueue+from scrapy.queue import ExecutionQueue, KeepAliveExecutionQueuefrom scrapy.spider import BaseSpiderfrom scrapy.http import Request