hgkw/keyword.py
branchkwmap-templates
changeset 106 61afa140949e
parent 105 2a8b0a71c2ec
child 107 fe4a0495eca8
equal deleted inserted replaced
105:2a8b0a71c2ec 106:61afa140949e
    89     provides keyword expansion function.
    89     provides keyword expansion function.
    90     '''
    90     '''
    91     def __init__(self, ui, repo):
    91     def __init__(self, ui, repo):
    92         self.ui = ui
    92         self.ui = ui
    93         self.repo = repo
    93         self.repo = repo
    94         self.templates = (dict(self.ui.configitems('keywordmaps'))
    94         self.templates = dict(ui.configitems('keywordmaps')) or deftemplates
    95                 or deftemplates)
       
    96         self.re_kw = re.compile(r'\$(%s)[^$]*?\$' %
    95         self.re_kw = re.compile(r'\$(%s)[^$]*?\$' %
    97                 '|'.join(re.escape(k) for k in self.templates.keys()))
    96                 '|'.join(re.escape(k) for k in self.templates.keys()))
    98         self.t = cmdutil.changeset_templater(self.ui, self.repo,
    97         self.t = cmdutil.changeset_templater(ui, repo, False, '', False)
    99                 False, '', False)
       
   100 
    98 
   101     def expand(self, mobj, path, node):
    99     def expand(self, mobj, path, node):
   102         '''Expands keyword with corresponding template.'''
   100         '''Expands keyword with corresponding template.'''
   103         kw = mobj.group(1)
   101         kw = mobj.group(1)
   104         template = templater.parsestring(self.templates[kw], quoted=False)
   102         template = templater.parsestring(self.templates[kw], quoted=False)
   128         def __init__(self, opener, path, repo,
   126         def __init__(self, opener, path, repo,
   129                      defversion=revlog.REVLOG_DEFAULT_VERSION):
   127                      defversion=revlog.REVLOG_DEFAULT_VERSION):
   130             super(kwfilelog, self).__init__(opener, path, defversion)
   128             super(kwfilelog, self).__init__(opener, path, defversion)
   131             self._repo = repo
   129             self._repo = repo
   132             self._path = path
   130             self._path = path
   133             self.kwt = kwtemplater(ui, self._repo)
   131             # only init kwtemplater if needed
       
   132             if not isinstance(repo, int) and kwfmatches(ui, repo, [path]):
       
   133                 self.kwt = kwtemplater(ui, repo)
       
   134             else:
       
   135                 self.kwt = None
   134 
   136 
   135         def iskwcandidate(self, data):
   137         def iskwcandidate(self, data):
   136             '''Decides whether to act on keywords.'''
   138             '''Decides whether to act on keywords.'''
   137             return (kwfmatches(ui, self._repo, [self._path])
   139             return self.kwt is not None and not util.binary(data)
   138                     and not util.binary(data))
       
   139 
   140 
   140         def read(self, node):
   141         def read(self, node):
   141             '''Substitutes keywords when reading filelog.'''
   142             '''Substitutes keywords when reading filelog.'''
   142             data = super(kwfilelog, self).read(node)
   143             data = super(kwfilelog, self).read(node)
   143             if self.iskwcandidate(data):
   144             if self.iskwcandidate(data):