Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefix config file with its type #1068

Merged
merged 1 commit into from
Jul 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prefix config file with its type
  • Loading branch information
slava-sh committed Jul 8, 2015
commit 531133615e29e121b31042e0573b5a7aa893a164
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