Show More
@@ -494,22 +494,32 b' def maketemplater(ui, tmpl, defaults=Non' | |||||
494 | t.cache[''] = tmpl |
|
494 | t.cache[''] = tmpl | |
495 | return t |
|
495 | return t | |
496 |
|
496 | |||
497 | def templateresources(ui, repo=None): |
|
497 | class templateresources(templater.resourcemapper): | |
498 |
""" |
|
498 | """Resource mapper designed for the default templatekw and function""" | |
499 | and function""" |
|
499 | ||
500 | resmap = { |
|
500 | def __init__(self, ui, repo=None): | |
501 | 'cache': {}, # for templatekw/funcs to store reusable data |
|
501 | self._resmap = { | |
502 | 'repo': repo, |
|
502 | 'cache': {}, # for templatekw/funcs to store reusable data | |
503 |
' |
|
503 | 'repo': repo, | |
504 | } |
|
504 | 'ui': ui, | |
|
505 | } | |||
505 |
|
506 | |||
506 | def getsome(context, mapping, key): |
|
507 | def knownkeys(self): | |
|
508 | return self._knownkeys | |||
|
509 | ||||
|
510 | def lookup(self, context, mapping, key): | |||
|
511 | get = self._gettermap.get(key) | |||
|
512 | if not get: | |||
|
513 | return None | |||
|
514 | return get(self, context, mapping, key) | |||
|
515 | ||||
|
516 | def _getsome(self, context, mapping, key): | |||
507 | v = mapping.get(key) |
|
517 | v = mapping.get(key) | |
508 | if v is not None: |
|
518 | if v is not None: | |
509 | return v |
|
519 | return v | |
510 | return resmap.get(key) |
|
520 | return self._resmap.get(key) | |
511 |
|
521 | |||
512 | def getctx(context, mapping, key): |
|
522 | def _getctx(self, context, mapping, key): | |
513 | ctx = mapping.get('ctx') |
|
523 | ctx = mapping.get('ctx') | |
514 | if ctx is not None: |
|
524 | if ctx is not None: | |
515 | return ctx |
|
525 | return ctx | |
@@ -517,20 +527,21 b' def templateresources(ui, repo=None):' | |||||
517 | if fctx is not None: |
|
527 | if fctx is not None: | |
518 | return fctx.changectx() |
|
528 | return fctx.changectx() | |
519 |
|
529 | |||
520 | def getrepo(context, mapping, key): |
|
530 | def _getrepo(self, context, mapping, key): | |
521 | ctx = getctx(context, mapping, 'ctx') |
|
531 | ctx = self._getctx(context, mapping, 'ctx') | |
522 | if ctx is not None: |
|
532 | if ctx is not None: | |
523 | return ctx.repo() |
|
533 | return ctx.repo() | |
524 | return getsome(context, mapping, key) |
|
534 | return self._getsome(context, mapping, key) | |
525 |
|
535 | |||
526 |
|
|
536 | _gettermap = { | |
527 | 'cache': getsome, |
|
537 | 'cache': _getsome, | |
528 | 'ctx': getctx, |
|
538 | 'ctx': _getctx, | |
529 | 'fctx': getsome, |
|
539 | 'fctx': _getsome, | |
530 | 'repo': getrepo, |
|
540 | 'repo': _getrepo, | |
531 | 'revcache': getsome, # per-ctx cache; set later |
|
541 | 'revcache': _getsome, # per-ctx cache; set later | |
532 | 'ui': getsome, |
|
542 | 'ui': _getsome, | |
533 | } |
|
543 | } | |
|
544 | _knownkeys = set(_gettermap.keys()) | |||
534 |
|
545 | |||
535 | def formatter(ui, out, topic, opts): |
|
546 | def formatter(ui, out, topic, opts): | |
536 | template = opts.get("template", "") |
|
547 | template = opts.get("template", "") |
@@ -48,6 +48,7 b' mappable' | |||||
48 |
|
48 | |||
49 | from __future__ import absolute_import, print_function |
|
49 | from __future__ import absolute_import, print_function | |
50 |
|
50 | |||
|
51 | import abc | |||
51 | import os |
|
52 | import os | |
52 |
|
53 | |||
53 | from .i18n import _ |
|
54 | from .i18n import _ | |
@@ -556,6 +557,26 b' def unquotestring(s):' | |||||
556 | return s |
|
557 | return s | |
557 | return s[1:-1] |
|
558 | return s[1:-1] | |
558 |
|
559 | |||
|
560 | class resourcemapper(object): | |||
|
561 | """Mapper of internal template resources""" | |||
|
562 | ||||
|
563 | __metaclass__ = abc.ABCMeta | |||
|
564 | ||||
|
565 | @abc.abstractmethod | |||
|
566 | def knownkeys(self): | |||
|
567 | """Return a set of supported resource keys""" | |||
|
568 | ||||
|
569 | @abc.abstractmethod | |||
|
570 | def lookup(self, context, mapping, key): | |||
|
571 | """Return a resource for the key if available; otherwise None""" | |||
|
572 | ||||
|
573 | class nullresourcemapper(resourcemapper): | |||
|
574 | def knownkeys(self): | |||
|
575 | return set() | |||
|
576 | ||||
|
577 | def lookup(self, context, mapping, key): | |||
|
578 | return None | |||
|
579 | ||||
559 | class engine(object): |
|
580 | class engine(object): | |
560 | '''template expansion engine. |
|
581 | '''template expansion engine. | |
561 |
|
582 | |||
@@ -586,7 +607,7 b' class engine(object):' | |||||
586 | if defaults is None: |
|
607 | if defaults is None: | |
587 | defaults = {} |
|
608 | defaults = {} | |
588 | if resources is None: |
|
609 | if resources is None: | |
589 |
resources = |
|
610 | resources = nullresourcemapper() | |
590 | self._defaults = defaults |
|
611 | self._defaults = defaults | |
591 | self._resources = resources |
|
612 | self._resources = resources | |
592 | self._aliasmap = _aliasrules.buildmap(aliases) |
|
613 | self._aliasmap = _aliasrules.buildmap(aliases) | |
@@ -595,7 +616,7 b' class engine(object):' | |||||
595 | def symbol(self, mapping, key): |
|
616 | def symbol(self, mapping, key): | |
596 | """Resolve symbol to value or function; None if nothing found""" |
|
617 | """Resolve symbol to value or function; None if nothing found""" | |
597 | v = None |
|
618 | v = None | |
598 | if key not in self._resources: |
|
619 | if key not in self._resources.knownkeys(): | |
599 | v = mapping.get(key) |
|
620 | v = mapping.get(key) | |
600 | if v is None: |
|
621 | if v is None: | |
601 | v = self._defaults.get(key) |
|
622 | v = self._defaults.get(key) | |
@@ -604,9 +625,7 b' class engine(object):' | |||||
604 | def resource(self, mapping, key): |
|
625 | def resource(self, mapping, key): | |
605 | """Return internal data (e.g. cache) used for keyword/function |
|
626 | """Return internal data (e.g. cache) used for keyword/function | |
606 | evaluation""" |
|
627 | evaluation""" | |
607 | v = None |
|
628 | v = self._resources.lookup(self, mapping, key) | |
608 | if key in self._resources: |
|
|||
609 | v = self._resources[key](self, mapping, key) |
|
|||
610 | if v is None: |
|
629 | if v is None: | |
611 | raise templateutil.ResourceUnavailable( |
|
630 | raise templateutil.ResourceUnavailable( | |
612 | _('template resource not available: %s') % key) |
|
631 | _('template resource not available: %s') % key) | |
@@ -717,7 +736,7 b' class templater(object):' | |||||
717 | - ``filters``: a dict of functions to transform a value into another. |
|
736 | - ``filters``: a dict of functions to transform a value into another. | |
718 | - ``defaults``: a dict of symbol values/functions; may be overridden |
|
737 | - ``defaults``: a dict of symbol values/functions; may be overridden | |
719 | by a ``mapping`` dict. |
|
738 | by a ``mapping`` dict. | |
720 |
- ``resources``: a |
|
739 | - ``resources``: a resourcemapper object to look up internal data | |
721 | (e.g. cache), inaccessible from user template. |
|
740 | (e.g. cache), inaccessible from user template. | |
722 | - ``cache``: a dict of preloaded template fragments. |
|
741 | - ``cache``: a dict of preloaded template fragments. | |
723 | - ``aliases``: a list of alias (name, replacement) pairs. |
|
742 | - ``aliases``: a list of alias (name, replacement) pairs. | |
@@ -729,8 +748,6 b' class templater(object):' | |||||
729 | filters = {} |
|
748 | filters = {} | |
730 | if defaults is None: |
|
749 | if defaults is None: | |
731 | defaults = {} |
|
750 | defaults = {} | |
732 | if resources is None: |
|
|||
733 | resources = {} |
|
|||
734 | if cache is None: |
|
751 | if cache is None: | |
735 | cache = {} |
|
752 | cache = {} | |
736 | self.cache = cache.copy() |
|
753 | self.cache = cache.copy() |
@@ -349,8 +349,8 b' def runsymbol(context, mapping, key, def' | |||||
349 | if callable(v) and getattr(v, '_requires', None) is None: |
|
349 | if callable(v) and getattr(v, '_requires', None) is None: | |
350 | # old templatekw: expand all keywords and resources |
|
350 | # old templatekw: expand all keywords and resources | |
351 | # (TODO: deprecate this after porting web template keywords to new API) |
|
351 | # (TODO: deprecate this after porting web template keywords to new API) | |
352 |
props = {k: |
|
352 | props = {k: context._resources.lookup(context, mapping, k) | |
353 |
for k |
|
353 | for k in context._resources.knownkeys()} | |
354 | # pass context to _showcompatlist() through templatekw._showlist() |
|
354 | # pass context to _showcompatlist() through templatekw._showlist() | |
355 | props['templ'] = context |
|
355 | props['templ'] = context | |
356 | props.update(mapping) |
|
356 | props.update(mapping) |
General Comments 0
You need to be logged in to leave comments.
Login now