Database: allow mysql, config in ini file
Also check for ini file existence at startup
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
*.db
|
*.db
|
||||||
|
fioul.ini
|
||||||
__pycache__
|
__pycache__
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
15
models.py
15
models.py
@@ -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
|
||||||
|
|
||||||
|
backend = conf["db"].get("backend", None)
|
||||||
|
if backend == "sqlite":
|
||||||
# SQLite database
|
# 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):
|
class BaseModel(Model):
|
||||||
|
|||||||
Reference in New Issue
Block a user