comparison mercurial/formatter.py @ 32838:615ec3f14aa9

formatter: wrap (tmpl, mapfile) by named tuple I'm going to add more options to the templatespec tuple. cmdutil.logtemplatespec() is just an alias now, but it will be changed to a factory function later.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 22 Apr 2017 18:48:38 +0900
parents 9d76812f9b0b
children 57c13c0d1cde
comparison
equal deleted inserted replaced
32837:50586a0a946f 32838:615ec3f14aa9
101 baz: foo, bar 101 baz: foo, bar
102 """ 102 """
103 103
104 from __future__ import absolute_import 104 from __future__ import absolute_import
105 105
106 import collections
106 import contextlib 107 import contextlib
107 import itertools 108 import itertools
108 import os 109 import os
109 110
110 from .i18n import _ 111 from .i18n import _
371 props['repo'] = props['ctx'].repo() 372 props['repo'] = props['ctx'].repo()
372 props['revcache'] = {} 373 props['revcache'] = {}
373 g = self._t(self._topic, ui=self._ui, cache=self._cache, **props) 374 g = self._t(self._topic, ui=self._ui, cache=self._cache, **props)
374 self._out.write(templater.stringify(g)) 375 self._out.write(templater.stringify(g))
375 376
377 templatespec = collections.namedtuple(r'templatespec',
378 r'tmpl mapfile')
379
376 def lookuptemplate(ui, topic, tmpl): 380 def lookuptemplate(ui, topic, tmpl):
377 """Find the template matching the given -T/--template spec 'tmpl' 381 """Find the template matching the given -T/--template spec 'tmpl'
378 382
379 'tmpl' can be any of the following: 383 'tmpl' can be any of the following:
380 384
389 loaded from user config. 393 loaded from user config.
390 """ 394 """
391 395
392 # looks like a literal template? 396 # looks like a literal template?
393 if '{' in tmpl: 397 if '{' in tmpl:
394 return tmpl, None 398 return templatespec(tmpl, None)
395 399
396 # perhaps a stock style? 400 # perhaps a stock style?
397 if not os.path.split(tmpl)[0]: 401 if not os.path.split(tmpl)[0]:
398 mapname = (templater.templatepath('map-cmdline.' + tmpl) 402 mapname = (templater.templatepath('map-cmdline.' + tmpl)
399 or templater.templatepath(tmpl)) 403 or templater.templatepath(tmpl))
400 if mapname and os.path.isfile(mapname): 404 if mapname and os.path.isfile(mapname):
401 return None, mapname 405 return templatespec(None, mapname)
402 406
403 # perhaps it's a reference to [templates] 407 # perhaps it's a reference to [templates]
404 t = ui.config('templates', tmpl) 408 t = ui.config('templates', tmpl)
405 if t: 409 if t:
406 return templater.unquotestring(t), None 410 return templatespec(templater.unquotestring(t), None)
407 411
408 if tmpl == 'list': 412 if tmpl == 'list':
409 ui.write(_("available styles: %s\n") % templater.stylelist()) 413 ui.write(_("available styles: %s\n") % templater.stylelist())
410 raise error.Abort(_("specify a template")) 414 raise error.Abort(_("specify a template"))
411 415
412 # perhaps it's a path to a map or a template 416 # perhaps it's a path to a map or a template
413 if ('/' in tmpl or '\\' in tmpl) and os.path.isfile(tmpl): 417 if ('/' in tmpl or '\\' in tmpl) and os.path.isfile(tmpl):
414 # is it a mapfile for a style? 418 # is it a mapfile for a style?
415 if os.path.basename(tmpl).startswith("map-"): 419 if os.path.basename(tmpl).startswith("map-"):
416 return None, os.path.realpath(tmpl) 420 return templatespec(None, os.path.realpath(tmpl))
417 with util.posixfile(tmpl, 'rb') as f: 421 with util.posixfile(tmpl, 'rb') as f:
418 tmpl = f.read() 422 tmpl = f.read()
419 return tmpl, None 423 return templatespec(tmpl, None)
420 424
421 # constant string? 425 # constant string?
422 return tmpl, None 426 return templatespec(tmpl, None)
423 427
424 def loadtemplater(ui, topic, spec, cache=None): 428 def loadtemplater(ui, topic, spec, cache=None):
425 """Create a templater from either a literal template or loading from 429 """Create a templater from either a literal template or loading from
426 a map file""" 430 a map file"""
427 tmpl, mapfile = spec 431 assert not (spec.tmpl and spec.mapfile)
428 assert not (tmpl and mapfile) 432 if spec.mapfile:
429 if mapfile: 433 return templater.templater.frommapfile(spec.mapfile, cache=cache)
430 return templater.templater.frommapfile(mapfile, cache=cache) 434 return maketemplater(ui, topic, spec.tmpl, cache=cache)
431 return maketemplater(ui, topic, tmpl, cache=cache)
432 435
433 def maketemplater(ui, topic, tmpl, cache=None): 436 def maketemplater(ui, topic, tmpl, cache=None):
434 """Create a templater from a string template 'tmpl'""" 437 """Create a templater from a string template 'tmpl'"""
435 aliases = ui.configitems('templatealias') 438 aliases = ui.configitems('templatealias')
436 t = templater.templater(cache=cache, aliases=aliases) 439 t = templater.templater(cache=cache, aliases=aliases)