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):
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")