Extract more config parser

This commit is contained in:
jonct 2024-07-15 00:49:53 -04:00
parent 2718c89613
commit a6c5ebbb0c
No known key found for this signature in database
1 changed files with 16 additions and 0 deletions

View File

@ -6,6 +6,8 @@ import configparser
import io import io
import re import re
from donor.jlmkr import DEFAULT_CONFIG
# Used in parser getters to indicate the default behavior when a specific # 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. # option is not found. Created to enable `None` as a valid fallback value.
@ -136,3 +138,17 @@ class ExceptionWithParser(Exception):
self.parser = parser self.parser = parser
self.message = message self.message = message
super().__init__(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