From a6c5ebbb0c26b4dddfc152e66b65051e6d737a5d Mon Sep 17 00:00:00 2001 From: jonct <2807816+jonct@users.noreply.github.com> Date: Mon, 15 Jul 2024 00:49:53 -0400 Subject: [PATCH] Extract more config parser --- src/jlmkr/utils/config_parser.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/jlmkr/utils/config_parser.py b/src/jlmkr/utils/config_parser.py index 5adb62b..9264ab4 100644 --- a/src/jlmkr/utils/config_parser.py +++ b/src/jlmkr/utils/config_parser.py @@ -6,6 +6,8 @@ import configparser import io import re +from donor.jlmkr import DEFAULT_CONFIG + # Used in parser getters to indicate the default behavior when a specific # option is not found. Created to enable `None` as a valid fallback value. @@ -136,3 +138,17 @@ class ExceptionWithParser(Exception): self.parser = parser self.message = message super().__init__(message) + + +def parse_config_file(jail_config_path): + config = KeyValueParser() + # Read default config to fallback to default values + # for keys not found in the jail_config_path file + config.read_default_string(DEFAULT_CONFIG) + try: + with open(jail_config_path, "r") as fp: + config.read_file(fp) + return config + except FileNotFoundError: + eprint(f"Unable to find config file: {jail_config_path}.") + return