@property
def configuration(self) -> _ConfigurationT:
"""
The object's configuration.
"""
if not hasattr(self, "_configuration"):
raise RuntimeError(
f"{self} has no configuration. {type(self)}.__init__() must ensure it is set."
)
return self._configuration
[docs]
def assert_configuration_file(
configuration: _ConfigurationT,
) -> AssertionChain[Path, _ConfigurationT]:
"""
Assert that configuration can be loaded from a file.
"""
async def _assert(configuration_file_path: Path) -> _ConfigurationT:
with (
AssertionFailedGroup().assert_valid() as errors,
# Change the working directory to allow relative paths to be resolved
# against the configuration file's directory path.
chdir(configuration_file_path.parent),
):
try:
with open(configuration_file_path) as f:
read_configuration = f.read()
except FileNotFoundError:
raise FileNotFound.new(configuration_file_path) from None
with errors.catch(plain(f"in {str(configuration_file_path.resolve())}")):
serde_format_type = await FORMAT_REPOSITORY.format_for(
configuration_file_path.suffix
)
serde_format = await FORMAT_REPOSITORY.new(serde_format_type)
configuration.load(serde_format.load(read_configuration))
return configuration
return assert_file_path().chain(lambda value: wait_to_thread(_assert(value)))