Project configuration
Utilities to read and process configuration for a project.
- class maykin_common.config.DocumentationParams(help_text: str = '', group: str | None = None, add_to_docs: bool = True, auto_display_default: bool = True)
Dataclass to define the parameters for documentation generation for environment variables loaded via the
maykin_common.config.config()helper- add_to_docs: bool
Indicates whether this environment variable should be displayed in the documentation.
- auto_display_default: bool
Indicates whether the documentation directives should display the specified default. Can be set to
Falseif you want to manually specify a default.
- group: str | None
The name of the group this environment variable belongs to. Defaults to “Required” or “Optional” depending on whether a
defaultis passed.
- help_text: str
The description of this environment variable.
- maykin_common.config.config(option: str, *, documentation: DocumentationParams | None = None) str
- maykin_common.config.config(option: str, *, default: str, split: Literal[True], cast: Callable[[str], T] | Undefined = undefined, documentation: DocumentationParams | None = None) Never
- maykin_common.config.config(option: str, *, default: Sequence[T], split: Literal[True], cast: Callable[[str], T] | Undefined = undefined, documentation: DocumentationParams | None = None) list[T]
- maykin_common.config.config(option: str, *, default: Undefined = undefined, split: Literal[True], cast: Undefined = undefined, documentation: DocumentationParams | None = None) list[str]
- maykin_common.config.config(option: str, *, default: Undefined = undefined, split: Literal[True], cast: Callable[[str], T], documentation: DocumentationParams | None = None) list[T]
- maykin_common.config.config(option: str, *, default: None, documentation: DocumentationParams | None = None) str | None
- maykin_common.config.config(option: str, *, default: T | Undefined = undefined, documentation: DocumentationParams | None = None) T
- maykin_common.config.config(option: str, *, default: None, cast: Callable, documentation: DocumentationParams | None = None) Never
- maykin_common.config.config(option: str, *, default: str | Undefined = undefined, cast: Callable[[str], T], documentation: DocumentationParams | None = None) T
Pull a config parameter from the environment.
Read the config variable
option. If it’s optional, use thedefaultvalue.If not
castparameter is provided, then thecastis derived from thedefaulttype when a default is provided. However, when you provide acastparameter explicitly, you must provide anydefaultas a string as it will be passed to the providedcastcallback.Note that
default=Nonedoes not mean there’s no default; omitting thedefaultkwarg entirely means there’s no default.Pass
split=Trueto split the comma-separated input into a list. If a default is provided, it must be a list.Examples:
>>> SECRET_KEY: str = config("SECRET_KEY") >>> DB_NAME: str = config("DB_NAME", default="my-awesome-project") >>> DB_PORT: int = config("DB_PORT", default=5432") >>> SESSION_COOKIE_DOMAIN: str | None = config( ... "SESSION_COOKIE_DOMAIN", default=None ... ) >>> ALLOWED_HOSTS: list[str] = config("ALLOWED_HOSTS", split=True, default=[]) >>> CUSTOM = config( ... "CUSTOM", ... default="123", ... cast=lambda v: int(v) if v is not None else None, ... ) # typed as int | None
The
documentationparameter is available to generate documentation for environment variables with Sphinx directives provided by this library:- Parameters:
documentation – See
maykin_common.config.DocumentationParams