class documentation

Invoke's primary configuration handling class. See :doc:`/concepts/configuration` for details on the configuration system this class implements, including the :ref:`configuration hierarchy <config-hierarchy>`. The rest of this class' documentation assumes familiarity with that document. **Access** Configuration values may be accessed and/or updated using dict syntax:: config['foo'] or attribute syntax:: config.foo Nesting works the same way - dict config values are turned into objects which honor both the dictionary protocol and the attribute-access method:: config['foo']['bar'] config.foo.bar **A note about attribute access and methods** This class implements the entire dictionary protocol: methods such as ``keys``, ``values``, ``items``, ``pop`` and so forth should all function as they do on regular dicts. It also implements new config-specific methods such as `load_system`, `load_collection`, `merge`, `clone`, etc. .. warning:: Accordingly, this means that if you have configuration options sharing names with these methods, you **must** use dictionary syntax (e.g. ``myconfig['keys']``) to access the configuration data. **Lifecycle** At initialization time, `.Config`: - creates per-level data structures; - stores any levels supplied to `__init__`, such as defaults or overrides, as well as the various config file paths/filename patterns; - and loads config files, if found (though typically this just means system and user-level files, as project and runtime files need more info before they can be found and loaded.) - This step can be skipped by specifying ``lazy=True``. At this point, `.Config` is fully usable - and because it pre-emptively loads some config files, those config files can affect anything that comes after, like CLI parsing or loading of task collections. In the CLI use case, further processing is done after instantiation, using the ``load_*`` methods such as `load_overrides`, `load_project`, etc: - the result of argument/option parsing is applied to the overrides level; - a project-level config file is loaded, as it's dependent on a loaded tasks collection; - a runtime config file is loaded, if its flag was supplied; - then, for each task being executed: - per-collection data is loaded (only possible now that we have collection & task in hand); - shell environment data is loaded (must be done at end of process due to using the rest of the config as a guide for interpreting env var names.) At this point, the config object is handed to the task being executed, as part of its execution `.Context`. Any modifications made directly to the `.Config` itself after this point end up stored in their own (topmost) config level, making it easier to debug final values. Finally, any *deletions* made to the `.Config` (e.g. applications of dict-style mutators like ``pop``, ``clear`` etc) are also tracked in their own structure, allowing the config object to honor such method calls without mutating the underlying source data. **Special class attributes** The following class-level attributes are used for low-level configuration of the config system itself, such as which file paths to load. They are primarily intended for overriding by subclasses. - ``prefix``: Supplies the default value for ``file_prefix`` (directly) and ``env_prefix`` (uppercased). See their descriptions for details. Its default value is ``"invoke"``. - ``file_prefix``: The config file 'basename' default (though it is not a literal basename; it can contain path parts if desired) which is appended to the configured values of ``system_prefix``, ``user_prefix``, etc, to arrive at the final (pre-extension) file paths. Thus, by default, a system-level config file path concatenates the ``system_prefix`` of ``/etc/`` with the ``file_prefix`` of ``invoke`` to arrive at paths like ``/etc/invoke.json``. Defaults to ``None``, meaning to use the value of ``prefix``. - ``env_prefix``: A prefix used (along with a joining underscore) to determine which environment variables are loaded as the env var configuration level. Since its default is the value of ``prefix`` capitalized, this means env vars like ``INVOKE_RUN_ECHO`` are sought by default. Defaults to ``None``, meaning to use the value of ``prefix``. .. versionadded:: 1.0

Static Method global_defaults Return the core default settings for Invoke.
Method __init__ Creates a new config object.
Method clone Return a copy of this configuration object.
Method load_base_conf_files Undocumented
Method load_collection Update collection-driven config data.
Method load_defaults Set or replace the 'defaults' configuration level, from ``data``.
Method load_overrides Set or replace the 'overrides' configuration level, from ``data``.
Method load_project Load a project-level config file, if possible.
Method load_runtime Load a runtime-level config file, if one was specified.
Method load_shell_env Load values from the shell environment.
Method load_system Load a system-level config file, if possible.
Method load_user Load a user-level config file, if possible.
Method merge Merge all config sources, in order.
Method set_project_location Set the directory path where a project-level config file may be found.
Method set_runtime_path Set the runtime config file path.
Class Variable env_prefix Undocumented
Class Variable file_prefix Undocumented
Class Variable prefix Undocumented
Method _clone_init_kwargs Supply kwargs suitable for initializing a new clone of this object.
Method _load_file Undocumented
Method _load_json Undocumented
Method _load_py Undocumented
Method _load_yaml Undocumented
Method _load_yml Undocumented
Method _merge_file Undocumented
Method _modify Update our user-modifications config level with new data.
Method _remove Like `._modify`, but for removal.

Inherited from DataProxy:

Class Method from_data Alternate constructor for 'baby' DataProxies used as sub-dict values.
Method __contains__ Undocumented
Method __delattr__ Undocumented
Method __delitem__ Undocumented
Method __eq__ Undocumented
Method __getattr__ Undocumented
Method __getitem__ Undocumented
Method __iter__ Undocumented
Method __len__ Undocumented
Method __repr__ Undocumented
Method __setattr__ Undocumented
Method __setitem__ Undocumented
Method clear Undocumented
Method pop Undocumented
Method popitem Undocumented
Method setdefault Undocumented
Method update Undocumented
Method _get Undocumented
Method _set Convenience workaround of default 'attrs are config keys' behavior.
Method _track_modification_of Undocumented
Method _track_removal_of Undocumented
Class Variable _proxies Undocumented
Property _is_leaf Undocumented
Property _is_root Undocumented
@staticmethod
def global_defaults(): (source)

Return the core default settings for Invoke. Generally only for use by `.Config` internals. For descriptions of these values, see :ref:`default-values`. Subclasses may choose to override this method, calling ``Config.global_defaults`` and applying `.merge_dicts` to the result, to add to or modify these values. .. versionadded:: 1.0

def __init__(self, overrides=None, defaults=None, system_prefix=None, user_prefix=None, project_location=None, runtime_path=None, lazy=False): (source)

Creates a new config object. :param dict defaults: A dict containing default (lowest level) config data. Default: `global_defaults`. :param dict overrides: A dict containing override-level config data. Default: ``{}``. :param str system_prefix: Base path for the global config file location; combined with the prefix and file suffixes to arrive at final file path candidates. Default: ``/etc/`` (thus e.g. ``/etc/invoke.yaml`` or ``/etc/invoke.json``). :param str user_prefix: Like ``system_prefix`` but for the per-user config file. These variables are joined as strings, not via path-style joins, so they may contain partial file paths; for the per-user config file this often means a leading dot, to make the final result a hidden file on most systems. Default: ``~/.`` (e.g. ``~/.invoke.yaml``). :param str project_location: Optional directory path of the currently loaded `.Collection` (as loaded by `.Loader`). When non-empty, will trigger seeking of per-project config files in this directory. :param str runtime_path: Optional file path to a runtime configuration file. Used to fill the penultimate slot in the config hierarchy. Should be a full file path to an existing file, not a directory path or a prefix. :param bool lazy: Whether to automatically load some of the lower config levels. By default (``lazy=False``), ``__init__`` automatically calls `load_system` and `load_user` to load system and user config files, respectively. For more control over what is loaded when, you can say ``lazy=True``, and no automatic loading is done. .. note:: If you give ``defaults`` and/or ``overrides`` as ``__init__`` kwargs instead of waiting to use `load_defaults` or `load_overrides` afterwards, those *will* still end up 'loaded' immediately.

def clone(self, into=None): (source)

Return a copy of this configuration object. The new object will be identical in terms of configured sources and any loaded (or user-manipulated) data, but will be a distinct object with as little shared mutable state as possible. Specifically, all `dict` values within the config are recursively recreated, with non-dict leaf values subjected to `copy.copy` (note: *not* `copy.deepcopy`, as this can cause issues with various objects such as compiled regexen or threading locks, often found buried deep within rich aggregates like API or DB clients). The only remaining config values that may end up shared between a config and its clone are thus those 'rich' objects that do not `copy.copy` cleanly, or compound non-dict objects (such as lists or tuples). :param into: A `.Config` subclass that the new clone should be "upgraded" to. Used by client libraries which have their own `.Config` subclasses that e.g. define additional defaults; cloning "into" one of these subclasses ensures that any new keys/subtrees are added gracefully, without overwriting anything that may have been pre-defined. Default: ``None`` (just clone into another regular `.Config`). :returns: A `.Config`, or an instance of the class given to ``into``. :raises: ``TypeError``, if ``into`` is given a value and that value is not a `.Config` subclass. .. versionadded:: 1.0

def load_base_conf_files(self): (source)

Undocumented

def load_collection(self, data, merge=True): (source)

Update collection-driven config data. `.load_collection` is intended for use by the core task execution machinery, which is responsible for obtaining collection-driven data. See :ref:`collection-configuration` for details. .. versionadded:: 1.0

def load_defaults(self, data, merge=True): (source)

Set or replace the 'defaults' configuration level, from ``data``. :param dict data: The config data to load as the defaults level. :param bool merge: Whether to merge the loaded data into the central config. Default: ``True``. :returns: ``None``. .. versionadded:: 1.0

def load_overrides(self, data, merge=True): (source)

Set or replace the 'overrides' configuration level, from ``data``. :param dict data: The config data to load as the overrides level. :param bool merge: Whether to merge the loaded data into the central config. Default: ``True``. :returns: ``None``. .. versionadded:: 1.0

def load_project(self, merge=True): (source)

Load a project-level config file, if possible. Checks the configured ``_project_prefix`` value derived from the path given to `set_project_location`, which is typically set to the directory containing the loaded task collection. Thus, if one were to run the CLI tool against a tasks collection ``/home/myuser/code/tasks.py``, `load_project` would seek out files like ``/home/myuser/code/invoke.yml``. :param bool merge: Whether to merge the loaded data into the central config. Default: ``True``. :returns: ``None``. .. versionadded:: 1.0

def load_runtime(self, merge=True): (source)

Load a runtime-level config file, if one was specified. When the CLI framework creates a `Config`, it sets ``_runtime_path``, which is a full path to the requested config file. This method attempts to load that file. :param bool merge: Whether to merge the loaded data into the central config. Default: ``True``. :returns: ``None``. .. versionadded:: 1.0

def load_shell_env(self): (source)

Load values from the shell environment. `.load_shell_env` is intended for execution late in a `.Config` object's lifecycle, once all other sources (such as a runtime config file or per-collection configurations) have been loaded. Loading from the shell is not terrifically expensive, but must be done at a specific point in time to ensure the "only known config keys are loaded from the env" behavior works correctly. See :ref:`env-vars` for details on this design decision and other info re: how environment variables are scanned and loaded. .. versionadded:: 1.0

def load_system(self, merge=True): (source)

Load a system-level config file, if possible. Checks the configured ``_system_prefix`` path, which defaults to ``/etc``, and will thus load files like ``/etc/invoke.yml``. :param bool merge: Whether to merge the loaded data into the central config. Default: ``True``. :returns: ``None``. .. versionadded:: 1.0

def load_user(self, merge=True): (source)

Load a user-level config file, if possible. Checks the configured ``_user_prefix`` path, which defaults to ``~/.``, and will thus load files like ``~/.invoke.yml``. :param bool merge: Whether to merge the loaded data into the central config. Default: ``True``. :returns: ``None``. .. versionadded:: 1.0

def merge(self): (source)

Merge all config sources, in order. .. versionadded:: 1.0

def set_project_location(self, path): (source)

Set the directory path where a project-level config file may be found. Does not do any file loading on its own; for that, see `load_project`. .. versionadded:: 1.0

def set_runtime_path(self, path): (source)

Set the runtime config file path. .. versionadded:: 1.0

env_prefix = (source)

Undocumented

file_prefix = (source)

Undocumented

Undocumented

def _clone_init_kwargs(self, into=None): (source)

Supply kwargs suitable for initializing a new clone of this object. Note that most of the `.clone` process involves copying data between two instances instead of passing init kwargs; however, sometimes you really do want init kwargs, which is why this method exists. :param into: The value of ``into`` as passed to the calling `.clone`. :returns: A `dict`.

def _load_file(self, prefix, absolute=False, merge=True): (source)

Undocumented

def _load_json(self, path): (source)

Undocumented

def _load_py(self, path): (source)

Undocumented

def _load_yaml(self, path): (source)

Undocumented

def _load_yml(self, path): (source)

Undocumented

def _merge_file(self, name, desc): (source)

Undocumented

def _modify(self, keypath, key, value): (source)

Update our user-modifications config level with new data. :param tuple keypath: The key path identifying the sub-dict being updated. May be an empty tuple if the update is occurring at the topmost level. :param str key: The actual key receiving an update. :param value: The value being written.

def _remove(self, keypath, key): (source)

Like `._modify`, but for removal.