config
JSON-compliant YAML-based configuration file loading with support for includes, symbol substitution, and live reloading.
Configuration file syntax
The configuration syntax is JSON-compliant YAML. Top-level keys map to smisk.config.config attributes:
smisk:
autoreload: True
mvc:
template_dir: templates/
myapp:
db_url: "sqlite:///myapp.db"
Includes
Use @include to split configuration across files:
@include: base.yaml
Paths can be expanded using glob, so you can include multiple files with a glob pattern:
@include: conf.d/*.yaml
@inherit
Use @inherit to extend configuration from another file, merging keys recursively.
Logging
Logging (the standard library logging module) is configured automatically. Supported handlers: StreamHandler, FileHandler, SysLogHandler. You can also configure a custom Formatter.
logging:
root:
level: DEBUG
handlers: [stderr]
handlers:
stderr:
class: StreamHandler
stream: ext://sys.stderr
Symbols
Use ${SYMBOL} in string values to substitute predefined or custom symbols.
Predefined symbols
| Symbol | Description |
|---|---|
${APPDIR} | Absolute path to the application directory |
${HOSTNAME} | Current machine hostname |
Practical use
from smisk.config import config
config.load("myapp.yaml")
print(config.myapp.db_url)
Attributes
smisk.config.configThe global
Configurationinstance.smisk.config.config_locationsList of directories searched for configuration files. Defaults include the application directory and
/etc.smisk.config.LOGGING_FORMATDefault log format string.
smisk.config.LOGGING_DATEFMTDefault log date format string.
Functions
smisk.config.configure_logging()Apply the
loggingsection of the current configuration using Python's logging subsystem.
Classes
- class
smisk.config.Configuration -
A lazy-loading configuration object. Attributes are created dynamically from YAML keys.
defaultsA dict of default values applied before loading from files.
default_symbolsPredefined symbol substitutions.
sourcesList of loaded source file paths.
filtersList of filter callables applied after loading.
filename_extRecognised configuration file extensions. Defaults to
[".yaml", ".json"].logging_keyThe key in config used for logging setup. Defaults to
"logging".input_encodingCharacter encoding of configuration files. Defaults to
"utf-8".max_include_depthMaximum nesting depth for
@includedirectives. Defaults to 8.__init__()Initialise an empty configuration object.
__call__()Shorthand for loading the default configuration file.
set_default(key, value)Set a default value for key that is used if the key is not found in any loaded file.
load(filename)Load and apply a configuration file.
loads(string)Load configuration from a string.
reload()Reload all configuration sources from disk.
reset()Reset to a blank state, clearing all loaded values.
add_filter(fn)Register a filter callable to be called after each load.