From 8a0b1d2d9f9629e4a186549b134ebd6b80a5d71b Mon Sep 17 00:00:00 2001 From: David Luevano Alvarado Date: Sat, 23 Apr 2022 21:49:53 -0600 Subject: add proper var expansion from config file --- src/pyssg/configuration.py | 12 +++++++++++- src/pyssg/pyssg.py | 6 ++---- src/pyssg/utils.py | 21 +++++++++++++-------- 3 files changed, 26 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/pyssg/configuration.py b/src/pyssg/configuration.py index b193cf8..32a010b 100644 --- a/src/pyssg/configuration.py +++ b/src/pyssg/configuration.py @@ -5,6 +5,8 @@ from datetime import datetime, timezone from configparser import ConfigParser from logging import Logger, getLogger +from .utils import get_expanded_path + log: Logger = getLogger(__name__) @@ -12,7 +14,15 @@ DEFAULT_CONFIG_PATH = '$XDG_CONFIG_HOME/pyssg/config.ini' VERSION = version('pyssg') +def __expand_all_paths(config: ConfigParser) -> None: + log.debug('expanding all path options') + for option in config.options('path'): + path: str = config.get('path', option) + config.set('path', option, get_expanded_path(path)) + + def __check_well_formed_config(config: ConfigParser) -> None: + log.debug('checking that config file is well formed') default_config: ConfigParser = ConfigParser() with rpath('pyssg.plt', 'default.ini') as p: log.debug('reading config file "%s"', p) @@ -35,8 +45,8 @@ def get_parsed_config(path: str) -> ConfigParser: log.debug('reading config file "%s"', path) config.read(path) - log.debug('checking that config file is well formed') __check_well_formed_config(config) + __expand_all_paths(config) # set other required options log.debug('setting extra config options') diff --git a/src/pyssg/pyssg.py b/src/pyssg/pyssg.py index 598bf41..eb042b6 100644 --- a/src/pyssg/pyssg.py +++ b/src/pyssg/pyssg.py @@ -5,7 +5,7 @@ from typing import Union from configparser import ConfigParser from logging import Logger, getLogger, DEBUG -from .utils import create_dir, copy_file, sanity_check_path +from .utils import create_dir, copy_file, get_expanded_path from .arg_parser import get_parsed_arguments from .configuration import get_parsed_config, DEFAULT_CONFIG_PATH, VERSION from .database import Database @@ -35,10 +35,8 @@ def main() -> None: log.info('pyssg v%s', VERSION) sys.exit(0) - log.debug('checking config file path') config_path: str = args['config'] if args['config'] else DEFAULT_CONFIG_PATH - config_path = os.path.normpath(os.path.expandvars(config_path)) - sanity_check_path(config_path) + config_path = get_expanded_path(config_path) config_dir, _ = os.path.split(config_path) log.debug('checked config file path, final config path "%s"', config_path) diff --git a/src/pyssg/utils.py b/src/pyssg/utils.py index a41249a..4b525cf 100644 --- a/src/pyssg/utils.py +++ b/src/pyssg/utils.py @@ -74,13 +74,6 @@ def copy_file(src: str, dst: str) -> None: log.info('file "%s" already exists, ignoring', dst) -def sanity_check_path(path: str) -> None: - if '$' in path: - log.error('"$" character found in path "%s";' - ' could be due to non-existant env var.', path) - sys.exit(1) - - # as seen in SO: https://stackoverflow.com/a/1131238 def get_checksum(path: str) -> str: log.debug('calculating md5 checksum for "%s"', path) @@ -89,4 +82,16 @@ def get_checksum(path: str) -> str: while chunk := f.read(4096): file_hash.update(chunk) - return file_hash.hexdigest() \ No newline at end of file + return file_hash.hexdigest() + + +def get_expanded_path(path: str) -> None: + log.debug('expanding path "%s"', path) + expanded_path: str = os.path.normpath(os.path.expandvars(path)) + if '$' in expanded_path: + log.error('"$" character found in expanded path "%s";' + ' could be due to non-existant env var.', expanded_path) + sys.exit(1) + log.debug('expanded path "%s" to "%s"', path, expanded_path) + + return expanded_path -- cgit v1.2.3-70-g09d2