fjkit 0.1.0.dev0__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.
Files changed (127) hide show
  1. fjkit/__init__.py +49 -0
  2. fjkit/auth/__init__.py +54 -0
  3. fjkit/auth/csrf.py +88 -0
  4. fjkit/auth/errors.py +31 -0
  5. fjkit/auth/plugin.py +496 -0
  6. fjkit/auth/sources.py +24 -0
  7. fjkit/auth/stores.py +141 -0
  8. fjkit/auth/types.py +141 -0
  9. fjkit/cli/__init__.py +67 -0
  10. fjkit/cli/build_css.py +135 -0
  11. fjkit/cli/check.py +193 -0
  12. fjkit/cli/eject.py +562 -0
  13. fjkit/cli/ejected.py +202 -0
  14. fjkit/cli/vocabulary.py +85 -0
  15. fjkit/config.py +130 -0
  16. fjkit/errors.py +156 -0
  17. fjkit/flash.py +189 -0
  18. fjkit/forms.py +145 -0
  19. fjkit/htmx.py +76 -0
  20. fjkit/icons.py +60 -0
  21. fjkit/messages.py +227 -0
  22. fjkit/mounting.py +72 -0
  23. fjkit/plugins.py +223 -0
  24. fjkit/py.typed +0 -0
  25. fjkit/rendering.py +554 -0
  26. fjkit/signing.py +63 -0
  27. fjkit/static/brand/torii-bolt.png +0 -0
  28. fjkit/static/dist/fjkit-luma.css +2 -0
  29. fjkit/static/dist/fjkit-lyra.css +2 -0
  30. fjkit/static/dist/fjkit-maia.css +2 -0
  31. fjkit/static/dist/fjkit-mira.css +2 -0
  32. fjkit/static/dist/fjkit-nova.css +2 -0
  33. fjkit/static/dist/fjkit-rhea.css +2 -0
  34. fjkit/static/dist/fjkit-sera.css +2 -0
  35. fjkit/static/dist/fjkit-vega.css +2 -0
  36. fjkit/static/icons/lucide.json +1 -0
  37. fjkit/static/js/errors.js +117 -0
  38. fjkit/static/js/multiselect.js +126 -0
  39. fjkit/static/js/reveal.js +51 -0
  40. fjkit/static/js/select.js +113 -0
  41. fjkit/static/src/fjkit.css +385 -0
  42. fjkit/static/vendor/basecoat/base/base.css +136 -0
  43. fjkit/static/vendor/basecoat/basecoat-base.css +2 -0
  44. fjkit/static/vendor/basecoat/basecoat-components.css +39 -0
  45. fjkit/static/vendor/basecoat/basecoat-luma.css +2 -0
  46. fjkit/static/vendor/basecoat/basecoat-lyra.css +2 -0
  47. fjkit/static/vendor/basecoat/basecoat-maia.css +2 -0
  48. fjkit/static/vendor/basecoat/basecoat-mira.css +2 -0
  49. fjkit/static/vendor/basecoat/basecoat-nova.css +2 -0
  50. fjkit/static/vendor/basecoat/basecoat-rhea.css +2 -0
  51. fjkit/static/vendor/basecoat/basecoat-sera.css +2 -0
  52. fjkit/static/vendor/basecoat/basecoat-vega.css +2 -0
  53. fjkit/static/vendor/basecoat/basecoat.all.css +1 -0
  54. fjkit/static/vendor/basecoat/basecoat.css +1 -0
  55. fjkit/static/vendor/basecoat/components/accordion.css +27 -0
  56. fjkit/static/vendor/basecoat/components/alert.css +38 -0
  57. fjkit/static/vendor/basecoat/components/avatar.css +47 -0
  58. fjkit/static/vendor/basecoat/components/badge.css +6 -0
  59. fjkit/static/vendor/basecoat/components/breadcrumb.css +30 -0
  60. fjkit/static/vendor/basecoat/components/button-group.css +54 -0
  61. fjkit/static/vendor/basecoat/components/button.css +6 -0
  62. fjkit/static/vendor/basecoat/components/card.css +26 -0
  63. fjkit/static/vendor/basecoat/components/chart.css +56 -0
  64. fjkit/static/vendor/basecoat/components/checkbox.css +12 -0
  65. fjkit/static/vendor/basecoat/components/collapsible.css +21 -0
  66. fjkit/static/vendor/basecoat/components/combobox.css +99 -0
  67. fjkit/static/vendor/basecoat/components/command.css +140 -0
  68. fjkit/static/vendor/basecoat/components/dialog.css +101 -0
  69. fjkit/static/vendor/basecoat/components/drawer.css +89 -0
  70. fjkit/static/vendor/basecoat/components/dropdown-menu.css +56 -0
  71. fjkit/static/vendor/basecoat/components/empty.css +26 -0
  72. fjkit/static/vendor/basecoat/components/field.css +82 -0
  73. fjkit/static/vendor/basecoat/components/form.css +9 -0
  74. fjkit/static/vendor/basecoat/components/input-group.css +74 -0
  75. fjkit/static/vendor/basecoat/components/input.css +32 -0
  76. fjkit/static/vendor/basecoat/components/item.css +50 -0
  77. fjkit/static/vendor/basecoat/components/kbd.css +6 -0
  78. fjkit/static/vendor/basecoat/components/label.css +13 -0
  79. fjkit/static/vendor/basecoat/components/native-select.css +12 -0
  80. fjkit/static/vendor/basecoat/components/popover.css +107 -0
  81. fjkit/static/vendor/basecoat/components/progress.css +14 -0
  82. fjkit/static/vendor/basecoat/components/radio.css +11 -0
  83. fjkit/static/vendor/basecoat/components/range.css +50 -0
  84. fjkit/static/vendor/basecoat/components/scrollbar.css +46 -0
  85. fjkit/static/vendor/basecoat/components/select.css +64 -0
  86. fjkit/static/vendor/basecoat/components/sidebar.css +101 -0
  87. fjkit/static/vendor/basecoat/components/skeleton.css +6 -0
  88. fjkit/static/vendor/basecoat/components/switch.css +8 -0
  89. fjkit/static/vendor/basecoat/components/table.css +34 -0
  90. fjkit/static/vendor/basecoat/components/tabs.css +31 -0
  91. fjkit/static/vendor/basecoat/components/textarea.css +7 -0
  92. fjkit/static/vendor/basecoat/components/toast.css +57 -0
  93. fjkit/static/vendor/basecoat/components/tooltip.css +82 -0
  94. fjkit/static/vendor/basecoat/js/all.min.js +1 -0
  95. fjkit/static/vendor/basecoat/styles/luma.css +1376 -0
  96. fjkit/static/vendor/basecoat/styles/lyra.css +1379 -0
  97. fjkit/static/vendor/basecoat/styles/maia.css +1364 -0
  98. fjkit/static/vendor/basecoat/styles/mira.css +1373 -0
  99. fjkit/static/vendor/basecoat/styles/nova.css +1387 -0
  100. fjkit/static/vendor/basecoat/styles/rhea.css +1381 -0
  101. fjkit/static/vendor/basecoat/styles/sera.css +1397 -0
  102. fjkit/static/vendor/basecoat/styles/vega.css +1386 -0
  103. fjkit/static/vendor/htmx/htmx.min.js +1 -0
  104. fjkit/static/vendor/htmx/json-enc.js +38 -0
  105. fjkit/styles.py +30 -0
  106. fjkit/templates/errors/page.html +92 -0
  107. fjkit/templates/ui/attrs.html +18 -0
  108. fjkit/templates/ui/button.html +64 -0
  109. fjkit/templates/ui/data.html +362 -0
  110. fjkit/templates/ui/disclosure.html +89 -0
  111. fjkit/templates/ui/feedback.html +313 -0
  112. fjkit/templates/ui/form.html +595 -0
  113. fjkit/templates/ui/icon.html +13 -0
  114. fjkit/templates/ui/layout.html +189 -0
  115. fjkit/templates/ui/nav.html +110 -0
  116. fjkit/templates/ui/overlay.html +624 -0
  117. fjkit/templates/ui/shell.html +170 -0
  118. fjkit/templates/ui/sidebar.html +170 -0
  119. fjkit/templates/ui/table.html +427 -0
  120. fjkit/templates/ui/tabs.html +133 -0
  121. fjkit/templating.py +365 -0
  122. fjkit/vendored.py +46 -0
  123. fjkit-0.1.0.dev0.dist-info/METADATA +119 -0
  124. fjkit-0.1.0.dev0.dist-info/RECORD +127 -0
  125. fjkit-0.1.0.dev0.dist-info/WHEEL +4 -0
  126. fjkit-0.1.0.dev0.dist-info/entry_points.txt +2 -0
  127. fjkit-0.1.0.dev0.dist-info/licenses/LICENSE +21 -0
fjkit/mounting.py ADDED
@@ -0,0 +1,72 @@
1
+ """One call to wire fjkit into a FastAPI app.
2
+
3
+ Two things have to happen before a route can render: the kit's assets need a
4
+ URL, and the Jinja Environment needs building. Both read the same `FjkitConfig`
5
+ and neither is useful without the other, so they are one call rather than two
6
+ an app must keep in step.
7
+
8
+ The Environment is built here, at app construction, rather than in lifespan.
9
+ It is per-process either way — every worker constructs the app — and building
10
+ it here means a `TestClient` used without its context manager still has
11
+ templates, which is the failure this consolidation removes.
12
+ """
13
+
14
+ from __future__ import annotations
15
+
16
+ from fastapi import FastAPI
17
+ from fastapi.staticfiles import StaticFiles
18
+
19
+ from fjkit.config import STATIC_DIR, FjkitConfig
20
+ from fjkit.errors import install_error_handlers
21
+ from fjkit.plugins import install_plugins
22
+ from fjkit.styles import resolve_style
23
+ from fjkit.templating import Templates
24
+
25
+ __all__ = ["mount_fjkit"]
26
+
27
+
28
+ def mount_fjkit(app: FastAPI, config: FjkitConfig | None = None) -> Templates:
29
+ """Mount the kit's static assets and build the Environment. Call once.
30
+
31
+ Serves the stylesheet and the vendored htmx/Basecoat JS out of the
32
+ installed package at `config.static_url`, then puts the `Templates` on
33
+ `app.state.templates`, where `@render` looks for it.
34
+
35
+ Returns the same `Templates` it stored, for a caller that renders outside a
36
+ request — a build script, a test.
37
+ """
38
+ config = config or FjkitConfig()
39
+
40
+ # Check the resolved pack, not just any pack: a wheel built before this pack
41
+ # existed would otherwise pass the check and 404 on the page. Resolving here
42
+ # as well as in the Environment is deliberate — an app that mounts but never
43
+ # renders should still fail at startup rather than on its first request.
44
+ style = resolve_style(config.style)
45
+ stylesheet = STATIC_DIR / "dist" / f"fjkit-{style}.css"
46
+ if not stylesheet.exists():
47
+ raise RuntimeError(
48
+ f"fjkit's stylesheet for style={style!r} is missing from the "
49
+ f"installed package ({stylesheet}).\n"
50
+ "A released wheel ships all eight style packs. If you are working on "
51
+ "fjkit itself, build them once with: uv run fjkit build-css"
52
+ )
53
+
54
+ app.mount(
55
+ config.static_url,
56
+ StaticFiles(directory=STATIC_DIR),
57
+ name="fjkit_static",
58
+ )
59
+
60
+ # Before the plugins, because Starlette keys exception handlers by class and
61
+ # the last registration wins: going first lets a plugin — or the app —
62
+ # replace the kit's answer to a rejected form rather than be shadowed by it.
63
+ install_error_handlers(app, config)
64
+
65
+ # Before the Environment: a plugin's `mount` may refuse a configuration
66
+ # outright, and failing there is cheaper and clearer than failing after
67
+ # every template in the app has been compiled.
68
+ install_plugins(app, config)
69
+
70
+ templates = Templates.create(config)
71
+ app.state.templates = templates
72
+ return templates
fjkit/plugins.py ADDED
@@ -0,0 +1,223 @@
1
+ """The extension seam — one object that contributes to the app and to Jinja.
2
+
3
+ A plugin exists because some features need more than a macro: a middleware, an
4
+ exception handler, a value in every template's context. Wiring that by hand
5
+ means an app keeps four things in the right order and in step, which is the
6
+ failure `mount_fjkit()` removes one level down.
7
+
8
+ Two contribution points, because the kit has two moments:
9
+
10
+ * `mount(AppSetup)` runs when the app is constructed — middleware, exception
11
+ handlers, routes, static files.
12
+ * `extend(EnvSetup)` runs when the Environment is built — template directories,
13
+ globals, filters, and the per-request context.
14
+
15
+ Both are optional. A plugin that only puts a value in every template writes
16
+ only `extend`. Hiding the split would be friendlier right up until someone's
17
+ `mount` silently never ran.
18
+
19
+ A plugin deliberately **cannot** inject markup into the shell. Such a hook
20
+ would let any plugin put a `<script>` on every page, and both "no build step in
21
+ the app" and the closed vocabulary would leave through it.
22
+ """
23
+
24
+ from __future__ import annotations
25
+
26
+ import warnings
27
+ from collections.abc import Callable, Mapping, Sequence
28
+ from dataclasses import dataclass, field
29
+ from pathlib import Path
30
+ from typing import TYPE_CHECKING, Any, Protocol, runtime_checkable
31
+
32
+ from fastapi import APIRouter, FastAPI, Request
33
+ from fastapi.staticfiles import StaticFiles
34
+
35
+ if TYPE_CHECKING:
36
+ from fjkit.config import FjkitConfig
37
+
38
+ __all__ = [
39
+ "AppSetup",
40
+ "ContextProcessor",
41
+ "EnvSetup",
42
+ "Plugin",
43
+ "PluginWarning",
44
+ "collect_env",
45
+ "install_plugins",
46
+ ]
47
+
48
+ #: Called once per render with the live request, returning values to merge into
49
+ #: the template context. **Synchronous**: it runs inside `Templates.page()`,
50
+ #: which a `def` handler reaches from the threadpool. A plugin that needs I/O
51
+ #: does it in middleware and leaves the result on `request.state` for the
52
+ #: processor to read, which is what the auth plugin does.
53
+ ContextProcessor = Callable[[Request], Mapping[str, object]]
54
+
55
+
56
+ class PluginWarning(UserWarning):
57
+ """A plugin's install-time check found a configuration that fails later.
58
+
59
+ Its own category, so an app can turn it into an error:
60
+ `warnings.simplefilter("error", PluginWarning)`.
61
+ """
62
+
63
+
64
+ @runtime_checkable
65
+ class Plugin(Protocol):
66
+ """What `FjkitConfig.plugins` accepts.
67
+
68
+ `name` identifies the plugin rather than labelling it: it detects duplicate
69
+ registration, names the `request.state` field the plugin owns, and appears
70
+ in the error when two plugins claim one global or context key.
71
+ """
72
+
73
+ name: str
74
+
75
+ def mount(self, setup: AppSetup) -> None:
76
+ """App construction. Optional."""
77
+
78
+ def extend(self, setup: EnvSetup) -> None:
79
+ """Environment build. Optional."""
80
+
81
+
82
+ class AppSetup:
83
+ """What a plugin may do to the app. Handed to `Plugin.mount`.
84
+
85
+ A pass-through to FastAPI rather than a wrapper with opinions, except that
86
+ everything it does is attributed to the plugin, so a misbehaving one is
87
+ named in the traceback rather than found by elimination.
88
+ """
89
+
90
+ __slots__ = ("app", "config", "_name")
91
+
92
+ def __init__(self, app: FastAPI, config: FjkitConfig, name: str) -> None:
93
+ self.app = app
94
+ self.config = config
95
+ self._name = name
96
+
97
+ def add_middleware(self, cls: type, /, **options: Any) -> None:
98
+ """Starlette runs middleware in reverse registration order, so a plugin
99
+ listed later in `FjkitConfig.plugins` wraps the earlier ones."""
100
+ self.app.add_middleware(cls, **options)
101
+
102
+ def add_exception_handler(self, exc: type[Exception] | int, handler: Callable) -> None:
103
+ self.app.add_exception_handler(exc, handler)
104
+
105
+ def include_router(self, router: APIRouter) -> None:
106
+ self.app.include_router(router)
107
+
108
+ def mount_static(self, url: str, directory: Path) -> None:
109
+ self.app.mount(url, StaticFiles(directory=directory), name=f"fjkit_{self._name}_static")
110
+
111
+ def warn(self, message: str) -> None:
112
+ """Report at startup a configuration that misbehaves later.
113
+
114
+ For the combinations only the plugin recognises — an in-memory store
115
+ under a production config, a token source that cannot refresh. Each
116
+ surfaces days later on another machine.
117
+ """
118
+ warnings.warn(f"[fjkit:{self._name}] {message}", PluginWarning, stacklevel=2)
119
+
120
+
121
+ class EnvSetup:
122
+ """What a plugin may contribute to Jinja. Handed to `Plugin.extend`.
123
+
124
+ Collects rather than mutates: the Environment does not exist yet, because
125
+ template directories have to be in the search path before it is built.
126
+ """
127
+
128
+ __slots__ = ("config", "_current", "_owners", "contributions")
129
+
130
+ def __init__(self, config: FjkitConfig) -> None:
131
+ self.config = config
132
+ self.contributions = EnvContributions()
133
+ self._current = ""
134
+ #: key -> the plugin that claimed it, for the collision message.
135
+ self._owners: dict[str, str] = {}
136
+
137
+ def add_template_dir(self, directory: Path) -> None:
138
+ """Add a template directory, searched after the app's and before the
139
+ kit's.
140
+
141
+ After the app's, so a plugin can never shadow a file the app wrote —
142
+ that would break `fjkit eject` (CHARTER A5) from a direction the app
143
+ cannot see. Before the kit's, so a plugin can replace a kit macro.
144
+ """
145
+ self.contributions.template_dirs.append(directory)
146
+
147
+ def add_global(self, name: str, value: object) -> None:
148
+ self._claim(f"global {name!r}", name)
149
+ self.contributions.globals[name] = value
150
+
151
+ def add_filter(self, name: str, fn: Callable[..., Any]) -> None:
152
+ self._claim(f"filter {name!r}", f"filter:{name}")
153
+ self.contributions.filters[name] = fn
154
+
155
+ def add_context_processor(self, fn: ContextProcessor, *, provides: Sequence[str]) -> None:
156
+ """Merge values into every template's context, once per render.
157
+
158
+ `provides` lists the keys the processor returns. Required rather than
159
+ inferred: without it, two plugins fighting over `user` show up only by
160
+ rendering a page and noticing. With it, the clash is a startup error
161
+ naming both plugins.
162
+ """
163
+ for key in provides:
164
+ self._claim(f"context key {key!r}", key)
165
+ self.contributions.processors.append((self._current, tuple(provides), fn))
166
+
167
+ def _claim(self, what: str, key: str) -> None:
168
+ owner = self._owners.get(key)
169
+ if owner is not None:
170
+ raise ValueError(
171
+ f"fjkit plugins {owner!r} and {self._current!r} both provide {what}. "
172
+ "Rename one, or drop one from FjkitConfig.plugins."
173
+ )
174
+ self._owners[key] = self._current
175
+
176
+
177
+ @dataclass(slots=True)
178
+ class EnvContributions:
179
+ """Everything the plugins added, ready for `build_environment` to apply."""
180
+
181
+ template_dirs: list[Path] = field(default_factory=list)
182
+ globals: dict[str, object] = field(default_factory=dict)
183
+ filters: dict[str, Callable[..., Any]] = field(default_factory=dict)
184
+ #: (plugin name, declared keys, callable) — the first two exist only to name
185
+ #: a processor that returns a key it never declared.
186
+ processors: list[tuple[str, tuple[str, ...], ContextProcessor]] = field(default_factory=list)
187
+
188
+
189
+ def collect_env(config: FjkitConfig) -> EnvContributions:
190
+ """Run every plugin's `extend`, in `FjkitConfig.plugins` order."""
191
+ setup = EnvSetup(config)
192
+ for plugin in _ordered(config):
193
+ setup._current = plugin.name
194
+ extend = getattr(plugin, "extend", None)
195
+ if extend is not None:
196
+ extend(setup)
197
+ return setup.contributions
198
+
199
+
200
+ def install_plugins(app: FastAPI, config: FjkitConfig) -> None:
201
+ """Run every plugin's `mount`, in `FjkitConfig.plugins` order."""
202
+ for plugin in _ordered(config):
203
+ mount = getattr(plugin, "mount", None)
204
+ if mount is not None:
205
+ mount(AppSetup(app, config, plugin.name))
206
+
207
+
208
+ def _ordered(config: FjkitConfig) -> tuple[Plugin, ...]:
209
+ """The configured plugins, with duplicate names rejected.
210
+
211
+ Two plugins under one name would collide on `request.state.<name>` and make
212
+ every later error message ambiguous, so the duplicate is refused at startup
213
+ rather than resolved by last-one-wins.
214
+ """
215
+ seen: set[str] = set()
216
+ for plugin in config.plugins:
217
+ name = getattr(plugin, "name", None)
218
+ if not name:
219
+ raise ValueError(f"fjkit plugin {plugin!r} has no `name`.")
220
+ if name in seen:
221
+ raise ValueError(f"fjkit plugin {name!r} is registered twice in FjkitConfig.plugins.")
222
+ seen.add(name)
223
+ return tuple(config.plugins)
fjkit/py.typed ADDED
File without changes