Add centralized path management and sample database generator

Co-authored-by: elisiariocouto <818914+elisiariocouto@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-09-11 22:20:25 +00:00
committed by Elisiário Couto
parent 0c030efef2
commit e9711339bd
9 changed files with 635 additions and 68 deletions

View File

@@ -5,6 +5,7 @@ from pathlib import Path
from typing import Dict, Any, Optional
from loguru import logger
from leggen.utils.paths import path_manager
class Config:
@@ -23,9 +24,10 @@ class Config:
if config_path is None:
config_path = os.environ.get(
"LEGGEN_CONFIG_FILE",
str(Path.home() / ".config" / "leggen" / "config.toml"),
"LEGGEN_CONFIG_FILE"
)
if not config_path:
config_path = str(path_manager.get_config_file_path())
self._config_path = config_path
@@ -53,9 +55,10 @@ class Config:
if config_path is None:
config_path = self._config_path or os.environ.get(
"LEGGEN_CONFIG_FILE",
str(Path.home() / ".config" / "leggen" / "config.toml"),
"LEGGEN_CONFIG_FILE"
)
if not config_path:
config_path = str(path_manager.get_config_file_path())
if config_path is None:
raise ValueError("No config path specified")

View File

@@ -121,6 +121,8 @@ def create_app() -> FastAPI:
def main():
import argparse
from pathlib import Path
from leggen.utils.paths import path_manager
parser = argparse.ArgumentParser(description="Start the Leggend API service")
parser.add_argument(
@@ -132,8 +134,24 @@ def main():
parser.add_argument(
"--port", type=int, default=8000, help="Port to bind to (default: 8000)"
)
parser.add_argument(
"--config-dir",
type=Path,
help="Directory containing configuration files (default: ~/.config/leggen)",
)
parser.add_argument(
"--database",
type=Path,
help="Path to SQLite database file (default: <config-dir>/leggen.db)",
)
args = parser.parse_args()
# Set up path manager with user-provided paths
if args.config_dir:
path_manager.set_config_dir(args.config_dir)
if args.database:
path_manager.set_database_path(args.database)
if args.reload:
# Use string import for reload to work properly
uvicorn.run(

View File

@@ -6,6 +6,7 @@ from loguru import logger
from leggend.config import config
import leggen.database.sqlite as sqlite_db
from leggen.utils.paths import path_manager
class DatabaseService:
@@ -280,9 +281,7 @@ class DatabaseService:
async def _check_balance_timestamp_migration_needed(self) -> bool:
"""Check if balance timestamps need migration"""
from pathlib import Path
db_path = Path.home() / ".config" / "leggen" / "leggen.db"
db_path = path_manager.get_database_path()
if not db_path.exists():
return False
@@ -310,9 +309,7 @@ class DatabaseService:
async def _migrate_balance_timestamps(self):
"""Convert all Unix timestamps to datetime strings"""
from pathlib import Path
db_path = Path.home() / ".config" / "leggen" / "leggen.db"
db_path = path_manager.get_database_path()
if not db_path.exists():
logger.warning("Database file not found, skipping migration")
return
@@ -399,9 +396,7 @@ class DatabaseService:
async def _check_null_transaction_ids_migration_needed(self) -> bool:
"""Check if null transaction IDs need migration"""
from pathlib import Path
db_path = Path.home() / ".config" / "leggen" / "leggen.db"
db_path = path_manager.get_database_path()
if not db_path.exists():
return False
@@ -429,9 +424,8 @@ class DatabaseService:
async def _migrate_null_transaction_ids(self):
"""Populate null internalTransactionId fields using transactionId from raw data"""
import uuid
from pathlib import Path
db_path = Path.home() / ".config" / "leggen" / "leggen.db"
db_path = path_manager.get_database_path()
if not db_path.exists():
logger.warning("Database file not found, skipping migration")
return
@@ -538,9 +532,7 @@ class DatabaseService:
async def _check_composite_key_migration_needed(self) -> bool:
"""Check if composite key migration is needed"""
from pathlib import Path
db_path = Path.home() / ".config" / "leggen" / "leggen.db"
db_path = path_manager.get_database_path()
if not db_path.exists():
return False
@@ -586,9 +578,7 @@ class DatabaseService:
async def _migrate_to_composite_key(self):
"""Migrate transactions table to use composite primary key (accountId, transactionId)"""
from pathlib import Path
db_path = Path.home() / ".config" / "leggen" / "leggen.db"
db_path = path_manager.get_database_path()
if not db_path.exists():
logger.warning("Database file not found, skipping migration")
return
@@ -704,10 +694,8 @@ class DatabaseService:
try:
import sqlite3
from pathlib import Path
db_path = Path.home() / ".config" / "leggen" / "leggen.db"
db_path.parent.mkdir(parents=True, exist_ok=True)
db_path = path_manager.get_database_path()
path_manager.ensure_database_dir_exists()
conn = sqlite3.connect(str(db_path))
cursor = conn.cursor()
@@ -786,10 +774,8 @@ class DatabaseService:
import sqlite3
import json
from pathlib import Path
db_path = Path.home() / ".config" / "leggen" / "leggen.db"
db_path.parent.mkdir(parents=True, exist_ok=True)
db_path = path_manager.get_database_path()
path_manager.ensure_database_dir_exists()
conn = sqlite3.connect(str(db_path))
cursor = conn.cursor()
@@ -888,10 +874,7 @@ class DatabaseService:
) -> None:
"""Persist account details to SQLite"""
try:
from pathlib import Path
db_path = Path.home() / ".config" / "leggen" / "leggen.db"
db_path.parent.mkdir(parents=True, exist_ok=True)
path_manager.ensure_database_dir_exists()
# Use the sqlite_db module function
sqlite_db.persist_account(account_data)

View File

@@ -6,6 +6,7 @@ from typing import Dict, Any, List
from loguru import logger
from leggend.config import config
from leggen.utils.paths import path_manager
def _log_rate_limits(response):
@@ -39,8 +40,8 @@ class GoCardlessService:
if self._token:
return self._token
# Use ~/.config/leggen for consistency with main config
auth_file = Path.home() / ".config" / "leggen" / "auth.json"
# Use path manager for auth file
auth_file = path_manager.get_auth_file_path()
if auth_file.exists():
try: