Turn regexes and escaped keywords into a propertycache
authorChristian Ebert <blacktrash@gmx.net>
Fri, 05 Nov 2010 10:00:11 +0000
changeset 854 3450d7f7d1a3
parent 853 65ec857290ab
child 855 eca16536349a
child 859 d36150edc231
Turn regexes and escaped keywords into a propertycache
hgkw/keyword.py
--- a/hgkw/keyword.py	Tue Nov 02 14:04:21 2010 +0100
+++ b/hgkw/keyword.py	Fri Nov 05 10:00:11 2010 +0000
@@ -163,8 +163,6 @@
         self.match = match.match(repo.root, '', [], inc, exc)
         self.restrict = kwtools['hgcmd'] in restricted.split()
         self.record = False
-        self.re_kw = None
-        self.re_kwexp = None
 
         kwmaps = self.ui.configitems('keywordmaps')
         if kwmaps: # override default templates
@@ -176,21 +174,20 @@
                                         'svnisodate': svnisodate,
                                         'svnutcdate': svnutcdate})
 
+    @util.propertycache
     def escape(self):
-        '''Returns bar-separated list of keywords.'''
+        '''Returns bar-separated and escaped keywords.'''
         return '|'.join(map(re.escape, self.templates.keys()))
 
+    @util.propertycache
     def rekw(self):
-        '''Compiles regex for unexpanded keywords on demand.'''
-        if self.re_kw is None:
-            self.re_kw = re.compile(r'\$(%s)\$' % self.escape())
-        return self.re_kw
+        '''Returns regex for unexpanded keywords.'''
+        return re.compile(r'\$(%s)\$' % self.escape)
 
+    @util.propertycache
     def rekwexp(self):
-        '''Compiles regex for expanded keywords on demand.'''
-        if self.re_kwexp is None:
-            self.re_kwexp = re.compile(r'\$(%s): [^$\n\r]*? \$' % self.escape())
-        return self.re_kwexp
+        '''Returns regex for expanded keywords.'''
+        return re.compile(r'\$(%s): [^$\n\r]*? \$' % self.escape)
 
     def substitute(self, data, path, ctx, subfunc):
         '''Replaces keywords in data with expanded template.'''
@@ -213,7 +210,7 @@
         '''Returns data with keywords expanded.'''
         if not self.restrict and self.match(path) and not util.binary(data):
             ctx = self.linkctx(path, node)
-            return self.substitute(data, path, ctx, self.rekw().sub)
+            return self.substitute(data, path, ctx, self.rekw.sub)
         return data
 
     def iskwfile(self, cand, ctx):
@@ -231,7 +228,7 @@
         if self.restrict or expand and lookup:
             mf = ctx.manifest()
         lctx = ctx
-        re_kw = (self.restrict or rekw) and self.rekw() or self.rekwexp()
+        re_kw = (self.restrict or rekw) and self.rekw or self.rekwexp
         msg = (expand and _('overwriting %s expanding keywords\n')
                or _('overwriting %s shrinking keywords\n'))
         for f in candidates:
@@ -260,7 +257,7 @@
     def shrink(self, fname, text):
         '''Returns text with all keyword substitutions removed.'''
         if self.match(fname) and not util.binary(text):
-            return _shrinktext(text, self.rekwexp().sub)
+            return _shrinktext(text, self.rekwexp.sub)
         return text
 
     def shrinklines(self, fname, lines):
@@ -268,7 +265,7 @@
         if self.match(fname):
             text = ''.join(lines)
             if not util.binary(text):
-                return _shrinktext(text, self.rekwexp().sub).splitlines(True)
+                return _shrinktext(text, self.rekwexp.sub).splitlines(True)
         return lines
 
     def wread(self, fname, data):