Commits

ta...@MacZiade  committed 9ff533d

now using EggInfoDirectories -> EggInfoDirectory -> EggInfo structure

  • Participants
  • Parent commits 88d5dd4
  • Branches directory

Comments (0)

Files changed (2)

         return open(fullpath, binary and 'rb' or 'r')
 
 #
-# Class for I/O caching
-#
-
-class CacheException(Exception):
-    pass
-
-class Cache(dict):
-    """Regular dict with state flags.
-
-      - completed : the cache is fully completed
-      - enabled: the cache is enabled/disabled
-    """
-    def __init__(self, *args, **kwargs):
-        self.completed = False
-        self.enabled = True
-        self._loading = False
-        super(Cache, self).__init__(*args, **kwargs)
-
-    def enable(self):
-        self.enabled = True
-
-    def disable(self):
-        if self._loading:
-            raise CacheException('The cache is being loaded.')
-        self.enabled = False
-        self.clear()
-
-    def clear(self):
-        if self._loading:
-            raise CacheException('The cache is being loaded.')
-        self.completed = False
-        super(Cache).clear()
-
-    def __enter__(self):
-        self._loading = True
-        return self
-
-    def __exit__(self, *args):
-        self._loading = False
-
-#
 # Directory represents a directory that contains egg-info files
 #
-class Directory(object):
+class EggInfoDirectory(list):
 
-    def __init__(self, path, cache_enabled=True):
+    def __init__(self, path):
         self.path = path
-        self._cache = Cache()
-        if not cache_enabled:
-            self._cache.disable()
-
-    #
-    # cache toggling
-    #
-    def _set_cache_enabled(self, value):
-        if value:
-            self._cache.enable()
-        else:
-            self._cache.disable()
-
-    def _get_cache_enabled(self):
-        return self._cache.enabled
-
-    cache_enabled = property(_get_cache_enabled, _set_cache_enabled)
-
-    def _cache_ready(self):
-        return self.cache_enabled and self._cache.completed
-
-    #
-    # public APIs
-    #
-    def egg_info_dirs(self):
-        """Returns the EGG-INFO directories."""
-        if self._cache_ready():
-            for egg_info in self._cache.values():
-                yield egg_info
-        else:
-            # revisiting the directory
-            with self._cache as cache:
-
-                for element in os.listdir(self.path):
-                    fullpath = join(self.path, element)
-                    # see if the path was not previously loaded
-                    if cache.enabled and fullpath in cache:
-                        yield cache[fullpath]
-                    else:
-                        # no, let's load it
-                        if is_egg_info(fullpath):
-                            egg_info = EggInfo(fullpath)
-                            if cache.enabled:
-                                cache[fullpath] = egg_info
-                            yield egg_info
-                # the cache is fully created
-                cache.completed = True
-
-    __iter__ = egg_info_dirs
+        # filling the list once (see if it's the best way)
+        # to minimize I/O
+        for element in os.listdir(self.path):
+            fullpath = join(self.path, element)
+            if is_egg_info(fullpath):
+                self.append(EggInfo(fullpath))
 
     def file_users(self, path):
         """Returns EggInfo instances for the projects that uses `path`."""
-        for egg_info in self.egg_info_dirs():
+        for egg_info in self:
             if egg_info.uses(path):
                 yield egg_info
 
     def owner(self, path):
         """Returns the owner of `path`."""
-        users = [egg_info for egg_info in self.egg_info_dirs()
-                 if egg_info.uses(path)]
+        users = [egg_info for egg_info in self if egg_info.uses(path)]
         if len(users) == 1:
             return users[0]
         return None
 # Directories is a collection of directories, initialized with a
 # list of paths.
 #
-class Directories(list):
-    pass
+class EggInfoDirectories(list):
+
+    def __init__(self, paths=sys.path):
+        super(EggInfoDirectories, self).__init__()
+        for path in paths:
+            if os.path.isdir(path):
+                self.append(EggInfoDirectory(path))
+
+    def get_egg_infos(self):
+        """Iterates on all .egg-info directories founded in sys.path.
+
+        Each returned element is an EggInfo instance.
+        Uses a memory cache to minimize I/O access.
+        """
+        for directory in self:
+            for egg_info in directory:
+                yield egg_info
+
+    def get_egg_info(self, project_name):
+        """Returns an EggInfo instance for the given project name.
+
+        If not found, returns None.
+        """
+        for directory in self:
+            for egg_info in directory:
+                if egg_info.name == project_name:
+                    return egg_info
+
+    def get_file_users(self, path):
+        """Iterates over all projects to find out which project uses the file.
+
+        Return EggInfo instances.
+        """
+        for directory in self:
+            for egg_info in directory.file_users(path):
+                yield egg_info
 
 #
-# high-level APIs
+# high level APIs
 #
+
 def get_egg_infos(paths=sys.path):
-    """Iterates on all .egg-info directories founded in sys.path.
-
-    Each returned element is an EggInfo instance.
-    Uses a memory cache to minimize I/O access.
-    """
-    for path in paths:
-        directory = Directory(path)
-        return directory.egg_info_dirs()
+    dirs = EggInfoDirectories(paths)
+    return dirs.get_egg_infos()
 
 def get_egg_info(project_name, paths=sys.path):
-    """Returns an EggInfo instance for the given project name.
-
-    If not found, returns None.
-    """
-    for project in get_egg_infos(paths):
-        if project.name == project_name:
-            return project
+    dirs = EggInfoDirectories(paths)
+    return dirs.get_egg_info(project_name)
 
 def get_file_users(path, paths=sys.path):
-    """Iterates over all projects to find out which project uses the file.
+    dirs = EggInfoDirectories(paths)
+    return dirs.get_file_users(path)
 
-    Return EggInfo instances.
-    """
-    for path_ in paths:
-        directory = Directory(path_)
-        for egg_info in directory.file_users(path):
-            yield egg_info
 

File test_pkgutil.py

     projects = list(get_egg_infos([SITE_PKG]))
     assert_equals(len(projects), 2)
 
-def test_get_egg_info():
+def test_egg_info_directory():
     assert_equals(get_egg_info('xxx', [SITE_PKG]), None)
 
     project = get_egg_info('mercurial', [SITE_PKG])
 
 
 def test_directory():
-    dir = Directory(SITE_PKG)
+    dir = EggInfoDirectory(SITE_PKG)
 
     egg_info = dir.owner('mercurial/filelog.pyc')
     assert egg_info.name == 'mercurial'