-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
39 lines (29 loc) · 986 Bytes
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import logging
import pyclbr
from pydoc import locate
import peewee
from config import COGS, DATABASE
logger = logging.getLogger(__name__)
class BaseModel(peewee.Model):
class Meta:
database = peewee.SqliteDatabase(DATABASE)
def db_setup():
"""Connect to the database and create tables for each model in each cog.models file"""
logger.info("Connecting to peewee database")
db = peewee.SqliteDatabase(DATABASE)
db.connect()
models = []
logger.info("Attaching cogs: %s", COGS)
for cog in COGS:
try:
module_info = pyclbr.readmodule(f"{cog}.models")
for i in module_info.values():
if (
i.super is not None
and isinstance(i.super, list)
and "BaseModel" in i.super
):
models.append(locate(f"{cog}.models.{i.name}"))
except ModuleNotFoundError:
pass
db.create_tables(models)