Merge pull request #1068 from slava-sh/836-add-config-type-prefix

Prefix config file with its type

Fixes #836
This commit is contained in:
Berker Peksag 2015-07-09 10:59:29 +03:00
commit ec3664dd30
5 changed files with 37 additions and 15 deletions

View File

@ -47,8 +47,8 @@ You can now run the app with the following command::
Commonly Used Arguments Commonly Used Arguments
^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^
* ``-c CONFIG, --config=CONFIG`` - Specify the path to a config file or * ``-c CONFIG, --config=CONFIG`` - Specify a config file in the form
Python module. ``$(PATH)``, ``file:$(PATH)``, or ``python:$(MODULE_NAME)``.
* ``-b BIND, --bind=BIND`` - Specify a server socket to bind. Server sockets * ``-b BIND, --bind=BIND`` - Specify a server socket to bind. Server sockets
can be any of ``$(HOST)``, ``$(HOST):$(PORT)``, or ``unix:$(PATH)``. can be any of ``$(HOST)``, ``$(HOST):$(PORT)``, or ``unix:$(PATH)``.
An IP is a valid ``$(HOST)``. An IP is a valid ``$(HOST)``.

View File

@ -16,14 +16,20 @@ Config File
config config
~~~~~~ ~~~~~~
* ``-c FILE, --config FILE`` * ``-c CONFIG, --config CONFIG``
* ``None`` * ``None``
The path to a Gunicorn config file, or python module. The Gunicorn config file.
A string of the form ``PATH``, ``file:PATH``, or ``python:MODULE_NAME``.
Only has an effect when specified on the command line or as part of an Only has an effect when specified on the command line or as part of an
application specific configuration. application specific configuration.
.. versionchanged:: 19.4
Loading the config from a Python module requires the ``python:``
prefix.
Server Socket Server Socket
------------- -------------

View File

@ -108,10 +108,15 @@ class Application(BaseApplication):
Exception or stop the process if the configuration file contains a syntax error. Exception or stop the process if the configuration file contains a syntax error.
""" """
try: if location.startswith("python:"):
cfg = self.get_config_from_module_name(module_name=location) module_name = location[len("python:"):]
except ImportError: cfg = self.get_config_from_module_name(module_name)
cfg = self.get_config_from_filename(filename=location) else:
if location.startswith("file:"):
filename = location[len("file:"):]
else:
filename = location
cfg = self.get_config_from_filename(filename)
for k, v in cfg.items(): for k, v in cfg.items():
# Ignore unknown names # Ignore unknown names
@ -127,9 +132,7 @@ class Application(BaseApplication):
return cfg return cfg
def load_config_from_file(self, filename): def load_config_from_file(self, filename):
return self.load_config_from_module_name_or_filename( return self.load_config_from_module_name_or_filename(location=filename)
location=filename
)
def load_config(self): def load_config(self):
# parse console args # parse console args

View File

@ -465,14 +465,20 @@ class ConfigFile(Setting):
name = "config" name = "config"
section = "Config File" section = "Config File"
cli = ["-c", "--config"] cli = ["-c", "--config"]
meta = "FILE" meta = "CONFIG"
validator = validate_string validator = validate_string
default = None default = None
desc = """\ desc = """\
The path to a Gunicorn config file, or python module. The Gunicorn config file.
A string of the form ``PATH``, ``file:PATH``, or ``python:MODULE_NAME``.
Only has an effect when specified on the command line or as part of an Only has an effect when specified on the command line or as part of an
application specific configuration. application specific configuration.
.. versionchanged:: 19.4
Loading the config from a Python module requires the ``python:``
prefix.
""" """
class Bind(Setting): class Bind(Setting):

View File

@ -183,8 +183,15 @@ def test_load_config():
assert app.cfg.workers == 3 assert app.cfg.workers == 3
assert app.cfg.proc_name == "fooey" assert app.cfg.proc_name == "fooey"
def test_load_config_explicit_file():
with AltArgs(["prog_name", "-c", "file:%s" % cfg_file()]):
app = NoConfigApp()
assert app.cfg.bind == ["unix:/tmp/bar/baz"]
assert app.cfg.workers == 3
assert app.cfg.proc_name == "fooey"
def test_load_config_module(): def test_load_config_module():
with AltArgs(["prog_name", "-c", cfg_module()]): with AltArgs(["prog_name", "-c", "python:%s" % cfg_module()]):
app = NoConfigApp() app = NoConfigApp()
assert app.cfg.bind == ["unix:/tmp/bar/baz"] assert app.cfg.bind == ["unix:/tmp/bar/baz"]
assert app.cfg.workers == 3 assert app.cfg.workers == 3
@ -197,7 +204,7 @@ def test_cli_overrides_config():
assert app.cfg.proc_name == "fooey" assert app.cfg.proc_name == "fooey"
def test_cli_overrides_config_module(): def test_cli_overrides_config_module():
with AltArgs(["prog_name", "-c", cfg_module(), "-b", "blarney"]): with AltArgs(["prog_name", "-c", "python:%s" % cfg_module(), "-b", "blarney"]):
app = NoConfigApp() app = NoConfigApp()
assert app.cfg.bind == ["blarney"] assert app.cfg.bind == ["blarney"]
assert app.cfg.proc_name == "fooey" assert app.cfg.proc_name == "fooey"