mirror of https://github.com/gmbrax/Pilgrim.git
Add the tomli library and create the config_manager.py to manage the configuration file and also add the dependency injection on the ui.py via the Application class
This commit is contained in:
parent
0adb230dd7
commit
3843be6e13
|
|
@ -20,6 +20,8 @@
|
|||
dependencies = [
|
||||
"sqlalchemy",
|
||||
"textual",
|
||||
"tomli",
|
||||
|
||||
|
||||
]
|
||||
[template.plugins.default]
|
||||
|
|
|
|||
|
|
@ -1,17 +1,18 @@
|
|||
from pilgrim.database import Database
|
||||
from pilgrim.service.servicemanager import ServiceManager
|
||||
from pilgrim.ui.ui import UIApp
|
||||
from pilgrim.utils import DirectoryManager
|
||||
from pilgrim.utils import DirectoryManager, ConfigManager
|
||||
|
||||
|
||||
class Application:
|
||||
def __init__(self):
|
||||
self.config_dir = DirectoryManager.get_config_directory()
|
||||
self.database = Database()
|
||||
self.config_manager = ConfigManager()
|
||||
session = self.database.session()
|
||||
session_manager = ServiceManager()
|
||||
session_manager.set_session(session)
|
||||
self.ui = UIApp(session_manager)
|
||||
self.ui = UIApp(session_manager,self.config_manager)
|
||||
|
||||
def run(self):
|
||||
self.database.create()
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ from pilgrim.service.servicemanager import ServiceManager
|
|||
from pilgrim.ui.screens.about_screen import AboutScreen
|
||||
from pilgrim.ui.screens.diary_list_screen import DiaryListScreen
|
||||
from pilgrim.ui.screens.edit_entry_screen import EditEntryScreen
|
||||
from pilgrim.utils import ConfigManager
|
||||
|
||||
CSS_FILE_PATH = Path(__file__).parent / "styles" / "pilgrim.css"
|
||||
|
||||
|
|
@ -16,9 +17,10 @@ CSS_FILE_PATH = Path(__file__).parent / "styles" / "pilgrim.css"
|
|||
class UIApp(App):
|
||||
CSS_PATH = CSS_FILE_PATH
|
||||
|
||||
def __init__(self,service_manager: ServiceManager, **kwargs):
|
||||
def __init__(self,service_manager: ServiceManager, config_manager: ConfigManager, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self.service_manager = service_manager
|
||||
self.config_manager = config_manager
|
||||
|
||||
|
||||
def on_mount(self) -> None:
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
from .directory_manager import DirectoryManager
|
||||
from .config_manager import ConfigManager
|
||||
|
||||
__all__ = ['DirectoryManager']
|
||||
__all__ = ['DirectoryManager', 'ConfigManager']
|
||||
|
|
|
|||
|
|
@ -0,0 +1,35 @@
|
|||
from threading import Lock
|
||||
|
||||
import tomli
|
||||
from pilgrim.utils import DirectoryManager
|
||||
|
||||
class SingletonMeta(type):
|
||||
_instances = {}
|
||||
_lock: Lock = Lock()
|
||||
|
||||
def __call__(cls, *args, **kwargs):
|
||||
with cls._lock:
|
||||
if cls not in cls._instances:
|
||||
instance = super().__call__(*args, **kwargs)
|
||||
cls._instances[cls] = instance
|
||||
return cls._instances[cls]
|
||||
|
||||
class ConfigManager(metaclass=SingletonMeta):
|
||||
def __init__(self):
|
||||
self.database_url = None
|
||||
self.database_type = None
|
||||
self.auto_open_diary = None
|
||||
self.auto_open_new_diary = None
|
||||
@staticmethod
|
||||
def read_config():
|
||||
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}")
|
||||
|
||||
def set_database_url(self):
|
||||
pass
|
||||
Loading…
Reference in New Issue