update code, switch to loguru
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
import sys
|
||||
from config import Config
|
||||
from loguru import logger
|
||||
from datetime import datetime
|
||||
import atexit
|
||||
import argparse
|
||||
__version__ = "0.2.0"
|
||||
__author__ = "Alexander Kirchner"
|
||||
__email__ = "alexander.kirchner@ph-freiburg.de"
|
||||
@@ -8,54 +12,60 @@ docport = 6543
|
||||
|
||||
|
||||
config = Config("config/settings.yaml")
|
||||
valid_args = ["--debug", "--log", "--no-backup", "--ic-logging", "--version", "-h","--no-documentation"]
|
||||
_config = config
|
||||
# Store original values that might be overridden by CLI
|
||||
|
||||
args_description = {
|
||||
"--debug": "Enable debug mode",
|
||||
"--log": "Enable logging",
|
||||
"--no-backup": "Disable database backup",
|
||||
"--ic-logging": "Enable icecream logging (not available in production)",
|
||||
"--version": "Show version",
|
||||
"-h": "Show help message and exit",
|
||||
"--no-documentation": "Disable documentation server and shortcut"
|
||||
}
|
||||
|
||||
args = sys.argv[1:]
|
||||
if any(arg not in valid_args for arg in args):
|
||||
print("Invalid argument present")
|
||||
print([arg for arg in args if arg not in valid_args])
|
||||
sys.exit()
|
||||
|
||||
def help():
|
||||
print("Ausleihsystem")
|
||||
print("Ein Ausleihsystem für Handbibliotheken")
|
||||
print("Version: {}".format(__version__))
|
||||
print("Valide Argumente:")
|
||||
print("args")
|
||||
print("--------")
|
||||
print("usage: main.py [-h] [--debug] [--log] [--no-backup] [--ic-logging] [--version] [--no-documentation]")
|
||||
print("options:")
|
||||
for arg in valid_args:
|
||||
print(f"{arg} : {args_description[arg]}")
|
||||
|
||||
# based on the arguments, set the config values
|
||||
if"-h" in args:
|
||||
help()
|
||||
sys.exit()
|
||||
if "--debug" in args:
|
||||
args = argparse.ArgumentParser()
|
||||
args.add_argument("--debug", help="Debugging mode", action="store_true")
|
||||
args.add_argument("--no-backup", help="Disable backups", action="store_true")
|
||||
args.add_argument("--version", help="Print version", action="store_true")
|
||||
args.add_argument(
|
||||
"--no-documentation", help="Disable documentation", action="store_true"
|
||||
)
|
||||
args = args.parse_args()
|
||||
|
||||
if args.version:
|
||||
print(f"Version: {__version__}")
|
||||
sys.exit(0)
|
||||
|
||||
# Override config values temporarily without saving
|
||||
changes_made = False
|
||||
|
||||
if args.debug:
|
||||
config.debug = True
|
||||
if "--log" in args:
|
||||
config.log_debug = True
|
||||
if "--no-backup" in args:
|
||||
config.no_backup = True
|
||||
if "--ic-logging" in args:
|
||||
config.ic_logging = True
|
||||
if "--no-documentation" in args:
|
||||
changes_made = True
|
||||
if args.no_backup:
|
||||
config.database.do_backup = False
|
||||
changes_made = True
|
||||
if args.no_documentation:
|
||||
config.documentation = False
|
||||
if "--version" in args:
|
||||
print(__version__)
|
||||
sys.exit()
|
||||
changes_made = True
|
||||
|
||||
if changes_made:
|
||||
config.save()
|
||||
|
||||
|
||||
def restore_config():
|
||||
global _config
|
||||
config._config = _config
|
||||
config.save()
|
||||
log.info("Restored configuration")
|
||||
|
||||
atexit.register(restore_config)
|
||||
log = logger
|
||||
log.remove()
|
||||
log.add("logs/application.log", rotation="1 week", compression="zip")
|
||||
# add a log for the current day
|
||||
log.add(
|
||||
f"logs/{datetime.now().strftime('%Y-%m-%d')}.log",
|
||||
rotation="1 day",
|
||||
compression="zip",
|
||||
)
|
||||
print(config.debug)
|
||||
if config.debug:
|
||||
import sys
|
||||
|
||||
log.add(
|
||||
sys.stderr,
|
||||
)
|
||||
Reference in New Issue
Block a user