mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
Add --print-config option to print the resolved settings at startup.
This commit is contained in:
parent
b15712924f
commit
13c5d72bd1
@ -191,7 +191,10 @@ class Application(BaseApplication):
|
||||
self.chdir()
|
||||
|
||||
def run(self):
|
||||
if self.cfg.check_config:
|
||||
if self.cfg.print_config:
|
||||
print(self.cfg)
|
||||
|
||||
if self.cfg.print_config or self.cfg.check_config:
|
||||
try:
|
||||
self.load()
|
||||
except:
|
||||
|
||||
@ -51,6 +51,16 @@ class Config(object):
|
||||
self.prog = prog or os.path.basename(sys.argv[0])
|
||||
self.env_orig = os.environ.copy()
|
||||
|
||||
def __str__(self):
|
||||
lines = []
|
||||
kmax = max(len(k) for k in self.settings)
|
||||
for k in sorted(self.settings):
|
||||
v = self.settings[k].value
|
||||
if callable(v):
|
||||
v = "<%s()>" % v.__qualname__
|
||||
lines.append(f"{k:{kmax}} = {v}")
|
||||
return "\n".join(lines)
|
||||
|
||||
def __getattr__(self, name):
|
||||
if name not in self.settings:
|
||||
raise AttributeError("No configuration setting for: %s" % name)
|
||||
@ -936,6 +946,18 @@ class ConfigCheck(Setting):
|
||||
"""
|
||||
|
||||
|
||||
class PrintConfig(Setting):
|
||||
name = "print_config"
|
||||
section = "Debugging"
|
||||
cli = ["--print-config"]
|
||||
validator = validate_bool
|
||||
action = "store_true"
|
||||
default = False
|
||||
desc = """\
|
||||
Print the resolved configuration.
|
||||
"""
|
||||
|
||||
|
||||
class PreloadApp(Setting):
|
||||
name = "preload_app"
|
||||
section = "Server Mechanics"
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
# See the NOTICE for more information.
|
||||
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
@ -435,3 +436,36 @@ def test_bind_fd():
|
||||
with AltArgs(["prog_name", "-b", "fd://42"]):
|
||||
app = NoConfigApp()
|
||||
assert app.cfg.bind == ["fd://42"]
|
||||
|
||||
|
||||
def test_str():
|
||||
c = config.Config()
|
||||
o = str(c)
|
||||
|
||||
# match the first few lines, some different types, but don't go OTT
|
||||
# to avoid needless test fails with changes
|
||||
OUTPUT_MATCH = {
|
||||
'access_log_format': '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"',
|
||||
'accesslog': 'None',
|
||||
'backlog': '2048',
|
||||
'bind': "['127.0.0.1:8000']",
|
||||
'capture_output': 'False',
|
||||
'child_exit': '<ChildExit.child_exit()>',
|
||||
}
|
||||
for i, line in enumerate(o.splitlines()):
|
||||
m = re.match(r'^(\w+)\s+= ', line)
|
||||
assert m, f"Config line {i} didn't match expected format: {line!r}"
|
||||
|
||||
key = m.group(1)
|
||||
try:
|
||||
s = OUTPUT_MATCH.pop(key)
|
||||
except KeyError:
|
||||
continue
|
||||
|
||||
line_re = fr'^{key}\s+= {re.escape(s)}$'
|
||||
assert re.match(line_re, line), f'{line_re!r} != {line!r}'
|
||||
|
||||
if not OUTPUT_MATCH:
|
||||
break
|
||||
else:
|
||||
assert False, f'missing expected setting lines? {list(OUTPUT_MATCH.keys())}'
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user