toolrack.config¶
Hold and parse key/value configurations.
The Config
describes valid configuration keys along with their types,
and performs parsing and type conversion from a dict of config options. It
also checks required keys and applies default values if an option is not
provided.
As an example:
config = Config(
ConfigKey('option1', 'int', default=4),
ConfigKey('option2', 'bool', required=True))
config.parse({'option2': 'true'})
returns {'option1': 4, 'option2': True}
.
-
class
toolrack.config.
Config
(*keys)¶ Parse a configuration dictionary.
A configuration has a set of keys of specific types.
-
extend
(*keys)¶ Return a new Config with additional keys.
-
keys
()¶ Return ConfigKeys sorted by name alphabetically.
-
parse
(config: Optional[Dict[str, Any]]) → Dict[str, Any]¶ Parse the provided configuration dict.
Returns a dict with configuration keys and values converted to the proper type. The dict includes only keys declared in the Config, with default values if not present in the config dict.
-
-
class
toolrack.config.
ConfigKey
(name: str, _type: str, description: str = '', required: bool = False, default: Optional[Any] = None, validator: Optional[Callable[[Any], None]] = None)¶ A key in the Configuration.
-
parse
(value)¶ Convert and validate a value.
-
validate
(value)¶ Validate a value based for the key.
Can be overridden by subclasses. It should raise a ValueError if the value is invalid.
-
-
class
toolrack.config.
ConfigKeyTypes
¶ Collection of type converters for ConfigKeys.
-
get_converter
(_type: str) → Callable¶ Return the converter method for the specified type.
-
-
exception
toolrack.config.
InvalidConfigValue
(key: str)¶
-
exception
toolrack.config.
MissingConfigKey
(key: str)¶