Debian Codegraph @

sekizai/helpers.py

raw symbols SCIP
1 from django.confscip-python python Django 5.2.15 `django.conf`/__init__: import settings
2 from django.template.basescip-python python Django 5.2.15 `django.template.base`/__init__: import Template, Variable, VariableNode
3 from django.template.contextscip-python python Django 5.2.15 `django.template.context`/__init__: import Context
4 from django.template.loaderscip-python python Django 5.2.15 `django.template.loader`/__init__: import get_template
5 from django.template.loader_tagsscip-python python Django 5.2.15 `django.template.loader_tags`/__init__: import BlockNode, ExtendsNode
6
7
8 def _get_nodelistscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_get_nodelist().(tplscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_get_nodelist().(tpl)):
9 if isinstance(tplscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_get_nodelist().(tpl), Template):
10 return tplscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_get_nodelist().(tpl).nodelist
11 else:
12 return tplscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_get_nodelist().(tpl).template.nodelist
13
14
15 def is_variable_extend_nodescip-python python django-sekizai 4.1.0 `sekizai.helpers`/is_variable_extend_node().(nodescip-python python django-sekizai 4.1.0 `sekizai.helpers`/is_variable_extend_node().(node)):
16 if hasattr(nodescip-python python django-sekizai 4.1.0 `sekizai.helpers`/is_variable_extend_node().(node), 'parent_name_expr') and nodescip-python python django-sekizai 4.1.0 `sekizai.helpers`/is_variable_extend_node().(node).parent_name_expr:
17 return True
18 if hasattr(nodescip-python python django-sekizai 4.1.0 `sekizai.helpers`/is_variable_extend_node().(node), 'parent_name') and hasattr(nodescip-python python django-sekizai 4.1.0 `sekizai.helpers`/is_variable_extend_node().(node).parent_name, 'filters'):
19 if (nodescip-python python django-sekizai 4.1.0 `sekizai.helpers`/is_variable_extend_node().(node).parent_name.filters or
20 isinstance(nodescip-python python django-sekizai 4.1.0 `sekizai.helpers`/is_variable_extend_node().(node).parent_name.var, Variable)):
21 return True
22 return False
23
24
25 def get_contextscip-python python django-sekizai 4.1.0 `sekizai.helpers`/get_context().():
26 context = Context()
27 context.template = Template('')
28 return context
29
30
31 def _extend_blocksscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_extend_blocks().(extend_nodescip-python python django-sekizai 4.1.0 `sekizai.helpers`/_extend_blocks().(extend_node), blocksscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_extend_blocks().(blocks)):
32 """
33 Extends the dictionary `blocks` with *new* blocks in the parent node
34 (recursive)
35 """
36 # we don't support variable extensions
37 if is_variable_extend_nodescip-python python django-sekizai 4.1.0 `sekizai.helpers`/is_variable_extend_node().(extend_nodescip-python python django-sekizai 4.1.0 `sekizai.helpers`/_extend_blocks().(extend_node)):
38 return
39 parent = extend_nodescip-python python django-sekizai 4.1.0 `sekizai.helpers`/_extend_blocks().(extend_node).get_parent(get_contextscip-python python django-sekizai 4.1.0 `sekizai.helpers`/get_context().())
40 # Search for new blocks
41 for node in _get_nodelistscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_get_nodelist().(parent).get_nodes_by_type(BlockNode):
42 if node.name not in blocksscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_extend_blocks().(blocks):
43 blocksscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_extend_blocks().(blocks)[node.name] = node
44 else:
45 # set this node as the super node (for {{ block.super }})
46 block = blocksscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_extend_blocks().(blocks)[node.name]
47 seen_supers = []
48 while (hasattr(block.super, 'nodelist') and
49 block.super not in seen_supers):
50 seen_supers.appendscip-python python python-stdlib 3.11 builtins/list#append().(block.super)
51 block = block.super
52 block.super = node
53 # search for further ExtendsNodes
54 for node in _get_nodelistscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_get_nodelist().(parent).get_nodes_by_type(ExtendsNode):
55 _extend_blocksscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_extend_blocks().(node, blocksscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_extend_blocks().(blocks))
56 break
57
58
59 def _extend_nodelistscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_extend_nodelist().(extend_nodescip-python python django-sekizai 4.1.0 `sekizai.helpers`/_extend_nodelist().(extend_node)):
60 """
61 Returns a list of namespaces found in the parent template(s) of this
62 ExtendsNode
63 """
64 # we don't support variable extensions (1.3 way)
65 if is_variable_extend_nodescip-python python django-sekizai 4.1.0 `sekizai.helpers`/is_variable_extend_node().(extend_nodescip-python python django-sekizai 4.1.0 `sekizai.helpers`/_extend_nodelist().(extend_node)):
66 return []
67 blocks = extend_nodescip-python python django-sekizai 4.1.0 `sekizai.helpers`/_extend_nodelist().(extend_node).blocks
68 _extend_blocksscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_extend_blocks().(extend_nodescip-python python django-sekizai 4.1.0 `sekizai.helpers`/_extend_nodelist().(extend_node), blocks)
69 found = []
70
71 for block in blocks.values():
72 found += _scan_namespacesscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_scan_namespaces().(block.nodelist, block)
73
74 parent_template = extend_nodescip-python python django-sekizai 4.1.0 `sekizai.helpers`/_extend_nodelist().(extend_node).get_parent(get_contextscip-python python django-sekizai 4.1.0 `sekizai.helpers`/get_context().())
75 # if this is the topmost template, check for namespaces outside of blocks
76 if not _get_nodelistscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_get_nodelist().(parent_template).get_nodes_by_type(ExtendsNode):
77 found += _scan_namespacesscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_scan_namespaces().(
78 _get_nodelistscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_get_nodelist().(parent_template),
79 None
80 )
81 else:
82 found += _scan_namespacesscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_scan_namespaces().(
83 _get_nodelistscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_get_nodelist().(parent_template),
84 extend_nodescip-python python django-sekizai 4.1.0 `sekizai.helpers`/_extend_nodelist().(extend_node)
85 )
86 return found
87
88
89 def _scan_namespacesscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_scan_namespaces().(nodelistscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_scan_namespaces().(nodelist), current_blockscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_scan_namespaces().(current_block)=None):
90 from sekizai.templatetags.sekizai_tagsscip-python python django-sekizai 4.1.0 `sekizai.templatetags.sekizai_tags`/__init__: import RenderBlock, WithData
91 found = []
92
93 for node in nodelistscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_scan_namespaces().(nodelist):
94 # check if this is RenderBlock node
95 if isinstance(node, (RenderBlock, WithData)):
96 # resolve it's name against a dummy context
97 found.appendscip-python python python-stdlib 3.11 builtins/list#append().(node.kwargs['name'].resolve({}))
98 found += _scan_namespacesscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_scan_namespaces().(node.blocks['nodelist'], node)
99 # handle {% extends ... %} tags if check_inheritance is True
100 elif isinstance(node, ExtendsNode):
101 found += _extend_nodelistscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_extend_nodelist().(node)
102 # in block nodes we have to scan for super blocks
103 elif isinstance(node, VariableNode) and current_blockscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_scan_namespaces().(current_block):
104 if node.filter_expression.token == 'block.super':
105 if hasattr(current_blockscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_scan_namespaces().(current_block).super, 'nodelist'):
106 found += _scan_namespacesscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_scan_namespaces().(
107 current_blockscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_scan_namespaces().(current_block).super.nodelist,
108 current_blockscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_scan_namespaces().(current_block).super
109 )
110 return found
111
112
113 def get_namespacesscip-python python django-sekizai 4.1.0 `sekizai.helpers`/get_namespaces().(templatescip-python python django-sekizai 4.1.0 `sekizai.helpers`/get_namespaces().(template)):
114 compiled_template = get_template(templatescip-python python django-sekizai 4.1.0 `sekizai.helpers`/get_namespaces().(template))
115 return _scan_namespacesscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_scan_namespaces().(_get_nodelistscip-python python django-sekizai 4.1.0 `sekizai.helpers`/_get_nodelist().(compiled_template))
116
117
118 def validate_templatescip-python python django-sekizai 4.1.0 `sekizai.helpers`/validate_template().(templatescip-python python django-sekizai 4.1.0 `sekizai.helpers`/validate_template().(template), namespacesscip-python python django-sekizai 4.1.0 `sekizai.helpers`/validate_template().(namespaces)):
119 """
120 Validates that a template (or it's parents if check_inheritance is True)
121 contain all given namespaces
122 """
123 if getattrscip-python python python-stdlib 3.11 builtins/getattr().(settings, 'SEKIZAI_IGNORE_VALIDATION', False):
124 return True
125 found = get_namespacesscip-python python django-sekizai 4.1.0 `sekizai.helpers`/get_namespaces().(templatescip-python python django-sekizai 4.1.0 `sekizai.helpers`/validate_template().(template))
126 for namespace in namespacesscip-python python django-sekizai 4.1.0 `sekizai.helpers`/validate_template().(namespaces):
127 if namespace not in found:
128 return False
129 return True
130
131
132 def get_varnamescip-python python django-sekizai 4.1.0 `sekizai.helpers`/get_varname().():
133 return getattrscip-python python python-stdlib 3.11 builtins/getattr().(settings, 'SEKIZAI_VARNAME', 'SEKIZAI_CONTENT_HOLDER')
134
135
136 class Watcherscip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#:
137 """
138 Watches a context for changes to the sekizai data, so it can be replayed
139 later. This is useful for caching.
140
141 NOTE: This class assumes you ONLY ADD, NEVER REMOVE data from the context!
142 """
143 def __init__scip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#__init__().(selfscip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#__init__().(self), contextscip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#__init__().(context)):
144 selfscip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#__init__().(self).contextscip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#context. = contextscip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#__init__().(context)
145 selfscip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#__init__().(self).frozenscip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#frozen. = {
146 key: listscip-python python python-stdlib 3.11 builtins/list#(value) for key, value in selfscip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#__init__().(self).datascip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#data()..items()
147 }
148
149 @propertyscip-python python python-stdlib 3.11 builtins/property#
150 def datascip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#data().(selfscip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#data().(self)):
151 return selfscip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#data().(self).contextscip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#context..get(get_varnamescip-python python django-sekizai 4.1.0 `sekizai.helpers`/get_varname().(), {})
152
153 def get_changesscip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#get_changes().(selfscip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#get_changes().(self)):
154 sfrozen = setscip-python python python-stdlib 3.11 builtins/set#(selfscip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#get_changes().(self).frozenscip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#frozen.)
155 sdata = setscip-python python python-stdlib 3.11 builtins/set#(selfscip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#get_changes().(self).datascip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#data().)
156 new_keys = sfrozen ^ sdata
157 changes = {}
158 for key in new_keys:
159 changes[key] = listscip-python python python-stdlib 3.11 builtins/list#(selfscip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#get_changes().(self).datascip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#data().[key])
160 shared_keys = sfrozen & sdata
161 for key in shared_keys:
162 old_set = setscip-python python python-stdlib 3.11 builtins/set#(selfscip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#get_changes().(self).frozenscip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#frozen.[key])
163 new_values = [
164 item for item in selfscip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#get_changes().(self).datascip-python python django-sekizai 4.1.0 `sekizai.helpers`/Watcher#data().[key] if item not in old_set
165 ]
166 changes[key] = new_values
167 return changes