Add --print-config option to print the resolved settings at startup.

This commit is contained in:
Robert Coup 2019-05-31 11:49:55 +01:00
parent b15712924f
commit 13c5d72bd1
3 changed files with 60 additions and 1 deletions

View File

@ -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:

View File

@ -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"

View File

@ -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())}'