diff --git a/dibbler/conf.py b/dibbler/conf.py index 0a4e404..4e29514 100644 --- a/dibbler/conf.py +++ b/dibbler/conf.py @@ -34,3 +34,32 @@ def load_config(config_path: Path | None = None): else: print("Could not read config file, it was neither provided nor readable in default location", file=sys.stderr) sys.exit(1) + +def config_db_string() -> str: + db_type = config["database"]["type"] + + if db_type == "sqlite": + path = Path(config["database"]["sqlite"]["path"]) + return f"sqlite:///{path.absolute()}" + + elif db_type == "postgresql": + host = config["database"]["postgresql"]["host"] + port = config["database"]["postgresql"].get("port", 5432) + username = config["database"]["postgresql"].get("username", "dibbler") + dbname = config["database"]["postgresql"].get("dbname", "dibbler") + + if "password_file" in config["database"]["postgresql"]: + with Path(config["database"]["postgresql"]["password_file"]).open("r") as f: + password = f.read().strip() + elif "password" in config["database"]["postgresql"]: + password = config["database"]["postgresql"]["password"] + else: + password = '' + + if host.startswith("/"): + return f"postgresql+psycopg2://{username}:{password}@/{dbname}?host={host}" + else: + return f"postgresql+psycopg2://{username}:{password}@{host}:{port}/{dbname}" + else: + print(f"Error: unknown database type '{db_type}'") + exit(1) diff --git a/dibbler/main.py b/dibbler/main.py index 33f25b4..de63975 100644 --- a/dibbler/main.py +++ b/dibbler/main.py @@ -4,7 +4,7 @@ from pathlib import Path from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker -from dibbler.conf import load_config +from dibbler.conf import load_config, config_db_string from dibbler.db import engine, session parser = argparse.ArgumentParser() @@ -34,10 +34,8 @@ def main(): load_config(args.config) - from dibbler.conf import config - global engine, session - engine = create_engine(config['database']['url']) + engine = create_engine(config_db_string()) session = sessionmaker(bind=engine) if args.subcommand == "loop": diff --git a/example-config.toml b/example-config.toml index 7abacb0..0a82593 100644 --- a/example-config.toml +++ b/example-config.toml @@ -5,8 +5,24 @@ show_tracebacks = true input_encoding = 'utf8' [database] -# url = "postgresql://robertem@127.0.0.1/pvvvv" -url = "sqlite:///test.db" +type = 'sqlite' + +[database.sqlite] +path = 'sqlite:///test.db' + +[database.postgresql] +host = 'localhost' +# host = '/run/postgresql' +port = 5432 + +username = 'dibbler' +dbname = 'dibbler' + +# You can either specify a path to a file containing the password, +# or just specify the password directly +# password = 'superhemlig' +# password_file = '/var/lib/dibbler/db-password' + [limits] low_credit_warning_limit = -100 @@ -15,5 +31,5 @@ user_recent_transaction_limit = 100 # See https://pypi.org/project/brother_ql/ for label types # Set rotate to False for endless labels [printer] -label_type = "62" +label_type = '62' label_rotate = false diff --git a/nix/module.nix b/nix/module.nix index db6a6ca..86b3335 100644 --- a/nix/module.nix +++ b/nix/module.nix @@ -77,7 +77,10 @@ in { groups.dibbler = { }; }; - services.dibbler.settings.database.url = lib.mkIf cfg.createLocalDatabase "postgresql://dibbler?host=/run/postgresql"; + services.dibbler.settings.database = lib.mkIf cfg.createLocalDatabase { + type = "postgresql"; + postgresql.host = "/run/postgresql"; + }; services.postgresql = lib.mkIf cfg.createLocalDatabase { ensureDatabases = [ "dibbler" ];