Skip to content

Commit

Permalink
added 'scrapy edit' command
Browse files Browse the repository at this point in the history
pablohoffman committed Jun 6, 2011
1 parent ffbc929 commit 9d9c887
Showing 5 changed files with 74 additions and 1 deletion.
20 changes: 20 additions & 0 deletions docs/topics/commands.rst
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions docs/topics/settings.rst
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion extras/scrapy_bash_completion
Original file line number Diff line number Diff line change
@@ -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 changes: 35 additions & 0 deletions scrapy/commands/edit.py
Original file line number Diff line number Diff line change
@@ -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))
9 changes: 9 additions & 0 deletions scrapy/settings/default_settings.py
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
"""

import sys, os
from os.path import join, abspath, dirname

BOT_NAME = 'scrapybot'
@@ -82,6 +83,14 @@

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 = {

0 comments on commit 9d9c887

Please sign in to comment.