| 1 |
import inspect |
| 2 |
import string |
| 3 |
import sys |
| 4 |
from collections.abc import Callable |
| 5 |
|
| 6 |
if sys.version_info >= (3, 14): |
| 7 |
import annotationlib |
| 8 |
|
| 9 |
def _signature(f: Callable) -> inspect.Signature: |
| 10 |
return inspect.signature(f, annotation_format=annotationlib.Format.FORWARDREF) |
| 11 |
else: |
| 12 |
_signature = inspect.signature |
| 13 |
|
| 14 |
TEMPLATE_FRAGMENT_KEY_TEMPLATE = "_template_fragment_cache_%s%s" |
| 15 |
# Used to remove control characters and whitespace from cache keys. |
| 16 |
valid_chars = set(string.ascii_letters + string.digits + "_.") |
| 17 |
del_chars = "".join(c for c in map(chr, range(256)) if c not in valid_chars) |
| 18 |
null_control = ({k: None for k in del_chars},) |
| 19 |
|
| 20 |
|
| 21 |
def wants_args(f: Callable) -> bool: |
| 22 |
"""Check if the function wants any positional, *args, or **kwargs arguments.""" |
| 23 |
return any( |
| 24 |
p.kind != inspect.Parameter.KEYWORD_ONLY for p in get_function_parameters(f) |
| 25 |
) |
| 26 |
|
| 27 |
|
| 28 |
def get_function_parameters(f: Callable) -> list: |
| 29 |
"""Get function parameters |
| 30 |
:param f |
| 31 |
:return: Parameter list of function |
| 32 |
""" |
| 33 |
return list(_signature(f).parameters.values()) |
| 34 |
|
| 35 |
|
| 36 |
def get_arg_names(f: Callable) -> list[str]: |
| 37 |
"""Return arguments of function |
| 38 |
:param f: |
| 39 |
:return: String list of arguments |
| 40 |
""" |
| 41 |
return [ |
| 42 |
parameter.name |
| 43 |
for parameter in get_function_parameters(f) |
| 44 |
if parameter.kind == parameter.POSITIONAL_OR_KEYWORD |
| 45 |
] |
| 46 |
|
| 47 |
|
| 48 |
def get_arg_default(f: Callable, position: int): |
| 49 |
arg = get_function_parameters(f)[position] |
| 50 |
arg_def = arg.default |
| 51 |
return arg_def if arg_def != inspect.Parameter.empty else None |
| 52 |
|
| 53 |
|
| 54 |
def get_id(obj): |
| 55 |
return getattr(obj, "__caching_id__", repr)(obj) |
| 56 |
|
| 57 |
|
| 58 |
def function_namespace(f, args=None): |
| 59 |
"""Attempts to returns unique namespace for function""" |
| 60 |
m_args = get_arg_names(f) |
| 61 |
|
| 62 |
instance_token = None |
| 63 |
|
| 64 |
instance_self = getattr(f, "__self__", None) |
| 65 |
|
| 66 |
if instance_self and not inspect.isclass(instance_self): |
| 67 |
instance_token = get_id(f.__self__) |
| 68 |
elif m_args and m_args[0] == "self" and args: |
| 69 |
instance_token = get_id(args[0]) |
| 70 |
|
| 71 |
module = f.__module__ |
| 72 |
|
| 73 |
if m_args and m_args[0] == "cls" and not inspect.isclass(args[0]): |
| 74 |
raise ValueError( |
| 75 |
"When using `delete_memoized` on a " |
| 76 |
"`@classmethod` you must provide the " |
| 77 |
"class as the first argument" |
| 78 |
) |
| 79 |
|
| 80 |
if hasattr(f, "__qualname__"): |
| 81 |
name = f.__qualname__ |
| 82 |
else: |
| 83 |
klass = getattr(f, "__self__", None) |
| 84 |
|
| 85 |
if klass and not inspect.isclass(klass): |
| 86 |
klass = klass.__class__ |
| 87 |
|
| 88 |
if not klass: |
| 89 |
klass = getattr(f, "im_class", None) |
| 90 |
|
| 91 |
if not klass: |
| 92 |
if m_args and args: |
| 93 |
if m_args[0] == "self": |
| 94 |
klass = args[0].__class__ |
| 95 |
elif m_args[0] == "cls": |
| 96 |
klass = args[0] |
| 97 |
|
| 98 |
if klass: |
| 99 |
name = klass.__name__ + "." + f.__name__ |
| 100 |
else: |
| 101 |
name = f.__name__ |
| 102 |
|
| 103 |
ns = ".".join((module, name)).translate(*null_control) |
| 104 |
|
| 105 |
ins = ( |
| 106 |
".".join((module, name, instance_token)).translate(*null_control) |
| 107 |
if instance_token |
| 108 |
else None |
| 109 |
) |
| 110 |
|
| 111 |
return ns, ins |
| 112 |
|
| 113 |
|
| 114 |
def make_template_fragment_key( |
| 115 |
fragment_name: str, vary_on: list[str] | None = None |
| 116 |
) -> str: |
| 117 |
"""Make a cache key for a specific fragment name.""" |
| 118 |
if vary_on: |
| 119 |
fragment_name = f"{fragment_name}_" |
| 120 |
else: |
| 121 |
vary_on = [] |
| 122 |
return TEMPLATE_FRAGMENT_KEY_TEMPLATE % (fragment_name, "_".join(vary_on)) |