Make path, node kwtemplater attribs kwmap-templates
authorChristian Ebert <blacktrash@gmx.net>
Fri, 20 Apr 2007 14:41:35 +0200
branchkwmap-templates
changeset 160 e45d804d28dd
parent 159 28fd5b5eb3ad
child 161 aac10ec0a8cd
Make path, node kwtemplater attribs Avoid lambda in keyword substitution.
hgkw/keyword.py
--- a/hgkw/keyword.py	Fri Apr 20 00:24:32 2007 +0200
+++ b/hgkw/keyword.py	Fri Apr 20 14:41:35 2007 +0200
@@ -121,9 +121,11 @@
     Sets up keyword templates, corresponding keyword regex, and
     provides keyword substitution functions.
     '''
-    def __init__(self, ui, repo):
+    def __init__(self, ui, repo, path='', node=None):
         self.ui = ui
         self.repo = repo
+        self.path = path
+        self.node = node
         templates = dict(ui.configitems('keywordmaps'))
         if templates:
             # parse templates here for less overhead in kwsub matchfunc
@@ -142,22 +144,22 @@
             self.t = cmdutil.changeset_templater(ui, repo,
                     False, None, '', False)
 
-    def kwsub(self, mobj, path, node):
+    def kwsub(self, mobj):
         '''Substitutes keyword using corresponding template.'''
         kw = mobj.group(1)
         self.t.use_template(self.templates[kw])
         self.ui.pushbuffer()
-        self.t.show(changenode=node, root=self.repo.root, file=path)
+        self.t.show(changenode=self.node, root=self.repo.root, file=self.path)
         keywordsub = templater.firstline(self.ui.popbuffer())
         return '$%s: %s $' % (kw, keywordsub)
 
-    def expand(self, path, node, filelog, data):
+    def expand(self, node, flog, data):
         '''Returns data with expanded keywords.'''
         if util.binary(data):
             return data
-        c = context.filectx(self.repo, path, fileid=node, filelog=filelog)
-        cnode = c.node()
-        return self.re_kw.sub(lambda m: self.kwsub(m, path, cnode), data)
+        c = context.filectx(self.repo, self.path, fileid=node, filelog=flog)
+        self.node = c.node()
+        return self.re_kw.sub(self.kwsub, data)
 
     def shrink(self, text):
         '''Returns text with all keyword substitutions removed.'''
@@ -165,14 +167,14 @@
             return text
         return self.re_kw.sub(r'$\1$', text)
 
-    def overwrite(self, candidates, node):
+    def overwrite(self, candidates):
         '''Overwrites candidates in working dir expanding keywords.'''
         files = []
         for f in candidates:
             data = self.repo.wfile(f).read()
             if not util.binary(data):
-                data, kwct = self.re_kw.subn(lambda m:
-                        self.kwsub(m, f, node), data)
+                self.path = f
+                data, kwct = self.re_kw.subn(self.kwsub, data)
                 if kwct:
                     self.ui.debug(_('overwriting %s expanding keywords\n') % f)
                     self.repo.wfile(f, 'w').write(data)
@@ -193,7 +195,7 @@
     def read(self, node):
         '''Substitutes keywords when reading filelog.'''
         data = super(kwfilelog, self).read(node)
-        return self.kwtemplater.expand(self.path, node, self, data)
+        return self.kwtemplater.expand(node, self, data)
 
     def add(self, text, meta, tr, link, p1=None, p2=None):
         '''Removes keyword substitutions when adding to filelog.'''
@@ -244,7 +246,7 @@
                 f = f[1:]
             # only use kwfilelog when needed
             if self.kwfmatcher(f):
-                kwt = kwtemplater(self.ui, self)
+                kwt = kwtemplater(self.ui, self, path=f)
                 return kwfilelog(self.sopener, f, kwt)
             else:
                 return filelog.filelog(self.sopener, f)
@@ -275,8 +277,8 @@
                 if not candidates:
                     return node
 
-                kwt = kwtemplater(self.ui, self)
-                kwt.overwrite(candidates, node)
+                kwt = kwtemplater(self.ui, self, node=node)
+                kwt.overwrite(candidates)
                 return node
             finally:
                 if wrelease: