mirror of https://github.com/gmbrax/Pilgrim.git
Add the database file path now is read by the configuration file
This commit is contained in:
parent
a540347127
commit
bdaa37e355
|
|
@ -1,20 +1,20 @@
|
||||||
from pilgrim.database import Database
|
from pilgrim.database import Database
|
||||||
from pilgrim.service.servicemanager import ServiceManager
|
from pilgrim.service.servicemanager import ServiceManager
|
||||||
from pilgrim.ui.ui import UIApp
|
from pilgrim.ui.ui import UIApp
|
||||||
from pilgrim.utils import DirectoryManager, ConfigManager
|
from pilgrim.utils import ConfigManager
|
||||||
|
|
||||||
|
|
||||||
class Application:
|
class Application:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.config_dir = DirectoryManager.get_config_directory()
|
|
||||||
self.database = Database()
|
|
||||||
self.config_manager = ConfigManager()
|
self.config_manager = ConfigManager()
|
||||||
|
self.database = Database(self.config_manager)
|
||||||
session = self.database.session()
|
session = self.database.session()
|
||||||
session_manager = ServiceManager()
|
session_manager = ServiceManager()
|
||||||
session_manager.set_session(session)
|
session_manager.set_session(session)
|
||||||
self.ui = UIApp(session_manager,self.config_manager)
|
self.ui = UIApp(session_manager,self.config_manager)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
self.config_manager.read_config()
|
||||||
self.database.create()
|
self.database.create()
|
||||||
self.ui.run()
|
self.ui.run()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,16 +5,17 @@ from pathlib import Path
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
|
from pilgrim.utils import ConfigManager
|
||||||
|
|
||||||
Base = declarative_base()
|
Base = declarative_base()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Database:
|
class Database:
|
||||||
def __init__(self,):
|
def __init__(self,config_manager:ConfigManager):
|
||||||
db_path = "./"
|
db_path = config_manager.database_url
|
||||||
self.engine = create_engine(
|
self.engine = create_engine(
|
||||||
f"sqlite:///{db_path}",
|
f"sqlite:///{config_manager.database_url}",
|
||||||
echo=False,
|
echo=False,
|
||||||
connect_args={"check_same_thread": False},
|
connect_args={"check_same_thread": False},
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,13 @@
|
||||||
|
import os.path
|
||||||
|
from os import PathLike
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
|
|
||||||
import tomli
|
import tomli
|
||||||
|
import tomli_w
|
||||||
|
|
||||||
from pilgrim.utils import DirectoryManager
|
from pilgrim.utils import DirectoryManager
|
||||||
|
|
||||||
|
|
||||||
class SingletonMeta(type):
|
class SingletonMeta(type):
|
||||||
_instances = {}
|
_instances = {}
|
||||||
_lock: Lock = Lock()
|
_lock: Lock = Lock()
|
||||||
|
|
@ -14,22 +19,78 @@ class SingletonMeta(type):
|
||||||
cls._instances[cls] = instance
|
cls._instances[cls] = instance
|
||||||
return cls._instances[cls]
|
return cls._instances[cls]
|
||||||
|
|
||||||
|
|
||||||
class ConfigManager(metaclass=SingletonMeta):
|
class ConfigManager(metaclass=SingletonMeta):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.database_url = None
|
self.database_url = None
|
||||||
self.database_type = None
|
self.database_type = None
|
||||||
self.auto_open_diary = None
|
self.auto_open_diary = None
|
||||||
self.auto_open_new_diary = None
|
self.auto_open_new_diary = None
|
||||||
@staticmethod
|
self.config_dir = DirectoryManager.get_config_directory()
|
||||||
def read_config():
|
self.__data = None
|
||||||
|
|
||||||
|
def read_config(self):
|
||||||
|
if os.path.exists(f"{DirectoryManager.get_config_directory()}/config.toml"):
|
||||||
|
try:
|
||||||
|
with open(f"{DirectoryManager.get_config_directory()}/config.toml", "rb") as f:
|
||||||
|
data = tomli.load(f)
|
||||||
|
print(data)
|
||||||
|
except FileNotFoundError:
|
||||||
|
print("Error: config.toml not found.")
|
||||||
|
except tomli.TOMLDecodeError as e:
|
||||||
|
print(f"Error decoding TOML: {e}")
|
||||||
|
|
||||||
|
self.__data = data
|
||||||
|
self.database_url = self.__data["database"]["url"]
|
||||||
|
self.database_type = self.__data["database"]["type"]
|
||||||
|
|
||||||
|
if self.__data["settings"]["diary"]["auto_open_diary_on_startup"] == "":
|
||||||
|
self.auto_open_diary = None
|
||||||
|
self.auto_open_new_diary = self.__data["settings"]["diary"]["auto_open_on_creation"]
|
||||||
|
else:
|
||||||
|
print("Error: config.toml not found.")
|
||||||
|
self.create_config()
|
||||||
|
self.read_config()
|
||||||
|
|
||||||
|
def create_config(self, config: dict = None):
|
||||||
|
default = {
|
||||||
|
"database": {
|
||||||
|
"url": f"{DirectoryManager.get_config_directory()}/database.db",
|
||||||
|
"type": "sqlite"
|
||||||
|
},
|
||||||
|
"settings": {
|
||||||
|
"diary": {
|
||||||
|
"auto_open_diary_on_startup": "",
|
||||||
|
"auto_open_on_creation": False
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if config is None:
|
||||||
|
config = default
|
||||||
try:
|
try:
|
||||||
with open(f"{DirectoryManager.get_config_directory()}/config.toml", "rb") as f:
|
with open(f"{DirectoryManager.get_config_directory()}/config.toml", "wb") as f:
|
||||||
data = tomli.load(f)
|
tomli_w.dump(config, f)
|
||||||
print(data)
|
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print("Error: config.toml not found.")
|
print("Error: config.toml not found.")
|
||||||
except tomli.TOMLDecodeError as e:
|
|
||||||
print(f"Error decoding TOML: {e}")
|
|
||||||
|
|
||||||
def set_database_url(self):
|
def save_config(self):
|
||||||
pass
|
if self.__data is None:
|
||||||
|
self.read_config()
|
||||||
|
self.__data["database"]["url"] = self.database_url
|
||||||
|
self.__data["database"]["type"] = self.database_type
|
||||||
|
self.__data["settings"]["diary"]["auto_open_diary_on_startup"] = self.auto_open_diary or ""
|
||||||
|
self.__data["settings"]["diary"]["auto_open_on_creation"] = self.auto_open_new_diary
|
||||||
|
|
||||||
|
self.create_config(self.__data)
|
||||||
|
|
||||||
|
def set_config_dir(self, value):
|
||||||
|
self.config_dir = value
|
||||||
|
|
||||||
|
def set_database_url(self, value: str):
|
||||||
|
self.database_url = value
|
||||||
|
|
||||||
|
def set_auto_open_diary(self, value: str):
|
||||||
|
self.auto_open_diary = value
|
||||||
|
|
||||||
|
def set_auto_open_new_diary(self, value: bool):
|
||||||
|
self.auto_open_new_diary = value
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue