From a609b1cb2b43fd17e03efa62314f679b47ae6cb5 Mon Sep 17 00:00:00 2001 From: David Luevano Alvarado Date: Fri, 24 Feb 2023 03:53:13 -0600 Subject: add utils tests, small refactor --- src/pyssg/configuration.py | 22 ++++++++++++---------- src/pyssg/utils.py | 20 ++++++++++++++++---- 2 files changed, 28 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/pyssg/configuration.py b/src/pyssg/configuration.py index 3cc5430..e2dc26b 100644 --- a/src/pyssg/configuration.py +++ b/src/pyssg/configuration.py @@ -12,10 +12,11 @@ DEFAULT_CONFIG_PATH: str = '$XDG_CONFIG_HOME/pyssg/config.yaml' VERSION: str = version('pyssg') -def __check_well_formed_config(config: dict, - config_base: list[dict], +def __check_well_formed_config(config: dict[str, Any], + config_base: list[dict[str, Any]], prefix_key: str = '') -> None: for key in config_base[0].keys(): + new_config_base: list[dict[str, Any]] = [] current_key: str = f'{prefix_key}.{key}' if prefix_key != '' else key log.debug('checking "%s"', current_key) if key not in config: @@ -27,7 +28,7 @@ def __check_well_formed_config(config: dict, try: config[key].keys() except AttributeError: - log.error('config doesn\'t have any dirs configs (dirs.*)') + log.error('config doesn\'t have any dirs (dirs.*)') sys.exit(1) if '/' not in config[key]: log.debug('key: %s; config.keys: %s', key, config[key].keys()) @@ -37,7 +38,7 @@ def __check_well_formed_config(config: dict, key, ', '.join(config[key].keys())) for dkey in config[key].keys(): new_current_key: str = f'{current_key}.{dkey}' - new_config_base: list[dict] = [config_base[1], config_base[1]] + new_config_base = [config_base[1], config_base[1]] __check_well_formed_config(config[key][dkey], new_config_base, new_current_key) @@ -46,11 +47,11 @@ def __check_well_formed_config(config: dict, if not config_base[0][key]: log.debug('"%s" doesn\'t need nested elements', current_key) continue - new_config_base: list[dict] = [config_base[0][key], config_base[1]] + new_config_base = [config_base[0][key], config_base[1]] __check_well_formed_config(config[key], new_config_base, current_key) -def __expand_all_paths(config: dict) -> None: +def __expand_all_paths(config: dict[str, Any]) -> None: log.debug('expanding all path options: %s', config['path'].keys()) for option in config['path'].keys(): config['path'][option] = get_expanded_path(config['path'][option]) @@ -59,10 +60,11 @@ def __expand_all_paths(config: dict) -> None: # not necessary to type deeper than the first dict def get_parsed_config(path: str, mc_package: str = 'mandatory_config.yaml', - plt_resource: str = 'pyssg.plt') -> list[dict]: + plt_resource: str = 'pyssg.plt') -> list[dict[str, Any]]: log.debug('reading config file "%s"', path) - config_all: list[dict] = get_parsed_yaml(path) - mandatory_config: list[dict] = get_parsed_yaml(mc_package, plt_resource) + config_all: list[dict[str, Any]] = get_parsed_yaml(path) + mandatory_config: list[dict[str, Any]] = get_parsed_yaml(mc_package, + plt_resource) log.info('found %s document(s) for config "%s"', len(config_all), path) log.debug('checking that config file is well formed') for config in config_all: @@ -74,7 +76,7 @@ def get_parsed_config(path: str, # not necessary to type deeper than the first dict, # static config means config that shouldn't be changed by the user def get_static_config(sc_package: str = 'static_config.yaml', - plt_resource: str = 'pyssg.plt') -> dict[str, dict]: + plt_resource: str = 'pyssg.plt') -> dict[str, Any]: log.debug('reading and setting static config') config: dict[str, Any] = get_parsed_yaml(sc_package, plt_resource)[0] diff --git a/src/pyssg/utils.py b/src/pyssg/utils.py index 8300a5c..b1ed8c1 100644 --- a/src/pyssg/utils.py +++ b/src/pyssg/utils.py @@ -20,7 +20,8 @@ def get_file_list(path: str, dirs[:] = [d for d in dirs if d not in exclude_dirs] for file in files: if file.endswith(exts): - # [1:] is required to remove the '/' at the beginning after replacing + # [1:] is required to remove the '/' + # at the beginning after replacing file_name: str = os.path.join(root, file).replace(path, '')[1:] file_list.append(file_name) log.debug('added file "%s" without "%s" part: "%s"', @@ -44,7 +45,8 @@ def get_dir_structure(path: str, if root in dir_list: dir_list.remove(root) log.debug('removed dir "%s" as it already is in the list', root) - # not removing the 'path' part here, as comparisons with 'root' would fail + # not removing the 'path' part here, + # as comparisons with 'root' would fail joined_dir: str = os.path.join(root, d) dir_list.append(joined_dir) log.debug('added dir "%s" to the list', joined_dir) @@ -53,19 +55,29 @@ def get_dir_structure(path: str, return [d.replace(path, '')[1:] for d in dir_list] +# TODO: probably change it so it returns a bool, easier to check def create_dir(path: str, p: bool = False, silent=False) -> None: + log_msg: str = '' try: if p: os.makedirs(path) else: os.mkdir(path) + log_msg = f'created directory "{path}"' if not silent: - log.info('created directory "%s"', path) + log.info(log_msg) + log.debug(log_msg) except FileExistsError: + log_msg = f'directory "{path}" exists, ignoring' if not silent: - log.info('directory "%s" already exists, ignoring', path) + log.info(log_msg) + log.debug(log_msg) +# TODO: change this as it doesn't take directories into account, +# a file can be copied into a directory, need to get the filename +# and use it when copying +# TODO: probably change it so it returns a bool, easier to check def copy_file(src: str, dst: str) -> None: if not os.path.exists(dst): shutil.copy2(src, dst) -- cgit v1.2.3-54-g00ecf