##// END OF EJS Templates
templater: move stringify() to templateutil module...
Yuya Nishihara -
r36938:6ff6e1d6 default
parent child Browse files
Show More
@@ -124,6 +124,7 b' from . import ('
124 templatefilters,
124 templatefilters,
125 templatekw,
125 templatekw,
126 templater,
126 templater,
127 templateutil,
127 util,
128 util,
128 )
129 )
129 from .utils import dateutil
130 from .utils import dateutil
@@ -403,7 +404,7 b' class templateformatter(baseformatter):'
403 props['revcache'] = {}
404 props['revcache'] = {}
404 props = pycompat.strkwargs(props)
405 props = pycompat.strkwargs(props)
405 g = self._t(ref, **props)
406 g = self._t(ref, **props)
406 self._out.write(templater.stringify(g))
407 self._out.write(templateutil.stringify(g))
407
408
408 def end(self):
409 def end(self):
409 baseformatter.end(self)
410 baseformatter.end(self)
@@ -30,6 +30,7 b' from .. import ('
30 repoview,
30 repoview,
31 templatefilters,
31 templatefilters,
32 templater,
32 templater,
33 templateutil,
33 ui as uimod,
34 ui as uimod,
34 util,
35 util,
35 wireprotoserver,
36 wireprotoserver,
@@ -378,7 +379,7 b' class hgweb(object):'
378 try:
379 try:
379 rctx.tmpl = rctx.templater(req)
380 rctx.tmpl = rctx.templater(req)
380 ctype = rctx.tmpl('mimetype', encoding=encoding.encoding)
381 ctype = rctx.tmpl('mimetype', encoding=encoding.encoding)
381 ctype = templater.stringify(ctype)
382 ctype = templateutil.stringify(ctype)
382
383
383 # check read permissions non-static content
384 # check read permissions non-static content
384 if cmd != 'static':
385 if cmd != 'static':
@@ -34,6 +34,7 b' from .. import ('
34 pycompat,
34 pycompat,
35 scmutil,
35 scmutil,
36 templater,
36 templater,
37 templateutil,
37 ui as uimod,
38 ui as uimod,
38 util,
39 util,
39 )
40 )
@@ -370,7 +371,7 b' class hgwebdir(object):'
370 virtual = req.dispatchpath.strip('/')
371 virtual = req.dispatchpath.strip('/')
371 tmpl = self.templater(req, nonce)
372 tmpl = self.templater(req, nonce)
372 ctype = tmpl('mimetype', encoding=encoding.encoding)
373 ctype = tmpl('mimetype', encoding=encoding.encoding)
373 ctype = templater.stringify(ctype)
374 ctype = templateutil.stringify(ctype)
374
375
375 # Global defaults. These can be overridden by any handler.
376 # Global defaults. These can be overridden by any handler.
376 res.status = '200 Script output follows'
377 res.status = '200 Script output follows'
@@ -33,6 +33,7 b' from . import ('
33 smartset,
33 smartset,
34 templatekw,
34 templatekw,
35 templater,
35 templater,
36 templateutil,
36 util,
37 util,
37 )
38 )
38 from .utils import dateutil
39 from .utils import dateutil
@@ -449,13 +450,15 b' class changesettemplater(changesetprinte'
449 self._parts.update(m)
450 self._parts.update(m)
450
451
451 if self._parts['docheader']:
452 if self._parts['docheader']:
452 self.ui.write(templater.stringify(self.t(self._parts['docheader'])))
453 self.ui.write(
454 templateutil.stringify(self.t(self._parts['docheader'])))
453
455
454 def close(self):
456 def close(self):
455 if self._parts['docfooter']:
457 if self._parts['docfooter']:
456 if not self.footer:
458 if not self.footer:
457 self.footer = ""
459 self.footer = ""
458 self.footer += templater.stringify(self.t(self._parts['docfooter']))
460 self.footer += templateutil.stringify(
461 self.t(self._parts['docfooter']))
459 return super(changesettemplater, self).close()
462 return super(changesettemplater, self).close()
460
463
461 def _show(self, ctx, copies, props):
464 def _show(self, ctx, copies, props):
@@ -470,11 +473,12 b' class changesettemplater(changesetprinte'
470 # since there's inherently a conflict between header (across items) and
473 # since there's inherently a conflict between header (across items) and
471 # separator (per item)
474 # separator (per item)
472 if self._parts['separator'] and index > 0:
475 if self._parts['separator'] and index > 0:
473 self.ui.write(templater.stringify(self.t(self._parts['separator'])))
476 self.ui.write(
477 templateutil.stringify(self.t(self._parts['separator'])))
474
478
475 # write header
479 # write header
476 if self._parts['header']:
480 if self._parts['header']:
477 h = templater.stringify(self.t(self._parts['header'], **props))
481 h = templateutil.stringify(self.t(self._parts['header'], **props))
478 if self.buffered:
482 if self.buffered:
479 self.header[ctx.rev()] = h
483 self.header[ctx.rev()] = h
480 else:
484 else:
@@ -484,12 +488,12 b' class changesettemplater(changesetprinte'
484
488
485 # write changeset metadata, then patch if requested
489 # write changeset metadata, then patch if requested
486 key = self._parts[self._tref]
490 key = self._parts[self._tref]
487 self.ui.write(templater.stringify(self.t(key, **props)))
491 self.ui.write(templateutil.stringify(self.t(key, **props)))
488 self._showpatch(ctx)
492 self._showpatch(ctx)
489
493
490 if self._parts['footer']:
494 if self._parts['footer']:
491 if not self.footer:
495 if not self.footer:
492 self.footer = templater.stringify(
496 self.footer = templateutil.stringify(
493 self.t(self._parts['footer'], **props))
497 self.t(self._parts['footer'], **props))
494
498
495 def templatespec(tmpl, mapfile):
499 def templatespec(tmpl, mapfile):
@@ -18,6 +18,7 b' from . import ('
18 pycompat,
18 pycompat,
19 registrar,
19 registrar,
20 templatekw,
20 templatekw,
21 templateutil,
21 url,
22 url,
22 util,
23 util,
23 )
24 )
@@ -376,18 +377,7 b' def stringify(thing):'
376 """Any type. Turns the value into text by converting values into
377 """Any type. Turns the value into text by converting values into
377 text and concatenating them.
378 text and concatenating them.
378 """
379 """
379 thing = templatekw.unwraphybrid(thing)
380 return templateutil.stringify(thing)
380 if util.safehasattr(thing, '__iter__') and not isinstance(thing, bytes):
381 if isinstance(thing, str):
382 # This is only reachable on Python 3 (otherwise
383 # isinstance(thing, bytes) would have been true), and is
384 # here to prevent infinite recursion bugs on Python 3.
385 raise error.ProgrammingError(
386 'stringify got unexpected unicode string: %r' % thing)
387 return "".join([stringify(t) for t in thing if t is not None])
388 if thing is None:
389 return ""
390 return pycompat.bytestr(thing)
391
381
392 @templatefilter('stripdir')
382 @templatefilter('stripdir')
393 def stripdir(text):
383 def stripdir(text):
@@ -1118,8 +1118,6 b' def expandaliases(tree, aliases):'
1118
1118
1119 # template engine
1119 # template engine
1120
1120
1121 stringify = templatefilters.stringify
1122
1123 def _flatten(thing):
1121 def _flatten(thing):
1124 '''yield a single stream from a possibly nested set of iterators'''
1122 '''yield a single stream from a possibly nested set of iterators'''
1125 thing = templatekw.unwraphybrid(thing)
1123 thing = templatekw.unwraphybrid(thing)
@@ -1366,7 +1364,7 b' class templater(object):'
1366 def render(self, mapping):
1364 def render(self, mapping):
1367 """Render the default unnamed template and return result as string"""
1365 """Render the default unnamed template and return result as string"""
1368 mapping = pycompat.strkwargs(mapping)
1366 mapping = pycompat.strkwargs(mapping)
1369 return stringify(self('', **mapping))
1367 return templateutil.stringify(self('', **mapping))
1370
1368
1371 def __call__(self, t, **mapping):
1369 def __call__(self, t, **mapping):
1372 mapping = pycompat.byteskwargs(mapping)
1370 mapping = pycompat.byteskwargs(mapping)
@@ -13,7 +13,6 b' from .i18n import _'
13 from . import (
13 from . import (
14 error,
14 error,
15 pycompat,
15 pycompat,
16 templatefilters,
17 templatekw,
16 templatekw,
18 util,
17 util,
19 )
18 )
@@ -24,6 +23,21 b' class ResourceUnavailable(error.Abort):'
24 class TemplateNotFound(error.Abort):
23 class TemplateNotFound(error.Abort):
25 pass
24 pass
26
25
26 def stringify(thing):
27 """Turn values into bytes by converting into text and concatenating them"""
28 thing = templatekw.unwraphybrid(thing)
29 if util.safehasattr(thing, '__iter__') and not isinstance(thing, bytes):
30 if isinstance(thing, str):
31 # This is only reachable on Python 3 (otherwise
32 # isinstance(thing, bytes) would have been true), and is
33 # here to prevent infinite recursion bugs on Python 3.
34 raise error.ProgrammingError(
35 'stringify got unexpected unicode string: %r' % thing)
36 return "".join([stringify(t) for t in thing if t is not None])
37 if thing is None:
38 return ""
39 return pycompat.bytestr(thing)
40
27 def findsymbolicname(arg):
41 def findsymbolicname(arg):
28 """Find symbolic name for the given compiled expression; returns None
42 """Find symbolic name for the given compiled expression; returns None
29 if nothing found reliably"""
43 if nothing found reliably"""
@@ -223,5 +237,3 b' def getdictitem(dictarg, key):'
223 if val is None:
237 if val is None:
224 return
238 return
225 return templatekw.wraphybridvalue(dictarg, key, val)
239 return templatekw.wraphybridvalue(dictarg, key, val)
226
227 stringify = templatefilters.stringify
@@ -1,7 +1,10 b''
1
1
2 $ cat > engine.py << EOF
2 $ cat > engine.py << EOF
3 >
3 >
4 > from mercurial import templater
4 > from mercurial import (
5 > templater,
6 > templateutil,
7 > )
5 >
8 >
6 > class mytemplater(object):
9 > class mytemplater(object):
7 > def __init__(self, loader, filters, defaults, resources, aliases):
10 > def __init__(self, loader, filters, defaults, resources, aliases):
@@ -31,7 +34,7 b''
31 > v = v(**props)
34 > v = v(**props)
32 > elif callable(v):
35 > elif callable(v):
33 > v = v(self, props)
36 > v = v(self, props)
34 > v = templater.stringify(v)
37 > v = templateutil.stringify(v)
35 > tmpl = tmpl.replace('{{%s}}' % k, v)
38 > tmpl = tmpl.replace('{{%s}}' % k, v)
36 > yield tmpl
39 > yield tmpl
37 >
40 >
General Comments 0
You need to be logged in to leave comments. Login now