diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2023-10-17 14:02:02 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2023-10-17 14:02:02 +0800 |
| commit | 0168014c8e492dce3b222d890d9b151e71fe93f0 (patch) | |
| tree | ed24106b66546019ec2b202f6509a2c0bb12a11f | |
| parent | ae02693350eba8b3b1a82ed0f3692b4e41a43f76 (diff) | |
fix & support more kind of argument
| -rw-r--r-- | write.py | 54 |
1 files changed, 48 insertions, 6 deletions
@@ -1,9 +1,13 @@ +#!/usr/bin/env python + from stdlib import * from sys import argv from datetime import datetime from configparser import ConfigParser as config +from configparser import NoOptionError, NoSectionError from os import path + expanduser = path.expanduser # function to get config @@ -13,16 +17,47 @@ else: printinfo(f"Using config file {expanduser('~')}/.config/jlog/jlog.conf") config = config() config.read(f"{expanduser('~')}/.config/jlog/jlog.conf") -log_file = config.get('global', 'file').replace('~', expanduser('~')) +try: + log_file = config.get('global', 'file').replace('~', expanduser('~')) +except NoSectionError: + raise NoSectionError(f"Section global not found in {expanduser('~')}/.config/jlog/jlog.conf") +except NoOptionError: + log_file = f"{expanduser('~')}/.config/jlog/jlog_{datetime.now()}" if not path.isfile(log_file): printerror(f"Log file {log_file} not found") printwarning(f"Creating new log file {log_file}") open(log_file, 'x').close() +try: + skip_ask = config.get('global','skip_ask') + try: + skip_ask = bool(skip_ask) + except ValueError: + raise ValueError("Invalid detailed_date") +except NoSectionError: + raise NoSectionError(f"Section global not found in {expanduser('~')}/.config/jlog/jlog.conf") +except NoOptionError: + printerror("Option date/skip_ask not found in {expanduser('~')}/.config/jlog/jlog.conf") + skip_ask = False +try: + ddate = config.get('global', 'detailed_date') + try: + ddate = bool(ddate) + except ValueError: + raise ValueError("Invalid detailed_date") +except NoSectionError: + raise NoSectionError(f"Section global not found in {expanduser('~')}/.config/jlog/jlog.conf") +except NoOptionError: + ddate = False else: printinfo(f"Using log file {log_file}") -if '-sk' not in argv: +if '-sk' in argv: + skip_ask = True +elif '-dd' in argv: + ddate = True + +if not skip_ask: try: if askinput("continue? (y/n)").lower() != 'y': printwarning("Reseting...") @@ -63,7 +98,12 @@ def convert_PRIORITY(priority) -> str: def log_message(priority: int, message: str): priority = convert_PRIORITY(priority) current_date = datetime.now() - return f"[{current_date.date()} {current_date.strftime('%A')} {current_date.hour:02d}:{current_date.minute:02d}] [{priority}]: {message}" + if not ddate: + return f"[{current_date.date()} {current_date.strftime('%A')} {current_date.hour:02d}:{current_date.minute:02d}] [{priority}]: {message}" + elif ddate: + return f"[{current_date}] [{priority}]: {message}" + + # main function(ui) def main(): @@ -76,12 +116,14 @@ def main(): log = log_message(priority, message) -if __name__ == "__main__" and not len(argv) in [3, 4]: +if __name__ == "__main__" and not len(argv) in [2, 3, 4]: main() -elif len(argv) in [3, 4]: +elif len(argv) in [2, 3, 4]: global log log = log_message(argv[1], argv[2]) +else: + exit(1) printinfo(f"Logging into file {log_file}") -open(log_file, 'a').write(log)
\ No newline at end of file +open(log_file, 'a').write(log + '\n')
\ No newline at end of file |
