1class AbstractProvider(object):
2    """Delegate class to provide requirement interface for the resolver."""
3
4    def identify(self, requirement_or_candidate):
5        """Given a requirement, return an identifier for it.
6
7        This is used to identify a requirement, e.g. whether two requirements
8        should have their specifier parts merged.
9        """
10        raise NotImplementedError
11
12    def get_preference(
13        self,
14        identifier,
15        resolutions,
16        candidates,
17        information,
18        backtrack_causes,
19    ):
20        """Produce a sort key for given requirement based on preference.
21
22        The preference is defined as "I think this requirement should be
23        resolved first". The lower the return value is, the more preferred
24        this group of arguments is.
25
26        :param identifier: An identifier as returned by ``identify()``. This
27            identifies the dependency matches of which should be returned.
28        :param resolutions: Mapping of candidates currently pinned by the
29            resolver. Each key is an identifier, and the value a candidate.
30            The candidate may conflict with requirements from ``information``.
31        :param candidates: Mapping of each dependency's possible candidates.
32            Each value is an iterator of candidates.
33        :param information: Mapping of requirement information of each package.
34            Each value is an iterator of *requirement information*.
35        :param backtrack_causes: Sequence of requirement information that were
36            the requirements that caused the resolver to most recently backtrack.
37
38        A *requirement information* instance is a named tuple with two members:
39
40        * ``requirement`` specifies a requirement contributing to the current
41          list of candidates.
42        * ``parent`` specifies the candidate that provides (dependend on) the
43          requirement, or ``None`` to indicate a root requirement.
44
45        The preference could depend on a various of issues, including (not
46        necessarily in this order):
47
48        * Is this package pinned in the current resolution result?
49        * How relaxed is the requirement? Stricter ones should probably be
50          worked on first? (I don't know, actually.)
51        * How many possibilities are there to satisfy this requirement? Those
52          with few left should likely be worked on first, I guess?
53        * Are there any known conflicts for this requirement? We should
54          probably work on those with the most known conflicts.
55
56        A sortable value should be returned (this will be used as the ``key``
57        parameter of the built-in sorting function). The smaller the value is,
58        the more preferred this requirement is (i.e. the sorting function
59        is called with ``reverse=False``).
60        """
61        raise NotImplementedError
62
63    def find_matches(self, identifier, requirements, incompatibilities):
64        """Find all possible candidates that satisfy given constraints.
65
66        :param identifier: An identifier as returned by ``identify()``. This
67            identifies the dependency matches of which should be returned.
68        :param requirements: A mapping of requirements that all returned
69            candidates must satisfy. Each key is an identifier, and the value
70            an iterator of requirements for that dependency.
71        :param incompatibilities: A mapping of known incompatibilities of
72            each dependency. Each key is an identifier, and the value an
73            iterator of incompatibilities known to the resolver. All
74            incompatibilities *must* be excluded from the return value.
75
76        This should try to get candidates based on the requirements' types.
77        For VCS, local, and archive requirements, the one-and-only match is
78        returned, and for a "named" requirement, the index(es) should be
79        consulted to find concrete candidates for this requirement.
80
81        The return value should produce candidates ordered by preference; the
82        most preferred candidate should come first. The return type may be one
83        of the following:
84
85        * A callable that returns an iterator that yields candidates.
86        * An collection of candidates.
87        * An iterable of candidates. This will be consumed immediately into a
88          list of candidates.
89        """
90        raise NotImplementedError
91
92    def is_satisfied_by(self, requirement, candidate):
93        """Whether the given requirement can be satisfied by a candidate.
94
95        The candidate is guarenteed to have been generated from the
96        requirement.
97
98        A boolean should be returned to indicate whether ``candidate`` is a
99        viable solution to the requirement.
100        """
101        raise NotImplementedError
102
103    def get_dependencies(self, candidate):
104        """Get dependencies of a candidate.
105
106        This should return a collection of requirements that `candidate`
107        specifies as its dependencies.
108        """
109        raise NotImplementedError
110
111
112class AbstractResolver(object):
113    """The thing that performs the actual resolution work."""
114
115    base_exception = Exception
116
117    def __init__(self, provider, reporter):
118        self.provider = provider
119        self.reporter = reporter
120
121    def resolve(self, requirements, **kwargs):
122        """Take a collection of constraints, spit out the resolution result.
123
124        This returns a representation of the final resolution state, with one
125        guarenteed attribute ``mapping`` that contains resolved candidates as
126        values. The keys are their respective identifiers.
127
128        :param requirements: A collection of constraints.
129        :param kwargs: Additional keyword arguments that subclasses may accept.
130
131        :raises: ``self.base_exception`` or its subclass.
132        """
133        raise NotImplementedError
134