dependency

Dependency resolver module.

exception confirm.utils.dependency.CircularDependencyError(dependencies)

Exception which is thrown when we’ve a circual dependency.

exception confirm.utils.dependency.DependencyError

Exception which is thrown when there’s a dependency error of some sort.

exception confirm.utils.dependency.MissingDependencyError(dependencies)

Exception which is thrown when there’s a dependency defined which isn’t existing / missing.

confirm.utils.dependency.resolve_dependency_order(dependencies, raise_on_missing=True)

Resolve a list of dependencies and return the order in which they’ve to be evaluated.

Parameters:
  • dependencies (dict) – A dict with all dependencies

  • raise_on_missing (bool) – Raise exception on missing dependencies

Returns:

The resolved dependency order

Return type:

list

Raises:

Hint

The function will take an input dict with the following format:

>>> deps = {
... 'a': ['b', 'e'],    # 'a' depends on 'b' and 'e'
... 'b': ['d',],        # 'b' depends on 'd'
... 'c': [],            # 'c' has no dependencies
... 'd': ['c'],         # 'd' depends on 'c'
... 'e': ['d'],         # 'd' depends on 'd'
... 'f': [],            # 'f' has no dependencies
... }

Resolving the dependencies will return a list like this:

>>> resolve_dependency_order(dependencies=deps)
[['c', 'f'], ['d'], ['b', 'e'], ['a']]

By default the function will raise a MissingDependencyError when there’s a dependency missing:

>>> deps = {
... 'a': ['b', 'c'],    # 'a' depends on 'b' and 'c', but 'b' is missing
... 'c': ['d'],         # 'c' depends on 'd', but 'd' is missing
... }
>>> resolve_dependency_order(dependencies=deps)
Traceback (most recent call last):
    ...
confirm.utils.dependency.MissingDependencyError: Missing dependencies (b, d)

However, instead of raising a MissingDependencyError, missing dependencies can also automatically be added:

>>> resolve_dependency_order(dependencies=deps, raise_on_missing=False)
[['b', 'd'], ['c'], ['a']]

In case of circual dependencies, a CircularDependencyError exception will be raised.

>>> deps = {
... 'a': ['b', 'c'],    # 'a' depends on 'c' (and vice-versa)
... 'b': [],            # 'b' has no dependencies
... 'c': ['a'],         # 'c' depends on 'a' (and vice-versa)
... }
>>> resolve_dependency_order(dependencies=deps)
Traceback (most recent call last):
    ...
confirm.utils.dependency.CircularDependencyError: Circular dependencies found (a->c, c->a)