# HG changeset patch # User Mads Kiilerich # Date 2019-07-31 21:53:59 # Node ID efd088f7903be45dd3ab387f89cf018641ca357f # Parent 0f33c1da2fd50ecd7565497876498b6941099ab4 kallithea-cli: introduce generic custom logging configuration for each cli command Command line commands have different needs for logging than the server process. To enable that use case, allow config sections to be enabled (and thus augment existing sections), depending on which kallithea-cli command is running. diff --git a/kallithea/bin/kallithea_cli_base.py b/kallithea/bin/kallithea_cli_base.py --- a/kallithea/bin/kallithea_cli_base.py +++ b/kallithea/bin/kallithea_cli_base.py @@ -13,8 +13,10 @@ # along with this program. If not, see . import click +import cStringIO import functools import os +import re import sys import kallithea @@ -31,6 +33,19 @@ import paste.deploy kallithea_cli_path = sys.argv[0] +def read_config(ini_file_name, strip_section_prefix): + """Read ini_file_name content, and for all sections like '[X:Y]' where X is + strip_section_prefix, replace the section name with '[Y]'.""" + + def repl(m): + if m.group(1) == strip_section_prefix: + return '[%s]' % m.group(2) + return m.group(0) + + with open(ini_file_name) as f: + return re.sub(r'^\[([^:]+):(.*)]', repl, f.read(), flags=re.MULTILINE) + + # This placeholder is the main entry point for the kallithea-cli command @click.group(context_settings=dict(help_option_names=['-h', '--help'])) def cli(): @@ -55,7 +70,8 @@ def register_command(config_file=False, def runtime_wrapper(config_file, *args, **kwargs): path_to_ini_file = os.path.realpath(config_file) kallithea.CONFIG = paste.deploy.appconfig('config:' + path_to_ini_file) - logging.config.fileConfig(path_to_ini_file) + config_bytes = read_config(path_to_ini_file, strip_section_prefix=annotated.__name__) + logging.config.fileConfig(cStringIO.StringIO(config_bytes)) if config_file_initialize_app: kallithea.config.middleware.make_app_without_logging(kallithea.CONFIG.global_conf, **kallithea.CONFIG.local_conf) kallithea.lib.utils.setup_cache_regions(kallithea.CONFIG)