e3db619bb352

Refactor the _incoming functionality into something that outgoing can share.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Tue, 14 Jul 2009 22:11:54 -0400
parents 68ca814becdc
children 7a87d26c99ac
branches/tags (none)
files prompt.py

Changes

diff -r 68ca814becdc -r e3db619bb352 prompt.py
--- a/prompt.py	Tue Jul 14 22:08:26 2009 -0400
+++ b/prompt.py	Tue Jul 14 22:11:54 2009 -0400
@@ -97,30 +97,32 @@
     def _basename(m):
         return _with_groups(m.groups(), path.basename(repo.root)) if repo.root else ''
     
-    def _incoming(m):
-        g = m.groups()
-        out_g = (g[0],) + (g[-1],)
-        
-        cache_dir = path.join(repo.root, CACHE_PATH)
-        cache = path.join(cache_dir, 'incoming')
-        if not path.isdir(cache_dir):
-            os.makedirs(cache_dir)
-        
-        cache_exists = path.isfile(cache)
-        
-        cache_time = datetime.fromtimestamp(os.stat(cache).st_mtime)
-        if not cache_exists or cache_time < datetime.now() - CACHE_TIMEOUT:
-            subprocess.Popen(['hg', 'prompt', '--cache-incoming'])
-        
-        if cache_exists:
-            with open(cache) as c:
-                count = len(c.readlines())
-                if g[1]:
-                    return _with_groups(out_g, str(count)) if count else ''
-                else:
-                    return _with_groups(out_g, '') if count else ''
-        else:
-            return ''
+    def _remote(kind):
+        def _r(m):
+            g = m.groups()
+            out_g = (g[0],) + (g[-1],)
+            
+            cache_dir = path.join(repo.root, CACHE_PATH)
+            cache = path.join(cache_dir, kind)
+            if not path.isdir(cache_dir):
+                os.makedirs(cache_dir)
+            
+            cache_exists = path.isfile(cache)
+            
+            cache_time = datetime.fromtimestamp(os.stat(cache).st_mtime)
+            if not cache_exists or cache_time < datetime.now() - CACHE_TIMEOUT:
+                subprocess.Popen(['hg', 'prompt', '--cache-%s' % kind])
+            
+            if cache_exists:
+                with open(cache) as c:
+                    count = len(c.readlines())
+                    if g[1]:
+                        return _with_groups(out_g, str(count)) if count else ''
+                    else:
+                        return _with_groups(out_g, '') if count else ''
+            else:
+                return ''
+        return _r
     
     def _outgoing(m):
         g = m.groups()
@@ -145,7 +147,7 @@
         'bookmark': _bookmark,
         'root': _root,
         'root\|basename': _basename,
-        'incoming(\|count)?': _incoming,
+        'incoming(\|count)?': _remote('incoming'),
         'outgoing(\|count)?': _outgoing,
     }