Source code for stdlibx.config.loaders.file

 1from __future__ import annotations
 2
 3from pathlib import Path
 4from typing import TYPE_CHECKING, Any, Callable, Mapping
 5
 6if TYPE_CHECKING:
 7    from os import PathLike
 8
 9    from stdlibx.config._types import SupportsRead
10
11
[docs] 12class FileLoader: 13 def __init__( 14 self, 15 path: str | PathLike[str], 16 parser: Callable[[SupportsRead[bytes]], Mapping[str, Any]], 17 *, 18 missing_ok: bool = False, 19 ) -> None: 20 self.__path = Path(path) 21 self.__missing_ok = missing_ok 22 self.__parser = parser 23
[docs] 24 def load(self) -> Mapping[str, Any]: 25 if self.__missing_ok is True and self.__path.exists() is False: 26 return {} 27 28 with open(self.__path, "rb") as fp: 29 return self.__parser(fp)