Updates the values in the config from a Python file. This function behaves as if the file was imported as module with the :meth:`from_object` function. :param filename: the filename of the config. This can either be an absolute filename or a filena
(
self, filename: str | os.PathLike[str], silent: bool = False
)
| 185 | return True |
| 186 | |
| 187 | def from_pyfile( |
| 188 | self, filename: str | os.PathLike[str], silent: bool = False |
| 189 | ) -> bool: |
| 190 | """Updates the values in the config from a Python file. This function |
| 191 | behaves as if the file was imported as module with the |
| 192 | :meth:`from_object` function. |
| 193 | |
| 194 | :param filename: the filename of the config. This can either be an |
| 195 | absolute filename or a filename relative to the |
| 196 | root path. |
| 197 | :param silent: set to ``True`` if you want silent failure for missing |
| 198 | files. |
| 199 | :return: ``True`` if the file was loaded successfully. |
| 200 | |
| 201 | .. versionadded:: 0.7 |
| 202 | `silent` parameter. |
| 203 | """ |
| 204 | filename = os.path.join(self.root_path, filename) |
| 205 | d = types.ModuleType("config") |
| 206 | d.__file__ = filename |
| 207 | try: |
| 208 | with open(filename, mode="rb") as config_file: |
| 209 | exec(compile(config_file.read(), filename, "exec"), d.__dict__) |
| 210 | except OSError as e: |
| 211 | if silent and e.errno in (errno.ENOENT, errno.EISDIR, errno.ENOTDIR): |
| 212 | return False |
| 213 | e.strerror = f"Unable to load configuration file ({e.strerror})" |
| 214 | raise |
| 215 | self.from_object(d) |
| 216 | return True |
| 217 | |
| 218 | def from_object(self, obj: object | str) -> None: |
| 219 | """Updates the values from the given object. An object can be of one |