summaryrefslogtreecommitdiff
path: root/pkg_resources.py
diff options
context:
space:
mode:
authorJason R. Coombs <jaraco@jaraco.com>2014-05-05 02:48:38 -0400
committerJason R. Coombs <jaraco@jaraco.com>2014-05-05 02:48:38 -0400
commit06877f1a4167e117ab251f5dac59e27fdfe630ec (patch)
treed1d9479261a433fb25b410a8339cb1d004a67746 /pkg_resources.py
parent09719162082439735da57a160fc9ea7fbe0f6935 (diff)
parentc35659d044bb7cdb8f0a5d14cf0947ae0f722fe1 (diff)
downloadpython-setuptools-git-06877f1a4167e117ab251f5dac59e27fdfe630ec.tar.gz
Merge pkg_resources.Environment.__getitem__() code cleanup (pull request #47 and subsequent changes).
Diffstat (limited to 'pkg_resources.py')
-rw-r--r--pkg_resources.py32
1 files changed, 10 insertions, 22 deletions
diff --git a/pkg_resources.py b/pkg_resources.py
index af11f583..54285ca8 100644
--- a/pkg_resources.py
+++ b/pkg_resources.py
@@ -776,7 +776,6 @@ class Environment(object):
running platform or Python version.
"""
self._distmap = {}
- self._cache = {}
self.platform = platform
self.python = python
self.scan(search_path)
@@ -813,28 +812,23 @@ class Environment(object):
def __getitem__(self, 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:
- dists = self._cache[project_name] = self._distmap[project_name]
- _sort_dists(dists)
+ Uses case-insensitive `project_name` comparison, assuming all the
+ project's distributions use their project's name converted to all
+ lowercase as their key.
- return self._cache[project_name]
+ """
+ distribution_key = project_name.lower()
+ return self._distmap.get(distribution_key, [])
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():
- dists = self._distmap.setdefault(dist.key,[])
+ dists = self._distmap.setdefault(dist.key, [])
if dist not in dists:
dists.append(dist)
- if dist.key in self._cache:
- _sort_dists(self._cache[dist.key])
+ dists.sort(key=operator.attrgetter('hashcmp'), reverse=True)
def best_match(self, req, working_set, installer=None):
"""Find distribution best matching `req` and usable on `working_set`
@@ -2607,12 +2601,6 @@ def parse_requirements(strs):
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:
def __init__(self, project_name, specs, extras):
"""DO NOT CALL THIS UNDOCUMENTED METHOD; use Requirement.parse()!"""