mirror of https://github.com/gmbrax/Pilgrim.git
Compare commits
3 Commits
3843be6e13
...
bdaa37e355
| Author | SHA1 | Date |
|---|---|---|
|
|
bdaa37e355 | |
|
|
a540347127 | |
|
|
fe35cb93bd |
|
|
@ -21,6 +21,7 @@
|
||||||
"sqlalchemy",
|
"sqlalchemy",
|
||||||
"textual",
|
"textual",
|
||||||
"tomli",
|
"tomli",
|
||||||
|
"tomli_w"
|
||||||
|
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -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,38 +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()
|
||||||
|
|
||||||
def get_database_path() -> Path:
|
|
||||||
"""
|
|
||||||
Get the database file path following XDG Base Directory specification.
|
|
||||||
Creates the directory if it doesn't exist.
|
|
||||||
"""
|
|
||||||
# Get home directory
|
|
||||||
home = Path.home()
|
|
||||||
|
|
||||||
# Create .pilgrim directory if it doesn't exist
|
|
||||||
pilgrim_dir = home / ".pilgrim"
|
|
||||||
pilgrim_dir.mkdir(exist_ok=True)
|
|
||||||
|
|
||||||
# Database file path
|
|
||||||
db_path = pilgrim_dir / "database.db"
|
|
||||||
|
|
||||||
# If database doesn't exist in new location but exists in current directory,
|
|
||||||
# migrate it
|
|
||||||
if not db_path.exists():
|
|
||||||
current_db = Path("database.db")
|
|
||||||
if current_db.exists():
|
|
||||||
shutil.copy2(current_db, db_path)
|
|
||||||
print(f"Database migrated from {current_db} to {db_path}")
|
|
||||||
|
|
||||||
return db_path
|
|
||||||
|
|
||||||
class Database:
|
class Database:
|
||||||
def __init__(self):
|
def __init__(self,config_manager:ConfigManager):
|
||||||
db_path = get_database_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
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import os
|
import os
|
||||||
|
import shutil
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -37,3 +38,26 @@ class DirectoryManager:
|
||||||
def get_diary_images_directory(directory_name: str) -> Path:
|
def get_diary_images_directory(directory_name: str) -> Path:
|
||||||
"""Returns the images directory path for a specific diary."""
|
"""Returns the images directory path for a specific diary."""
|
||||||
return DirectoryManager.get_diary_data_directory(directory_name) / "images"
|
return DirectoryManager.get_diary_data_directory(directory_name) / "images"
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_database_path() -> Path:
|
||||||
|
"""
|
||||||
|
Get the database file path following XDG Base Directory specification.
|
||||||
|
Creates the directory if it doesn't exist.
|
||||||
|
"""
|
||||||
|
|
||||||
|
pilgrim_dir = DirectoryManager.get_config_directory()
|
||||||
|
pilgrim_dir.mkdir(exist_ok=True)
|
||||||
|
|
||||||
|
# Database file path
|
||||||
|
db_path = pilgrim_dir / "database.db"
|
||||||
|
|
||||||
|
# If database doesn't exist in new location but exists in current directory,
|
||||||
|
# migrate it
|
||||||
|
if not db_path.exists():
|
||||||
|
current_db = Path("database.db")
|
||||||
|
if current_db.exists():
|
||||||
|
shutil.copy2(current_db, db_path)
|
||||||
|
print(f"Database migrated from {current_db} to {db_path}")
|
||||||
|
|
||||||
|
return db_path
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue