Skip to content

Commit

Permalink
Move four PackageFinder methods.
Browse files Browse the repository at this point in the history
This moves the methods to before the method where they are called.
  • Loading branch information
cjerdonek committed Aug 23, 2019
1 parent aa14d3a commit 5e80b82
Showing 1 changed file with 57 additions and 57 deletions.
114 changes: 57 additions & 57 deletions src/pip/_internal/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,63 @@ def make_link_evaluator(self, project_name):
ignore_requires_python=self._ignore_requires_python,
)

def _sort_links(self, links):
# type: (Iterable[Link]) -> List[Link]
"""
Returns elements of links in order, non-egg links first, egg links
second, while eliminating duplicates
"""
eggs, no_eggs = [], []
seen = set() # type: Set[Link]
for link in links:
if link not in seen:
seen.add(link)
if link.egg_fragment:
eggs.append(link)
else:
no_eggs.append(link)
return no_eggs + eggs

def _log_skipped_link(self, link, reason):
# type: (Link, Text) -> None
if link not in self._logged_links:
# Mark this as a unicode string to prevent "UnicodeEncodeError:
# 'ascii' codec can't encode character" in Python 2 when
# the reason contains non-ascii characters.
# Also, put the link at the end so the reason is more visible
# and because the link string is usually very long.
logger.debug(u'Skipping link: %s: %s', reason, link)
self._logged_links.add(link)

def get_install_candidate(self, link_evaluator, link):
# type: (LinkEvaluator, Link) -> Optional[InstallationCandidate]
"""
If the link is a candidate for install, convert it to an
InstallationCandidate and return it. Otherwise, return None.
"""
is_candidate, result = link_evaluator.evaluate_link(link)
if not is_candidate:
if result:
self._log_skipped_link(link, reason=result)
return None

return InstallationCandidate(
project=link_evaluator.project_name,
link=link,
# Convert the Text result to str since InstallationCandidate
# accepts str.
version=str(result),
)

def _package_versions(self, link_evaluator, links):
# type: (LinkEvaluator, Iterable[Link]) -> List[InstallationCandidate]
result = []
for link in self._sort_links(links):
candidate = self.get_install_candidate(link_evaluator, link)
if candidate is not None:
result.append(candidate)
return result

def find_all_candidates(self, project_name):
# type: (str) -> List[InstallationCandidate]
"""Find all available InstallationCandidate for project_name
Expand Down Expand Up @@ -1178,63 +1235,6 @@ def _get_pages(self, locations, project_name):

yield page

def _sort_links(self, links):
# type: (Iterable[Link]) -> List[Link]
"""
Returns elements of links in order, non-egg links first, egg links
second, while eliminating duplicates
"""
eggs, no_eggs = [], []
seen = set() # type: Set[Link]
for link in links:
if link not in seen:
seen.add(link)
if link.egg_fragment:
eggs.append(link)
else:
no_eggs.append(link)
return no_eggs + eggs

def _log_skipped_link(self, link, reason):
# type: (Link, Text) -> None
if link not in self._logged_links:
# Mark this as a unicode string to prevent "UnicodeEncodeError:
# 'ascii' codec can't encode character" in Python 2 when
# the reason contains non-ascii characters.
# Also, put the link at the end so the reason is more visible
# and because the link string is usually very long.
logger.debug(u'Skipping link: %s: %s', reason, link)
self._logged_links.add(link)

def get_install_candidate(self, link_evaluator, link):
# type: (LinkEvaluator, Link) -> Optional[InstallationCandidate]
"""
If the link is a candidate for install, convert it to an
InstallationCandidate and return it. Otherwise, return None.
"""
is_candidate, result = link_evaluator.evaluate_link(link)
if not is_candidate:
if result:
self._log_skipped_link(link, reason=result)
return None

return InstallationCandidate(
project=link_evaluator.project_name,
link=link,
# Convert the Text result to str since InstallationCandidate
# accepts str.
version=str(result),
)

def _package_versions(self, link_evaluator, links):
# type: (LinkEvaluator, Iterable[Link]) -> List[InstallationCandidate]
result = []
for link in self._sort_links(links):
candidate = self.get_install_candidate(link_evaluator, link)
if candidate is not None:
result.append(candidate)
return result


def _find_name_version_sep(fragment, canonical_name):
# type: (str, str) -> int
Expand Down

0 comments on commit 5e80b82

Please sign in to comment.