labthings_fastapi.server.config_model
Pydantic models to enable server configuration to be loaded from file.
The models in this module allow ThingConfig dataclasses to be constructed
from dictionaries or JSON files. They also describe the full server configuration
with ServerConfigModel. These models are used by the
labthings_fastapi.server.cli module to start servers based on configuration
files or strings.
Attributes
Exceptions
Failed to import Thing. Raise with import traceback. |
Classes
The information needed to add a |
|
The configuration parameters for a |
Functions
|
Prevent errors during import from causing odd validation errors. |
|
Validate a Thing name by checking it's not in a list of banned names. |
Ensure every Thing is defined by a |
Module Contents
- exception labthings_fastapi.server.config_model.ThingImportFailure
Bases:
BaseExceptionFailed to import Thing. Raise with import traceback.
Initialize self. See help(type(self)) for accurate signature.
- labthings_fastapi.server.config_model.contain_import_errors(value: Any, handler: pydantic.ValidatorFunctionWrapHandler) Any
Prevent errors during import from causing odd validation errors.
This is used to wrap the pydantic ImportString validator, and ensures that any module that won’t import shows up with a single clear error.
- Parameters:
value – The value being validated.
handler – The validator handler.
- Returns:
The validated value.
- Raises:
ThingImportFailure – if an import error occurs, with the stack trace from retrying the import.
Exception – In the unlikely event that the import error cannot be reproduced
- labthings_fastapi.server.config_model.ThingImportString
- class labthings_fastapi.server.config_model.ThingConfig(/, **data: Any)
Bases:
pydantic.BaseModelThe information needed to add a
Thingto aThingServer.Create a new model by parsing and validating input data from keyword arguments.
Raises [
ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.selfis explicitly positional-only to allowselfas a field name.- cls: ThingImportString = None
- args: collections.abc.Sequence[Any] = None
- kwargs: collections.abc.Mapping[str, Any] = None
- thing_slots: collections.abc.Mapping[str, str | collections.abc.Iterable[str] | None] = None
- labthings_fastapi.server.config_model.RESERVED_THING_NAMES = ('things', 'cls')
- labthings_fastapi.server.config_model.check_reserved_thing_names(name: str) str
Validate a Thing name by checking it’s not in a list of banned names.
- Parameters:
name – the name to check.
- Returns:
the name, if valid.
- Raises:
ValueError – if the name is not valid.
- labthings_fastapi.server.config_model.ThingName
- type labthings_fastapi.server.config_model.ThingsConfig = Mapping[ThingName, ThingConfig | ThingImportString]
- class labthings_fastapi.server.config_model.ThingServerConfig(/, **data: Any)
Bases:
pydantic.BaseModelThe configuration parameters for a
ThingServer.Create a new model by parsing and validating input data from keyword arguments.
Raises [
ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.selfis explicitly positional-only to allowselfas a field name.- things: ThingsConfig = None
- classmethod check_things(things: ThingsConfig) ThingsConfig
Check that the thing configurations can be normalised.
It’s possible to specify the things as a mapping from names to classes. We use
pydantic.ImportStringas the type of the classes: this takes a string, and imports the corresponding Python object. When loading config from JSON, this does the right thing - but when loading from Python objects it will accept any Python object.This validator runs
normalise_thing_configto check each value is either a validThingConfigor a type or a mapping. If it’s a mapping, we will attempt to make aThingConfigfrom it. If it’s atypewe will create aThingConfigusing that type as the class. We don’t check forThingsubclasses in this module to avoid a dependency loop.- Parameters:
things – The validated value of the field.
- Returns:
A copy of the input, with all values converted to
ThingConfiginstances.
- property thing_configs: collections.abc.Mapping[ThingName, ThingConfig]
A copy of the
thingsfield where every value is aThingConfig.The field validator on
thingsalready ensures it returns a mapping, but it’s not typed strictly, to allow Things to be specified with just a class.This property returns the list of
ThingConfigobjects, and is typed strictly.
- labthings_fastapi.server.config_model.normalise_things_config(things: ThingsConfig) collections.abc.Mapping[ThingName, ThingConfig]
Ensure every Thing is defined by a
ThingConfigobject.Things may be specified either using a
ThingConfigobject, or just a bareThingsubclass, if the other parameters are not needed. To simplify code that uses the configuration, this function wraps bare classes in aThingConfigso the values are uniformly typed.- Parameters:
things – A mapping of names to Things, either classes or
ThingConfigobjects.- Returns:
A mapping of names to
ThingConfigobjects.- Raises:
ValueError – if a Python object is passed that’s neither a
typenor adict.