Register data filters in a localrepo instead of util
authorPatrick Mezard <pmezard@gmail.com>
Mon, 28 Jan 2008 21:39:47 +0100
changeset 5966 11af38a592ae
parent 5965 abe373e16fe6
child 5967 f8ad3b76e923
Register data filters in a localrepo instead of util - Changing data filters implementation is easier, adddatafilter() can rewrap filter after inspecting their prototype - Custom data filters really belongs to localrepo, mixing them with generic wrapper like "pipefilter" or "tempfilter" looks wrong. - util.filtertable should not be accessed from extensions
hgext/win32text.py
mercurial/localrepo.py
--- a/hgext/win32text.py	Mon Jan 28 16:35:02 2008 +0100
+++ b/hgext/win32text.py	Mon Jan 28 21:39:47 2008 +0100
@@ -62,12 +62,12 @@
         return dumbencode(s, cmd)
     return s
 
-util.filtertable.update({
+_filters = {
     'dumbdecode:': dumbdecode,
     'dumbencode:': dumbencode,
     'cleverdecode:': cleverdecode,
     'cleverencode:': cleverencode,
-    })
+    }
 
 def forbidcrlf(ui, repo, hooktype, node, **kwargs):
     halt = False
@@ -99,3 +99,10 @@
                   '[decode]\n'
                   '** = cleverdecode:\n'))
     return halt
+
+def reposetup(ui, repo):
+    if not repo.local():
+        return
+    for name, fn in _filters.iteritems():
+        repo.adddatafilter(name, fn)
+
--- a/mercurial/localrepo.py	Mon Jan 28 16:35:02 2008 +0100
+++ b/mercurial/localrepo.py	Mon Jan 28 21:39:47 2008 +0100
@@ -83,6 +83,7 @@
         self.branchcache = None
         self.nodetagscache = None
         self.filterpats = {}
+        self._datafilters = {}
         self._transref = self._lockref = self._wlockref = None
 
     def __getattr__(self, name):
@@ -485,17 +486,27 @@
             l = []
             for pat, cmd in self.ui.configitems(filter):
                 mf = util.matcher(self.root, "", [pat], [], [])[1]
-                l.append((mf, cmd))
+                fn = None
+                for name, filterfn in self._datafilters.iteritems():
+                    if cmd.startswith(name): 
+                        fn = filterfn
+                        break
+                if not fn:
+                    fn = lambda s, c: util.filter(s, c)
+                l.append((mf, fn, cmd))
             self.filterpats[filter] = l
 
-        for mf, cmd in self.filterpats[filter]:
+        for mf, fn, cmd in self.filterpats[filter]:
             if mf(filename):
                 self.ui.debug(_("filtering %s through %s\n") % (filename, cmd))
-                data = util.filter(data, cmd)
+                data = fn(data, cmd)
                 break
 
         return data
 
+    def adddatafilter(self, name, filter):
+        self._datafilters[name] = filter
+
     def wread(self, filename):
         if self._link(filename):
             data = os.readlink(self.wjoin(filename))