hgkw/keyword.py
branchkwmap-templates
changeset 111 94315baadcaf
parent 110 b0b85b383f36
child 112 914455f5959a
--- a/hgkw/keyword.py	Thu Jan 18 01:50:18 2007 +0100
+++ b/hgkw/keyword.py	Thu Jan 18 02:17:10 2007 +0100
@@ -76,13 +76,19 @@
     '''Returns hgdate in cvs-like UTC format.'''
     return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))
 
-def getkwconfig(ui, repo):
-    inc = [pat for pat, opt in ui.configitems('keyword') if opt != 'ignore']
-    if not inc:
-        ui.warn(_('keyword: no filename globs for substitution\n'))
-        return None, None
-    exc = [pat for pat, opt in ui.configitems('keyword') if opt == 'ignore']
-    return inc, exc
+def getmodulename():
+    '''Makes sure pretxncommit-hook can import keyword module
+    regardless of where its located.'''
+    for k, v in sys.modules.iteritems():
+        if v is None:
+            continue
+        if not hasattr(v, '__file__'):
+            continue
+        if v.__file__.startswith(__file__):
+            return k
+    else:
+        sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
+        return os.path.splitext(os.path.basename(__file__))[0]
 
 
 class kwtemplater(object):
@@ -119,11 +125,14 @@
     if not repo.local():
         return
 
-    inc, exc = getkwconfig(ui, repo)
+    # get glob patterns to detect filenames
+    # for inclusion in or exclusion from keyword substitution
+    inc = [pat for pat, opt in ui.configitems('keyword') if opt != 'ignore']
     if not inc:
-        # no files configured for keyword substitution:
-        # no need to burden repo with extra ballast
+        ui.warn(_('keyword: no filename globs for substitution\n'))
         return
+    exc = [pat for pat, opt in ui.configitems('keyword') if opt == 'ignore']
+
 
     class kwrepo(repo.__class__):
         def file(self, f):
@@ -174,30 +183,17 @@
                 text = self._repo.kwt.re_kw.sub(r'$\1$', text)
             return super(kwfilelog, self).cmp(node, text)
 
+
     filelog.filelog = kwfilelog
     repo.__class__ = kwrepo
 
     # create filematching function once for repo
-    setattr(repo, 'kwfmatcher',
-            util.matcher(repo.root, inc=inc, exc=['.hg*']+exc)[1])
+    repo.kwfmatcher = util.matcher(repo.root, inc=inc, exc=['.hg*']+exc)[1]
     # initialize kwtemplater once for repo
-    setattr(repo, 'kwt', kwtemplater(ui, repo))
-
-    # make pretxncommit hook import kwmodule regardless of where it's located
-    for k, v in sys.modules.iteritems():
-        if v is None:
-            continue
-        if not hasattr(v, '__file__'):
-            continue
-        if v.__file__.startswith(__file__):
-            mod = k
-            break
-    else:
-        sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
-        mod = os.path.splitext(os.path.basename(__file__))[0]
-    ui.setconfig('hooks', 'pretxncommit.keyword', 'python:%s.pretxnkw' % mod)
-
-    del inc, exc, mod
+    repo.kwt = kwtemplater(ui, repo)
+    # configure pretxncommit hook
+    ui.setconfig('hooks', 'pretxncommit.keyword',
+            'python:%s.pretxnkw' % getmodulename())
 
 
 def pretxnkw(ui, repo, hooktype, **args):
@@ -219,7 +215,7 @@
         data = repo.wfile(f).read()
         if not util.binary(data):
             data, kwct = repo.kwt.re_kw.subn(lambda m:
-                    repo.kwt.expand(m, f, node), data)
+                            repo.kwt.expand(m, f, node), data)
             if kwct:
                 ui.debug(_('overwriting %s expanding keywords\n' % f))
                 repo.wfile(f, 'w').write(data)