diff options
Diffstat (limited to 'src/pyssg/utils.py')
-rw-r--r-- | src/pyssg/utils.py | 56 |
1 files changed, 23 insertions, 33 deletions
diff --git a/src/pyssg/utils.py b/src/pyssg/utils.py index d391ccf..216f535 100644 --- a/src/pyssg/utils.py +++ b/src/pyssg/utils.py @@ -12,8 +12,9 @@ log: Logger = getLogger(__name__) def get_file_list(path: str, exts: tuple[str], exclude_dirs: list[str] = []) -> list[str]: - log.debug('retrieving file list in path "%s" that contain file' - ' extensions %s except directories %s', path, exts, exclude_dirs) + log.debug('retrieving file list in "%s",' + ' extensions %s, except dirs %s', + path, exts, exclude_dirs) file_list: list[str] = [] for root, dirs, files in os.walk(path): if exclude_dirs != []: @@ -23,20 +24,20 @@ def get_file_list(path: str, if file.endswith(exts): # [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"', - file, path, file_name) + fname: str = os.path.join(root, file) + fname = fname.replace(path, '')[1:] + file_list.append(fname) + log.debug('added "%s"', fname) else: - log.debug('ignoring file "%s" as it doesn\'t contain' - ' any of the extensions %s', file, exts) + log.debug('ignoring "%s", doesn\'t contain' + ' extensions %s', file, exts) return file_list def get_dir_structure(path: str, exclude: list[str] = []) -> list[str]: - log.debug('retrieving dir structure in path "%s" except directories (%s)', - path, ', '.join(exclude)) + log.debug('retrieving dir structure in "%s",' + ' except dirs %s', path, exclude) dir_list: list[str] = [] for root, dirs, files in os.walk(path): if exclude != []: @@ -45,34 +46,25 @@ def get_dir_structure(path: str, for d in dirs: 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 - joined_dir: str = os.path.join(root, d) - dir_list.append(joined_dir) - log.debug('added dir "%s" to the list', joined_dir) - log.debug('removing "%s" from all dirs in list', path) + dname: str = os.path.join(root, d) + dir_list.append(dname) + log.debug('added dir "%s" to the list', dname) # [1:] is required to remove the '/' at the beginning after replacing 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 = '' +def create_dir(path: str, p: bool = False) -> None: try: if p: os.makedirs(path) else: os.mkdir(path) - log_msg = f'created directory "{path}"' - if not silent: - log.info(log_msg) - log.debug(log_msg) + log.info('created directory "%s"', path) except FileExistsError: - log_msg = f'directory "{path}" exists, ignoring' - if not silent: - log.info(log_msg) - log.debug(log_msg) + log.debug('directory "%s" exists, ignoring', path) # TODO: change this as it doesn't take directories into account, @@ -87,7 +79,6 @@ def copy_file(src: str, dst: str) -> None: log.info('file "%s" already exists, ignoring', dst) -# only used for database, but keeping it here as it is an independent function # as seen in SO: https://stackoverflow.com/a/1131238 def get_checksum(path: str) -> str: log.debug('calculating md5 checksum for "%s"', path) @@ -99,14 +90,13 @@ def get_checksum(path: str) -> str: def get_expanded_path(path: str) -> str: - 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) + epath: str = os.path.normpath(os.path.expandvars(path)) + if '$' in epath: + log.error('"$" character found in expanded path "%s",' + ' could be due to non-existant env var', epath) sys.exit(1) - log.debug('expanded path "%s" to "%s"', path, expanded_path) - return expanded_path + log.debug('expanded path "%s" to "%s"', path, epath) + return epath def get_time_now(fmt: str, tz: timezone=timezone.utc) -> str: |