4
1
Fork 1

Init DataBase && LogSystem

This commit is contained in:
trueold89 2024-05-04 04:10:21 +03:00
parent d4a46ca5ad
commit 0bb8ceb6c1
Signed by: trueold89
GPG Key ID: C122E85DD49E6B30
2 changed files with 64 additions and 0 deletions

33
DataBase.py Normal file
View File

@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
from sqlite3 import connect as sqconnect
class DataBase(object):
def __init__(self, path: str, table: str) -> None:
self.path = path
self.table = table
def __execute(self, request: str, additional=None):
with sqconnect(self.path) as db:
cursor = db.cursor()
if additional is not None:
cursor.execute(request, additional)
else:
cursor.execute(request)
db.commit()
def createTable(self, table_content: str) -> None:
self.__execute(f'''
CREATE TABLE IF NOT EXISTS {self.table}(
id INTEGER PRIMARY KEY,
{table_content}
)
''')
def insert(self, data: dict) -> None:
fields = ", ".join(list(data.keys()))
values = list(data.values())
self.__execute(f'''
INSERT INTO {self.table} ({fields}) VALUES ({str("?," * len(data))[:-1]})
''', tuple(values))

31
LogSystem.py Normal file
View File

@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
from datetime import datetime
from TYPES import LogTypes
from DataBase import DataBase
from Config import DBPath
class Log(object):
def __init__(self, logtype: LogTypes, location: str, message: str) -> None:
self.logtype = logtype
self.location = location
self.msg = message
self.toDB()
date = datetime.now().date()
time = datetime.now().time().strftime("%H:%M:%S")
def __str__(self):
return f"[{self.date} | {self.time}] [{self.logtype.value}] [{self.location}] {self.msg}"
def toDB(self):
db = DataBase(DBPath, "logs")
db.createTable("date TEXT, time TEXT, type TEXT, location TEXT, message TEXT")
db.insert({
"date": self.date,
"time": self.time,
"type": self.logtype.value,
"location": self.location,
"message": self.msg
})