labthings_fastapi.server.config_model ===================================== .. py:module:: labthings_fastapi.server.config_model .. autoapi-nested-parse:: 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 ---------- .. autoapisummary:: labthings_fastapi.server.config_model.ThingImportString labthings_fastapi.server.config_model.RESERVED_THING_NAMES labthings_fastapi.server.config_model.ThingName labthings_fastapi.server.config_model.ThingsConfig Exceptions ---------- .. autoapisummary:: labthings_fastapi.server.config_model.ThingImportFailure Classes ------- .. autoapisummary:: labthings_fastapi.server.config_model.ThingConfig labthings_fastapi.server.config_model.ThingServerConfig Functions --------- .. autoapisummary:: labthings_fastapi.server.config_model.contain_import_errors labthings_fastapi.server.config_model.check_reserved_thing_names labthings_fastapi.server.config_model.normalise_things_config Module Contents --------------- .. py:exception:: ThingImportFailure Bases: :py:obj:`BaseException` Failed to import Thing. Raise with import traceback. Initialize self. See help(type(self)) for accurate signature. .. py:function:: 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. :param value: The value being validated. :param handler: The validator handler. :return: The validated value. :raises ThingImportFailure: if an import error occurs, with the stack trace from retrying the import. :raises Exception: In the unlikely event that the import error cannot be reproduced .. py:data:: ThingImportString .. py:class:: ThingConfig(/, **data: Any) Bases: :py:obj:`pydantic.BaseModel` The information needed to add a `~lt.Thing` to a `~lt.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. `self` is explicitly positional-only to allow `self` as a field name. .. py:attribute:: cls :type: ThingImportString :value: None .. py:attribute:: args :type: collections.abc.Sequence[Any] :value: None .. py:attribute:: kwargs :type: collections.abc.Mapping[str, Any] :value: None .. py:attribute:: thing_slots :type: collections.abc.Mapping[str, str | collections.abc.Iterable[str] | None] :value: None .. py:data:: RESERVED_THING_NAMES :value: ('things', 'cls') .. py:function:: check_reserved_thing_names(name: str) -> str Validate a Thing name by checking it's not in a list of banned names. :param name: the name to check. :return: the name, if valid. :raises ValueError: if the name is not valid. .. py:data:: ThingName .. py:type:: ThingsConfig :canonical: Mapping[ThingName, ThingConfig | ThingImportString] .. py:class:: ThingServerConfig(/, **data: Any) Bases: :py:obj:`pydantic.BaseModel` The configuration parameters for a `~lt.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. `self` is explicitly positional-only to allow `self` as a field name. .. py:attribute:: things :type: ThingsConfig :value: None .. py:method:: check_things(things: ThingsConfig) -> ThingsConfig :classmethod: 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.ImportString` as 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_config` to check each value is either a valid `ThingConfig` or a type or a mapping. If it's a mapping, we will attempt to make a `ThingConfig` from it. If it's a `type` we will create a `ThingConfig` using that type as the class. We don't check for `~lt.Thing` subclasses in this module to avoid a dependency loop. :param things: The validated value of the field. :return: A copy of the input, with all values converted to `ThingConfig` instances. .. py:property:: thing_configs :type: collections.abc.Mapping[ThingName, ThingConfig] A copy of the ``things`` field where every value is a ``ThingConfig``\ . The field validator on ``things`` already 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 `ThingConfig` objects, and is typed strictly. .. py:attribute:: settings_folder :type: str | None :value: None .. py:attribute:: api_prefix :type: str :value: None .. py:attribute:: enable_global_lock :type: bool :value: None .. py:attribute:: application_config :type: dict[str, Any] | None :value: None .. py:function:: normalise_things_config(things: ThingsConfig) -> collections.abc.Mapping[ThingName, ThingConfig] Ensure every Thing is defined by a `ThingConfig` object. Things may be specified either using a `ThingConfig` object, or just a bare `~lt.Thing` subclass, if the other parameters are not needed. To simplify code that uses the configuration, this function wraps bare classes in a `ThingConfig` so the values are uniformly typed. :param things: A mapping of names to Things, either classes or `ThingConfig` objects. :return: A mapping of names to `ThingConfig` objects. :raises ValueError: if a Python object is passed that's neither a `type` nor a `dict`\ .