Commits

ta...@MacZiade.local  committed a6efc5d

now using cache in EggInfoDirectories

  • Participants
  • Parent commits 7eb4f89

Comments (0)

Files changed (1)

 import csv
 import sys
 import re
+import threading
 
 from distutils.dist  import  DistributionMetadata
 
 # Directories is a collection of directories, initialized with a
 # list of paths.
 #
-class EggInfoDirectories(list):
+class EggInfoDirectories(object):
 
-    def __init__(self, paths=sys.path):
-        super(EggInfoDirectories, self).__init__()
+    def __init__(self, paths=sys.path, cacheable=True):
+        self._cacheable = True
+        self._cached_dirs = {}
+        self._paths = []
+        self._egg_infos = []
+
+    #
+    # cache managment
+    #
+    def _set_cacheable(self, value):
+        if not value:
+            self._cache_dirs.clear()
+        self._cacheable = value
+
+    def _get_cacheable(self):
+        return self._cacheable
+
+    cacheable = property(_get_cacheable, _set_cacheable)
+
+    def purge_cache(self):
+        self._cached_dirs.clear()
+
+    #
+    # container APIs
+    #
+    def __iter__(self):
+        return iter(self._egg_infos)
+
+    def append(self, egg_info):
+        self._egg_infos.append(egg_info)
+        self._paths.append(egg_info.path)
+        if self.cacheable:
+            self._cached_dirs[egg_info.path] = egg_info
+
+    def clear(self):
+        self._egg_infos.clear()
+        self._paths.clear()
+
+    def remove(self, egg_info):
+        self._egg_infos.remove(egg_info)
+        self._paths.remove(path)
+
+    #
+    # public APIs
+    #
+    def load(self, paths):
         for path in paths:
-            if os.path.isdir(path):
+            if path in self._paths:
+                continue
+            if self.cacheable and path in self._cached_dirs:
+                self.append(self._cached_dirs[path])
+            elif os.path.isdir(path):
                 self.append(EggInfoDirectory(path))
 
+    def reload(self):
+        paths = [d.path for d in self]
+        self.clear()
+        self.load(paths)
+
     def get_egg_infos(self):
         """Iterates on all .egg-info directories founded in sys.path.
 
                 yield egg_info
 
 #
-# high level APIs
+# high level APIs with a global EggInfoDirectories instance
 #
 
+egg_infos = EggInfoDirectories()
+
 def get_egg_infos(paths=sys.path):
-    dirs = EggInfoDirectories(paths)
-    return dirs.get_egg_infos()
+    egg_infos.load(paths)
+    return egg_infos.get_egg_infos()
 
 def get_egg_info(project_name, paths=sys.path):
-    dirs = EggInfoDirectories(paths)
-    return dirs.get_egg_info(project_name)
+    egg_infos.load(paths)
+    return egg_infos.get_egg_info(project_name)
 
 def get_file_users(path, paths=sys.path):
-    dirs = EggInfoDirectories(paths)
-    return dirs.get_file_users(path)
+    egg_infos.load(paths)
+    return egg_infos.get_file_users(path)
 
 def egg_info_dirname(name, version):
     name = re.sub('[^A-Za-z0-9.]+', '_', name)