[PATCH] Provide better context for custom Python encode/decode filters

Patrick Mézard pmezard at gmail.com
Sun Jan 27 10:13:11 CST 2008


Jesse Glick a écrit :
> Jesse Glick wrote:
>> # HG changeset patch
>> # User Jesse Glick <jesse.glick at sun.com>
>> # Date 1198297277 18000
>> # Node ID dbf62abe9c0fba6559c226ded72251507e742891
>> # Parent  1b4ed187319a26c2a6f01836266a8e9ae32705cc
>> Provide better context for custom Python encode/decode filters.
>> While some can function with just some text and an optional command name,
>> others may want a repository object, a ui object, and a file path.
>> Use the enhanced information to good effect in win32text.dumbdecode's warning.
> 
> Any update on whether this would be accepted? I don't think I have heard 
> any objections, it was just forgotten.
> 
> (Or Patrick Mézard's related patch in the same thread?)
> 
> Also see new comment on issue #302.

Hello,

Here is the rebased version of your patch assuming mine is pushed. Tests are unchanged and I won't paste them. It will be pushed if it looks good to you.
Main differences:
- exchanged filename/repo arguments positions. Most functions seem to take (ui, repo, ...).
- moved the inspect logic in localrepo._filter() and run it one at every filter creation.


diff --git a/hgext/win32text.py b/hgext/win32text.py
--- a/hgext/win32text.py
+++ b/hgext/win32text.py
@@ -30,18 +30,17 @@
 # regexp for single LF without CR preceding.
 re_single_lf = re.compile('(^|[^\r])\n', re.MULTILINE)
 
-def dumbdecode(s, cmd):
+def dumbdecode(s, cmd, ui=None, repo=None, filename=None, **kwargs):
     # warn if already has CRLF in repository.
     # it might cause unexpected eol conversion.
     # see issue 302:
     #   http://www.selenic.com/mercurial/bts/issue302
-    if '\r\n' in s:
-        u = ui.ui()
-        u.warn(_('WARNING: file in repository already has CRLF line ending \n'
-                 ' which does not need eol conversion by win32text plugin.\n'
-                 ' Please reconsider encode/decode setting in'
-                 ' mercurial.ini or .hg/hgrc\n'
-                 ' before next commit.\n'))
+    if '\r\n' in s and ui and filename and repo:
+        ui.warn(_('WARNING: %s already has CRLF line endings\n'
+                  'and does not need EOL conversion by the win32text plugin.\n'
+                  'Before your next commit, please reconsider your '
+                  'encode/decode settings in \nMercurial.ini or %s.\n') %
+                (filename, repo.join('hgrc')))
     # replace single LF to CRLF
     return re_single_lf.sub('\\1\r\n', s)
 
@@ -52,9 +51,9 @@
     if '\0' in s: return False
     return True
 
-def cleverdecode(s, cmd):
+def cleverdecode(s, cmd, **kwargs):
     if clevertest(s, cmd):
-        return dumbdecode(s, cmd)
+        return dumbdecode(s, cmd, **kwargs)
     return s
 
 def cleverencode(s, cmd):
diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -10,7 +10,7 @@
 import repo, changegroup
 import changelog, dirstate, filelog, manifest, context, weakref
 import re, lock, transaction, tempfile, stat, errno, ui
-import os, revlog, time, util, extensions, hook
+import os, revlog, time, util, extensions, hook, inspect
 
 class localrepository(repo.repository):
     capabilities = util.set(('lookup', 'changegroupsubset'))
@@ -492,14 +492,22 @@
                         fn = filterfn
                         break
                 if not fn:
-                    fn = lambda s, c: util.filter(s, c)
+                    def filterfn(s, cmd, **kwargs):
+                        return util.filter(s, cmd)
+                    fn = filterfn
+                # Wrap old filters not supporting **kwargs
+                if not inspect.getargspec(fn)[2]:
+                    oldfn = fn
+                    def compatfn(s, cmd, **kwargs):
+                        return oldfn(s, cmd)
+                    fn = compatfn
                 l.append((mf, fn, cmd))
             self.filterpats[filter] = l
 
         for mf, fn, cmd in self.filterpats[filter]:
             if mf(filename):
                 self.ui.debug(_("filtering %s through %s\n") % (filename, cmd))
-                data = fn(data, cmd)
+                data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
                 break
 
         return data




More information about the Mercurial-devel mailing list