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
fioul.ini
__pycache__

View File

@@ -2,4 +2,10 @@ from configparser import ConfigParser
import os
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]
host = localhost
port = 9980

View File

@@ -1,11 +1,24 @@
from config import conf
from peewee import SqliteDatabase
from peewee import Model
from peewee import AutoField, DateField, IntegerField
from peewee import IntegrityError
backend = conf["db"].get("backend", None)
if backend == "sqlite":
# SQLite database
db = SqliteDatabase("fioul.db")
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):