python-liquid 2.3.6__py3-none-any.whl → 3.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- liquid/__about__.py +1 -0
- liquid/__init__.py +161 -187
- liquid/_base_tag.py +66 -0
- liquid/{utils/chain_map.py → _chain_map.py} +18 -19
- liquid/_context.py +465 -0
- liquid/_drop.py +108 -0
- liquid/_filter.py +174 -0
- liquid/_io.py +18 -0
- liquid/_lax_parser_mixin.py +100 -0
- liquid/_lexer.py +93 -0
- liquid/_lexer_extended.py +623 -0
- liquid/_lexer_legacy.py +536 -0
- liquid/_loader.py +84 -0
- liquid/{utils/lru_cache.py → _lru_cache.py} +28 -36
- liquid/_markup.py +253 -0
- liquid/_messages.py +292 -0
- liquid/_missing.py +3 -0
- liquid/_nothing.py +28 -0
- liquid/_parser.py +460 -0
- liquid/_parser_extended.py +293 -0
- liquid/_parser_legacy.py +819 -0
- liquid/_resource_limits.py +50 -0
- liquid/_slice.py +41 -0
- liquid/_static_analysis.py +503 -0
- liquid/_strftime.py +186 -0
- liquid/_template.py +536 -0
- liquid/drops/__init__.py +20 -0
- liquid/drops/forloop.py +78 -0
- liquid/drops/iterable.py +25 -0
- liquid/drops/range.py +76 -0
- liquid/drops/self_.py +45 -0
- liquid/drops/tablerowloop.py +142 -0
- liquid/drops/undefined.py +228 -0
- liquid/environment.py +717 -551
- liquid/exceptions.py +67 -214
- liquid/expressions.py +1184 -0
- liquid/filters/__init__.py +134 -0
- liquid/filters/abs.py +7 -0
- liquid/filters/append.py +5 -0
- liquid/filters/at_least.py +9 -0
- liquid/filters/at_most.py +9 -0
- liquid/filters/babel.py +560 -0
- liquid/filters/base64.py +26 -0
- liquid/filters/capitalize.py +5 -0
- liquid/filters/ceil.py +8 -0
- liquid/filters/compact.py +25 -0
- liquid/filters/concat.py +11 -0
- liquid/filters/date.py +27 -0
- liquid/filters/default.py +25 -0
- liquid/filters/divided_by.py +21 -0
- liquid/filters/downcase.py +5 -0
- liquid/filters/escape.py +56 -0
- liquid/filters/find.py +56 -0
- liquid/filters/first.py +19 -0
- liquid/filters/floor.py +8 -0
- liquid/filters/html.py +23 -0
- liquid/filters/index.py +12 -0
- liquid/filters/join.py +13 -0
- liquid/filters/json_.py +30 -0
- liquid/filters/last.py +17 -0
- liquid/filters/map.py +5 -0
- liquid/filters/minus.py +13 -0
- liquid/filters/modulo.py +18 -0
- liquid/filters/newline_to_br.py +16 -0
- liquid/filters/plus.py +13 -0
- liquid/filters/prepend.py +5 -0
- liquid/filters/reject.py +19 -0
- liquid/filters/remove.py +36 -0
- liquid/filters/replace.py +32 -0
- liquid/filters/reverse.py +5 -0
- liquid/filters/round.py +35 -0
- liquid/filters/size.py +25 -0
- liquid/filters/slice.py +39 -0
- liquid/filters/sort.py +148 -0
- liquid/filters/split.py +34 -0
- liquid/filters/squish.py +9 -0
- liquid/filters/strip.py +13 -0
- liquid/filters/strip_html.py +19 -0
- liquid/filters/strip_newlines.py +17 -0
- liquid/filters/sum.py +21 -0
- liquid/filters/times.py +13 -0
- liquid/filters/translate.py +380 -0
- liquid/filters/truncate.py +39 -0
- liquid/filters/uniq.py +11 -0
- liquid/filters/upcase.py +5 -0
- liquid/filters/url.py +18 -0
- liquid/filters/where.py +18 -0
- liquid/loaders/__init__.py +17 -0
- liquid/loaders/caching_file_system_loader.py +64 -0
- liquid/loaders/choice_loader.py +93 -0
- liquid/loaders/dict_loader.py +56 -0
- liquid/{builtin/loaders → loaders}/file_system_loader.py +51 -55
- liquid/{builtin/loaders → loaders}/mixins.py +39 -51
- liquid/loaders/package_loader.py +110 -0
- liquid/tags/__init__.py +61 -0
- liquid/tags/assign.py +68 -0
- liquid/tags/capture.py +83 -0
- liquid/tags/case.py +199 -0
- liquid/tags/comment.py +56 -0
- liquid/tags/cycle.py +131 -0
- liquid/tags/decrement.py +55 -0
- liquid/tags/doc.py +56 -0
- liquid/tags/echo.py +65 -0
- liquid/tags/else_.py +53 -0
- liquid/tags/error.py +51 -0
- liquid/tags/extends.py +448 -0
- liquid/tags/for_.py +421 -0
- liquid/tags/if_.py +156 -0
- liquid/tags/ifchanged.py +69 -0
- liquid/tags/include.py +223 -0
- liquid/tags/increment.py +55 -0
- liquid/tags/inline_comment.py +70 -0
- liquid/tags/liquid.py +56 -0
- liquid/tags/macro.py +281 -0
- liquid/tags/output.py +58 -0
- liquid/tags/raw.py +60 -0
- liquid/tags/render.py +245 -0
- liquid/tags/tablerow.py +254 -0
- liquid/tags/template_comment.py +56 -0
- liquid/tags/translate.py +316 -0
- liquid/tags/unless.py +121 -0
- liquid/tags/with_.py +83 -0
- liquid/tokens.py +370 -0
- {python_liquid-2.3.6.dist-info → python_liquid-3.0.0.dist-info}/METADATA +32 -39
- python_liquid-3.0.0.dist-info/RECORD +128 -0
- {python_liquid-2.3.6.dist-info → python_liquid-3.0.0.dist-info}/WHEEL +1 -1
- {python_liquid-2.3.6.dist-info → python_liquid-3.0.0.dist-info}/licenses/LICENSE +1 -1
- liquid/analyze_tags.py +0 -214
- liquid/ast.py +0 -252
- liquid/builtin/__init__.py +0 -203
- liquid/builtin/content.py +0 -41
- liquid/builtin/drops/__init__.py +0 -3
- liquid/builtin/drops/drops.py +0 -13
- liquid/builtin/expressions/__init__.py +0 -47
- liquid/builtin/expressions/_tokenize.py +0 -186
- liquid/builtin/expressions/arguments.py +0 -207
- liquid/builtin/expressions/filtered.py +0 -387
- liquid/builtin/expressions/logical.py +0 -672
- liquid/builtin/expressions/loop.py +0 -275
- liquid/builtin/expressions/path.py +0 -163
- liquid/builtin/expressions/primitive.py +0 -417
- liquid/builtin/filters/__init__.py +0 -0
- liquid/builtin/filters/array.py +0 -329
- liquid/builtin/filters/extra.py +0 -83
- liquid/builtin/filters/math.py +0 -129
- liquid/builtin/filters/misc.py +0 -137
- liquid/builtin/filters/string.py +0 -416
- liquid/builtin/illegal.py +0 -28
- liquid/builtin/loaders/__init__.py +0 -0
- liquid/builtin/loaders/caching_file_system_loader.py +0 -61
- liquid/builtin/loaders/choice_loader.py +0 -94
- liquid/builtin/loaders/dict_loader.py +0 -64
- liquid/builtin/loaders/package_loader.py +0 -132
- liquid/builtin/output.py +0 -74
- liquid/builtin/tags/__init__.py +0 -0
- liquid/builtin/tags/assign_tag.py +0 -75
- liquid/builtin/tags/capture_tag.py +0 -98
- liquid/builtin/tags/case_tag.py +0 -287
- liquid/builtin/tags/comment_tag.py +0 -77
- liquid/builtin/tags/cycle_tag.py +0 -132
- liquid/builtin/tags/decrement_tag.py +0 -59
- liquid/builtin/tags/doc_tag.py +0 -68
- liquid/builtin/tags/echo_tag.py +0 -45
- liquid/builtin/tags/for_tag.py +0 -337
- liquid/builtin/tags/if_tag.py +0 -209
- liquid/builtin/tags/ifchanged_tag.py +0 -94
- liquid/builtin/tags/include_tag.py +0 -251
- liquid/builtin/tags/increment_tag.py +0 -60
- liquid/builtin/tags/inline_comment_tag.py +0 -47
- liquid/builtin/tags/liquid_tag.py +0 -183
- liquid/builtin/tags/render_tag.py +0 -371
- liquid/builtin/tags/tablerow_tag.py +0 -320
- liquid/builtin/tags/unless_tag.py +0 -218
- liquid/context.py +0 -615
- liquid/expression.py +0 -41
- liquid/extra/__init__.py +0 -98
- liquid/extra/filters/__init__.py +0 -33
- liquid/extra/filters/_json.py +0 -33
- liquid/extra/filters/array.py +0 -60
- liquid/extra/filters/babel.py +0 -552
- liquid/extra/filters/html.py +0 -32
- liquid/extra/filters/translate.py +0 -448
- liquid/extra/tags/__init__.py +0 -17
- liquid/extra/tags/_with.py +0 -80
- liquid/extra/tags/extends_tag.py +0 -576
- liquid/extra/tags/macro_tag.py +0 -270
- liquid/extra/tags/snippet_tag.py +0 -107
- liquid/extra/tags/translate_tag.py +0 -388
- liquid/filter.py +0 -230
- liquid/future/__init__.py +0 -3
- liquid/future/environment.py +0 -30
- liquid/future/filters/__init__.py +0 -1
- liquid/future/tags/__init__.py +0 -1
- liquid/lex.py +0 -260
- liquid/limits.py +0 -66
- liquid/loader.py +0 -141
- liquid/messages.py +0 -357
- liquid/mode.py +0 -12
- liquid/output.py +0 -35
- liquid/parser.py +0 -119
- liquid/span.py +0 -35
- liquid/static_analysis.py +0 -443
- liquid/stream.py +0 -148
- liquid/stringify.py +0 -31
- liquid/tag.py +0 -47
- liquid/template.py +0 -658
- liquid/token.py +0 -145
- liquid/undefined.py +0 -218
- liquid/utils/__init__.py +0 -15
- liquid/utils/html.py +0 -57
- liquid/utils/text.py +0 -28
- python_liquid-2.3.6.dist-info/RECORD +0 -96
liquid/lex.py
DELETED
|
@@ -1,260 +0,0 @@
|
|
|
1
|
-
"""Tokenize Liquid templates.
|
|
2
|
-
|
|
3
|
-
The template lexer generates a stream of template literals, tags, output statements and
|
|
4
|
-
expressions, where each expression token is an unscanned string. Lexing of expression
|
|
5
|
-
tokens is delegated to the "parse" method of each registered Tag.
|
|
6
|
-
"""
|
|
7
|
-
|
|
8
|
-
from __future__ import annotations
|
|
9
|
-
|
|
10
|
-
import re
|
|
11
|
-
from functools import lru_cache
|
|
12
|
-
from functools import partial
|
|
13
|
-
from typing import Callable
|
|
14
|
-
from typing import Iterable
|
|
15
|
-
from typing import Iterator
|
|
16
|
-
from typing import Pattern
|
|
17
|
-
|
|
18
|
-
from liquid.exceptions import LiquidSyntaxError
|
|
19
|
-
from liquid.token import TOKEN_COMMENT
|
|
20
|
-
from liquid.token import TOKEN_CONTENT
|
|
21
|
-
from liquid.token import TOKEN_DOC
|
|
22
|
-
from liquid.token import TOKEN_EOF
|
|
23
|
-
from liquid.token import TOKEN_EXPRESSION
|
|
24
|
-
from liquid.token import TOKEN_OUTPUT
|
|
25
|
-
from liquid.token import TOKEN_TAG
|
|
26
|
-
from liquid.token import Token
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
def compile_liquid_rules(
|
|
30
|
-
tag_start_string: str = r"{%",
|
|
31
|
-
tag_end_string: str = r"%}",
|
|
32
|
-
statement_start_string: str = r"{{",
|
|
33
|
-
statement_end_string: str = r"}}",
|
|
34
|
-
comment_start_string: str = r"",
|
|
35
|
-
comment_end_string: str = r"",
|
|
36
|
-
) -> Pattern[str]:
|
|
37
|
-
"""Compile rules for lexing liquid templates."""
|
|
38
|
-
tag_s = re.escape(tag_start_string)
|
|
39
|
-
tag_e = re.escape(tag_end_string)
|
|
40
|
-
stmt_s = re.escape(statement_start_string)
|
|
41
|
-
stmt_e = re.escape(statement_end_string)
|
|
42
|
-
comment_s = re.escape(comment_start_string)
|
|
43
|
-
comment_e = re.escape(comment_end_string)
|
|
44
|
-
|
|
45
|
-
raw_pattern = (
|
|
46
|
-
rf"{tag_s}-?\s*raw\s*(?P<rsr>-?){tag_e}"
|
|
47
|
-
r"(?P<raw>.*?)"
|
|
48
|
-
rf"{tag_s}-?\s*endraw\s*(?P<rsr_e>-?){tag_e}"
|
|
49
|
-
)
|
|
50
|
-
|
|
51
|
-
doc_pattern = (
|
|
52
|
-
rf"{tag_s}-?\s*doc\s*(?P<lsd>-?){tag_e}"
|
|
53
|
-
r"(?P<doc>.*?)"
|
|
54
|
-
rf"{tag_s}-?\s*enddoc\s*(?P<rsd>-?){tag_e}"
|
|
55
|
-
)
|
|
56
|
-
|
|
57
|
-
output_pattern = (
|
|
58
|
-
rf"{stmt_s}-?\s*"
|
|
59
|
-
rf"(?P<stmt>(?:(?!-?{stmt_e})[\s\S])*(?<!\s))\s*"
|
|
60
|
-
rf"(?P<rss>-?){stmt_e}"
|
|
61
|
-
)
|
|
62
|
-
|
|
63
|
-
# The "name" group is zero or more characters so that a malformed tag (one
|
|
64
|
-
# with no name) does not get treated as a literal.
|
|
65
|
-
#
|
|
66
|
-
# The `#` in the `name` group is specifically for the inline comment tag.
|
|
67
|
-
tag_pattern = (
|
|
68
|
-
rf"{tag_s}-?\s*"
|
|
69
|
-
rf"(?P<name>#|\w+)\s*"
|
|
70
|
-
rf"(?P<expr>(?:(?!-?{tag_e})[\s\S])*(?<!\s))\s*"
|
|
71
|
-
rf"(?P<rst>-?){tag_e}"
|
|
72
|
-
)
|
|
73
|
-
|
|
74
|
-
if not comment_start_string:
|
|
75
|
-
# Do not support shorthand comment syntax
|
|
76
|
-
content_pattern = rf".+?(?=(({tag_s}|{stmt_s})(?P<rstrip>-?))|$)"
|
|
77
|
-
|
|
78
|
-
liquid_rules = [
|
|
79
|
-
("RAW", raw_pattern),
|
|
80
|
-
("DOC", doc_pattern),
|
|
81
|
-
(TOKEN_OUTPUT, output_pattern),
|
|
82
|
-
("TAG", tag_pattern),
|
|
83
|
-
(TOKEN_CONTENT, content_pattern),
|
|
84
|
-
]
|
|
85
|
-
else:
|
|
86
|
-
content_pattern = rf".+?(?=(({tag_s}|{stmt_s}|{comment_s})(?P<rstrip>-?))|$)"
|
|
87
|
-
comment_pattern = rf"{comment_s}(?P<comment>.*?)(?P<rsc>-?){comment_e}"
|
|
88
|
-
|
|
89
|
-
liquid_rules = [
|
|
90
|
-
("RAW", raw_pattern),
|
|
91
|
-
("DOC", doc_pattern),
|
|
92
|
-
("COMMENT", comment_pattern),
|
|
93
|
-
(TOKEN_OUTPUT, output_pattern),
|
|
94
|
-
("TAG", tag_pattern),
|
|
95
|
-
(TOKEN_CONTENT, content_pattern),
|
|
96
|
-
]
|
|
97
|
-
|
|
98
|
-
return _compile_rules(liquid_rules)
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
def _compile_rules(rules: Iterable[tuple[str, str]]) -> Pattern[str]:
|
|
102
|
-
"""Compile the given rules into a single regular expression."""
|
|
103
|
-
pattern = "|".join(f"(?P<{name}>{pattern})" for name, pattern in rules)
|
|
104
|
-
return re.compile(pattern, re.DOTALL)
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
def _tokenize_template(source: str, rules: Pattern[str]) -> Iterator[Token]: # noqa: PLR0912, PLR0915
|
|
108
|
-
lstrip = False
|
|
109
|
-
comment_index = 0
|
|
110
|
-
comment_text: list[str] = []
|
|
111
|
-
comment_depth = 0
|
|
112
|
-
|
|
113
|
-
for match in rules.finditer(source):
|
|
114
|
-
kind = match.lastgroup
|
|
115
|
-
assert kind is not None
|
|
116
|
-
|
|
117
|
-
value = match.group()
|
|
118
|
-
|
|
119
|
-
if comment_depth:
|
|
120
|
-
if kind == "TAG":
|
|
121
|
-
name = match.group("name")
|
|
122
|
-
if name == "endcomment":
|
|
123
|
-
comment_depth -= 1
|
|
124
|
-
if not comment_depth:
|
|
125
|
-
yield Token(
|
|
126
|
-
kind=TOKEN_COMMENT,
|
|
127
|
-
value="".join(comment_text),
|
|
128
|
-
start_index=comment_index,
|
|
129
|
-
source=source,
|
|
130
|
-
)
|
|
131
|
-
|
|
132
|
-
comment_index = 0
|
|
133
|
-
comment_text.clear()
|
|
134
|
-
|
|
135
|
-
yield Token(
|
|
136
|
-
kind=TOKEN_TAG,
|
|
137
|
-
value=name,
|
|
138
|
-
start_index=match.start("name"),
|
|
139
|
-
source=source,
|
|
140
|
-
)
|
|
141
|
-
|
|
142
|
-
lstrip = bool(match.group("rst"))
|
|
143
|
-
continue
|
|
144
|
-
elif name == "comment":
|
|
145
|
-
comment_depth += 1
|
|
146
|
-
|
|
147
|
-
comment_text.append(value)
|
|
148
|
-
continue
|
|
149
|
-
|
|
150
|
-
if kind == TOKEN_OUTPUT:
|
|
151
|
-
yield Token(
|
|
152
|
-
kind=TOKEN_OUTPUT,
|
|
153
|
-
value=match.group(),
|
|
154
|
-
start_index=match.start(),
|
|
155
|
-
source=source,
|
|
156
|
-
)
|
|
157
|
-
|
|
158
|
-
yield Token(
|
|
159
|
-
TOKEN_EXPRESSION,
|
|
160
|
-
match.group("stmt"),
|
|
161
|
-
start_index=match.start("stmt"),
|
|
162
|
-
source=source,
|
|
163
|
-
)
|
|
164
|
-
|
|
165
|
-
lstrip = bool(match.group("rss"))
|
|
166
|
-
continue
|
|
167
|
-
|
|
168
|
-
elif kind == "TAG":
|
|
169
|
-
name = match.group("name")
|
|
170
|
-
yield Token(
|
|
171
|
-
kind=TOKEN_TAG,
|
|
172
|
-
value=name,
|
|
173
|
-
start_index=match.start("name"),
|
|
174
|
-
source=source,
|
|
175
|
-
)
|
|
176
|
-
|
|
177
|
-
value = match.group("expr")
|
|
178
|
-
lstrip = bool(match.group("rst"))
|
|
179
|
-
|
|
180
|
-
if value:
|
|
181
|
-
yield Token(
|
|
182
|
-
kind=TOKEN_EXPRESSION,
|
|
183
|
-
value=value,
|
|
184
|
-
start_index=match.start("expr"),
|
|
185
|
-
source=source,
|
|
186
|
-
)
|
|
187
|
-
|
|
188
|
-
if name == "comment":
|
|
189
|
-
if not comment_depth:
|
|
190
|
-
comment_index = match.end()
|
|
191
|
-
comment_depth += 1
|
|
192
|
-
|
|
193
|
-
continue
|
|
194
|
-
|
|
195
|
-
elif kind == "COMMENT":
|
|
196
|
-
lstrip = bool(match.group("rsc"))
|
|
197
|
-
value = match.group("comment")
|
|
198
|
-
|
|
199
|
-
elif kind == "RAW":
|
|
200
|
-
kind = TOKEN_CONTENT
|
|
201
|
-
value = match.group("raw")
|
|
202
|
-
lstrip = bool(match.group("rsr"))
|
|
203
|
-
|
|
204
|
-
elif kind == "DOC":
|
|
205
|
-
kind = TOKEN_DOC
|
|
206
|
-
value = match.group("doc")
|
|
207
|
-
lstrip = bool(match.group("rsd"))
|
|
208
|
-
|
|
209
|
-
elif kind == TOKEN_CONTENT:
|
|
210
|
-
if lstrip:
|
|
211
|
-
value = value.lstrip()
|
|
212
|
-
if match.group("rstrip"):
|
|
213
|
-
value = value.rstrip()
|
|
214
|
-
|
|
215
|
-
if not value:
|
|
216
|
-
continue
|
|
217
|
-
|
|
218
|
-
if value.startswith(r"{{"):
|
|
219
|
-
raise LiquidSyntaxError(
|
|
220
|
-
"expected '}}', found end of file",
|
|
221
|
-
token=Token(
|
|
222
|
-
TOKEN_EOF,
|
|
223
|
-
value=match.group(),
|
|
224
|
-
start_index=match.start(),
|
|
225
|
-
source=source,
|
|
226
|
-
),
|
|
227
|
-
)
|
|
228
|
-
if value.startswith(r"{%"):
|
|
229
|
-
raise LiquidSyntaxError(
|
|
230
|
-
"missing tag name or closing delimiter",
|
|
231
|
-
token=Token(
|
|
232
|
-
TOKEN_EOF,
|
|
233
|
-
value=match.group(),
|
|
234
|
-
start_index=match.start(),
|
|
235
|
-
source=source,
|
|
236
|
-
),
|
|
237
|
-
)
|
|
238
|
-
|
|
239
|
-
yield Token(kind, value, start_index=match.start(), source=source)
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
@lru_cache(maxsize=128)
|
|
243
|
-
def get_lexer(
|
|
244
|
-
tag_start_string: str = r"{%",
|
|
245
|
-
tag_end_string: str = r"%}",
|
|
246
|
-
statement_start_string: str = r"{{",
|
|
247
|
-
statement_end_string: str = r"}}",
|
|
248
|
-
comment_start_string: str = "",
|
|
249
|
-
comment_end_string: str = "",
|
|
250
|
-
) -> Callable[[str], Iterator[Token]]:
|
|
251
|
-
"""Return a template lexer using the given tag and statement delimiters."""
|
|
252
|
-
rules = compile_liquid_rules(
|
|
253
|
-
tag_start_string,
|
|
254
|
-
tag_end_string,
|
|
255
|
-
statement_start_string,
|
|
256
|
-
statement_end_string,
|
|
257
|
-
comment_start_string,
|
|
258
|
-
comment_end_string,
|
|
259
|
-
)
|
|
260
|
-
return partial(_tokenize_template, rules=rules)
|
liquid/limits.py
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
"""System wide limits.
|
|
2
|
-
|
|
3
|
-
Ideally these limits would be configurable from a liquid.Environment, but our
|
|
4
|
-
template parser functions, filter decorators and expression objects don't have
|
|
5
|
-
access to an Environment. Changing that would require significant refactoring
|
|
6
|
-
resulting in too many breaking changes.
|
|
7
|
-
"""
|
|
8
|
-
|
|
9
|
-
import os
|
|
10
|
-
import sys
|
|
11
|
-
from typing import Any
|
|
12
|
-
|
|
13
|
-
from liquid.exceptions import LiquidValueError
|
|
14
|
-
|
|
15
|
-
_MIN_STR_INT = 640
|
|
16
|
-
_DEFAULT_MAX_STR_INT = 4300
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
def _init_int_max_str_digits() -> int:
|
|
20
|
-
var = "LIQUIDINTMAXSTRDIGITS"
|
|
21
|
-
env = os.environ.get(var)
|
|
22
|
-
|
|
23
|
-
if env is None:
|
|
24
|
-
if hasattr(sys, "get_int_max_str_digits"):
|
|
25
|
-
return sys.get_int_max_str_digits() # type: ignore
|
|
26
|
-
|
|
27
|
-
var = "PYTHONINTMAXSTRDIGITS"
|
|
28
|
-
env = os.environ.get(var)
|
|
29
|
-
|
|
30
|
-
if env is None:
|
|
31
|
-
return _DEFAULT_MAX_STR_INT
|
|
32
|
-
|
|
33
|
-
if not env.isdigit():
|
|
34
|
-
raise TypeError(
|
|
35
|
-
f"{var}: invalid limit; must be >= {_MIN_STR_INT} or 0 for unlimited"
|
|
36
|
-
)
|
|
37
|
-
|
|
38
|
-
max_digits = int(env)
|
|
39
|
-
|
|
40
|
-
if max_digits == 0 or max_digits >= _MIN_STR_INT:
|
|
41
|
-
return max_digits
|
|
42
|
-
|
|
43
|
-
raise ValueError(
|
|
44
|
-
f"{var}: invalid limit; must be >= {_MIN_STR_INT} or 0 for unlimited"
|
|
45
|
-
)
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
MAX_STR_INT = _init_int_max_str_digits()
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
def to_int(val: Any) -> int:
|
|
52
|
-
"""Prevent DoS by very large str to int conversion.
|
|
53
|
-
|
|
54
|
-
See https://github.com/python/cpython/issues/95778
|
|
55
|
-
"""
|
|
56
|
-
if (
|
|
57
|
-
isinstance(val, (str, bytes, bytearray))
|
|
58
|
-
and MAX_STR_INT != 0
|
|
59
|
-
and len(val) > MAX_STR_INT
|
|
60
|
-
):
|
|
61
|
-
raise LiquidValueError(
|
|
62
|
-
f"integer string conversion limit ({MAX_STR_INT}) reached: "
|
|
63
|
-
f"value has {len(val)} digits",
|
|
64
|
-
token=None,
|
|
65
|
-
)
|
|
66
|
-
return int(val)
|
liquid/loader.py
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
"""Base class for all template loaders."""
|
|
2
|
-
|
|
3
|
-
from __future__ import annotations
|
|
4
|
-
|
|
5
|
-
from abc import ABC
|
|
6
|
-
from abc import abstractmethod
|
|
7
|
-
from pathlib import Path
|
|
8
|
-
from typing import TYPE_CHECKING
|
|
9
|
-
from typing import Awaitable
|
|
10
|
-
from typing import Callable
|
|
11
|
-
from typing import Mapping
|
|
12
|
-
from typing import NamedTuple
|
|
13
|
-
from typing import Optional
|
|
14
|
-
from typing import Union
|
|
15
|
-
|
|
16
|
-
if TYPE_CHECKING:
|
|
17
|
-
from .context import RenderContext
|
|
18
|
-
from .environment import Environment
|
|
19
|
-
from .template import BoundTemplate
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
class BaseLoader(ABC):
|
|
23
|
-
"""Base class for all template loaders."""
|
|
24
|
-
|
|
25
|
-
@abstractmethod
|
|
26
|
-
def get_source(
|
|
27
|
-
self,
|
|
28
|
-
env: Environment,
|
|
29
|
-
template_name: str,
|
|
30
|
-
*,
|
|
31
|
-
context: Optional[RenderContext] = None,
|
|
32
|
-
**kwargs: object,
|
|
33
|
-
) -> TemplateSource:
|
|
34
|
-
"""Get source information for a template.
|
|
35
|
-
|
|
36
|
-
Args:
|
|
37
|
-
env: The `Environment` attempting to load the template source text.
|
|
38
|
-
template_name: A name or identifier for a template's source text.
|
|
39
|
-
context: An optional render context that can be used to narrow the template
|
|
40
|
-
source search space.
|
|
41
|
-
kwargs: Arbitrary arguments that can be used to narrow the template source
|
|
42
|
-
search space.
|
|
43
|
-
"""
|
|
44
|
-
|
|
45
|
-
async def get_source_async(
|
|
46
|
-
self,
|
|
47
|
-
env: Environment,
|
|
48
|
-
template_name: str,
|
|
49
|
-
*,
|
|
50
|
-
context: Optional[RenderContext] = None,
|
|
51
|
-
**kwargs: object,
|
|
52
|
-
) -> TemplateSource:
|
|
53
|
-
"""An async version of `get_source`.
|
|
54
|
-
|
|
55
|
-
The default implementation delegates to `get_source()`.
|
|
56
|
-
"""
|
|
57
|
-
return self.get_source(env, template_name, context=context, **kwargs)
|
|
58
|
-
|
|
59
|
-
def load(
|
|
60
|
-
self,
|
|
61
|
-
env: Environment,
|
|
62
|
-
name: str,
|
|
63
|
-
*,
|
|
64
|
-
globals: Optional[Mapping[str, object]] = None, # noqa: A002
|
|
65
|
-
context: Optional[RenderContext] = None,
|
|
66
|
-
**kwargs: object,
|
|
67
|
-
) -> BoundTemplate:
|
|
68
|
-
"""Find and parse template source code.
|
|
69
|
-
|
|
70
|
-
Args:
|
|
71
|
-
env: The `Environment` attempting to load the template source text.
|
|
72
|
-
name: A name or identifier for a template's source text.
|
|
73
|
-
globals: A mapping of render context variables attached to the
|
|
74
|
-
resulting template.
|
|
75
|
-
context: An optional render context that can be used to narrow the template
|
|
76
|
-
source search space.
|
|
77
|
-
kwargs: Arbitrary arguments that can be used to narrow the template source
|
|
78
|
-
search space.
|
|
79
|
-
"""
|
|
80
|
-
source, full_name, uptodate, matter = self.get_source(
|
|
81
|
-
env, name, context=context, **kwargs
|
|
82
|
-
)
|
|
83
|
-
|
|
84
|
-
path = Path(full_name)
|
|
85
|
-
|
|
86
|
-
template = env.from_string(
|
|
87
|
-
source,
|
|
88
|
-
name=path.name,
|
|
89
|
-
path=path,
|
|
90
|
-
globals=globals,
|
|
91
|
-
matter=matter,
|
|
92
|
-
)
|
|
93
|
-
|
|
94
|
-
template.uptodate = uptodate
|
|
95
|
-
return template
|
|
96
|
-
|
|
97
|
-
async def load_async(
|
|
98
|
-
self,
|
|
99
|
-
env: Environment,
|
|
100
|
-
name: str,
|
|
101
|
-
*,
|
|
102
|
-
globals: Union[Mapping[str, object], None] = None, # noqa: A002
|
|
103
|
-
context: Union[RenderContext, None] = None,
|
|
104
|
-
**kwargs: object,
|
|
105
|
-
) -> BoundTemplate:
|
|
106
|
-
"""An async version of `load()`."""
|
|
107
|
-
source, full_name, uptodate, matter = await self.get_source_async(
|
|
108
|
-
env, name, context=context, **kwargs
|
|
109
|
-
)
|
|
110
|
-
|
|
111
|
-
template = env.from_string(
|
|
112
|
-
source,
|
|
113
|
-
name=name,
|
|
114
|
-
path=Path(full_name),
|
|
115
|
-
globals=globals,
|
|
116
|
-
matter=matter,
|
|
117
|
-
)
|
|
118
|
-
|
|
119
|
-
template.uptodate = uptodate
|
|
120
|
-
return template
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
UpToDate = Union[Callable[[], bool], Callable[[], Awaitable[bool]], None]
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
class TemplateSource(NamedTuple):
|
|
127
|
-
"""A Liquid template source as returned by the `get_source` method of a `loader`.
|
|
128
|
-
|
|
129
|
-
Attributes:
|
|
130
|
-
text: The liquid template source code.
|
|
131
|
-
name: The liquid template file name or other string identifying its origin.
|
|
132
|
-
uptodate: Optional callable that will return `True` if the template is up to
|
|
133
|
-
date, or `False` if it needs to be reloaded.
|
|
134
|
-
matter: Optional mapping containing variables associated with the template.
|
|
135
|
-
Could be "front matter" or other meta data.
|
|
136
|
-
"""
|
|
137
|
-
|
|
138
|
-
text: str
|
|
139
|
-
name: str
|
|
140
|
-
uptodate: Union[Callable[[], bool], Callable[[], Awaitable[bool]], None]
|
|
141
|
-
matter: Union[dict[str, object], None] = None
|