mirror of https://github.com/scrapy/scrapy.git
Changed format of scrapy.cfg file to contain a [settings] section and a 'default' key inside it, instead of the other way around
This commit is contained in:
parent
fb69655a9f
commit
37776618a3
|
|
@ -1,2 +1,2 @@
|
|||
[default]
|
||||
settings = googledir.settings
|
||||
[settings]
|
||||
default = googledir.settings
|
||||
|
|
|
|||
|
|
@ -1,2 +1,2 @@
|
|||
[default]
|
||||
settings = imdb.settings
|
||||
[settings]
|
||||
default = imdb.settings
|
||||
|
|
|
|||
|
|
@ -1,2 +1,2 @@
|
|||
[default]
|
||||
settings = googledir.settings
|
||||
[settings]
|
||||
default = googledir.settings
|
||||
|
|
|
|||
|
|
@ -87,8 +87,8 @@ def _check_deprecated_scrapy_ctl(argv, inproject):
|
|||
if not os.path.exists(cfg_path):
|
||||
with open(cfg_path, 'w') as f:
|
||||
f.write("# generated automatically - feel free to edit" + os.linesep)
|
||||
f.write("[default]" + os.linesep)
|
||||
f.write("settings = %s" % settings.settings_module_path + os.linesep)
|
||||
f.write("[settings]" + os.linesep)
|
||||
f.write("default = %s" % settings.settings_module_path + os.linesep)
|
||||
|
||||
def _run_print_help(parser, func, *a, **kw):
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import os
|
|||
import cPickle as pickle
|
||||
|
||||
from scrapy.conf import default_settings
|
||||
from scrapy.utils.conf import set_scrapy_settings_envvar
|
||||
from scrapy.utils.conf import init_env
|
||||
|
||||
import_ = lambda x: __import__(x, {}, {}, [''])
|
||||
|
||||
|
|
@ -60,7 +60,7 @@ class EnvironmentSettings(Settings):
|
|||
self.disabled = os.environ.get('SCRAPY_SETTINGS_DISABLED', False)
|
||||
if self.ENVVAR not in os.environ:
|
||||
project = os.environ.get('SCRAPY_PROJECT', 'default')
|
||||
set_scrapy_settings_envvar(project)
|
||||
init_env(project)
|
||||
settings_module_path = os.environ.get(self.ENVVAR, 'scrapy_settings')
|
||||
self.set_settings_module(settings_module_path)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,2 +1,2 @@
|
|||
[default]
|
||||
settings = ${project_name}.settings
|
||||
[settings]
|
||||
default = ${project_name}.settings
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import sys
|
||||
import os
|
||||
from ConfigParser import RawConfigParser
|
||||
from ConfigParser import SafeConfigParser
|
||||
from operator import itemgetter
|
||||
|
||||
def build_component_list(base, custom):
|
||||
|
|
@ -17,10 +17,14 @@ def build_component_list(base, custom):
|
|||
|
||||
def arglist_to_dict(arglist):
|
||||
"""Convert a list of arguments like ['arg1=val1', 'arg2=val2', ...] to a
|
||||
dict"""
|
||||
dict
|
||||
"""
|
||||
return dict(x.split('=', 1) for x in arglist)
|
||||
|
||||
def closest_scrapy_cfg(path='.', prevpath=None):
|
||||
"""Return the path to the closest scrapy.cfg file by traversing the current
|
||||
directory and its parents
|
||||
"""
|
||||
if path == prevpath:
|
||||
return ''
|
||||
path = os.path.abspath(path)
|
||||
|
|
@ -29,13 +33,25 @@ def closest_scrapy_cfg(path='.', prevpath=None):
|
|||
return cfgfile
|
||||
return closest_scrapy_cfg(os.path.dirname(path), path)
|
||||
|
||||
def set_scrapy_settings_envvar(project='default', set_syspath=True):
|
||||
scrapy_cfg = closest_scrapy_cfg()
|
||||
cfg_sources = [scrapy_cfg, os.path.expanduser('~/.scrapy.cfg'), '/etc/scrapy.cfg']
|
||||
cfg = RawConfigParser()
|
||||
cfg.read(cfg_sources)
|
||||
if cfg.has_option(project, 'settings'):
|
||||
os.environ['SCRAPY_SETTINGS_MODULE'] = cfg.get(project, 'settings')
|
||||
projdir = os.path.dirname(scrapy_cfg)
|
||||
def init_env(project='default', set_syspath=True):
|
||||
"""Initialize environment to use command-line tool from inside a project
|
||||
dir. This sets the Scrapy settings module and modifies the Python path to
|
||||
be able to locate the project module.
|
||||
"""
|
||||
cfg = get_config()
|
||||
if cfg.has_option('settings', project):
|
||||
os.environ['SCRAPY_SETTINGS_MODULE'] = cfg.get('settings', project)
|
||||
closest = closest_scrapy_cfg()
|
||||
if closest:
|
||||
projdir = os.path.dirname(closest)
|
||||
if set_syspath and projdir not in sys.path:
|
||||
sys.path.append(projdir)
|
||||
|
||||
def get_config(use_closest=True):
|
||||
"""Get Scrapy config file as a SafeConfigParser"""
|
||||
sources = [os.path.expanduser('~/.scrapy.cfg'), '/etc/scrapy.cfg']
|
||||
if use_closest:
|
||||
sources.insert(0, closest_scrapy_cfg())
|
||||
cfg = SafeConfigParser()
|
||||
cfg.read(sources)
|
||||
return cfg
|
||||
|
|
|
|||
Loading…
Reference in New Issue