diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2023-10-16 20:12:56 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2023-10-16 20:12:56 +0800 |
| commit | f46c1abac274eb653abfeb713b8940fe0eeb7b64 (patch) | |
| tree | 044dab9de2aca92a1d6d7d4ba2ceb43b19fd3dbc /write.py | |
| parent | 8369b139d67521ffd06293e43a04bf0948c6ce16 (diff) | |
update argv and log
Diffstat (limited to 'write.py')
| -rw-r--r-- | write.py | 57 |
1 files changed, 49 insertions, 8 deletions
@@ -1,10 +1,44 @@ from stdlib import * from sys import argv from datetime import datetime +from configparser import ConfigParser as config +from os import path + +expanduser = path.expanduser + +# function to get config +if not path.isfile(f"{expanduser('~')}/.config/jlog/jlog.conf"): + raise FileNotFoundError(f"Config {expanduser('~')}/.config/jlog/jlog.conf not found") +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('~')) +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() + +else: + printinfo(f"Using log file {log_file}") + +if '-sk' not in argv: + try: + if askinput("continue? (y/n)").lower() != 'y': + printwarning("Reseting...") + from os import execv + from sys import executable + execv(executable, ['python3'] + argv) + except AttributeError: + printwarning("Reseting...") + from os import execv + from sys import executable + execv(executable, ['python3'] + argv) + def convert_PRIORITY(priority) -> str: try: - priority = int(priority[1]) + priority = int(priority) if priority == 1: priority = "DEBUG" elif priority == 2: @@ -18,8 +52,9 @@ def convert_PRIORITY(priority) -> str: elif priority == 6: priority = "FATAL" except ValueError: + priority = priority.upper() if priority not in ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]: - raise ValueError("Invalid argument") + raise ValueError("Invalid priority") return priority @@ -32,15 +67,21 @@ def log_message(priority: int, message: str): # main function(ui) def main(): - priority = ainputf("Priority of log messages: ") + global log + priority = askinput("Priority of log messages: ") # check if priority is valid - message = ainputf("Message to log: ") + message = askinput("Message to log: ") - aprintf(log_message(priority, message)) + log = log_message(priority, message) -if __name__ == "__main__" and not len(argv) == 3: +if __name__ == "__main__" and not len(argv) in [3, 4]: main() -elif len(argv) == 3: - print(log_message(argv[1], argv[2]))
\ No newline at end of file +elif len(argv) in [3, 4]: + global log + log = log_message(argv[1], argv[2]) + + +printinfo(f"Logging into file {log_file}") +open(log_file, 'a').write(log)
\ No newline at end of file |
