CircularDependencyError   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 11
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A for_bears() 0 9 2
1
class CircularDependencyError(Exception):
2
3
    @classmethod
4
    def for_bears(cls, bears):
5
        """
6
        Creates the CircularDependencyError with a helpful message about the
7
        dependency.
8
        """
9
        bear_names = [bear.name for bear in bears]
10
11
        return cls("Circular dependency detected: " + " -> ".join(bear_names))
12
13
14
def _resolve(bears, resolved_bears, seen):
15
    for bear in bears:
16
        if bear in resolved_bears:
17
            continue
18
19
        missing = bear.missing_dependencies(resolved_bears)
20
        if not missing:
21
            resolved_bears.append(bear)
22
            continue
23
24
        if bear in seen:
25
            seen.append(bear)
26
            raise CircularDependencyError.for_bears(seen)
27
28
        seen.append(bear)
29
        resolved_bears = _resolve(missing, resolved_bears, seen)
30
        resolved_bears.append(bear)
31
        seen.remove(bear)  # Already resolved, no candidate for circular dep
32
33
    return resolved_bears
34
35
36
def resolve(bears):
37
    """
38
    Collects all dependencies of the given bears. This will also remove
39
    duplicates.
40
41
    :param bears: The given bears. Will not be modified.
42
    :return:      The new list of bears, sorted so that it can be executed
43
                  sequentially without dependency issues.
44
    """
45
    return _resolve(bears, [], [])
46