Config
This module provides base a class for configs that can be loaded from files, environment variables or command line arguments.
Creating and loading configs
Derive your config from the Config
base class. Inside of its body,
define config fields using type annotations. This is similar to how you
create dataclasses:
class AppConfig(Config):
app_version: str
deployment_strategy: str = 'ROLLING'
Config
defines __init__ that takes keyword arguments for defined
fields, as well as some constructors that load config from environment
variables, files, and so on.
Upon creation, all fields that aren’t explicitly initialized and don’t have
defaults are considered missing. Accessing them
will raise AttributeError
.
Here’s an example of loading our config from command line arguments and environment variables:
# Load config from env.
config = AppConfig.load_from_env()
# Create and setup parser.
parser = argparse.ArgumentParser()
AppConfig.setup_parser(parser)
# Parse arguments, convert them to config,
# and merge with config loaded from env.
config.update(AppConfig.load_from_args(parser.parse_args()))
- class yuio.config.Config(**kwargs)[source]
Base class for configs.
- update(other: _Self)[source]
Update fields in this config with fields from another config.
This function is similar to
dict.update()
.
- classmethod load_from_env() _Self [source]
Load config from environment variables.
Use
Config.update()
to merge several loaded configs into one.
- classmethod load_from_args(args: Optional[Namespace] = None) _Self [source]
Load config from parsed command line arguments.
This method assumes that arguments parser was configured with
Config.setup_parser()
.Use
Config.update()
to merge several loaded configs into one.If args is not given, will create a parser and parse arguments from
sys.argv
.
- classmethod load_from_config(config: Dict[str, Any], ignore_unknown_fields: bool = False) _Self [source]
Load config from parsed config file.
This method takes a dict with arbitrary values that resulted from parsing type-rich configs such as
.yaml
or.json
.For example:
with open('conf.yml') as file: config = Config.load_from_config(yaml.load(file))
Use
Config.update()
to merge several loaded configs into one.
- classmethod load_from_json(path: str, ignore_unknown_fields: bool = False)[source]
Load config from a
.json
file.
- classmethod setup_parser(parser: Optional[ArgumentParser] = None) ArgumentParser [source]
Add fields from this config as flags to an argparse parser.
If parser is not given, will create one.
Advanced field configuration
By default, Config
infers names for env variables and flags,
appropriate parsers, and other things from field’s name and type hint.
If you need to override them, theres the field()
function:
class AppConfig(Config):
app_version: str = field(
default='HEAD',
help='git tag or commit hash at which a new version is built.',
)
deployment_strategy: str = field(
default='ROLLING',
help='strategy that will be used to deploy new pods.',
parser=yuio.parse.OneOf(
yuio.parse.StrUpper(),
['ROLLING', 'READONLY', 'DOWNTIME']
)
- yuio.config.field(*, help: Optional[str] = 'None', env: Optional[str] = 'None', flag: Optional[Union[str, List[str]]] = 'None') Any [source]
- yuio.config.field(default: T = _MISSING, *, parser: Parser[T], help: Optional[str] = 'None', env: Optional[str] = 'None', flag: Optional[Union[str, List[str]]] = 'None') T
Field descriptor, used for additional configuration of fields.
This is similar to what
dataclasses.field()
does.- Parameters
default – default value for config field, used if field is missing from config.
parser – parser that will be used to parse env vars, configs and CLI arguments. By default, it’s inferred from type hint.
help – help message that will be used in CLI argument description.
env – name of environment variable that will be used for this field. By default, it’s inferred from field name. Set to
disabled()
to disable parsing from environment variable.flag – name of a CLI flag (or a list of names) that will be used for this field. By default, it’s inferred from field name. Set to
disabled()
to disable parsing from command line arguments.