API¶
-
class
twelvefactor.
Config
(environ=None)¶ Bases:
object
Config environment parser.
This class allows chosen configuration values to be extracted from the processes environment variables and converted into the relevant types.
parser = Config() config = parser({ 'DEBUG': { 'type': bool, 'default': False, }, 'SECRET_KEY': str, })
The above will populate the
config
variable with two values,DEBUG
will be populated with abool
from the environment variable of the same name, throwing an exception on invalid values and defaulting toFalse
when none is provided, andSECRET_KEY
will be astr
and throw aConfigError
when no value is found in the environment.An optional
environ
param can be passed in order to override the environment.- Parameters
environ (
Optional
[Mapping
[str
,str
]]) – environment dictionary, defaults toos.environ
-
__call__
(schema)¶ Parse the environment according to a schema.
-
get
(key, default=<object object>, type_=<class 'str'>, subtype=<class 'str'>, mapper=None)¶ Parse a value from an environment variable.
>>> os.environ['FOO'] <<< '12345' >>> >>> os.environ['BAR'] <<< '1,2,3,4' >>> >>> 'BAZ' in os.environ <<< False >>> >>> parser = Config() >>> parser.get('FOO', type_=int) <<< 12345 >>> >>> parser.get('BAR', type_=list, subtype=int) <<< [1, 2, 3, 4] >>> >>> parser.get('BAZ', default='abc123') <<< 'abc123' >>> >>> parser.get('FOO', type_=int, mapper=lambda x: x*10) <<< 123450
- Parameters
- Return type
- Returns
the parsed config value
-
parse
(value, type_=<class 'str'>, subtype=<class 'str'>)¶ Parse value from string.
Convert
value
to>>> parser = Config() >>> parser.parse('12345', type_=int) <<< 12345 >>> >>> parser.parse('1,2,3,4', type_=list, subtype=int) <<< [1, 2, 3, 4]
-
twelvefactor.
SchemaItem
¶
A type annotation for the definition of a single item in a the schema.
-
twelvefactor.
Schema
¶
A type annotation for the Schema.
-
twelvefactor.
config
¶
An instance of Config
.