#!/opt/homebrew/bin/python3 """ MAIN FILE FOR WORKLOGGER author: Wacky404 """ import json import sys import os import os.path as osp import logging import utils.paths_util_worklogger as pu from funcs_worklogger import configure, add_log, combine_log from args_worklogger import parser from pathlib import Path from utils.log_util_worklogger import logger, setup_logging from typing import Optional SAVEPATH: Path | str BACKUPPATH: Path | str formats: dict = { "TEXT": '.txt', "JSON": '.json', "CSV": '.csv' } # creates a NameSpace of arguments that were made args = parser.parse_args() numeric_loglevel = getattr(logging, str(args.log).upper()) if isinstance(numeric_loglevel, int): setup_logging(numeric_loglevel) else: setup_logging() dotfile: list | None = None # this will change once this moves to another directory; depends on install path if osp.exists(osp.join(os.getcwd(), os.pardir)): dotfile = list(Path(osp.join(osp.expanduser('~'), ".config")).glob( '**/*.workloggerconfig.json')) logger.debug(f"Found dotfile(s): {dotfile}") if len(dotfile) > 1: logger.error( f"You have {len(dotfile)} config files, using defaults...") dotfile = None elif len(dotfile) == 1: logger.debug(f"Using user config file in {str(dotfile[0])}") elif len(dotfile) == 0: logger.debug(f"Could not find your config file, using defaults...") dotfile = None settings: dict | None = None if dotfile is not None: with open(dotfile[0], 'r') as fd: try: settings = json.load(fd) except Exception as e: logger.exception(str(e)) logger.error( "There was an error loading your config. Using defaults.") if settings is not None: SAVEPATH = Path(osp.join(osp.expanduser("~"), settings['savepath'])) BACKUPPATH = Path(osp.join(osp.expanduser("~"), settings['backuppath'])) try: numeric_loglevel_settings = getattr( logging, str(settings['loglvl']).upper()) setup_logging(numeric_loglevel_settings) except Exception as e: logger.exception(str(e)) dir_list: list = [d for d in [ SAVEPATH, BACKUPPATH] if not osp.exists(d)] if len(dir_list) > 0: configure(dir_list=dir_list) else: SAVEPATH = Path(osp.join(osp.expanduser("~"), "Documents")) BACKUPPATH = Path(osp.join(osp.expanduser("~"), "Documents", "worklogger")) if 'extension' in vars(args).keys(): combine_log(target_job=args.job, specified_ext=formats[str(args.extension).upper( )], delete=args.delete, savepath=SAVEPATH, backuppath=BACKUPPATH) sys.exit() jname_proj: dict[str, dict] = {} if settings is not None: for j in settings['jobs']: jname_proj[str(j['name']).upper()] = j['projects'][0] logger.debug(f"{jname_proj}") for name, projs in jname_proj.items(): projs_new: dict[str, str] = {} for key, proj in projs.items(): projs_new[str(key).upper()] = proj projs = projs_new jname_proj[name] = projs logger.debug(f"{jname_proj}") try: filepath = osp.join(SAVEPATH, f"{str(args.job).upper()}{formats[str( settings['fileformat']).upper()]}") filepath_backup = osp.join(BACKUPPATH, f"{str(args.job).upper()}{formats[str( settings['fileformat']).upper()]}") for _path in [filepath, filepath_backup]: if not osp.exists(_path): with open(_path, 'x'): pass except Exception as e: logger.exception(str(e)) else: try: filepath = osp.join(SAVEPATH, f"{str(args.job).upper()}.txt") filepath_backup = osp.join(BACKUPPATH, f"{str(args.job).upper()}.txt") for _path in [filepath, filepath_backup]: if not osp.exists(_path): with open(_path, 'x'): pass except Exception as e: logger.exception(str(e)) if settings is not None: add_log(file_format=formats[str( settings['fileformat']).upper()], proj_settings=jname_proj, savepath=SAVEPATH, backuppath=BACKUPPATH, job=args.job, proj=args.project, loc=args.location, time=args.time, start=args.start, end=args.end, message=args.message) else: add_log(file_format=None, proj_settings=None, savepath=SAVEPATH, backuppath=BACKUPPATH, job=args.job, proj=args.project, loc=args.location, time=args.time, start=args.start, end=args.end, message=args.message)