[gunicorn] added ``gunicorn.conf.py`` as default in case of no -c is specified

This commit is contained in:
Jorge Niedbalski 2013-06-21 15:30:31 -03:00
parent 54b4ffb68e
commit cf6e765323
3 changed files with 43 additions and 34 deletions

View File

@ -14,7 +14,8 @@ config
~~~~~~ ~~~~~~
* ``-c FILE, --config FILE`` * ``-c FILE, --config FILE``
* ``None`` * ``gunicorn.conf.py`` if the file exists on the current directory otherwise
``None`` is used.
The path to a Gunicorn config file. The path to a Gunicorn config file.

View File

@ -9,7 +9,7 @@ import traceback
from gunicorn import util from gunicorn import util
from gunicorn.arbiter import Arbiter from gunicorn.arbiter import Arbiter
from gunicorn.config import Config from gunicorn.config import Config, get_default_config_file
from gunicorn import debug from gunicorn import debug
from gunicorn.six import execfile_ from gunicorn.six import execfile_
@ -36,6 +36,37 @@ class Application(object):
sys.stderr.flush() sys.stderr.flush()
sys.exit(1) sys.exit(1)
def load_config_from_file(self, filename):
if not os.path.exists(filename):
raise RuntimeError("%r doesn't exist" % filename)
cfg = {
"__builtins__": __builtins__,
"__name__": "__config__",
"__file__": filename,
"__doc__": None,
"__package__": None
}
try:
execfile_(filename, cfg, cfg)
except Exception:
print("Failed to read config file: %s" % filename)
traceback.print_exc()
sys.exit(1)
for k, v in cfg.items():
# Ignore unknown names
if k not in self.cfg.settings:
continue
try:
self.cfg.set(k.lower(), v)
except:
sys.stderr.write("Invalid value for %s: %s\n\n" % (k, v))
raise
return cfg
def load_config(self): def load_config(self):
# init configuration # init configuration
self.cfg = Config(self.usage, prog=self.prog) self.cfg = Config(self.usage, prog=self.prog)
@ -52,6 +83,13 @@ class Application(object):
for k, v in cfg.items(): for k, v in cfg.items():
self.cfg.set(k.lower(), v) self.cfg.set(k.lower(), v)
default_config = get_default_config_file()
if args.config:
self.load_config_from_file(args.config)
elif default_config is not None:
self.load_config_from_file(default_config)
# Lastly, update the configuration with any command line # Lastly, update the configuration with any command line
# settings. # settings.
for k, v in args.__dict__.items(): for k, v in args.__dict__.items():
@ -61,36 +99,6 @@ class Application(object):
continue continue
self.cfg.set(k.lower(), v) self.cfg.set(k.lower(), v)
# Load up the config file if its found.
if args.config:
if not os.path.exists(args.config):
raise RuntimeError("%r doesn't exist" % args.config)
cfg = {
"__builtins__": __builtins__,
"__name__": "__config__",
"__file__": args.config,
"__doc__": None,
"__package__": None
}
try:
execfile_(args.config, cfg, cfg)
except Exception:
print("Failed to read config file: %s" % args.config)
traceback.print_exc()
sys.exit(1)
for k, v in cfg.items():
# Ignore unknown names
if k not in self.cfg.settings:
continue
try:
self.cfg.set(k.lower(), v)
except:
sys.stderr.write("Invalid value for %s: %s\n\n" % (k, v))
raise
def init(self, parser, opts, args): def init(self, parser, opts, args):
raise NotImplementedError raise NotImplementedError

View File

@ -199,7 +199,7 @@ class Setting(object):
"dest": self.name, "dest": self.name,
"action": self.action or "store", "action": self.action or "store",
"type": self.type or str, "type": self.type or str,
"default": self.default or None, "default": None,
"help": help_txt "help": help_txt
} }
@ -380,7 +380,7 @@ class ConfigFile(Setting):
cli = ["-c", "--config"] cli = ["-c", "--config"]
meta = "FILE" meta = "FILE"
validator = validate_string validator = validate_string
default = get_default_config_file() default = None
desc = """\ desc = """\
The path to a Gunicorn config file. The path to a Gunicorn config file.