From abe17d8c575f8d407184cb78de8a340159c589bd Mon Sep 17 00:00:00 2001 From: WorldTeacher Date: Tue, 3 Jun 2025 13:16:03 +0200 Subject: [PATCH] fix path decoding --- src/__init__.py | 4 ++-- src/backend/database.py | 33 ++++++++++++++--------------- src/backend/delete_temp_contents.py | 5 +---- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/__init__.py b/src/__init__.py index 2661405..fdef22f 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -4,8 +4,8 @@ import os settings = Config("config/config.yaml") -if not os.path.exists(settings.database.temp): - os.mkdir(settings.database.temp) +if not os.path.exists(settings.database.temp.expanduser()): + settings.database.temp.expanduser().mkdir(parents=True, exist_ok=True) from .utils.icon import Icon __version__ = "0.2.1" diff --git a/src/backend/database.py b/src/backend/database.py index 645d023..2341db3 100644 --- a/src/backend/database.py +++ b/src/backend/database.py @@ -19,9 +19,11 @@ from src.backend.db import ( CREATE_TABLE_SUBJECTS, CREATE_TABLE_USER, ) +from pathlib import Path from src.errors import AppPresentError, NoResultError from src.logic import ApparatData, BookData, Prof, Apparat, ELSA from src.logic.constants import SEMAP_MEDIA_ACCOUNTS +from src.utils.blob import create_blob from .semester import Semester from string import ascii_lowercase as lower, digits, punctuation import loguru @@ -29,7 +31,7 @@ import sys log = loguru.logger log.remove() -log.add(sys.stdout) +log.add(sys.stdout, level="INFO") log.add("logs/application.log", rotation="1 MB", retention="10 days") @@ -44,7 +46,7 @@ class Database: Initialize the database and create the tables if they do not exist. """ - def __init__(self, db_path: str = None): + def __init__(self, db_path: Path = None): """ Default constructor for the database class @@ -52,17 +54,17 @@ class Database: db_path (str, optional): Optional Path for testing / specific purposes. Defaults to None. """ if db_path is None: - self.db_path = self.database.path + self.database.name - self.db_path = self.db_path.replace("~", str(Path.home())) + self.db_path = Path(self.database.path.expanduser(), self.database.name) + # self.db_path = self.db_path.replace("~", str(Path.home())) log.debug(self.db_path) else: self.db_path = db_path self.checkDatabaseStatus() def checkDatabaseStatus(self): - path = self.database.path - path = path.replace("~", str(Path.home())) - path = os.path.abspath(path) + path = self.database.path.expanduser() + # path = path.replace("~", str(Path.home())) + # path = os.path.abspath(path) if not os.path.exists(path): # create path # log.debug(path) @@ -503,11 +505,9 @@ class Database: str: The filename of the recreated file """ blob = self.getBlob(filename, app_id) - tempdir = self.database.temp - tempdir = tempdir.replace("~", str(Path.home())) - tempdir_path = Path(tempdir) - if not os.path.exists(tempdir_path): - os.mkdir(tempdir_path) + tempdir = self.database.temp.expanduser() + if not tempdir.exists(): + tempdir.mkdir(parents=True, exist_ok=True) file = tempfile.NamedTemporaryFile( delete=False, dir=tempdir_path, mode="wb", suffix=f".{filetype}" ) @@ -1463,11 +1463,10 @@ class Database: "SELECT fileblob FROM elsa_files WHERE filename=?", (filename,), one=True )[0] # log.debug(blob) - tempdir = self.database.temp - tempdir = tempdir.replace("~", str(Path.home())) - tempdir_path = Path(tempdir) - if not os.path.exists(tempdir_path): - os.mkdir(tempdir_path) + tempdir = self.database.temp.expanduser() + if not tempdir.exists(): + tempdir.mkdir(parents=True, exist_ok=True) + file = tempfile.NamedTemporaryFile( delete=False, dir=tempdir_path, mode="wb", suffix=f".{filetype}" ) diff --git a/src/backend/delete_temp_contents.py b/src/backend/delete_temp_contents.py index c4fbfc5..21f393c 100644 --- a/src/backend/delete_temp_contents.py +++ b/src/backend/delete_temp_contents.py @@ -9,10 +9,7 @@ def delete_temp_contents(): """ delete_temp_contents deletes the contents of the temp directory. """ - path = database.temp - path = path.replace("~", str(Path.home())) - path = Path(path) - path = path.resolve() + path = database.temp.expanduser() for root, dirs, files in os.walk(path): for file in files: os.remove(os.path.join(root, file))