Commit 054db41a authored by Stefan Behnel's avatar Stefan Behnel

allow cached loading of TempitaUtilityCode in simple cases (all context entries are hashable)

parent 1f760065
...@@ -459,6 +459,7 @@ def sub_tempita(s, context, file=None, name=None): ...@@ -459,6 +459,7 @@ def sub_tempita(s, context, file=None, name=None):
from ..Tempita import sub from ..Tempita import sub
return sub(s, **context) return sub(s, **context)
class TempitaUtilityCode(UtilityCode): class TempitaUtilityCode(UtilityCode):
def __init__(self, name=None, proto=None, impl=None, init=None, file=None, context=None, **kwargs): def __init__(self, name=None, proto=None, impl=None, init=None, file=None, context=None, **kwargs):
if context is None: if context is None:
...@@ -469,6 +470,18 @@ class TempitaUtilityCode(UtilityCode): ...@@ -469,6 +470,18 @@ class TempitaUtilityCode(UtilityCode):
super(TempitaUtilityCode, self).__init__( super(TempitaUtilityCode, self).__init__(
proto, impl, init=init, name=name, file=file, **kwargs) proto, impl, init=init, name=name, file=file, **kwargs)
@classmethod
def load_cached(cls, utility_code_name, from_file=None, context=None, __cache={}):
context_key = tuple(sorted(context.items())) if context else None
assert hash(context_key) is not None # raise TypeError if not hashable
key = (cls, from_file, utility_code_name, context_key)
try:
return __cache[key]
except KeyError:
pass
code = __cache[key] = cls.load(utility_code_name, from_file, context=context)
return code
def none_or_sub(self, s, context): def none_or_sub(self, s, context):
""" """
Format a string in this utility code with context. If None, do nothing. Format a string in this utility code with context. If None, do nothing.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment