-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1166 from scrapy/spider-loader
[MRG+1] rename SpiderManager to SpiderLoader
- Loading branch information
Showing
25 changed files
with
179 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,22 @@ | ||
from zope.interface import Interface | ||
|
||
class ISpiderManager(Interface): | ||
class ISpiderLoader(Interface): | ||
|
||
def from_settings(settings): | ||
"""Returns an instance of the class for the given settings""" | ||
"""Return an instance of the class for the given settings""" | ||
|
||
def load(spider_name): | ||
"""Returns the Spider class for the given spider name. If the spider | ||
"""Return the Spider class for the given spider name. If the spider | ||
name is not found, it must raise a KeyError.""" | ||
|
||
def list(): | ||
"""Return a list with the names of all spiders available in the | ||
project""" | ||
|
||
def find_by_request(request): | ||
"""Returns the list of spiders names that can handle the given request""" | ||
"""Return the list of spiders names that can handle the given request""" | ||
|
||
|
||
# ISpiderManager is deprecated, don't use it! | ||
# An alias is kept for backwards compatibility. | ||
ISpiderManager = ISpiderLoader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import absolute_import | ||
|
||
from zope.interface import implementer | ||
|
||
from scrapy.interfaces import ISpiderLoader | ||
from scrapy.utils.misc import walk_modules | ||
from scrapy.utils.spider import iter_spider_classes | ||
|
||
|
||
@implementer(ISpiderLoader) | ||
class SpiderLoader(object): | ||
""" | ||
SpiderLoader is a class which locates and loads spiders | ||
in a Scrapy project. | ||
""" | ||
def __init__(self, settings): | ||
self.spider_modules = settings.getlist('SPIDER_MODULES') | ||
self._spiders = {} | ||
for name in self.spider_modules: | ||
for module in walk_modules(name): | ||
self._load_spiders(module) | ||
|
||
def _load_spiders(self, module): | ||
for spcls in iter_spider_classes(module): | ||
self._spiders[spcls.name] = spcls | ||
|
||
@classmethod | ||
def from_settings(cls, settings): | ||
return cls(settings) | ||
|
||
def load(self, spider_name): | ||
""" | ||
Return the Spider class for the given spider name. If the spider | ||
name is not found, raise a KeyError. | ||
""" | ||
try: | ||
return self._spiders[spider_name] | ||
except KeyError: | ||
raise KeyError("Spider not found: {}".format(spider_name)) | ||
|
||
def find_by_request(self, request): | ||
""" | ||
Return the list of spider names that can handle the given request. | ||
""" | ||
return [name for name, cls in self._spiders.items() | ||
if cls.handles_request(request)] | ||
|
||
def list(self): | ||
""" | ||
Return a list with the names of all spiders available in the project. | ||
""" | ||
return list(self._spiders.keys()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,7 @@ | ||
""" | ||
SpiderManager is the class which locates and manages all website-specific | ||
spiders | ||
Backwards compatibility shim. Use scrapy.spiderloader instead. | ||
""" | ||
from scrapy.spiderloader import SpiderLoader | ||
from scrapy.utils.deprecate import create_deprecated_class | ||
|
||
from zope.interface import implementer | ||
import six | ||
|
||
from scrapy.interfaces import ISpiderManager | ||
from scrapy.utils.misc import walk_modules | ||
from scrapy.utils.spider import iter_spider_classes | ||
|
||
|
||
@implementer(ISpiderManager) | ||
class SpiderManager(object): | ||
|
||
def __init__(self, settings): | ||
self.spider_modules = settings.getlist('SPIDER_MODULES') | ||
self._spiders = {} | ||
for name in self.spider_modules: | ||
for module in walk_modules(name): | ||
self._load_spiders(module) | ||
|
||
def _load_spiders(self, module): | ||
for spcls in iter_spider_classes(module): | ||
self._spiders[spcls.name] = spcls | ||
|
||
@classmethod | ||
def from_settings(cls, settings): | ||
return cls(settings) | ||
|
||
def load(self, spider_name): | ||
try: | ||
return self._spiders[spider_name] | ||
except KeyError: | ||
raise KeyError("Spider not found: {}".format(spider_name)) | ||
|
||
def find_by_request(self, request): | ||
return [name for name, cls in six.iteritems(self._spiders) | ||
if cls.handles_request(request)] | ||
|
||
def list(self): | ||
return list(self._spiders.keys()) | ||
SpiderManager = create_deprecated_class('SpiderManager', SpiderLoader) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.