micropy.config

Configuration files and interfaces for them.

Classes

Config(*args[, source_format, default])

Configuration File Interface.

JSONConfigSource(path)

JSON Config File Source.

DictConfigSource([config])

class micropy.config.Config(*args, source_format=<class 'micropy.config.config_json.JSONConfigSource'>, default={})

Bases: object

Configuration File Interface.

Automatically syncs config in memory with config saved to disk.

Parameters
  • path (Path) – Path to save file at.

  • source_format (ConfigSource, optional) – Configuration File Format. Defaults to JSONConfigSource.

  • default (dict, optional) – Default configuration. Defaults to {}.

add(key, value)

Overwrite or add config value.

Parameters
  • key (str) – Key to set

  • value (Any) – Value to add or update too

Return type

Any

Returns

Updated config

extend(key, value, unique=False)

Extend a list in config at key path.

Parameters
  • key (str) – Key to path to extend.

  • value (List[Any]) – List of values to extend by.

  • unique (bool) – Only extend values if not already in values.

Return type

dict

Returns

Updated Config

get(key, default=None)

Retrieve config value.

Parameters
  • key (str) – Key (in dot-notation) of value to return.

  • default (Any, optional) – Default value to return. Defaults to None.

Returns

Value at key given

Return type

Any

parse_key(key)

Parses key.

Splits it into a path and ‘final key’ object. Each key is seperates by a: “/”

Example

>>> self.parse_key('item/subitem/value')
(('item', 'subitem'), 'value')
Parameters

key (str) – key in dot notation

Returns

Parsed key

Return type

Tuple[Sequence[str], str]

pop(key)

Delete and return value at key.

Parameters

key (str) – Key to pop.

Returns

Popped value.

Return type

Any

search(key)

Retrieve all values at key (with glob pattern).

Parameters

key – Key with pattern to search with.

Returns

Values matching key and pattern.

set(key, value)

Set config value.

Parameters
  • key (str) – Key (in dot-notation) to update.

  • value (Any) – Value to set

Returns

Updated config

Return type

Any

sync()

Sync in-memory config with disk.

Returns

updated config

Return type

dict

upsert(key, value)

Update or insert values into key list or dict.

Parameters
  • key (str) – Key to value to upsert.

  • value (Union[List[Any], dict]) – Value to upsert by.

Return type

dict

Returns

Updated config.

class micropy.config.JSONConfigSource(path)

Bases: micropy.config.config_source.ConfigSource

JSON Config File Source.

Parameters

path (Path) – Path to save config too.

property exists

Property to check if source exists.

Return type

bool

property file_path

Path to config file.

Return type

Path

prepare()

Method to prepare on enter.

process()

Load config from JSON file.

Returns

config in file

Return type

dict

save(content)

Save current config.

Parameters

content (dict) – content to write to file.

Returns

path to config file.

Return type

Path

class micropy.config.DictConfigSource(config={})

Bases: micropy.config.config_source.ConfigSource

property exists

Property to check if source exists.

Return type

bool

prepare()

Method to prepare on enter.

process()

Read and process config file.

Returns

Config file content

Return type

dict

save(content)

Method to save config.

Return type

dict