updates
This commit is contained in:
@@ -8,12 +8,12 @@ from typing import Any
|
||||
|
||||
from omegaconf import OmegaConf
|
||||
|
||||
from src.backend.db import main as template
|
||||
|
||||
# from src.data import pickles
|
||||
import pickle
|
||||
from src.logic.constants import SEMAP_MEDIA_ACCOUNTS
|
||||
from src.logic.dataclass import ApparatData, BookData
|
||||
from log import MyLogger
|
||||
from src.logic.log import MyLogger
|
||||
from icecream import ic
|
||||
config = OmegaConf.load("config.yaml")
|
||||
|
||||
@@ -35,7 +35,6 @@ class Database:
|
||||
pass
|
||||
def create_database(self):
|
||||
#create database from template
|
||||
template(self.database_path)
|
||||
subjects = config.subjects
|
||||
for subject in subjects:
|
||||
self.cur.execute(f"INSERT INTO subjects (name) VALUES ('{subject}')")
|
||||
|
||||
87
src/backend/database_rewrite.py
Normal file
87
src/backend/database_rewrite.py
Normal file
@@ -0,0 +1,87 @@
|
||||
import datetime
|
||||
import os
|
||||
import re
|
||||
import sqlite3 as sql
|
||||
import tempfile
|
||||
import pickle
|
||||
from src.logic.log import MyLogger
|
||||
from icecream import ic
|
||||
from typing import List, Tuple, Dict, Any, Optional, Union
|
||||
from omegaconf import OmegaConf
|
||||
from src.backend.db import CREATE_TABLE_APPARAT, CREATE_TABLE_MESSAGES, CREATE_TABLE_MEDIA, CREATE_TABLE_APPKONTOS, CREATE_TABLE_FILES, CREATE_TABLE_MESSAGES, CREATE_TABLE_PROF, CREATE_TABLE_USER, CREATE_TABLE_SUBJECTS
|
||||
from src.logic.constants import SEMAP_MEDIA_ACCOUNTS
|
||||
from src.logic.dataclass import ApparatData, BookData
|
||||
|
||||
config = OmegaConf.load("config.yaml")
|
||||
logger = MyLogger(__name__)
|
||||
|
||||
def load_pickle(data):
|
||||
return pickle.loads(data)
|
||||
def dump_pickle(data):
|
||||
return pickle.dumps(data)
|
||||
def create_blob(data):
|
||||
with open(data, "rb") as f:
|
||||
return f.read()
|
||||
def create_file(filename:str, blob):
|
||||
with tempfile.TemporaryDirectory(delete=False) as tmpdir:
|
||||
filepath = os.path.join(tmpdir, filename)
|
||||
with open(filepath, "wb") as f:
|
||||
f.write(blob)
|
||||
return filepath
|
||||
|
||||
class Database:
|
||||
def __init__(self, db_path: str = None):
|
||||
if db_path is None:
|
||||
self.db_path = config.database.path
|
||||
else:
|
||||
self.db_path = db_path
|
||||
if self.get_db_contents() is None:
|
||||
self.create_tables()
|
||||
|
||||
def get_db_contents(self):
|
||||
try:
|
||||
with sql.connect(self.db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT * FROM sqlite_master WHERE type='table'")
|
||||
return cursor.fetchall()
|
||||
except sql.OperationalError:
|
||||
return None
|
||||
|
||||
def connect(self):
|
||||
return sql.connect(self.db_path)
|
||||
|
||||
def close_connection(self, conn: sql.Connection):
|
||||
conn.close()
|
||||
|
||||
def create_tables(self):
|
||||
conn = self.connect()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(CREATE_TABLE_APPARAT)
|
||||
cursor.execute(CREATE_TABLE_MESSAGES)
|
||||
cursor.execute(CREATE_TABLE_MEDIA)
|
||||
cursor.execute(CREATE_TABLE_APPKONTOS)
|
||||
cursor.execute(CREATE_TABLE_FILES)
|
||||
cursor.execute(CREATE_TABLE_PROF)
|
||||
cursor.execute(CREATE_TABLE_USER)
|
||||
cursor.execute(CREATE_TABLE_SUBJECTS)
|
||||
conn.commit()
|
||||
self.close_connection(conn)
|
||||
|
||||
def query_db(self, query: str, args: Tuple = (), one: bool = False):
|
||||
conn = self.connect()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(query, args)
|
||||
rv = cursor.fetchall()
|
||||
conn.commit()
|
||||
self.close_connection(conn)
|
||||
return (rv[0] if rv else None) if one else rv
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,73 +1,4 @@
|
||||
import sqlite3
|
||||
from omegaconf import OmegaConf
|
||||
config = OmegaConf.load("config.yaml")
|
||||
subjects = config.subjects
|
||||
|
||||
# Connect to the database
|
||||
def main(database):
|
||||
conn = sqlite3.connect(database)
|
||||
conn.execute(
|
||||
"""CREATE TABLE app_kontos (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
app_id INTEGER,
|
||||
konto INTEGER,
|
||||
passwort TEXT,
|
||||
FOREIGN KEY (app_id) REFERENCES semesterapparat (id)
|
||||
)"""
|
||||
)
|
||||
|
||||
conn.execute(
|
||||
"""CREATE TABLE files (
|
||||
id INTEGER PRIMARY KEY,
|
||||
filename TEXT,
|
||||
fileblob BLOB,
|
||||
app_id INTEGER,
|
||||
filetyp TEXT,
|
||||
prof_id INTEGER REFERENCES prof (id),
|
||||
FOREIGN KEY (app_id) REFERENCES semesterapparat (id)
|
||||
)"""
|
||||
)
|
||||
|
||||
conn.execute(
|
||||
"""CREATE TABLE media (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
bookdata BLOB,
|
||||
app_id INTEGER,
|
||||
prof_id INTEGER,
|
||||
deleted INTEGER DEFAULT (0),
|
||||
available BOOLEAN,
|
||||
reservation BOOLEAN,
|
||||
FOREIGN KEY (prof_id) REFERENCES prof (id),
|
||||
FOREIGN KEY (app_id) REFERENCES semesterapparat (id)
|
||||
)"""
|
||||
)
|
||||
|
||||
conn.execute(
|
||||
"""CREATE TABLE messages (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
created_at date NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
message TEXT NOT NULL,
|
||||
remind_at date NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
user_id INTEGER NOT NULL,
|
||||
appnr INTEGER,
|
||||
FOREIGN KEY (user_id) REFERENCES user (id)
|
||||
)"""
|
||||
)
|
||||
|
||||
conn.execute(
|
||||
"""CREATE TABLE prof (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
titel TEXT,
|
||||
fname TEXT,
|
||||
lname TEXT,
|
||||
fullname TEXT NOT NULL UNIQUE,
|
||||
mail TEXT,
|
||||
telnr TEXT
|
||||
)"""
|
||||
)
|
||||
|
||||
conn.execute(
|
||||
"""CREATE TABLE semesterapparat (
|
||||
CREATE_TABLE_APPARAT = """CREATE TABLE semesterapparat (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
name TEXT,
|
||||
prof_id INTEGER,
|
||||
@@ -84,10 +15,52 @@ def main(database):
|
||||
konto INTEGER REFERENCES app_kontos (id),
|
||||
FOREIGN KEY (prof_id) REFERENCES prof (id)
|
||||
)"""
|
||||
)
|
||||
|
||||
conn.execute(
|
||||
"""CREATE TABLE user (
|
||||
CREATE_TABLE_MEDIA = """CREATE TABLE media (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
bookdata BLOB,
|
||||
app_id INTEGER,
|
||||
prof_id INTEGER,
|
||||
deleted INTEGER DEFAULT (0),
|
||||
available BOOLEAN,
|
||||
reservation BOOLEAN,
|
||||
FOREIGN KEY (prof_id) REFERENCES prof (id),
|
||||
FOREIGN KEY (app_id) REFERENCES semesterapparat (id)
|
||||
)"""
|
||||
CREATE_TABLE_APPKONTOS = """CREATE TABLE app_kontos (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
app_id INTEGER,
|
||||
konto INTEGER,
|
||||
passwort TEXT,
|
||||
FOREIGN KEY (app_id) REFERENCES semesterapparat (id)
|
||||
)"""
|
||||
CREATE_TABLE_FILES = """CREATE TABLE files (
|
||||
id INTEGER PRIMARY KEY,
|
||||
filename TEXT,
|
||||
fileblob BLOB,
|
||||
app_id INTEGER,
|
||||
filetyp TEXT,
|
||||
prof_id INTEGER REFERENCES prof (id),
|
||||
FOREIGN KEY (app_id) REFERENCES semesterapparat (id)
|
||||
)"""
|
||||
CREATE_TABLE_MESSAGES = """CREATE TABLE messages (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
created_at date NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
message TEXT NOT NULL,
|
||||
remind_at date NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
user_id INTEGER NOT NULL,
|
||||
appnr INTEGER,
|
||||
FOREIGN KEY (user_id) REFERENCES user (id)
|
||||
)"""
|
||||
CREATE_TABLE_PROF = """CREATE TABLE prof (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
titel TEXT,
|
||||
fname TEXT,
|
||||
lname TEXT,
|
||||
fullname TEXT NOT NULL UNIQUE,
|
||||
mail TEXT,
|
||||
telnr TEXT
|
||||
)"""
|
||||
CREATE_TABLE_USER = """CREATE TABLE user (
|
||||
id integer NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
username TEXT NOT NULL UNIQUE,
|
||||
@@ -97,34 +70,9 @@ def main(database):
|
||||
email TEXT UNIQUE,
|
||||
name TEXT
|
||||
)"""
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE subjects (
|
||||
CREATE_TABLE_SUBJECTS = """CREATE TABLE subjects (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
name TEXT NOT NULL UNIQUE
|
||||
)
|
||||
"""
|
||||
)
|
||||
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE aliases (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
subject_id INTEGER,
|
||||
FOREIGN KEY (subject_id) REFERENCES subjects (id)
|
||||
)
|
||||
"""
|
||||
)
|
||||
|
||||
# Commit the changes and close the connection
|
||||
conn.commit()
|
||||
#insert subjects
|
||||
|
||||
|
||||
conn.close()
|
||||
)"""
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
65
src/logic/log.py
Normal file
65
src/logic/log.py
Normal file
@@ -0,0 +1,65 @@
|
||||
import logging
|
||||
import os
|
||||
|
||||
if not os.path.exists("logs"):
|
||||
os.mkdir("logs")
|
||||
with open("logs/application.log", "w") as f:
|
||||
pass
|
||||
|
||||
# Create a common file handler for all loggers
|
||||
common_file_handler = logging.FileHandler("logs/application.log")
|
||||
common_file_handler.setLevel(logging.DEBUG)
|
||||
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
||||
common_file_handler.setFormatter(formatter)
|
||||
|
||||
|
||||
class MyLogger:
|
||||
def __init__(self, logger_name):
|
||||
self.logger = logging.getLogger(logger_name)
|
||||
self.logger.setLevel(logging.DEBUG)
|
||||
self.logger.addHandler(common_file_handler)
|
||||
self.encoding = "utf-8"
|
||||
|
||||
def log_info(self, message: str):
|
||||
# ensure that the message is encoded in utf-8
|
||||
self.logger.info(message.encode(self.encoding))
|
||||
|
||||
def log_debug(self, message: str):
|
||||
self.logger.debug(message.encode(self.encoding))
|
||||
|
||||
def log_warning(self, message: str):
|
||||
self.logger.warning(message.encode(self.encoding))
|
||||
|
||||
def log_error(self, message: str):
|
||||
self.logger.error(message.encode(self.encoding))
|
||||
|
||||
def log_critical(self, message: str):
|
||||
self.logger.critical(message.encode(self.encoding))
|
||||
|
||||
def log_exception(self, message: str):
|
||||
self.logger.exception(message)
|
||||
|
||||
|
||||
# Usage example:
|
||||
if __name__ == "__main__":
|
||||
logger1 = MyLogger("Logger1")
|
||||
logger2 = MyLogger("Logger2")
|
||||
|
||||
logger1.log_info("This is an info message from Logger1")
|
||||
logger1.log_debug("This is a debug message from Logger1")
|
||||
logger1.log_warning("This is a warning message from Logger1")
|
||||
logger1.log_error("This is an error message from Logger1")
|
||||
logger1.log_critical("This is a critical message from Logger1")
|
||||
|
||||
logger2.log_info("This is an info message from Logger2")
|
||||
logger2.log_debug("This is a debug message from Logger2")
|
||||
logger2.log_warning("This is a warning message from Logger2")
|
||||
logger2.log_error("This is an error message from Logger2")
|
||||
logger2.log_critical("This is a critical message from Logger2")
|
||||
|
||||
try:
|
||||
# Simulate an exception
|
||||
raise Exception("An exception occurred")
|
||||
except Exception as e:
|
||||
logger1.log_exception("An exception occurred in Logger1")
|
||||
logger2.log_exception("An exception occurred in Logger2")
|
||||
@@ -4,7 +4,7 @@ import time
|
||||
from PyQt6.QtCore import QThread, pyqtSignal
|
||||
|
||||
from src.backend.database import Database
|
||||
from log import MyLogger
|
||||
from src.logic.log import MyLogger
|
||||
from src.transformers import RDS_AVAIL_DATA
|
||||
from src.logic.webrequest import BibTextTransformer, WebRequest
|
||||
import sqlite3
|
||||
|
||||
@@ -18,7 +18,7 @@ from src.logic import c_sort
|
||||
from src.backend.database import Database
|
||||
from src.logic.constants import APP_NRS, PROF_TITLES
|
||||
from src.logic.dataclass import ApparatData, BookData, MailData
|
||||
from log import MyLogger
|
||||
from src.logic.log import MyLogger
|
||||
from src.logic.threads import BookGrabber,AvailChecker
|
||||
from src.ui import (
|
||||
App_Ext_Dialog,
|
||||
|
||||
@@ -3,7 +3,7 @@ from bs4 import BeautifulSoup
|
||||
from omegaconf import OmegaConf
|
||||
|
||||
from src.logic.dataclass import BookData
|
||||
from log import MyLogger
|
||||
from src.logic.log import MyLogger
|
||||
from src.transformers import ARRAYData, BibTeXData, COinSData, RDSData, RISData
|
||||
#import sleep_and_retry decorator to retry requests
|
||||
from ratelimit import limits, sleep_and_retry
|
||||
|
||||
@@ -1 +1,6 @@
|
||||
from .transformers import RDS_AVAIL_DATA, ARRAYData, COinSData, BibTeXData, RISData, RDSData
|
||||
from .transformers import (RDS_AVAIL_DATA,
|
||||
ARRAYData,
|
||||
COinSData,
|
||||
BibTeXData,
|
||||
RISData,
|
||||
RDSData)
|
||||
@@ -9,7 +9,7 @@ from typing import Any, List, Optional
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from src.logic.dataclass import BookData
|
||||
from log import MyLogger
|
||||
from src.logic.log import MyLogger
|
||||
|
||||
logger = MyLogger("transformers.py")
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
from PyQt6 import QtCore, QtGui, QtWidgets
|
||||
|
||||
from log import MyLogger
|
||||
from src.logic.log import MyLogger
|
||||
from src.logic.threads import AutoAdder
|
||||
|
||||
logger = MyLogger("AutoTitleAdder")
|
||||
|
||||
Reference in New Issue
Block a user