added 'scrapy edit' command

This commit is contained in:
Pablo Hoffman 2011-06-05 22:02:56 -03:00
parent ffbc9295f6
commit 9d9c8877da
5 changed files with 74 additions and 1 deletions

View File

@ -151,6 +151,7 @@ Project-only commands:
* :command:`crawl`
* :command:`list`
* :command:`edit`
* :command:`parse`
* :command:`genspider`
* :command:`server`
@ -269,6 +270,25 @@ Usage example::
spider1
spider2
.. command:: edit
edit
----
* Syntax: ``scrapy edit <spider>``
* Requires project: *yes*
Edit the given spider using the editor defined in the :setting:`EDITOR`
setting.
This command is provided only as a convenient shortcut for the most common
case, the developer is of course free to choose any tool or IDE to write and
debug his spiders.
Usage example::
$ scrapy edit spider1
.. command:: fetch
fetch

View File

@ -451,6 +451,15 @@ The class used to detect and filter duplicate requests.
The default (``RequestFingerprintDupeFilter``) filters based on request fingerprint
(using ``scrapy.utils.request.request_fingerprint``) and grouping per domain.
.. setting:: EDITOR
EDITOR
------
The editor to use for editing spiders with the :command:`edit` command. It
defaults to the ``EDITOR`` environment variable, if set. Otherwise, it defaults
to ``vi`` (on Unix systems) or the IDLE editor (on Windows).
.. setting:: ENCODING_ALIASES
ENCODING_ALIASES

View File

@ -5,7 +5,7 @@ _scrapy_completion() {
cmd=${COMP_WORDS[1]}
cur=${COMP_WORDS[2]}
case "$cmd" in
crawl)
crawl|edit)
spiders=$(scrapy list 2>/dev/null) || spiders=""
COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W "$spiders" -- "$cur"))
;;

35
scrapy/commands/edit.py Normal file
View File

@ -0,0 +1,35 @@
import sys, os
from scrapy.command import ScrapyCommand
from scrapy.exceptions import UsageError
class Command(ScrapyCommand):
requires_project = True
default_settings = {'LOG_ENABLED': False}
def syntax(self):
return "<spider>"
def short_desc(self):
return "Edit spider"
def long_desc(self):
return "Edit a spider using the editor defined in EDITOR setting"
def _err(self, msg):
sys.stderr.write(msg + os.linesep)
self.exitcode = 1
def run(self, args, opts):
if len(args) != 1:
raise UsageError()
editor = self.crawler.settings['EDITOR']
try:
spider = self.crawler.spiders.create(args[0])
except KeyError:
return self._err("Spider not found: %s" % args[0])
sfile = sys.modules[spider.__module__].__file__
sfile = sfile.replace('.pyc', '.py')
self.exitcode = os.system('%s "%s"' % (editor, sfile))

View File

@ -13,6 +13,7 @@ Scrapy developers, if you add a setting here remember to:
"""
import sys, os
from os.path import join, abspath, dirname
BOT_NAME = 'scrapybot'
@ -82,6 +83,14 @@ DOWNLOADER_STATS = True
DUPEFILTER_CLASS = 'scrapy.contrib.dupefilter.RequestFingerprintDupeFilter'
try:
EDITOR = os.environ['EDITOR']
except KeyError:
if sys.platform == 'win32':
EDITOR = '%s -m idlelib.idle'
else:
EDITOR = 'vi'
ENCODING_ALIASES = {}
ENCODING_ALIASES_BASE = {