comparison mercurial/templater.py @ 39582:28f974d83c0a

templater: remove unused context argument from most resourcemapper functions While working on demand loading of ctx/fctx objects, I noticed that it's quite easy to create infinite recursion by carelessly using the template context in the resource mapper. Let's make that not happen.
author Yuya Nishihara <yuya@tcha.org>
date Thu, 07 Jun 2018 23:27:54 +0900
parents 3813c6b7337c
children 8fa26f3baf30
comparison
equal deleted inserted replaced
39581:68ce242c8b4b 39582:28f974d83c0a
546 """Mapper of internal template resources""" 546 """Mapper of internal template resources"""
547 547
548 __metaclass__ = abc.ABCMeta 548 __metaclass__ = abc.ABCMeta
549 549
550 @abc.abstractmethod 550 @abc.abstractmethod
551 def availablekeys(self, context, mapping): 551 def availablekeys(self, mapping):
552 """Return a set of available resource keys based on the given mapping""" 552 """Return a set of available resource keys based on the given mapping"""
553 553
554 @abc.abstractmethod 554 @abc.abstractmethod
555 def knownkeys(self): 555 def knownkeys(self):
556 """Return a set of supported resource keys""" 556 """Return a set of supported resource keys"""
557 557
558 @abc.abstractmethod 558 @abc.abstractmethod
559 def lookup(self, context, mapping, key): 559 def lookup(self, mapping, key):
560 """Return a resource for the key if available; otherwise None""" 560 """Return a resource for the key if available; otherwise None"""
561 561
562 @abc.abstractmethod 562 @abc.abstractmethod
563 def populatemap(self, context, origmapping, newmapping): 563 def populatemap(self, context, origmapping, newmapping):
564 """Return a dict of additional mapping items which should be paired 564 """Return a dict of additional mapping items which should be paired
565 with the given new mapping""" 565 with the given new mapping"""
566 566
567 class nullresourcemapper(resourcemapper): 567 class nullresourcemapper(resourcemapper):
568 def availablekeys(self, context, mapping): 568 def availablekeys(self, mapping):
569 return set() 569 return set()
570 570
571 def knownkeys(self): 571 def knownkeys(self):
572 return set() 572 return set()
573 573
574 def lookup(self, context, mapping, key): 574 def lookup(self, mapping, key):
575 return None 575 return None
576 576
577 def populatemap(self, context, origmapping, newmapping): 577 def populatemap(self, context, origmapping, newmapping):
578 return {} 578 return {}
579 579
616 """Create combined mapping from the original mapping and partial 616 """Create combined mapping from the original mapping and partial
617 mapping to override the original""" 617 mapping to override the original"""
618 # do not copy symbols which overrides the defaults depending on 618 # do not copy symbols which overrides the defaults depending on
619 # new resources, so the defaults will be re-evaluated (issue5612) 619 # new resources, so the defaults will be re-evaluated (issue5612)
620 knownres = self._resources.knownkeys() 620 knownres = self._resources.knownkeys()
621 newres = self._resources.availablekeys(self, newmapping) 621 newres = self._resources.availablekeys(newmapping)
622 mapping = {k: v for k, v in origmapping.iteritems() 622 mapping = {k: v for k, v in origmapping.iteritems()
623 if (k in knownres # not a symbol per self.symbol() 623 if (k in knownres # not a symbol per self.symbol()
624 or newres.isdisjoint(self._defaultrequires(k)))} 624 or newres.isdisjoint(self._defaultrequires(k)))}
625 mapping.update(newmapping) 625 mapping.update(newmapping)
626 mapping.update( 626 mapping.update(
643 v = self._defaults.get(key) 643 v = self._defaults.get(key)
644 return v 644 return v
645 645
646 def availableresourcekeys(self, mapping): 646 def availableresourcekeys(self, mapping):
647 """Return a set of available resource keys based on the given mapping""" 647 """Return a set of available resource keys based on the given mapping"""
648 return self._resources.availablekeys(self, mapping) 648 return self._resources.availablekeys(mapping)
649 649
650 def knownresourcekeys(self): 650 def knownresourcekeys(self):
651 """Return a set of supported resource keys""" 651 """Return a set of supported resource keys"""
652 return self._resources.knownkeys() 652 return self._resources.knownkeys()
653 653
654 def resource(self, mapping, key): 654 def resource(self, mapping, key):
655 """Return internal data (e.g. cache) used for keyword/function 655 """Return internal data (e.g. cache) used for keyword/function
656 evaluation""" 656 evaluation"""
657 v = self._resources.lookup(self, mapping, key) 657 v = self._resources.lookup(mapping, key)
658 if v is None: 658 if v is None:
659 raise templateutil.ResourceUnavailable( 659 raise templateutil.ResourceUnavailable(
660 _('template resource not available: %s') % key) 660 _('template resource not available: %s') % key)
661 return v 661 return v
662 662