Skip to content

Commit

Permalink
cleanup config
Browse files Browse the repository at this point in the history
  • Loading branch information
malmans2 committed Jul 30, 2024
1 parent abe8477 commit dd3e116
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions cacholote/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,25 @@
from . import database

_SETTINGS: Settings | None = None
_DEFAULT_CACHE_DIR = pathlib.Path(tempfile.gettempdir()) / "cacholote"
_DEFAULT_CACHE_DIR.mkdir(exist_ok=True)
_DEFAULT_CACHE_DB_URLPATH = f"sqlite:///{_DEFAULT_CACHE_DIR / 'cacholote.db'}"
_DEFAULT_CACHE_FILES_URLPATH = f"{_DEFAULT_CACHE_DIR / 'cache_files'}"
_DEFAULT_LOGGER = structlog.get_logger(
wrapper_class=structlog.make_filtering_bound_logger(logging.WARNING)
)


def _get_tmp_path() -> pathlib.Path:
tmp_path = pathlib.Path(tempfile.gettempdir()) / "cacholote"
tmp_path.mkdir(exist_ok=True)
return tmp_path


def _default_cache_db_urlpath() -> str:
return f"sqlite:///{_get_tmp_path() / 'cacholote.db'}"


def _default_cache_files_urlpaths() -> list[str]:
config = os.environ.get("CACHOLOTE_CACHE_FILES_URLPATHS_CONFIG")
if config:
if (config := os.getenv("CACHOLOTE_CACHE_FILES_URLPATHS_CONFIG")) is not None:
return pathlib.Path(config).read_text().splitlines()
return [_DEFAULT_CACHE_FILES_URLPATH]
return [str(_get_tmp_path() / "cache_files.db")]


class Context(abc.ABC):
Expand All @@ -60,7 +65,9 @@ def upload_log(self, *args: Any, **kwargs: Any) -> None: ...

class Settings(pydantic_settings.BaseSettings):
use_cache: bool = True
cache_db_urlpath: Optional[str] = _DEFAULT_CACHE_DB_URLPATH
cache_db_urlpath: Optional[str] = pydantic.Field(
default_factory=_default_cache_db_urlpath
)
create_engine_kwargs: dict[str, Any] = {}
sessionmaker: Optional[sa.orm.sessionmaker[sa.orm.Session]] = None
cache_files_urlpaths: list[str] = pydantic.Field(
Expand Down

0 comments on commit dd3e116

Please sign in to comment.