From 531133615e29e121b31042e0573b5a7aa893a164 Mon Sep 17 00:00:00 2001 From: Slava Shklyaev Date: Tue, 7 Jul 2015 09:37:09 +0300 Subject: [PATCH] Prefix config file with its type --- docs/source/run.rst | 4 ++-- docs/source/settings.rst | 10 ++++++++-- gunicorn/app/base.py | 17 ++++++++++------- gunicorn/config.py | 10 ++++++++-- tests/test_config.py | 11 +++++++++-- 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/docs/source/run.rst b/docs/source/run.rst index 5e9bc3fa..bb862b38 100644 --- a/docs/source/run.rst +++ b/docs/source/run.rst @@ -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)``. diff --git a/docs/source/settings.rst b/docs/source/settings.rst index d8733982..d44c7349 100644 --- a/docs/source/settings.rst +++ b/docs/source/settings.rst @@ -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 ------------- diff --git a/gunicorn/app/base.py b/gunicorn/app/base.py index d1ee6c92..f3d3b4f1 100644 --- a/gunicorn/app/base.py +++ b/gunicorn/app/base.py @@ -108,10 +108,15 @@ class Application(BaseApplication): 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 @@ -127,9 +132,7 @@ class Application(BaseApplication): 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 diff --git a/gunicorn/config.py b/gunicorn/config.py index 148197a2..e735d104 100644 --- a/gunicorn/config.py +++ b/gunicorn/config.py @@ -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): diff --git a/tests/test_config.py b/tests/test_config.py index 0f448bb4..f235564e 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -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 @@ -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"