This commit is contained in:
51
src/config.py
Normal file
51
src/config.py
Normal file
@@ -0,0 +1,51 @@
|
||||
"""Configuration management for Code Pattern Search CLI."""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Config:
|
||||
"""Configuration settings for the CLI."""
|
||||
|
||||
cache_dir: Optional[Path] = None
|
||||
cache_ttl: int = 3600
|
||||
token: Optional[str] = None
|
||||
verbose: bool = False
|
||||
default_repos: int = 10
|
||||
max_repos: int = 100
|
||||
request_timeout: int = 30
|
||||
rate_limit_retries: int = 3
|
||||
retry_backoff_factor: float = 0.5
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
"""Post-initialization processing."""
|
||||
if self.cache_dir is None:
|
||||
self.cache_dir = self._default_cache_dir()
|
||||
|
||||
def _default_cache_dir(self) -> Path:
|
||||
"""Get default cache directory."""
|
||||
return Path.home() / ".cache" / "code-pattern-search"
|
||||
|
||||
@classmethod
|
||||
def from_env(cls) -> "Config":
|
||||
"""Create configuration from environment variables."""
|
||||
cache_dir = os.getenv("CPS_CACHE_DIR")
|
||||
cache_ttl = os.getenv("CPS_CACHE_TTL")
|
||||
|
||||
return cls(
|
||||
cache_dir=Path(cache_dir) if cache_dir else None,
|
||||
cache_ttl=int(cache_ttl) if cache_ttl else 3600,
|
||||
token=os.getenv("GITHUB_TOKEN"),
|
||||
)
|
||||
|
||||
def get_cache_dir(self) -> Path:
|
||||
"""Get the cache directory, creating it if necessary."""
|
||||
if self.cache_dir:
|
||||
self.cache_dir.mkdir(parents=True, exist_ok=True)
|
||||
return self.cache_dir
|
||||
default_dir = self._default_cache_dir()
|
||||
default_dir.mkdir(parents=True, exist_ok=True)
|
||||
return default_dir
|
||||
Reference in New Issue
Block a user