From 4d75ae5df62b985f4a5866d1f3463f9ef226c7c9 Mon Sep 17 00:00:00 2001 From: matlag Date: Fri, 16 Aug 2024 00:20:37 -0400 Subject: [PATCH] Functional with logging --- link_fixer.py | 14 +++++++++----- search_file.py | 6 +++--- test/test_link_fixer.py | 2 +- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/link_fixer.py b/link_fixer.py index 7fe417d..d64ad10 100644 --- a/link_fixer.py +++ b/link_fixer.py @@ -15,7 +15,8 @@ class Link(FileType): def fix(cls, ln_str, tgt_dir_str): logging.captureWarnings(True) print() - + print(f"Link fixer called for link {ln_str} targeting {tgt_dir_str}") + logging.info(f"Link fixer called for link {ln_str} targeting {tgt_dir_str}") link = Path(ln_str) tgt_dir = Path(tgt_dir_str) @@ -25,7 +26,7 @@ class Link(FileType): #print(f"Error: could not access link {ln_str}.") sys.exit(f"Error: could not access link {ln_str}.") - logging.debug(f"fix called on {ln_str}, determined type: {to_be_fixed}") + logging.debug(f"Determined input type: {to_be_fixed}") if not tgt_dir.is_dir(): sys.exit(f"Error: target dir {tgt_dir} does not seem to exist or be a directory. Abort.") @@ -40,14 +41,16 @@ class Link(FileType): tgt = search_file(pointed, tgt_dir) logging.debug(f"Search for matching target {pointed} in {tgt_dir} returned {tgt}") except Exception as e: - warnings.warn(f"No match for link target in {tgt_dir_str}. Link {link} not modified.", Warning) + logging.warning(f"No match for {pointed} in {tgt_dir_str} . {link} not modified") + warnings.warn("link_fix aborted.", Warning) return if tgt != "": cls._swap_link(link, tgt) + logging.info(f"Link now pointing to {tgt} . Swap of link successfully completed.") else: warnings.warn(f"Reaching point at which link is to be fixed, but Variable {tgt} seems to be unset!", Warning) case "directory": - logging.debug(f"Now walking through directory {tgt_dir} searching for broken links") + logging.info(f"Now walking through directory {tgt_dir} searching for broken links") for (root, dirs, files) in os.walk(link): for name in files: filetype = cls.get_file_type(root+"/"+name) @@ -55,6 +58,7 @@ class Link(FileType): if filetype == "broken-link": logging.info(f"Calling link fixer on {root+"/"+name} targeting {tgt_dir_str}") cls.fix(root+"/"+name, tgt_dir_str) + logging.info(f"All repairable broken links in {ln_str} should be.") @classmethod def _swap_link(cls, lnk, tgt): @@ -124,7 +128,7 @@ if __name__ == "__main__": tgt_dir = args.tgt_path logger = logging.getLogger('linkfixer') logging.basicConfig( - format = '%(asctime)s %(module)s %(levelname)s: %(message)s', + format = '%(asctime)s %(module)s %(levelname)s: %(message)s\n', level=args.loglevel.upper() ) logging.debug(f"Log configuration set") diff --git a/search_file.py b/search_file.py index a9a4651..0ad8d84 100644 --- a/search_file.py +++ b/search_file.py @@ -26,7 +26,7 @@ def print_num_list(paths): n += 1 return 0 -def select_path(paths): +def select_path(filename, paths): l = len(paths) if l == 0 : raise Exception("Error select_path - empty list") @@ -34,7 +34,7 @@ def select_path(paths): return paths[0] c = '0' while not (c.isdigit() and 0 < int(c) <= l): - print("Multiple matches found.") + print(f"Multiple matches found for {filename}") print("Please select target:") print_num_list(paths) c = input(f"Please select file to link to: (1-{l})") @@ -44,5 +44,5 @@ def search_file(filename, path): paths = search_all_match(filename, path) if len(paths) == 0 : raise Exception(f"No match found for {filename}") - p = select_path(paths) + p = select_path(filename, paths) return p diff --git a/test/test_link_fixer.py b/test/test_link_fixer.py index 84d4c6f..7424e8a 100644 --- a/test/test_link_fixer.py +++ b/test/test_link_fixer.py @@ -36,7 +36,7 @@ class TestLinkFixer(unittest.TestCase): ln_gd_error = "Error: link test/data/lnk_dr/ln_valid is not broken. Abort." ln_ns_error = "Error: link test/data/tgt_dir/tgt_file.txt is not a link at all. Abort." tgt_invalid_error = "Error: target dir test/data/tgt does not seem to exist or be a directory. Abort." - ln_no_target_warning = "No match for link target in test/data/tgt_dir. Link test/data/lnk_dir/ln_no_target not modified." + ln_no_target_warning = "link_fix aborted." def setup_test(self): try: