Commit 06877f1a authored by Jason R. Coombs's avatar Jason R. Coombs

Merge pkg_resources.Environment.__getitem__() code cleanup (pull request #47...

Merge pkg_resources.Environment.__getitem__() code cleanup (pull request #47 and subsequent changes).
parents 09719162 c35659d0
...@@ -776,7 +776,6 @@ class Environment(object): ...@@ -776,7 +776,6 @@ class Environment(object):
running platform or Python version. running platform or Python version.
""" """
self._distmap = {} self._distmap = {}
self._cache = {}
self.platform = platform self.platform = platform
self.python = python self.python = python
self.scan(search_path) self.scan(search_path)
...@@ -813,28 +812,23 @@ class Environment(object): ...@@ -813,28 +812,23 @@ class Environment(object):
def __getitem__(self, project_name): def __getitem__(self, project_name):
"""Return a newest-to-oldest list of distributions for `project_name` """Return a newest-to-oldest list of distributions for `project_name`
"""
try:
return self._cache[project_name]
except KeyError:
project_name = project_name.lower()
if project_name not in self._distmap:
return []
if project_name not in self._cache: Uses case-insensitive `project_name` comparison, assuming all the
dists = self._cache[project_name] = self._distmap[project_name] project's distributions use their project's name converted to all
_sort_dists(dists) lowercase as their key.
return self._cache[project_name] """
distribution_key = project_name.lower()
return self._distmap.get(distribution_key, [])
def add(self, dist): def add(self, dist):
"""Add `dist` if we ``can_add()`` it and it isn't already added""" """Add `dist` if we ``can_add()`` it and it has not already been added
"""
if self.can_add(dist) and dist.has_version(): if self.can_add(dist) and dist.has_version():
dists = self._distmap.setdefault(dist.key,[]) dists = self._distmap.setdefault(dist.key, [])
if dist not in dists: if dist not in dists:
dists.append(dist) dists.append(dist)
if dist.key in self._cache: dists.sort(key=operator.attrgetter('hashcmp'), reverse=True)
_sort_dists(self._cache[dist.key])
def best_match(self, req, working_set, installer=None): def best_match(self, req, working_set, installer=None):
"""Find distribution best matching `req` and usable on `working_set` """Find distribution best matching `req` and usable on `working_set`
...@@ -2607,12 +2601,6 @@ def parse_requirements(strs): ...@@ -2607,12 +2601,6 @@ def parse_requirements(strs):
yield Requirement(project_name, specs, extras) yield Requirement(project_name, specs, extras)
def _sort_dists(dists):
tmp = [(dist.hashcmp, dist) for dist in dists]
tmp.sort()
dists[::-1] = [d for hc, d in tmp]
class Requirement: class Requirement:
def __init__(self, project_name, specs, extras): def __init__(self, project_name, specs, extras):
"""DO NOT CALL THIS UNDOCUMENTED METHOD; use Requirement.parse()!""" """DO NOT CALL THIS UNDOCUMENTED METHOD; use Requirement.parse()!"""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment