Skip to content

Commit

Permalink
Merge pull request #1068 from slava-sh/836-add-config-type-prefix
Browse files Browse the repository at this point in the history
Prefix config file with its type

Fixes #836
  • Loading branch information
berkerpeksag committed Jul 9, 2015
2 parents d971eb4 + 5311336 commit ec3664d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
4 changes: 2 additions & 2 deletions docs/source/run.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ You can now run the app with the following command::
Commonly Used Arguments
^^^^^^^^^^^^^^^^^^^^^^^

* ``-c CONFIG, --config=CONFIG`` - Specify the path to a config file or
Python module.
* ``-c CONFIG, --config=CONFIG`` - Specify a config file in the form
``$(PATH)``, ``file:$(PATH)``, or ``python:$(MODULE_NAME)``.
* ``-b BIND, --bind=BIND`` - Specify a server socket to bind. Server sockets
can be any of ``$(HOST)``, ``$(HOST):$(PORT)``, or ``unix:$(PATH)``.
An IP is a valid ``$(HOST)``.
Expand Down
10 changes: 8 additions & 2 deletions docs/source/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@ Config File
config
~~~~~~

* ``-c FILE, --config FILE``
* ``-c CONFIG, --config CONFIG``
* ``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
application specific configuration.

.. versionchanged:: 19.4
Loading the config from a Python module requires the ``python:``
prefix.

Server Socket
-------------

Expand Down
17 changes: 10 additions & 7 deletions gunicorn/app/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,15 @@ def load_config_from_module_name_or_filename(self, location):
Exception or stop the process if the configuration file contains a syntax error.
"""

try:
cfg = self.get_config_from_module_name(module_name=location)
except ImportError:
cfg = self.get_config_from_filename(filename=location)
if location.startswith("python:"):
module_name = location[len("python:"):]
cfg = self.get_config_from_module_name(module_name)
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():
# Ignore unknown names
Expand All @@ -127,9 +132,7 @@ def load_config_from_module_name_or_filename(self, location):
return cfg

def load_config_from_file(self, filename):
return self.load_config_from_module_name_or_filename(
location=filename
)
return self.load_config_from_module_name_or_filename(location=filename)

def load_config(self):
# parse console args
Expand Down
10 changes: 8 additions & 2 deletions gunicorn/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,14 +465,20 @@ class ConfigFile(Setting):
name = "config"
section = "Config File"
cli = ["-c", "--config"]
meta = "FILE"
meta = "CONFIG"
validator = validate_string
default = None
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
application specific configuration.
.. versionchanged:: 19.4
Loading the config from a Python module requires the ``python:``
prefix.
"""

class Bind(Setting):
Expand Down
11 changes: 9 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,15 @@ def test_load_config():
assert app.cfg.workers == 3
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():
with AltArgs(["prog_name", "-c", cfg_module()]):
with AltArgs(["prog_name", "-c", "python:%s" % cfg_module()]):
app = NoConfigApp()
assert app.cfg.bind == ["unix:/tmp/bar/baz"]
assert app.cfg.workers == 3
Expand All @@ -197,7 +204,7 @@ def test_cli_overrides_config():
assert app.cfg.proc_name == "fooey"

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()
assert app.cfg.bind == ["blarney"]
assert app.cfg.proc_name == "fooey"
Expand Down

0 comments on commit ec3664d

Please sign in to comment.