| 47 | |
| 48 | |
| 49 | class Config: |
| 50 | def __init__( |
| 51 | self, |
| 52 | env_file: str | Path | None = None, |
| 53 | environ: Mapping[str, str] = environ, |
| 54 | env_prefix: str = "", |
| 55 | encoding: str = "utf-8", |
| 56 | ) -> None: |
| 57 | self.environ = environ |
| 58 | self.env_prefix = env_prefix |
| 59 | self.file_values: dict[str, str] = {} |
| 60 | if env_file is not None: |
| 61 | if not os.path.isfile(env_file): |
| 62 | warnings.warn(f"Config file '{env_file}' not found.") |
| 63 | else: |
| 64 | self.file_values = self._read_file(env_file, encoding) |
| 65 | |
| 66 | @overload |
| 67 | def __call__(self, key: str, *, default: None) -> str | None: ... |
| 68 | |
| 69 | @overload |
| 70 | def __call__(self, key: str, cast: type[T], default: T = ...) -> T: ... |
| 71 | |
| 72 | @overload |
| 73 | def __call__(self, key: str, cast: type[str] = ..., default: str = ...) -> str: ... |
| 74 | |
| 75 | @overload |
| 76 | def __call__( |
| 77 | self, |
| 78 | key: str, |
| 79 | cast: Callable[[Any], T] = ..., |
| 80 | default: Any = ..., |
| 81 | ) -> T: ... |
| 82 | |
| 83 | @overload |
| 84 | def __call__(self, key: str, cast: type[str] = ..., default: T = ...) -> T | str: ... |
| 85 | |
| 86 | def __call__( |
| 87 | self, |
| 88 | key: str, |
| 89 | cast: Callable[[Any], Any] | None = None, |
| 90 | default: Any = undefined, |
| 91 | ) -> Any: |
| 92 | return self.get(key, cast, default) |
| 93 | |
| 94 | def get( |
| 95 | self, |
| 96 | key: str, |
| 97 | cast: Callable[[Any], Any] | None = None, |
| 98 | default: Any = undefined, |
| 99 | ) -> Any: |
| 100 | key = self.env_prefix + key |
| 101 | if key in self.environ: |
| 102 | value = self.environ[key] |
| 103 | return self._perform_cast(key, value, cast) |
| 104 | if key in self.file_values: |
| 105 | value = self.file_values[key] |
| 106 | return self._perform_cast(key, value, cast) |
no outgoing calls