trolldb.config.config module

The module which handles parsing and validating the config (YAML) file.

The validation is performed using Pydantic.

Note

Some functions/methods in this module are decorated with the Pydantic @validate_call which checks the arguments during the function calls.

trolldb.config.config.Timeout

A type hint for the timeout in seconds (non-negative float).

trolldb.config.config.id_must_be_valid(id_like_string: str) ObjectId

Checks that the given string can be converted to a valid MongoDB ObjectId.

Parameters:

id_like_string – The string to be converted to an ObjectId.

Returns:

The ObjectId object if successful.

Raises:
  • ValidationError – If the given argument is not of type str.

  • ValueError – If the given string cannot be converted to a valid ObjectId. This will ultimately turn into a pydantic validation error.

Note

The reason that we change the type of the raised error is the following. As per the requirements of Pydantic, one can either raise a ValueError or AssertionError in a custom validator. Here we have defined a custom validator for a MongoDB object ID. When it fails it raises InvalidId which is not a valid exception to signify validation failure in Pydantic, hence the need to catch the error and raise a different one.

Reference:

https://docs.pydantic.dev/latest/concepts/validators/#handling-errors-in-validators

trolldb.config.config.MongoObjectId

The type hint validator for object IDs.

alias of Annotated[str, pydantic.functional_validators.AfterValidator]

class trolldb.config.config.APIServerConfig(url: pydantic.AnyUrl)[source]

Bases: NamedTuple

A named tuple to hold all the configurations of the API server (excluding the database).

Note

The attributes herein are a subset of the keyword arguments accepted by FastAPI class and are directly passed to the FastAPI class. Consult trolldb.api.api.run_server() on how these configurations are treated.

url: pydantic.AnyUrl

The URL of the API server including the port, e.g. mongodb://localhost:8000.

_asdict()

Return a new dict which maps field names to their values.

_field_defaults = {}
_fields = ('url',)
classmethod _make(iterable)

Make a new APIServerConfig object from a sequence or iterable

_replace(**kwds)

Return a new APIServerConfig object replacing specified fields with new values

class trolldb.config.config.DatabaseConfig(main_database_name: str, main_collection_name: str, url: pydantic.MongoDsn, timeout: pydantic.PositiveFloat)[source]

Bases: NamedTuple

A named tuple to hold all the configurations of the Database which will be used by the MongoDB instance.

main_database_name: str

The name of the main database which includes the main_collection, e.g. "satellite_database".

main_collection_name: str

The name of the main collection which resides inside the main_database and includes the actual data for the files, e.g. "files"

url: pydantic.MongoDsn

The URL of the MongoDB server including the port part, e.g. "mongodb://localhost:27017"

timeout: pydantic.PositiveFloat

The timeout in seconds (non-negative float), after which an exception is raised if a connection with the MongoDB instance is not established successfully, e.g. 1.5.

_asdict()

Return a new dict which maps field names to their values.

_field_defaults = {}
_fields = ('main_database_name', 'main_collection_name', 'url', 'timeout')
classmethod _make(iterable)

Make a new DatabaseConfig object from a sequence or iterable

_replace(**kwds)

Return a new DatabaseConfig object replacing specified fields with new values

trolldb.config.config.SubscriberConfig

A dictionary to hold all the configurations of the subscriber.

TODO: This has to be moved to the posttroll package.

alias of dict[Any, Any]

class trolldb.config.config.AppConfig(*args: Any, **kwargs: Any)[source]

Bases: BaseModel

A model to hold all the configurations of the application, i.e. the API server, the database, and the subscriber.

This will be used by Pydantic to validate the parsed YAML file.

api_server: APIServerConfig
database: DatabaseConfig
subscriber: dict[Any, Any]
prepend_uris: bool = False
trolldb.config.config.parse_config(file) AppConfig

Parses and validates the configurations from a YAML file (descriptor).

Parameters:

file – A path-like object or an integer file descriptor. This will be directly passed to the open() function. For example, it can be the filename (absolute or relative) of a valid YAML file which holds the configurations.

Returns:

An instance of AppConfig.