configurations.py•1.96 kB
import sys
from pathlib import Path
from pydantic_settings import BaseSettings, SettingsConfigDict
from .make_logger import make_logger
logger = make_logger(__name__)
class Settings(BaseSettings):
APP_NAME: str = "mcp-server-data-wrangler"
APP_VERSION: str = "0.0.1"
HOST: str = "0.0.0.0"
PORT: int = 8000
model_config = SettingsConfigDict(extra="allow")
@property
def STORAGE_PATH(self) -> Path:
"""Get the resolved storage path and ensure it exists.
Returns:
Path: The absolute storage path.
"""
path = self._get_storage_path_from_args() or Path.home() / self.APP_NAME
path = path.resolve()
path.mkdir(parents=True, exist_ok=True)
return path
def _get_storage_path_from_args(self) -> Path | None:
"""Extract storage path from command line arguments.
Returns:
Path | None: The storage path if specified in arguments, None otherwise.
"""
args = sys.argv[1:]
# If not enough arguments
if len(args) < 2:
return None
# Look for the --storage-path option
try:
storage_path_index = args.index("--storage-path")
except ValueError:
return None
# Early return if --storage-path is the last argument
if storage_path_index + 1 >= len(args):
return None
# Try to resolve the path
try:
path = Path(args[storage_path_index + 1])
return path.resolve()
except (TypeError, ValueError) as e:
# TypeError: If the path argument is not string-like
# ValueError: If the path string is malformed
logger.warning(f"Invalid storage path format: {e}")
except OSError as e:
# OSError: If the path contains invalid characters or is too long
logger.warning(f"Invalid storage path: {e}")
return None