Make keyword.py depend on resurrected kwutil.py
This makes pretxnkw perhaps a bit slower, but is more readable.
--- a/hgkw/keyword.py Thu Dec 21 11:02:21 2006 +0100
+++ b/hgkw/keyword.py Thu Dec 21 16:00:45 2006 +0100
@@ -1,9 +1,13 @@
-from mercurial import hg, filelog, revlog, context, util
-import os.path, re
-
-hgkeywords = 'Id|Header|Author|Date|Revision|RCSFile|Source'
+from mercurial.i18n import _
+from mercurial import filelog, revlog, context, util
+import re
def reposetup(ui, repo):
+ try:
+ from hgkw import kwutil
+ except ImportError, e:
+ raise util.Abort(_('%s\nkeyword extension needs package hgkw\n') % e)
+
if not repo.local():
return
@@ -36,31 +40,20 @@
mf = util.matcher(self._repo.root,
'', [pat], [], [])[1]
if mf(f):
- re_kw = re.compile(r'\$(%s)\$' % hgkeywords)
def kwexpand(matchobj):
- RCSFile = os.path.basename(f)+',v'
- Source = os.path.join(self._repo.root, f)+',v'
- Revision = c.changectx()
- Date = util.datestr(date=c.date())
- Author = c.user()
- revdateauth = '%s %s %s' % (
- Revision,
- util.datestr(date=c.date(),
- format=util.defaultdateformats[0]),
- util.shortuser(Author))
- Header = '%s %s' % (Source, revdateauth)
- Id = '%s %s' % (RCSFile, revdateauth)
- return '$%s: %s $' % (
- matchobj.group(1), eval(matchobj.group(1)))
+ return kwutil.kwexpand(matchobj,
+ self._repo, c.changectx(), f,
+ c.date(), c.user())
+ re_kw = re.compile(r'\$(%s)\$' % kwutil.hgkeywords)
return re_kw.sub(kwexpand, data)
return data
def add(self, text, meta, tr, link, p1=None, p2=None):
if (not util.binary(text) and
self._repo.ui.config('keyword', 'remove', True)):
- re_kw = re.compile(r'\$(%s): [^$]+? \$' % hgkeywords)
+ re_kw = re.compile(r'\$(%s): [^$]+? \$' % kwutil.hgkeywords)
text = re_kw.sub(r'$\1$', text)
return super(kwfilelog, self).add(text, meta, tr, link, p1, p2)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/hgkw/kwutil.py Thu Dec 21 16:00:45 2006 +0100
@@ -0,0 +1,26 @@
+'''
+kwutil provides required little helpers for the Mercurial
+keyword extension and the Python pretxncommit hook pretxnkw.
+'''
+
+from mercurial import util
+import os.path
+
+# supported keywords for use in regexes
+hgkeywords = 'Id|Header|Author|Date|Revision|RCSFile|Source'
+
+def kwexpand(matchobj, repo, Revision, f, date, Author):
+ '''Called by keyword extension and pretxnkw pretxncomit hook,
+ sets supported keywords as local variables and evaluates them
+ to their expansion if matchobj is equal to their string
+ representation.'''
+ RCSFile = os.path.basename(f)+',v'
+ Source = os.path.join(repo.root, f)+',v'
+ Date = util.datestr(date)
+ revdateauth = '%s %s %s' % (Revision,
+ util.datestr(date=date, format=util.defaultdateformats[0]),
+ # %Y-%m-%d %H:%M:%S
+ util.shortuser(Author))
+ Header = '%s %s' % (Source, revdateauth)
+ Id = '%s %s' % (RCSFile, revdateauth)
+ return '$%s: %s $' % (matchobj.group(1), eval(matchobj.group(1)))
--- a/hgkw/pretxnkw.py Thu Dec 21 11:02:21 2006 +0100
+++ b/hgkw/pretxnkw.py Thu Dec 21 16:00:45 2006 +0100
@@ -1,8 +1,7 @@
+from hgkw import kwutil
from mercurial.i18n import _
from mercurial import cmdutil, commands, util
-import os.path, re, sys
-
-hgkeywords = 'Id|Header|Author|Date|Revision|RCSFile|Source'
+import re, sys
def pretxnkw(ui, repo, hooktype, **args):
'''Collects candidates for keyword expansion on commit
@@ -43,28 +42,15 @@
return False
user, date = repo.changelog.read(repo.changelog.tip())[1:3]
- strdate = util.datestr(date=date)
- shortuser = util.shortuser(user)
- shortdate = util.datestr(date=date, format=util.defaultdateformats[0])
- # %Y-%m-%d %H:%M:%S
-
- re_kw = re.compile(r'\$(%s)(: [^$]+? )?\$' % hgkeywords)
+ re_kw = re.compile(r'\$(%s)(: [^$]+? )?\$' % kwutil.hgkeywords)
for f in files:
data = repo.wfile(f).read()
if not util.binary(data):
def kwexpand(matchobj):
- RCSFile = os.path.basename(f)+',v'
- Source = os.path.join(repo.root, f)+',v'
- Revision = args['node'][:12]
- Date = strdate
- Author = user
- revdateauth = '%s %s %s' % (Revision, shortdate, shortuser)
- Header = '%s %s' % (Source, revdateauth)
- Id = '%s %s' % (RCSFile, revdateauth)
- return '$%s: %s $' % (
- matchobj.group(1), eval(matchobj.group(1)))
+ return kwutil.kwexpand(matchobj,
+ repo, args['node'][:12], f, date, user)
data, kwct = re_kw.subn(kwexpand, data)
if kwct: