Remove dependency on os.environ from default settings

Avoid loading settings from environment in scrapy core.
Instead it's better to populate them from the starting
shell or an embedding script.
This commit is contained in:
nyov 2016-03-01 07:12:19 +00:00
parent cc06b6b1f6
commit 2240f00a13
5 changed files with 20 additions and 17 deletions

View File

@ -291,12 +291,12 @@ edit
* Syntax: ``scrapy edit <spider>`` * Syntax: ``scrapy edit <spider>``
* Requires project: *yes* * Requires project: *yes*
Edit the given spider using the editor defined in the :setting:`EDITOR` Edit the given spider using the editor defined in the ``EDITOR`` environment
setting. variable or (if unset) the :setting:`EDITOR` setting.
This command is provided only as a convenience shortcut for the most common This command is provided only as a convenience shortcut for the most common
case, the developer is of course free to choose any tool or IDE to write and case, the developer is of course free to choose any tool or IDE to write and
debug his spiders. debug spiders.
Usage example:: Usage example::

View File

@ -635,11 +635,11 @@ Setting :setting:`DUPEFILTER_DEBUG` to ``True`` will make it log all duplicate r
EDITOR EDITOR
------ ------
Default: `depends on the environment` Default: ``vi`` (on Unix systems) or the IDLE editor (on Windows)
The editor to use for editing spiders with the :command:`edit` command. It The editor to use for editing spiders with the :command:`edit` command.
defaults to the ``EDITOR`` environment variable, if set. Otherwise, it defaults Additionally, if the ``EDITOR`` environment variable is set, the :command:`edit`
to ``vi`` (on Unix systems) or the IDLE editor (on Windows). command will prefer it over the default setting.
.. setting:: EXTENSIONS .. setting:: EXTENSIONS

View File

@ -1,5 +1,5 @@
from __future__ import print_function from __future__ import print_function
import sys import sys, os
import optparse import optparse
import cProfile import cProfile
import inspect import inspect
@ -106,6 +106,12 @@ def execute(argv=None, settings=None):
if settings is None: if settings is None:
settings = get_project_settings() settings = get_project_settings()
# set EDITOR from environment if available
try:
editor = os.environ['EDITOR']
except KeyError: pass
else:
settings['EDITOR'] = editor
check_deprecated_settings(settings) check_deprecated_settings(settings)
# --- backwards compatibility for scrapy.conf.settings singleton --- # --- backwards compatibility for scrapy.conf.settings singleton ---

View File

@ -3,6 +3,7 @@ import sys, os
from scrapy.commands import ScrapyCommand from scrapy.commands import ScrapyCommand
from scrapy.exceptions import UsageError from scrapy.exceptions import UsageError
class Command(ScrapyCommand): class Command(ScrapyCommand):
requires_project = True requires_project = True
@ -15,7 +16,8 @@ class Command(ScrapyCommand):
return "Edit spider" return "Edit spider"
def long_desc(self): def long_desc(self):
return "Edit a spider using the editor defined in EDITOR setting" return ("Edit a spider using the editor defined in the EDITOR environment"
" variable or else the EDITOR setting")
def _err(self, msg): def _err(self, msg):
sys.stderr.write(msg + os.linesep) sys.stderr.write(msg + os.linesep)

View File

@ -13,7 +13,6 @@ Scrapy developers, if you add a setting here remember to:
""" """
import os
import sys import sys
from importlib import import_module from importlib import import_module
from os.path import join, abspath, dirname from os.path import join, abspath, dirname
@ -111,13 +110,9 @@ DOWNLOADER_STATS = True
DUPEFILTER_CLASS = 'scrapy.dupefilters.RFPDupeFilter' DUPEFILTER_CLASS = 'scrapy.dupefilters.RFPDupeFilter'
try: EDITOR = 'vi'
EDITOR = os.environ['EDITOR'] if sys.platform == 'win32':
except KeyError: EDITOR = '%s -m idlelib.idle'
if sys.platform == 'win32':
EDITOR = '%s -m idlelib.idle'
else:
EDITOR = 'vi'
EXTENSIONS = {} EXTENSIONS = {}