Merge pull request #260 from llonchj/entry_point

Commands module
This commit is contained in:
Pablo Hoffman 2013-03-08 05:49:08 -08:00
commit b5b37197bf
2 changed files with 28 additions and 1 deletions

View File

@ -16,4 +16,19 @@ it's properly merged) . Use at your own risk.
This documentation is a work in progress. Use at your own risk.
*No experimental features at this time*
Add commands using external libraries
-------------------------------------
You can also add Scrapy commands from an external library by adding `scrapy.commands` section into entry_points in the `setup.py`.
The following example adds `my_command` command::
from setuptools import setup, find_packages
setup(name='scrapy-mymodule',
entry_points={
'scrapy.commands': [
'my_command=my_scrapy_module.commands:MyCommand',
],
},
)

View File

@ -2,6 +2,7 @@ import sys
import optparse
import cProfile
import inspect
import pkg_resources
import scrapy
from scrapy.crawler import CrawlerProcess
@ -30,8 +31,19 @@ def _get_commands_from_module(module, inproject):
d[cmdname] = cmd()
return d
def _get_commands_from_entry_points(inproject, group='scrapy.commands'):
cmds = {}
for entry_point in pkg_resources.iter_entry_points(group):
obj = entry_point.load()
if inspect.isclass(obj):
cmds[entry_point.name] = obj()
else:
raise Exception("Invalid entry point %s" % entry_point.name)
return cmds
def _get_commands_dict(settings, inproject):
cmds = _get_commands_from_module('scrapy.commands', inproject)
cmds.update(_get_commands_from_entry_points(inproject))
cmds_module = settings['COMMANDS_MODULE']
if cmds_module:
cmds.update(_get_commands_from_module(cmds_module, inproject))