[meld] Create a CachedVc class to gather all common code



commit b86bfd15f24c8d83b30b52c8ad25616a8248500a
Author: Vincent Legoll <vincent legoll gmail com>
Date:   Tue Apr 21 20:48:33 2009 +0200

    Create a CachedVc class to gather all common code
    duplicated between VC plugins that cache repository
    status informations.
    
    There are slight behaviour changes:
    
    bzr, monotone & git used to have a get_tree() which
    always called lookup_tree() if it was not already cached,
    but without caching at that time either. I think this was
    wrong and only cut'n'pasted between plugins. But
    would like to hear from the creators of those plugins.
    
    The remaining of the patch is mostly renaming and
    passing an additional "directory" argument to
    _lookup_tree_cache() as was required for bzr, but
    which remains unused for other plugins.
---
 vc/_vc.py      |   21 +++++++++++++++++++++
 vc/bzr.py      |   23 +++--------------------
 vc/darcs.py    |   21 +++------------------
 vc/git.py      |   22 +++-------------------
 vc/monotone.py |   19 +++----------------
 vc/tla.py      |   21 +++------------------
 6 files changed, 36 insertions(+), 91 deletions(-)

diff --git a/vc/_vc.py b/vc/_vc.py
index 4881861..248e4fa 100644
--- a/vc/_vc.py
+++ b/vc/_vc.py
@@ -163,3 +163,24 @@ class Vc(object):
 
     def _get_dirsandfiles(self, directory, dirs, files):
         raise NotImplementedError()
+
+
+class CachedVc(Vc):
+
+    def __init__(self, location):
+        super(CachedVc, self).__init__(location)
+        self._tree_cache = None
+
+    def cache_inventory(self, directory):
+        self._tree_cache = self._lookup_tree_cache(directory)
+
+    def uncache_inventory(self):
+        self._tree_cache = None
+
+    def _lookup_tree_cache(self, directory):
+        raise NotImplementedError()
+
+    def _get_tree_cache(self, directory):
+        if self._tree_cache is None:
+            self.cache_inventory(directory)
+        return self._tree_cache
diff --git a/vc/bzr.py b/vc/bzr.py
index 7aa4c06..7c929c4 100644
--- a/vc/bzr.py
+++ b/vc/bzr.py
@@ -26,7 +26,7 @@ import os
 import errno
 import _vc
 
-class Vc(_vc.Vc):
+class Vc(_vc.CachedVc):
 
     CMD = "bzr"
     NAME = "Bazaar-NG"
@@ -34,10 +34,6 @@ class Vc(_vc.Vc):
     PATCH_STRIP_NUM = 0
     PATCH_INDEX_RE = "^=== modified file '(.*)'$"
 
-    def __init__(self, location):
-        self._tree_cache = None
-        super(Vc, self).__init__(location)
-
     def commit_command(self, message):
         return [self.CMD,"commit","-m",message]
     def diff_command(self):
@@ -53,13 +49,7 @@ class Vc(_vc.Vc):
     def get_working_directory(self, workdir):
         return self.root
 
-    def cache_inventory(self, rootdir):
-        self._tree_cache = self.lookup_tree(rootdir)
-
-    def uncache_inventory(self):
-        self._tree_cache = None
-
-    def lookup_tree(self, rootdir):
+    def _lookup_tree_cache(self, rootdir):
         branch_root = os.popen("%s root %s" % (self.CMD, rootdir)).read().rstrip('\n')
         while 1:
             try:
@@ -88,15 +78,8 @@ class Vc(_vc.Vc):
                     tree_state[os.path.join(rootdir, entry[2:])] = cur_state
         return tree_state
 
-    def get_tree(self, directory):
-        if self._tree_cache is None:
-            return self.lookup_tree(directory)
-        else:
-            return self._tree_cache
-        
     def _get_dirsandfiles(self, directory, dirs, files):
-
-        tree = self.get_tree(directory)
+        tree = self._get_tree_cache(directory)
 
         retfiles = []
         retdirs = []
diff --git a/vc/darcs.py b/vc/darcs.py
index 2a37314..cd48ed3 100644
--- a/vc/darcs.py
+++ b/vc/darcs.py
@@ -33,7 +33,7 @@ STATES = {
     "R": _vc.STATE_REMOVED
 }
 
-class Vc(_vc.Vc):
+class Vc(_vc.CachedVc):
 
     CMD = "darcs"
     NAME = "Darcs"
@@ -41,10 +41,6 @@ class Vc(_vc.Vc):
     PATCH_STRIP_NUM = 1
     PATCH_INDEX_RE = "--- old.+?/(.+?)\\t+.*[0-9]{4}$"
 
-    def __init__(self, location):
-        self._cached_statuses = None
-        super(Vc, self).__init__(location)
-
     def commit_command(self, message):
         return [self.CMD, "record",
                 "--skip-long-comment",
@@ -73,25 +69,14 @@ class Vc(_vc.Vc):
 
     def get_working_directory(self, workdir):
         return self.root
- 
-    def cache_inventory(self, rootdir):
-        self._cached_statuses = self._calculate_statuses()
-
-    def uncache_inventory(self):
-        self._cached_statuses = None
 
     def _get_dirsandfiles(self, directory, dirs, files):
-        whatsnew = self._get_cached_statuses()
+        whatsnew = self._get_tree_cache(directory)
         retfiles, retdirs = (self._get_statuses(whatsnew, files, _vc.File),
                              self._get_statuses(whatsnew, dirs, _vc.Dir))
         return retfiles, retdirs
 
-    def _get_cached_statuses(self):
-        if self._cached_statuses is None:
-            self._cached_statuses = self._calculate_statuses()
-        return self._cached_statuses
-    
-    def _calculate_statuses(self):
+    def _lookup_tree_cache(self, rootdir):
         non_boring = self._get_whatsnew()
         boring = self._get_whatsnew(boring=True)
         for path in boring:
diff --git a/vc/git.py b/vc/git.py
index 54db228..f5c42f3 100644
--- a/vc/git.py
+++ b/vc/git.py
@@ -31,7 +31,7 @@ import os
 import errno
 import _vc
 
-class Vc(_vc.Vc):
+class Vc(_vc.CachedVc):
 
     CMD = "git"
     NAME = "Git"
@@ -39,10 +39,6 @@ class Vc(_vc.Vc):
     PATCH_STRIP_NUM = 1
     PATCH_INDEX_RE = "^diff --git a/(.*) b/.*$"
 
-    def __init__(self, location):
-        self._tree_cache = None
-        super(Vc, self).__init__(location)
-
     def commit_command(self, message):
         return [self.CMD,"commit","-m",message]
     def diff_command(self):
@@ -61,13 +57,7 @@ class Vc(_vc.Vc):
         else:
             return ''
 
-    def cache_inventory(self, topdir):
-        self._tree_cache = self.lookup_tree()
-
-    def uncache_inventory(self):
-        self._tree_cache = None
-
-    def lookup_tree(self):
+    def _lookup_tree_cache(self, rootdir):
         while 1:
             try:
                 proc = os.popen("cd %s && %s status --untracked-files" % (self.root, self.CMD))
@@ -110,15 +100,9 @@ class Vc(_vc.Vc):
                     tree_state[os.path.join(self.root, dst)] = _vc.STATE_NEW
         return tree_state
 
-    def get_tree(self):
-        if self._tree_cache is None:
-            return self.lookup_tree()
-        else:
-            return self._tree_cache
-
     def _get_dirsandfiles(self, directory, dirs, files):
 
-        tree = self.get_tree()
+        tree = self._get_tree_cache(directory)
 
         retfiles = []
         retdirs = []
diff --git a/vc/monotone.py b/vc/monotone.py
index 68cab1f..7fb4b75 100644
--- a/vc/monotone.py
+++ b/vc/monotone.py
@@ -26,14 +26,13 @@ import os
 import _vc
 import errno
 
-class Vc(_vc.Vc):
+class Vc(_vc.CachedVc):
     NAME = "Monotone"
     VC_METADATA = ['MT', '_MTN']
     PATCH_STRIP_NUM = 0
     PATCH_INDEX_RE = "^[+]{3,3} ([^  ]*)\t[0-9a-f]{40,40}$"
 
     def __init__(self, location):
-        self._tree_cache = None
         self.interface_version = 0.0
         self.choose_monotone_version()
         super(Vc, self).__init__(os.path.normpath(location))
@@ -69,13 +68,7 @@ class Vc(_vc.Vc):
     def get_working_directory(self, workdir):
         return self.root
 
-    def cache_inventory(self, rootdir):
-        self._tree_cache = self.lookup_tree()
-
-    def uncache_inventory(self):
-        self._tree_cache = None
-
-    def lookup_tree(self):
+    def _lookup_tree_cache(self, rootdir):
         while 1:
             try:
                 entries = os.popen("cd %s && %s automate inventory" %
@@ -228,15 +221,9 @@ class Vc(_vc.Vc):
 
         return tree_state
 
-    def get_tree(self):
-        if self._tree_cache is None:
-            return self.lookup_tree()
-        else:
-            return self._tree_cache
-
     def _get_dirsandfiles(self, directory, dirs, files):
 
-        tree = self.get_tree()
+        tree = self._get_tree_cache(directory)
 
         retfiles = []
         retdirs = []
diff --git a/vc/tla.py b/vc/tla.py
index 7610758..d5f7c34 100644
--- a/vc/tla.py
+++ b/vc/tla.py
@@ -57,7 +57,7 @@ STATES = {
     "-/": _vc.STATE_MODIFIED,
 }
 
-class Vc(_vc.Vc):
+class Vc(_vc.CachedVc):
 
     CMD = "tla"
     NAME = "Arch"
@@ -66,10 +66,6 @@ class Vc(_vc.Vc):
     PATCH_STRIP_NUM = 1
     PATCH_INDEX_RE = "--- orig/(.*)"
 
-    def __init__(self, location):
-        self._cached_statuses = None
-        super(Vc, self).__init__(location)
-
     def commit_command(self, message):
         return [self.CMD, "commit",
                 "-s", message]
@@ -92,25 +88,14 @@ class Vc(_vc.Vc):
 
     def get_working_directory(self, workdir):
         return self.root
- 
-    def cache_inventory(self, rootdir):
-        self._cached_statuses = self._calculate_statuses()
-
-    def uncache_inventory(self):
-        self._cached_statuses = None
 
     def _get_dirsandfiles(self, directory, dirs, files):
-        whatsnew = self._get_cached_statuses()
+        whatsnew = self._get_tree_cache(directory)
         retfiles, retdirs = (self._get_statuses(whatsnew, files, _vc.File),
                              self._get_statuses(whatsnew, dirs, _vc.Dir))
         return retfiles, retdirs
 
-    def _get_cached_statuses(self):
-        if self._cached_statuses is None:
-            self._cached_statuses = self._calculate_statuses()
-        return self._cached_statuses
-    
-    def _calculate_statuses(self):
+    def _lookup_tree_cache(self, rootdir):
         whatsnew = {}
         commandline = ('%s changes -d %s' % (self.CMD, self.root))
         while 1:



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]