hgkw/keyword.py
changeset 377 ce5eb725d0c6
parent 368 82e2b3807073
child 378 0683aaf458d8
equal deleted inserted replaced
376:f44fb15a0f30 377:ce5eb725d0c6
   130     def substitute(self, path, data, node, subfunc):
   130     def substitute(self, path, data, node, subfunc):
   131         '''Obtains file's changenode if node not given,
   131         '''Obtains file's changenode if node not given,
   132         and calls given substitution function.'''
   132         and calls given substitution function.'''
   133         if node is None:
   133         if node is None:
   134             # kwrepo.wwrite except when overwriting on commit
   134             # kwrepo.wwrite except when overwriting on commit
   135             if self.ctx is None:
       
   136                 self.ctx = self.repo.changectx()
       
   137             try:
   135             try:
   138                 fnode = self.ctx.filenode(path)
   136                 fnode = self.ctx.filenode(path)
   139                 fl = self.repo.file(path)
   137                 fl = self.repo.file(path)
   140                 c = context.filectx(self.repo, path, fileid=fnode, filelog=fl)
   138                 c = context.filectx(self.repo, path, fileid=fnode, filelog=fl)
   141                 node = c.node()
   139                 node = c.node()
   152             ekw = templatefilters.firstline(self.ui.popbuffer())
   150             ekw = templatefilters.firstline(self.ui.popbuffer())
   153             return '$%s: %s $' % (kw, ekw)
   151             return '$%s: %s $' % (kw, ekw)
   154 
   152 
   155         return subfunc(kwsub, data)
   153         return subfunc(kwsub, data)
   156 
   154 
   157     def expand(self, path, data, node):
   155     def expand(self, path, data, ctx):
   158         '''Returns data with keywords expanded.'''
   156         '''Returns data with keywords expanded.'''
   159         if util.binary(data):
   157         if util.binary(data):
   160             return data
   158             return data
   161         return self.substitute(path, data, node, self.re_kw.sub)
   159         if self.ctx is None:
       
   160             self.ctx = ctx or self.repo.changectx()
       
   161         return self.substitute(path, data, None, self.re_kw.sub)
   162 
   162 
   163     def process(self, path, data, expand, ctx, node):
   163     def process(self, path, data, expand, ctx, node):
   164         '''Returns a tuple: data, count.
   164         '''Returns a tuple: data, count.
   165         Count is number of keywords/keyword substitutions,
   165         Count is number of keywords/keyword substitutions,
   166         telling caller whether to act on file containing data.'''
   166         telling caller whether to act on file containing data.'''
   244     %s   basename of file being printed
   244     %s   basename of file being printed
   245     %d   dirname of file being printed, or '.' if in repo root
   245     %d   dirname of file being printed, or '.' if in repo root
   246     %p   root-relative path name of file being printed
   246     %p   root-relative path name of file being printed
   247     '''
   247     '''
   248     ctx = repo.changectx(opts['rev'])
   248     ctx = repo.changectx(opts['rev'])
   249     try:
       
   250         repo._kwt.ctx = ctx
       
   251         kw = True
       
   252     except AttributeError:
       
   253         kw = False
       
   254     err = 1
   249     err = 1
   255     for src, abs, rel, exact in cmdutil.walk(repo, (file1,) + pats, opts,
   250     for src, abs, rel, exact in cmdutil.walk(repo, (file1,) + pats, opts,
   256                                              ctx.node()):
   251                                              ctx.node()):
   257         fp = cmdutil.make_file(repo, opts['output'], ctx.node(), pathname=abs)
   252         fp = cmdutil.make_file(repo, opts['output'], ctx.node(), pathname=abs)
   258         data = ctx.filectx(abs).data()
   253         data = ctx.filectx(abs).data()
   259         if kw and repo._kwt.matcher(abs):
   254         try:
   260             data = repo._kwt.expand(abs, data, None)
   255             if repo._kwt.matcher(abs):
       
   256                 data = repo._kwt.expand(abs, data, ctx)
       
   257         except AttributeError:
       
   258             pass
   261         fp.write(data)
   259         fp.write(data)
   262         err = 0
   260         err = 0
   263     return err
   261     return err
   264 
   262 
   265 def demo(ui, repo, *args, **opts):
   263 def demo(ui, repo, *args, **opts):
   492             req.respond(webcommands.HTTP_OK, web.ctype)
   490             req.respond(webcommands.HTTP_OK, web.ctype)
   493             return content
   491             return content
   494         path = fctx.path()
   492         path = fctx.path()
   495         text = fctx.data()
   493         text = fctx.data()
   496         if kwt.matcher(path):
   494         if kwt.matcher(path):
   497             text = kwt.expand(path, text, fctx.node())
   495             text = kwt.expand(path, text, web.changectx(req))
   498         mt = mimetypes.guess_type(path)[0]
   496         mt = mimetypes.guess_type(path)[0]
   499         if mt is None or util.binary(text):
   497         if mt is None or util.binary(text):
   500             mt = mt or 'application/octet-stream'
   498             mt = mt or 'application/octet-stream'
   501         req.respond(webcommands.HTTP_OK, mt, path, len(text))
   499         req.respond(webcommands.HTTP_OK, mt, path, len(text))
   502         return [text]
   500         return [text]