Functional with logging

This commit is contained in:
2024-08-16 00:20:37 -04:00
parent 8f3b9946b0
commit 4d75ae5df6
3 changed files with 13 additions and 9 deletions

View File

@@ -15,7 +15,8 @@ class Link(FileType):
def fix(cls, ln_str, tgt_dir_str): def fix(cls, ln_str, tgt_dir_str):
logging.captureWarnings(True) logging.captureWarnings(True)
print() 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) link = Path(ln_str)
tgt_dir = Path(tgt_dir_str) tgt_dir = Path(tgt_dir_str)
@@ -25,7 +26,7 @@ class Link(FileType):
#print(f"Error: could not access link {ln_str}.") #print(f"Error: could not access link {ln_str}.")
sys.exit(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(): if not tgt_dir.is_dir():
sys.exit(f"Error: target dir {tgt_dir} does not seem to exist or be a directory. Abort.") 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) tgt = search_file(pointed, tgt_dir)
logging.debug(f"Search for matching target {pointed} in {tgt_dir} returned {tgt}") logging.debug(f"Search for matching target {pointed} in {tgt_dir} returned {tgt}")
except Exception as e: 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 return
if tgt != "": if tgt != "":
cls._swap_link(link, tgt) cls._swap_link(link, tgt)
logging.info(f"Link now pointing to {tgt} . Swap of link successfully completed.")
else: else:
warnings.warn(f"Reaching point at which link is to be fixed, but Variable {tgt} seems to be unset!", Warning) warnings.warn(f"Reaching point at which link is to be fixed, but Variable {tgt} seems to be unset!", Warning)
case "directory": 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 (root, dirs, files) in os.walk(link):
for name in files: for name in files:
filetype = cls.get_file_type(root+"/"+name) filetype = cls.get_file_type(root+"/"+name)
@@ -55,6 +58,7 @@ class Link(FileType):
if filetype == "broken-link": if filetype == "broken-link":
logging.info(f"Calling link fixer on {root+"/"+name} targeting {tgt_dir_str}") logging.info(f"Calling link fixer on {root+"/"+name} targeting {tgt_dir_str}")
cls.fix(root+"/"+name, tgt_dir_str) cls.fix(root+"/"+name, tgt_dir_str)
logging.info(f"All repairable broken links in {ln_str} should be.")
@classmethod @classmethod
def _swap_link(cls, lnk, tgt): def _swap_link(cls, lnk, tgt):
@@ -124,7 +128,7 @@ if __name__ == "__main__":
tgt_dir = args.tgt_path tgt_dir = args.tgt_path
logger = logging.getLogger('linkfixer') logger = logging.getLogger('linkfixer')
logging.basicConfig( 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() level=args.loglevel.upper()
) )
logging.debug(f"Log configuration set") logging.debug(f"Log configuration set")

View File

@@ -26,7 +26,7 @@ def print_num_list(paths):
n += 1 n += 1
return 0 return 0
def select_path(paths): def select_path(filename, paths):
l = len(paths) l = len(paths)
if l == 0 : if l == 0 :
raise Exception("Error select_path - empty list") raise Exception("Error select_path - empty list")
@@ -34,7 +34,7 @@ def select_path(paths):
return paths[0] return paths[0]
c = '0' c = '0'
while not (c.isdigit() and 0 < int(c) <= l): 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("Please select target:")
print_num_list(paths) print_num_list(paths)
c = input(f"Please select file to link to: (1-{l})") 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) paths = search_all_match(filename, path)
if len(paths) == 0 : if len(paths) == 0 :
raise Exception(f"No match found for {filename}") raise Exception(f"No match found for {filename}")
p = select_path(paths) p = select_path(filename, paths)
return p return p

View File

@@ -36,7 +36,7 @@ class TestLinkFixer(unittest.TestCase):
ln_gd_error = "Error: link test/data/lnk_dr/ln_valid is not broken. Abort." 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." 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." 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): def setup_test(self):
try: try: