Failed Conditions
Pull Request — master (#2076)
by Abdeali
02:04
created

coalib/collecting/Dependencies.py (5 issues)

1
class CircularDependencyError(Exception):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable Exception does not seem to be defined.
Loading history...
2
3
    @classmethod
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable classmethod does not seem to be defined.
Loading history...
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]
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable bears does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable bear does not seem to be defined.
Loading history...
10
11
        return cls("Circular dependency detected: " + " -> ".join(bear_names))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable bear_names does not seem to be defined.
Loading history...
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 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