mirror of https://github.com/gmbrax/Pilgrim.git
64 lines
2.2 KiB
Python
64 lines
2.2 KiB
Python
import os
|
|
import shutil
|
|
from pathlib import Path
|
|
|
|
|
|
class DirectoryManager:
|
|
@staticmethod
|
|
def get_config_directory() -> Path:
|
|
"""
|
|
Get the ~/.pilgrim directory path.
|
|
Creates it if it doesn't exist.
|
|
"""
|
|
home = Path.home()
|
|
config_dir = home / ".pilgrim"
|
|
config_dir.mkdir(exist_ok=True)
|
|
os.chmod(config_dir, 0o700)
|
|
return config_dir
|
|
|
|
@staticmethod
|
|
def get_diaries_root() -> Path:
|
|
"""Returns the path to the diaries directory."""
|
|
diaries_dir = DirectoryManager.get_config_directory() / "diaries"
|
|
diaries_dir.mkdir(exist_ok=True)
|
|
os.chmod(diaries_dir, 0o700)
|
|
return diaries_dir
|
|
|
|
@staticmethod
|
|
def get_diary_directory(directory_name: str) -> Path:
|
|
"""Returns the directory path for a specific diary."""
|
|
return DirectoryManager.get_diaries_root() / directory_name
|
|
|
|
@staticmethod
|
|
def get_diary_data_directory(directory_name: str) -> Path:
|
|
"""Returns the data directory path for a specific diary."""
|
|
return DirectoryManager.get_diary_directory(directory_name) / "data"
|
|
|
|
@staticmethod
|
|
def get_diary_images_directory(directory_name: str) -> Path:
|
|
"""Returns the images directory path for a specific diary."""
|
|
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()
|
|
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():
|
|
try:
|
|
shutil.copy2(current_db, db_path)
|
|
# Consider using logging instead of print
|
|
print(f"Database migrated from {current_db} to {db_path}")
|
|
except (OSError, shutil.Error) as e:
|
|
raise RuntimeError(f"Failed to migrate database: {e}")
|
|
|
|
return db_path
|