From 0168014c8e492dce3b222d890d9b151e71fe93f0 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Tue, 17 Oct 2023 14:02:02 +0800 Subject: fix & support more kind of argument --- write.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 6 deletions(-) (limited to 'write.py') diff --git a/write.py b/write.py index df902db..6141bd1 100644 --- a/write.py +++ b/write.py @@ -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 -- cgit v1.2.3