pboot-view 3.1.6.1__tar.gz

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.
Files changed (63) hide show
  1. pboot_view-3.1.6.1/.gitignore +7 -0
  2. pboot_view-3.1.6.1/CHANGES.md +38 -0
  3. pboot_view-3.1.6.1/LICENSE.txt +28 -0
  4. pboot_view-3.1.6.1/PKG-INFO +78 -0
  5. pboot_view-3.1.6.1/README.md +54 -0
  6. pboot_view-3.1.6.1/pyproject.toml +47 -0
  7. pboot_view-3.1.6.1/src/pboot_view/__init__.py +43 -0
  8. pboot_view-3.1.6.1/src/pboot_view/_identifier.py +6 -0
  9. pboot_view-3.1.6.1/src/pboot_view/async_utils.py +99 -0
  10. pboot_view-3.1.6.1/src/pboot_view/bccache.py +408 -0
  11. pboot_view-3.1.6.1/src/pboot_view/compiler.py +1998 -0
  12. pboot_view-3.1.6.1/src/pboot_view/constants.py +20 -0
  13. pboot_view-3.1.6.1/src/pboot_view/debug.py +191 -0
  14. pboot_view-3.1.6.1/src/pboot_view/defaults.py +48 -0
  15. pboot_view-3.1.6.1/src/pboot_view/environment.py +1672 -0
  16. pboot_view-3.1.6.1/src/pboot_view/exceptions.py +166 -0
  17. pboot_view-3.1.6.1/src/pboot_view/ext.py +870 -0
  18. pboot_view-3.1.6.1/src/pboot_view/filters.py +1873 -0
  19. pboot_view-3.1.6.1/src/pboot_view/idtracking.py +318 -0
  20. pboot_view-3.1.6.1/src/pboot_view/lexer.py +868 -0
  21. pboot_view-3.1.6.1/src/pboot_view/loaders.py +693 -0
  22. pboot_view-3.1.6.1/src/pboot_view/meta.py +112 -0
  23. pboot_view-3.1.6.1/src/pboot_view/nativetypes.py +130 -0
  24. pboot_view-3.1.6.1/src/pboot_view/nodes.py +1206 -0
  25. pboot_view-3.1.6.1/src/pboot_view/optimizer.py +48 -0
  26. pboot_view-3.1.6.1/src/pboot_view/parser.py +1049 -0
  27. pboot_view-3.1.6.1/src/pboot_view/py.typed +0 -0
  28. pboot_view-3.1.6.1/src/pboot_view/runtime.py +1062 -0
  29. pboot_view-3.1.6.1/src/pboot_view/sandbox.py +436 -0
  30. pboot_view-3.1.6.1/src/pboot_view/tests.py +256 -0
  31. pboot_view-3.1.6.1/src/pboot_view/utils.py +766 -0
  32. pboot_view-3.1.6.1/src/pboot_view/visitor.py +92 -0
  33. pboot_view-3.1.6.1/tests/conftest.py +49 -0
  34. pboot_view-3.1.6.1/tests/res/__init__.py +0 -0
  35. pboot_view-3.1.6.1/tests/res/package.zip +0 -0
  36. pboot_view-3.1.6.1/tests/res/templates/broken.html +3 -0
  37. pboot_view-3.1.6.1/tests/res/templates/foo/test.html +1 -0
  38. pboot_view-3.1.6.1/tests/res/templates/mojibake.txt +1 -0
  39. pboot_view-3.1.6.1/tests/res/templates/syntaxerror.html +4 -0
  40. pboot_view-3.1.6.1/tests/res/templates/test.html +1 -0
  41. pboot_view-3.1.6.1/tests/res/templates2/foo +2 -0
  42. pboot_view-3.1.6.1/tests/test_api.py +435 -0
  43. pboot_view-3.1.6.1/tests/test_async.py +734 -0
  44. pboot_view-3.1.6.1/tests/test_async_filters.py +321 -0
  45. pboot_view-3.1.6.1/tests/test_bytecode_cache.py +77 -0
  46. pboot_view-3.1.6.1/tests/test_compile.py +108 -0
  47. pboot_view-3.1.6.1/tests/test_core_tags.py +603 -0
  48. pboot_view-3.1.6.1/tests/test_debug.py +117 -0
  49. pboot_view-3.1.6.1/tests/test_ext.py +739 -0
  50. pboot_view-3.1.6.1/tests/test_filters.py +883 -0
  51. pboot_view-3.1.6.1/tests/test_idtracking.py +290 -0
  52. pboot_view-3.1.6.1/tests/test_imports.py +205 -0
  53. pboot_view-3.1.6.1/tests/test_inheritance.py +410 -0
  54. pboot_view-3.1.6.1/tests/test_lexnparse.py +1030 -0
  55. pboot_view-3.1.6.1/tests/test_loader.py +436 -0
  56. pboot_view-3.1.6.1/tests/test_nativetypes.py +172 -0
  57. pboot_view-3.1.6.1/tests/test_nodes.py +3 -0
  58. pboot_view-3.1.6.1/tests/test_pickle.py +6 -0
  59. pboot_view-3.1.6.1/tests/test_regression.py +767 -0
  60. pboot_view-3.1.6.1/tests/test_runtime.py +125 -0
  61. pboot_view-3.1.6.1/tests/test_security.py +202 -0
  62. pboot_view-3.1.6.1/tests/test_tests.py +233 -0
  63. pboot_view-3.1.6.1/tests/test_utils.py +205 -0
@@ -0,0 +1,7 @@
1
+ __pycache__/
2
+ *.pyc
3
+ dist/
4
+ build/
5
+ .venv/
6
+ *.egg-info/
7
+ .pytest_cache/
@@ -0,0 +1,38 @@
1
+ pboot-view
2
+ ==========
3
+
4
+ Version 3.1.6.1
5
+ ---------------
6
+
7
+ Forked from Jinja2 3.1.6 (BSD-3-Clause, Pallets) so pboot owns its template
8
+ engine instead of pinning a third-party one.
9
+
10
+ - Package renamed to ``pboot_view``. Internal absolute imports, the import the
11
+ compiler writes into generated template modules
12
+ (``from pboot_view.runtime import ...``), the default bytecode cache prefix
13
+ (``pboot_view/bytecode/``) and the default cache file names
14
+ (``__pboot_view_%s.cache``, ``_pboot_view-cache-<uid>``) follow the rename;
15
+ the on-disk cache of a Jinja2 install is not picked up, which is what you
16
+ want — they are different engines with different generated code.
17
+
18
+ - The public API is unchanged. ``Environment``, ``Template``, every loader,
19
+ filter, test, extension, the sandbox, the native types and the async
20
+ variants keep their names and behaviour; pboot's ``Jinja2Templates``
21
+ equivalent works against this package without a single call site changing
22
+ anything but the import.
23
+
24
+ - Upstream's test suite is included, renamed, and is the correctness contract
25
+ for the fork: everything that passes against Jinja2 3.1.6 passes here.
26
+
27
+ - ``__version__`` is ``3.1.6.1`` — ``3.1.6`` plus the fork's own patch level.
28
+
29
+ Not done yet, listed so it does not get lost:
30
+
31
+ - ``markupsafe`` is still a dependency, exactly as upstream. Removing it (a
32
+ pure-Python ``Markup``/``escape`` with the C path optional) is the next step
33
+ towards pboot having no third-party dependencies at all.
34
+
35
+ - Upstream's ``docs/`` tree was not carried over; it documents Jinja2's
36
+ syntax, which is this package's syntax, but the paths inside it point at
37
+ ``jinja2``. The API reference that matters for pboot lives in pboot's own
38
+ docs.
@@ -0,0 +1,28 @@
1
+ Copyright 2007 Pallets
2
+
3
+ Redistribution and use in source and binary forms, with or without
4
+ modification, are permitted provided that the following conditions are
5
+ met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright
8
+ notice, this list of conditions and the following disclaimer.
9
+
10
+ 2. Redistributions in binary form must reproduce the above copyright
11
+ notice, this list of conditions and the following disclaimer in the
12
+ documentation and/or other materials provided with the distribution.
13
+
14
+ 3. Neither the name of the copyright holder nor the names of its
15
+ contributors may be used to endorse or promote products derived from
16
+ this software without specific prior written permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21
+ PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
+ HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24
+ TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,78 @@
1
+ Metadata-Version: 2.5
2
+ Name: pboot-view
3
+ Version: 3.1.6.1
4
+ Summary: The template engine that ships with pboot — a Jinja2 3.1.6 fork.
5
+ Project-URL: Source, https://gitee.com/pboot-org/pboot-view
6
+ Project-URL: Homepage, https://github.com/pengdeyou/pboot
7
+ Maintainer: pboot-org
8
+ License-Expression: BSD-3-Clause
9
+ License-File: LICENSE.txt
10
+ Classifier: Development Status :: 5 - Production/Stable
11
+ Classifier: Environment :: Web Environment
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved :: BSD License
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python
16
+ Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
17
+ Classifier: Topic :: Text Processing :: Markup :: HTML
18
+ Classifier: Typing :: Typed
19
+ Requires-Python: >=3.9
20
+ Requires-Dist: markupsafe>=2.0
21
+ Provides-Extra: i18n
22
+ Requires-Dist: babel>=2.7; extra == 'i18n'
23
+ Description-Content-Type: text/markdown
24
+
25
+ # pboot-view
26
+
27
+ The template engine that ships with [pboot](https://gitee.com/pboot-org/pboot).
28
+
29
+ It is a fork of **Jinja2 3.1.6** (BSD-3-Clause, Pallets) with exactly one
30
+ structural change: the import package is `pboot_view` instead of `jinja2`, so
31
+ pboot can depend on a template engine it owns rather than on a third-party
32
+ package that may change under it. Everything else is upstream behaviour,
33
+ byte for byte — `Environment`, `Template`, the loaders, the filters, the
34
+ tests-as-documentation, the sandbox, the extensions, the async support.
35
+
36
+ ```python
37
+ from pboot_view import Environment, FileSystemLoader, select_autoescape
38
+
39
+ env = Environment(
40
+ loader=FileSystemLoader("templates"),
41
+ autoescape=select_autoescape(["html", "xml"]),
42
+ )
43
+ print(env.get_template("index.html").render(title="hello"))
44
+ ```
45
+
46
+ ## Why a fork instead of a dependency
47
+
48
+ pboot's whole point is to own its stack. `jinja2` is 14k lines that pboot calls
49
+ through five entry points, and pinning a third-party major version means either
50
+ following upstream's schedule or freezing at an old release. Owning the fork
51
+ means the template engine moves when pboot moves: we can drop the `MarkupSafe`
52
+ requirement, pre-import nothing at start-up, and ship a version that matches the
53
+ framework, on the framework's release cadence.
54
+
55
+ ## What changed relative to Jinja2 3.1.6
56
+
57
+ - The import package is `pboot_view`. Internal absolute imports, the code the
58
+ compiler emits (`from pboot_view.runtime import ...`), and the bytecode cache
59
+ paths (`pboot_view/bytecode/`, `__pboot_view_%s.cache`) follow.
60
+ - `__version__` is `3.1.6.1`, the fork's own version.
61
+ - Upstream's own test suite is included and renamed; it is the correctness
62
+ contract for the fork.
63
+
64
+ Nothing else was rewritten. Divergence from upstream will be recorded in
65
+ [CHANGES.md](CHANGES.md).
66
+
67
+ ## Tests
68
+
69
+ ```bash
70
+ pytest tests -q # upstream's suite, ~1000 tests
71
+ ```
72
+
73
+ `trio` is needed for the async tests.
74
+
75
+ ## Licence
76
+
77
+ BSD-3-Clause, same as upstream. See [LICENSE.txt](LICENSE.txt). The fork keeps
78
+ Pallets' copyright notice; the rename and any later changes are pboot-org's.
@@ -0,0 +1,54 @@
1
+ # pboot-view
2
+
3
+ The template engine that ships with [pboot](https://gitee.com/pboot-org/pboot).
4
+
5
+ It is a fork of **Jinja2 3.1.6** (BSD-3-Clause, Pallets) with exactly one
6
+ structural change: the import package is `pboot_view` instead of `jinja2`, so
7
+ pboot can depend on a template engine it owns rather than on a third-party
8
+ package that may change under it. Everything else is upstream behaviour,
9
+ byte for byte — `Environment`, `Template`, the loaders, the filters, the
10
+ tests-as-documentation, the sandbox, the extensions, the async support.
11
+
12
+ ```python
13
+ from pboot_view import Environment, FileSystemLoader, select_autoescape
14
+
15
+ env = Environment(
16
+ loader=FileSystemLoader("templates"),
17
+ autoescape=select_autoescape(["html", "xml"]),
18
+ )
19
+ print(env.get_template("index.html").render(title="hello"))
20
+ ```
21
+
22
+ ## Why a fork instead of a dependency
23
+
24
+ pboot's whole point is to own its stack. `jinja2` is 14k lines that pboot calls
25
+ through five entry points, and pinning a third-party major version means either
26
+ following upstream's schedule or freezing at an old release. Owning the fork
27
+ means the template engine moves when pboot moves: we can drop the `MarkupSafe`
28
+ requirement, pre-import nothing at start-up, and ship a version that matches the
29
+ framework, on the framework's release cadence.
30
+
31
+ ## What changed relative to Jinja2 3.1.6
32
+
33
+ - The import package is `pboot_view`. Internal absolute imports, the code the
34
+ compiler emits (`from pboot_view.runtime import ...`), and the bytecode cache
35
+ paths (`pboot_view/bytecode/`, `__pboot_view_%s.cache`) follow.
36
+ - `__version__` is `3.1.6.1`, the fork's own version.
37
+ - Upstream's own test suite is included and renamed; it is the correctness
38
+ contract for the fork.
39
+
40
+ Nothing else was rewritten. Divergence from upstream will be recorded in
41
+ [CHANGES.md](CHANGES.md).
42
+
43
+ ## Tests
44
+
45
+ ```bash
46
+ pytest tests -q # upstream's suite, ~1000 tests
47
+ ```
48
+
49
+ `trio` is needed for the async tests.
50
+
51
+ ## Licence
52
+
53
+ BSD-3-Clause, same as upstream. See [LICENSE.txt](LICENSE.txt). The fork keeps
54
+ Pallets' copyright notice; the rename and any later changes are pboot-org's.
@@ -0,0 +1,47 @@
1
+ [project]
2
+ name = "pboot-view"
3
+ version = "3.1.6.1"
4
+ description = "The template engine that ships with pboot — a Jinja2 3.1.6 fork."
5
+ readme = "README.md"
6
+ license = "BSD-3-Clause"
7
+ license-files = ["LICENSE.txt"]
8
+ maintainers = [{name = "pboot-org"}]
9
+ classifiers = [
10
+ "Development Status :: 5 - Production/Stable",
11
+ "Environment :: Web Environment",
12
+ "Intended Audience :: Developers",
13
+ "License :: OSI Approved :: BSD License",
14
+ "Operating System :: OS Independent",
15
+ "Programming Language :: Python",
16
+ "Topic :: Internet :: WWW/HTTP :: Dynamic Content",
17
+ "Topic :: Text Processing :: Markup :: HTML",
18
+ "Typing :: Typed",
19
+ ]
20
+ requires-python = ">=3.9"
21
+ dependencies = ["markupsafe>=2.0"]
22
+
23
+ [project.optional-dependencies]
24
+ i18n = ["Babel>=2.7"]
25
+
26
+ [project.urls]
27
+ Source = "https://gitee.com/pboot-org/pboot-view"
28
+ Homepage = "https://github.com/pengdeyou/pboot"
29
+
30
+ [build-system]
31
+ requires = ["hatchling"]
32
+ build-backend = "hatchling.build"
33
+
34
+ [tool.hatch.build.targets.wheel]
35
+ packages = ["src/pboot_view"]
36
+
37
+ [tool.pytest.ini_options]
38
+ testpaths = ["tests"]
39
+ filterwarnings = ["error"]
40
+ # 本机 venv 里装了 pytest-asyncio,它会在 configure 阶段发 DeprecationWarning;
41
+ # filterwarnings=error 会把它升级成 INTERNALERROR。这个仓库不用 pytest-asyncio,
42
+ # 显式给个默认 loop scope 让插件闭嘴即可。
43
+ asyncio_default_fixture_loop_scope = "function"
44
+
45
+ [tool.ruff]
46
+ src = ["src"]
47
+ line-length = 100
@@ -0,0 +1,43 @@
1
+ """pboot-view: the template engine that ships with pboot.
2
+
3
+ A fork of Jinja2 3.1.6 (BSD-3-Clause, Pallets) with the import package renamed
4
+ to ``pboot_view`` so pboot can depend on its own template engine instead of a
5
+ third-party one. The public API is unchanged: ``Environment``, ``Template``,
6
+ the loaders, the filters, the sandbox and the extensions all behave as they do
7
+ upstream. See ``CHANGES.md`` for the fork history and ``LICENSE.txt`` for the
8
+ original licence.
9
+ """
10
+
11
+ from .bccache import BytecodeCache as BytecodeCache
12
+ from .bccache import FileSystemBytecodeCache as FileSystemBytecodeCache
13
+ from .bccache import MemcachedBytecodeCache as MemcachedBytecodeCache
14
+ from .environment import Environment as Environment
15
+ from .environment import Template as Template
16
+ from .exceptions import TemplateAssertionError as TemplateAssertionError
17
+ from .exceptions import TemplateError as TemplateError
18
+ from .exceptions import TemplateNotFound as TemplateNotFound
19
+ from .exceptions import TemplateRuntimeError as TemplateRuntimeError
20
+ from .exceptions import TemplatesNotFound as TemplatesNotFound
21
+ from .exceptions import TemplateSyntaxError as TemplateSyntaxError
22
+ from .exceptions import UndefinedError as UndefinedError
23
+ from .loaders import BaseLoader as BaseLoader
24
+ from .loaders import ChoiceLoader as ChoiceLoader
25
+ from .loaders import DictLoader as DictLoader
26
+ from .loaders import FileSystemLoader as FileSystemLoader
27
+ from .loaders import FunctionLoader as FunctionLoader
28
+ from .loaders import ModuleLoader as ModuleLoader
29
+ from .loaders import PackageLoader as PackageLoader
30
+ from .loaders import PrefixLoader as PrefixLoader
31
+ from .runtime import ChainableUndefined as ChainableUndefined
32
+ from .runtime import DebugUndefined as DebugUndefined
33
+ from .runtime import make_logging_undefined as make_logging_undefined
34
+ from .runtime import StrictUndefined as StrictUndefined
35
+ from .runtime import Undefined as Undefined
36
+ from .utils import clear_caches as clear_caches
37
+ from .utils import is_undefined as is_undefined
38
+ from .utils import pass_context as pass_context
39
+ from .utils import pass_environment as pass_environment
40
+ from .utils import pass_eval_context as pass_eval_context
41
+ from .utils import select_autoescape as select_autoescape
42
+
43
+ __version__ = "3.1.6.1"
@@ -0,0 +1,6 @@
1
+ import re
2
+
3
+ # generated by scripts/generate_identifier_pattern.py
4
+ pattern = re.compile(
5
+ r"[\w·̀-ͯ·҃-֑҇-ׇֽֿׁׂׅׄؐ-ًؚ-ٰٟۖ-ۜ۟-۪ۤۧۨ-ܑۭܰ-݊ަ-ް߫-߽߳ࠖ-࠙ࠛ-ࠣࠥ-ࠧࠩ-࡙࠭-࡛࣓-ࣣ࣡-ःऺ-़ा-ॏ॑-ॗॢॣঁ-ঃ়া-ৄেৈো-্ৗৢৣ৾ਁ-ਃ਼ਾ-ੂੇੈੋ-੍ੑੰੱੵઁ-ઃ઼ા-ૅે-ૉો-્ૢૣૺ-૿ଁ-ଃ଼ା-ୄେୈୋ-୍ୖୗୢୣஂா-ூெ-ைொ-்ௗఀ-ఄా-ౄె-ైొ-్ౕౖౢౣಁ-ಃ಼ಾ-ೄೆ-ೈೊ-್ೕೖೢೣഀ-ഃ഻഼ാ-ൄെ-ൈൊ-്ൗൢൣංඃ්ා-ුූෘ-ෟෲෳัิ-ฺ็-๎ັິ-ູົຼ່-ໍ༹༘༙༵༷༾༿ཱ-྄྆྇ྍ-ྗྙ-ྼ࿆ါ-ှၖ-ၙၞ-ၠၢ-ၤၧ-ၭၱ-ၴႂ-ႍႏႚ-ႝ፝-፟ᜒ-᜔ᜲ-᜴ᝒᝓᝲᝳ឴-៓៝᠋-᠍ᢅᢆᢩᤠ-ᤫᤰ-᤻ᨗ-ᨛᩕ-ᩞ᩠-᩿᩼᪰-᪽ᬀ-ᬄ᬴-᭄᭫-᭳ᮀ-ᮂᮡ-ᮭ᯦-᯳ᰤ-᰷᳐-᳔᳒-᳨᳭ᳲ-᳴᳷-᳹᷀-᷹᷻-᷿‿⁀⁔⃐-⃥⃜⃡-⃰℘℮⳯-⵿⳱ⷠ-〪ⷿ-゙゚〯꙯ꙴ-꙽ꚞꚟ꛰꛱ꠂ꠆ꠋꠣ-ꠧꢀꢁꢴ-ꣅ꣠-꣱ꣿꤦ-꤭ꥇ-꥓ꦀ-ꦃ꦳-꧀ꧥꨩ-ꨶꩃꩌꩍꩻ-ꩽꪰꪲ-ꪴꪷꪸꪾ꪿꫁ꫫ-ꫯꫵ꫶ꯣ-ꯪ꯬꯭ﬞ︀-️︠-︯︳︴﹍-﹏_𐇽𐋠𐍶-𐍺𐨁-𐨃𐨅𐨆𐨌-𐨏𐨸-𐨿𐨺𐫦𐫥𐴤-𐽆𐴧-𐽐𑀀-𑀂𑀸-𑁆𑁿-𑂂𑂰-𑂺𑄀-𑄂𑄧-𑄴𑅅𑅆𑅳𑆀-𑆂𑆳-𑇀𑇉-𑇌𑈬-𑈷𑈾𑋟-𑋪𑌀-𑌃𑌻𑌼𑌾-𑍄𑍇𑍈𑍋-𑍍𑍗𑍢𑍣𑍦-𑍬𑍰-𑍴𑐵-𑑆𑑞𑒰-𑓃𑖯-𑖵𑖸-𑗀𑗜𑗝𑘰-𑙀𑚫-𑚷𑜝-𑜫𑠬-𑠺𑨁-𑨊𑨳-𑨹𑨻-𑨾𑩇𑩑-𑩛𑪊-𑪙𑰯-𑰶𑰸-𑰿𑲒-𑲧𑲩-𑲶𑴱-𑴶𑴺𑴼𑴽𑴿-𑵅𑵇𑶊-𑶎𑶐𑶑𑶓-𑶗𑻳-𑻶𖫰-𖫴𖬰-𖬶𖽑-𖽾𖾏-𖾒𛲝𛲞𝅥-𝅩𝅭-𝅲𝅻-𝆂𝆅-𝆋𝆪-𝆭𝉂-𝉄𝨀-𝨶𝨻-𝩬𝩵𝪄𝪛-𝪟𝪡-𝪯𞀀-𞀆𞀈-𞀘𞀛-𞀡𞀣𞀤𞀦-𞣐𞀪-𞣖𞥄-𞥊󠄀-󠇯]+" # noqa: B950
6
+ )
@@ -0,0 +1,99 @@
1
+ import inspect
2
+ import typing as t
3
+ from functools import WRAPPER_ASSIGNMENTS
4
+ from functools import wraps
5
+
6
+ from .utils import _PassArg
7
+ from .utils import pass_eval_context
8
+
9
+ if t.TYPE_CHECKING:
10
+ import typing_extensions as te
11
+
12
+ V = t.TypeVar("V")
13
+
14
+
15
+ def async_variant(normal_func): # type: ignore
16
+ def decorator(async_func): # type: ignore
17
+ pass_arg = _PassArg.from_obj(normal_func)
18
+ need_eval_context = pass_arg is None
19
+
20
+ if pass_arg is _PassArg.environment:
21
+
22
+ def is_async(args: t.Any) -> bool:
23
+ return t.cast(bool, args[0].is_async)
24
+
25
+ else:
26
+
27
+ def is_async(args: t.Any) -> bool:
28
+ return t.cast(bool, args[0].environment.is_async)
29
+
30
+ # Take the doc and annotations from the sync function, but the
31
+ # name from the async function. Pallets-Sphinx-Themes
32
+ # build_function_directive expects __wrapped__ to point to the
33
+ # sync function.
34
+ async_func_attrs = ("__module__", "__name__", "__qualname__")
35
+ normal_func_attrs = tuple(set(WRAPPER_ASSIGNMENTS).difference(async_func_attrs))
36
+
37
+ @wraps(normal_func, assigned=normal_func_attrs)
38
+ @wraps(async_func, assigned=async_func_attrs, updated=())
39
+ def wrapper(*args, **kwargs): # type: ignore
40
+ b = is_async(args)
41
+
42
+ if need_eval_context:
43
+ args = args[1:]
44
+
45
+ if b:
46
+ return async_func(*args, **kwargs)
47
+
48
+ return normal_func(*args, **kwargs)
49
+
50
+ if need_eval_context:
51
+ wrapper = pass_eval_context(wrapper)
52
+
53
+ wrapper.jinja_async_variant = True # type: ignore[attr-defined]
54
+ return wrapper
55
+
56
+ return decorator
57
+
58
+
59
+ _common_primitives = {int, float, bool, str, list, dict, tuple, type(None)}
60
+
61
+
62
+ async def auto_await(value: t.Union[t.Awaitable["V"], "V"]) -> "V":
63
+ # Avoid a costly call to isawaitable
64
+ if type(value) in _common_primitives:
65
+ return t.cast("V", value)
66
+
67
+ if inspect.isawaitable(value):
68
+ return await t.cast("t.Awaitable[V]", value)
69
+
70
+ return value
71
+
72
+
73
+ class _IteratorToAsyncIterator(t.Generic[V]):
74
+ def __init__(self, iterator: "t.Iterator[V]"):
75
+ self._iterator = iterator
76
+
77
+ def __aiter__(self) -> "te.Self":
78
+ return self
79
+
80
+ async def __anext__(self) -> V:
81
+ try:
82
+ return next(self._iterator)
83
+ except StopIteration as e:
84
+ raise StopAsyncIteration(e.value) from e
85
+
86
+
87
+ def auto_aiter(
88
+ iterable: "t.Union[t.AsyncIterable[V], t.Iterable[V]]",
89
+ ) -> "t.AsyncIterator[V]":
90
+ if hasattr(iterable, "__aiter__"):
91
+ return iterable.__aiter__()
92
+ else:
93
+ return _IteratorToAsyncIterator(iter(iterable))
94
+
95
+
96
+ async def auto_to_list(
97
+ value: "t.Union[t.AsyncIterable[V], t.Iterable[V]]",
98
+ ) -> t.List["V"]:
99
+ return [x async for x in auto_aiter(value)]