Database: allow mysql, config in ini file

Also check for ini file existence at startup
This commit is contained in:
2022-07-20 23:31:12 +02:00
parent 7f33df91ef
commit 25c5a4179c
4 changed files with 36 additions and 3 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
*.db *.db
fioul.ini
__pycache__ __pycache__

View File

@@ -2,4 +2,10 @@ from configparser import ConfigParser
import os import os
conf = ConfigParser() conf = ConfigParser()
conf.read(os.path.dirname(__file__) + "/fioul.ini") ini_file = os.path.dirname(__file__) + "/fioul.ini"
try:
conf.read_file(open(ini_file))
except FileNotFoundError:
print("Configuration file 'fioul.ini' not found")
print("Please take a look at 'fioul.ini.example' to create one")
exit(42)

View File

@@ -1,3 +1,16 @@
[db]
backend = sqlite
[sqlite]
filename = fioul.db
[mysql]
database = fioul
user = please change me
password = please change me
host = localhost
port = 3306
[server] [server]
host = localhost host = localhost
port = 9980 port = 9980

View File

@@ -1,11 +1,24 @@
from config import conf
from peewee import SqliteDatabase from peewee import SqliteDatabase
from peewee import Model from peewee import Model
from peewee import AutoField, DateField, IntegerField from peewee import AutoField, DateField, IntegerField
from peewee import IntegrityError from peewee import IntegrityError
# SQLite database backend = conf["db"].get("backend", None)
db = SqliteDatabase("fioul.db") if backend == "sqlite":
# SQLite database
db = SqliteDatabase(conf["sqlite"].get("filename", "sqlite.db"))
elif backend == "mysql":
db = MySQLDatabase(
conf["mysql"].get("database"),
user=conf["mysql"].get("user"),
password=conf["mysql"].get("password"),
host=conf["mysql"].get("host", "localhost"),
port=conf["mysql"].getint("port", 3306),
)
else:
db = None
class BaseModel(Model): class BaseModel(Model):