API reference
cfig.config
This module defines the Configuration class.
- class cfig.config.Configuration(*, sources: list[Source] | None = None)
A collection of proxies with methods to easily define more.
- DEFAULT_SOURCES: list[Source] = [<cfig.sources.env.EnvironmentSource object>, <cfig.sources.envfile.EnvironmentFileSource object>]
The sources used in
__init__()if no other source is specified.
- class ProxyDict(dict=None, /, **kwargs)
An extended
dictwith methods to perform some actions on the contained proxies.- resolve() dict[str, Any]
Resolve all values of the proxies inside this dictionary.
- Raises:
.errors.BatchResolutionFailure – If it was not possible to resolve at least one value.
- Returns:
A
dictcontaining all the resolved, unproxied, values.
- proxies: ProxyDict
Dictionary mapping configuration keys belonging to this
Configurationto the proxy caching their values.Typed with
typing.Anyso that proxies can be typed as the object they cache.
- docs: dict[str, str]
Dictionary mapping configuration keys belonging to this
Configurationto a description of what they should contain.
- optional(key: str | None = None, doc: str | None = None) Callable[[Callable[[str | None], TYPE]], TYPE]
Mark a function as a resolver for a required configuration value.
It is a decorator factory, and therefore should be used like so:
@config.optional() def MY_KEY(val: str) -> str: return val
Key can be overridden manually with the
keyparameter.Docstring can be overridden manually with the
docparameter.
- required(key: str | None = None, doc: str | None = None) Callable[[Callable[[str], TYPE]], TYPE]
Mark a function as a resolver for a required configuration value.
It is a decorator factory, and therefore should be used like so:
@config.required() def MY_KEY(val: str) -> str: return val
Key can be overridden manually with the
keyparameter.Docstring can be overridden manually with the
docparameter.
- register(key, proxy, doc)
Register a new proxy in this Configuration.
- cli()
Run the command-line interface.
cfig.errors
This module contains all possible exceptions occurring related to cfig.
- exception cfig.errors.DeveloperError
Bases:
CfigErrorA developer-side error: the user has no way to solve it.
- exception cfig.errors.DefinitionError
Bases:
DeveloperErrorAn error is present in the definition of a
cfig.Configuration.
- exception cfig.errors.UnknownResolverNameError
Bases:
DefinitionErrorIt was not possible to get the name of the resolver.
Perhaps a call to
functools.wraps()is missing?
- exception cfig.errors.ProxyRegistrationError
Bases:
DefinitionErrorAn error occurred during the proxy registration step.
- exception cfig.errors.DuplicateProxyNameError
Bases:
ProxyRegistrationErrorAnother proxy with the same name is already registered.
- exception cfig.errors.UserError
Bases:
CfigErrorA user-side error: the developer of the application has no way to fix it.
- exception cfig.errors.ConfigurationError
Bases:
UserErrorAn error is present in the configuration specified by the user.
- exception cfig.errors.MissingValueError
Bases:
ConfigurationErrorA required configuration key has no value.
- exception cfig.errors.InvalidValueError
Bases:
ConfigurationErrorA configuration key has an invalid value.
This error should be raised by the developer in resolvers if the developer knows that a invalid value has been passed, for example:
@config.required() def INTEGER(val): try: return int(val) except ValueError: raise InvalidValueError("Not an int.")
It is not raised automatically, as certain errors might be caused by a mistake in the programming of the resolver.
- exception cfig.errors.BatchResolutionFailure(errors: dict[str, Exception])
Bases:
BaseExceptionA cumulative error which sums the errors occurred while resolving proxied configuration values.
It inherits from
BaseExceptionto be distinguishable from regular :class:`Exception`s occouring inside the resolvers.It uses some formatting tricks to display the missing keys in the configuration error message:
$ python -m cfig.sample.usage Traceback (most recent call last): ... File "./cfig/sample/usage.py", line 7, in <module> config.proxies.resolve() File "./cfig/config.py", line 59, in resolve raise errors.BatchResolutionFailure(errors=errors_dict) cfig.errors.BatchResolutionFailure: 4 errors occurred during the resolution of the config: * EXAMPLE_NUMBER → InvalidValueError: Not an int. * TELEGRAM_BOT_TOKEN → MissingValueError: TELEGRAM_BOT_TOKEN * DISCORD_CLIENT_SECRET → MissingValueError: DISCORD_CLIENT_SECRET * DATABASE_URI → MissingValueError: DATABASE_URI
cfig.sources.base
This module defines the Source abstract class.
- class cfig.sources.base.Source
A source of values to be tapped by configurations.
Abstract class. Cannot be instantiated. Should be inherited from other source classes.
Other packages can add more sources directly to the
cfig.sourcesnamespace package.
cfig.sources.env
This module defines the EnvironmentSource Source.
- class cfig.sources.env.EnvironmentSource(*, prefix: str = '', suffix: str = '', environment=None)
Bases:
SourceA source which gets values from environment variables.
- prefix: str
The prefix to be prepended to all environment variable names.
For example,
PROD_for production environment variables.
- suffix: str
The suffix to be appended to all environment variable names.
For example,
_VALfor raw values.
- environment
The environment to retrieve variable values from.
Defaults to
os.environ.
cfig.sources.envfile
This module defines the EnvironmentFileSource Source.
- class cfig.sources.envfile.EnvironmentFileSource(*, prefix: str = '', suffix: str = '_FILE', environment=None)
Bases:
EnvironmentSourceA source which gets values from files at paths specified in environment variables.
Useful for example with Docker Secrets.