wijjit.config.Config

class wijjit.config.Config(defaults=None)[source]

Configuration dictionary with Flask-like interface.

Works like a regular dict but provides additional methods for loading configuration from different sources.

Parameters:

defaults (dict, optional) – Initial default values

Examples

>>> config = Config()
>>> config['DEBUG'] = True
>>> config.update(ENABLE_MOUSE=False)
>>> config.from_object(MyConfigClass)
__init__(defaults=None)[source]

Initialize configuration.

Parameters:

defaults (dict, optional) – Initial default values

Return type:

None

Methods

__init__([defaults])

Initialize configuration.

clear()

Remove all items from the dict.

copy()

Return a shallow copy of the dict.

from_envvar(variable_name[, silent])

Load config from file path in environment variable.

from_mapping([mapping])

Load config from a dict or kwargs.

from_object(obj)

Load config from an object.

from_prefixed_env([prefix])

Load config from environment variables with prefix.

from_pyfile(filename[, silent])

Load config from a Python file.

fromkeys(iterable[, value])

Create a new dictionary with keys from iterable and values set to value.

get(key[, default])

Return the value for key if key is in the dictionary, else default.

get_namespace(namespace[, lowercase, ...])

Get all config values for a namespace.

items()

Return a set-like object providing a view on the dict's items.

keys()

Return a set-like object providing a view on the dict's keys.

pop(k[,d])

If the key is not found, return the default if given; otherwise, raise a KeyError.

popitem()

Remove and return a (key, value) pair as a 2-tuple.

setdefault(key[, default])

Insert key with a value of default if key is not in the dictionary.

update([E, ]**F)

If E is present and has a .keys() method, then does: for k in E.keys(): D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values()

Return an object providing a view on the dict's values.

from_object(obj)[source]

Load config from an object.

Loads all uppercase attributes from the given object.

Parameters:

obj (Any) – Object with uppercase attributes (module, class, or instance)

Return type:

None

Examples

>>> import config
>>> app.config.from_object(config)
>>> class Config:
...     DEBUG = True
...     ENABLE_MOUSE = False
>>> app.config.from_object(Config)

A dotted string path is resolved to the module or attribute it names:

>>> app.config.from_object("myproject.settings")
>>> app.config.from_object("myproject.settings.ProdConfig")
from_pyfile(filename, silent=False)[source]

Load config from a Python file.

Parameters:
  • filename (str) – Path to Python file

  • silent (bool) – If True, silently ignore missing files

Returns:

True if file was loaded, False otherwise

Return type:

bool

Examples

>>> app.config.from_pyfile('config.py')
>>> app.config.from_pyfile('/etc/myapp/config.py')
from_envvar(variable_name, silent=False)[source]

Load config from file path in environment variable.

Parameters:
  • variable_name (str) – Name of environment variable containing config file path

  • silent (bool) – If True, silently ignore missing variable/file

Returns:

True if file was loaded, False otherwise

Return type:

bool

Examples

>>> # export WIJJIT_SETTINGS=/path/to/config.py
>>> app.config.from_envvar('WIJJIT_SETTINGS')
from_mapping(mapping=None, **kwargs)[source]

Load config from a dict or kwargs.

Parameters:
  • mapping (dict, optional) – Dictionary of config values

  • **kwargs – Additional config values

Return type:

None

Examples

>>> app.config.from_mapping({'DEBUG': True, 'ENABLE_MOUSE': False})
>>> app.config.from_mapping(DEBUG=True, ENABLE_MOUSE=False)
from_prefixed_env(prefix='WIJJIT_')[source]

Load config from environment variables with prefix.

Environment variable names are converted to config keys by removing the prefix. Values are automatically parsed as bool/int/float when possible.

Parameters:

prefix (str) – Prefix for environment variables (default: WIJJIT_)

Return type:

None

Examples

>>> # export WIJJIT_DEBUG=1
>>> # export WIJJIT_ENABLE_MOUSE=0
>>> # export WIJJIT_LOG_LEVEL=DEBUG
>>> app.config.from_prefixed_env('WIJJIT_')
get_namespace(namespace, lowercase=True, trim_namespace=True)[source]

Get all config values for a namespace.

Parameters:
  • namespace (str) – Namespace prefix (e.g., NOTIFICATION_)

  • lowercase (bool) – Convert keys to lowercase

  • trim_namespace (bool) – Remove namespace prefix from keys

Returns:

Dictionary of matching config values

Return type:

dict

Examples

>>> # Get all notification settings
>>> notif_config = app.config.get_namespace('NOTIFICATION_')
>>> # Returns: {'duration': 3.0, 'position': 'top_right', ...}