load from config, don't hardcode

This commit is contained in:
pettai 2025-03-05 16:04:57 +01:00
parent f448be8bf8
commit 76f403a6ed
Signed by: pettai
GPG key ID: D536054C16A6F808

View file

@ -1,9 +1,18 @@
#!/usr/bin/env python3
import yaml
import sqlite3
try:
with sqlite3.connect('/var/lib/knot_rest/database.db') as conn:
with open('/etc/knot_rest/knot_rest.yaml') as stream:
yamlconf = yaml.safe_load(stream)
except Exception as e:
print(e)
knotrestdb = yamlconf["database"].removeprefix("sqlite:///")
try:
with sqlite3.connect(knotrestdb) as conn:
cur = conn.cursor()
cur.execute('select username, description, logged_in from user')
rows = cur.fetchall()
@ -14,7 +23,7 @@ try:
description = "(no description)"
lastlogin = row[2]
if lastlogin == None:
lastlogin = "***never logged in***"
lastlogin = "**never logged in**"
print(f"{username:<27}{description:<27}{lastlogin:<23}")
except sqlite3.OperationalError as e:
print(e)