From 11b76c9265e52af5347991e6825e43da0260bb4d Mon Sep 17 00:00:00 2001 From: kba Date: Tue, 16 Jul 2024 17:36:27 +0200 Subject: [PATCH] logging: extract get_logging_config_files function, use to skip tests if logging.conf present --- src/ocrd_utils/__init__.py | 1 + src/ocrd_utils/logging.py | 22 ++++++++++++++-------- tests/test_decorators.py | 4 +++- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/ocrd_utils/__init__.py b/src/ocrd_utils/__init__.py index d03c2a920c..2055758a89 100644 --- a/src/ocrd_utils/__init__.py +++ b/src/ocrd_utils/__init__.py @@ -169,6 +169,7 @@ disableLogging, getLevelName, getLogger, + get_logging_config_files, initLogging, setOverrideLogLevel, ) diff --git a/src/ocrd_utils/logging.py b/src/ocrd_utils/logging.py index 6245f99b76..bb771fc0ce 100644 --- a/src/ocrd_utils/logging.py +++ b/src/ocrd_utils/logging.py @@ -128,6 +128,19 @@ def setOverrideLogLevel(lvl, silent=not config.OCRD_LOGGING_DEBUG): print(f'[LOGGING] Overriding ocrd log level to {lvl}', file=sys.stderr) ocrd_logger.setLevel(lvl) +def get_logging_config_files(): + """ + Return a list of all ``ocrd_logging.conf`` files found in CWD, HOME or /etc. + """ + CONFIG_PATHS = [ + Path.cwd(), + Path.home(), + Path('/etc'), + ] + return [f for f \ + in [p / 'ocrd_logging.conf' for p in CONFIG_PATHS] \ + if f.exists()] + def initLogging(builtin_only=False, force_reinit=False, silent=not config.OCRD_LOGGING_DEBUG): """ Reset ``ocrd`` logger, read logging configuration if exists, otherwise use basicConfig @@ -164,14 +177,7 @@ def initLogging(builtin_only=False, force_reinit=False, silent=not config.OCRD_L config_file = None if not builtin_only: - CONFIG_PATHS = [ - Path.cwd(), - Path.home(), - Path('/etc'), - ] - config_file = [f for f \ - in [p / 'ocrd_logging.conf' for p in CONFIG_PATHS] \ - if f.exists()] + config_file = get_logging_config_files() if config_file: if len(config_file) > 1 and not silent: print(f"[LOGGING] Multiple logging configuration files found at {config_file}, using first one", file=sys.stderr) diff --git a/tests/test_decorators.py b/tests/test_decorators.py index 3c38f20789..5ab2880053 100644 --- a/tests/test_decorators.py +++ b/tests/test_decorators.py @@ -15,7 +15,7 @@ ocrd_loglevel, ocrd_cli_wrap_processor, ) # pylint: disable=protected-access -from ocrd_utils import pushd_popd, VERSION as OCRD_VERSION, disableLogging, initLogging +from ocrd_utils import pushd_popd, VERSION as OCRD_VERSION, disableLogging, initLogging, get_logging_config_files @click.command() @ocrd_cli_options @@ -60,6 +60,8 @@ def test_loglevel_invalid(self): assert "'foo' is not one of" in err def test_loglevel_override(self): + if get_logging_config_files(): + pytest.skip(f"ocrd_logging.conf found at {get_logging_config_files()}, skipping logging test") import logging disableLogging() assert logging.getLogger('ocrd').getEffectiveLevel() == logging.WARNING