resource-engine-devtools 0.1.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.
- resource_engine_devtools/__init__.py +6 -0
- resource_engine_devtools/ai/__init__.py +1 -0
- resource_engine_devtools/ai/example.py +66 -0
- resource_engine_devtools/ai/installer.py +424 -0
- resource_engine_devtools/ai/skill/SKILL.md +130 -0
- resource_engine_devtools/ai/skill/examples/README.md +7 -0
- resource_engine_devtools/ai/skill/examples/invoices/__init__.py +0 -0
- resource_engine_devtools/ai/skill/examples/invoices/app.py +245 -0
- resource_engine_devtools/ai/skill/examples/invoices/application/__init__.py +0 -0
- resource_engine_devtools/ai/skill/examples/invoices/application/actions.py +61 -0
- resource_engine_devtools/ai/skill/examples/invoices/application/balances.py +51 -0
- resource_engine_devtools/ai/skill/examples/invoices/application/customers.py +35 -0
- resource_engine_devtools/ai/skill/examples/invoices/application/hooks.py +61 -0
- resource_engine_devtools/ai/skill/examples/invoices/application/processes.py +80 -0
- resource_engine_devtools/ai/skill/examples/invoices/application/resource.py +112 -0
- resource_engine_devtools/ai/skill/examples/invoices/application/schemas.py +164 -0
- resource_engine_devtools/ai/skill/examples/invoices/application/tags.py +77 -0
- resource_engine_devtools/ai/skill/examples/invoices/authz.py +126 -0
- resource_engine_devtools/ai/skill/examples/invoices/bootstrap.py +80 -0
- resource_engine_devtools/ai/skill/examples/invoices/domain/__init__.py +0 -0
- resource_engine_devtools/ai/skill/examples/invoices/domain/entities.py +112 -0
- resource_engine_devtools/ai/skill/examples/invoices/domain/events.py +22 -0
- resource_engine_devtools/ai/skill/examples/invoices/domain/state_machine.py +88 -0
- resource_engine_devtools/ai/skill/examples/invoices/infrastructure/__init__.py +0 -0
- resource_engine_devtools/ai/skill/examples/invoices/infrastructure/persistence.py +121 -0
- resource_engine_devtools/ai/skill/examples/invoices/locale/en/LC_MESSAGES/messages.po +682 -0
- resource_engine_devtools/ai/skill/examples/invoices/locale/es/LC_MESSAGES/messages.po +685 -0
- resource_engine_devtools/ai/skill/examples/invoices/notifications.py +47 -0
- resource_engine_devtools/ai/skill/examples/invoices/templates/email/base.html +7 -0
- resource_engine_devtools/ai/skill/examples/invoices/templates/email/invoice_approved.html +5 -0
- resource_engine_devtools/ai/skill/examples/invoices/templates/email/invoice_approved.subject.txt +1 -0
- resource_engine_devtools/ai/skill/examples/invoices/templates/email/invoice_approved.txt +5 -0
- resource_engine_devtools/ai/skill/examples/invoices/tests/conftest.py +40 -0
- resource_engine_devtools/ai/skill/examples/invoices/tests/test_invoices_engine.py +241 -0
- resource_engine_devtools/ai/skill/examples/invoices/worker.py +53 -0
- resource_engine_devtools/ai/skill/llms.txt +25 -0
- resource_engine_devtools/ai/skill/references/anti-patterns.md +209 -0
- resource_engine_devtools/ai/skill/references/docstrings.md +97 -0
- resource_engine_devtools/ai/skill/references/errors.md +81 -0
- resource_engine_devtools/ai/skill/references/lifecycle.md +118 -0
- resource_engine_devtools/ai/skill/references/permissions.md +111 -0
- resource_engine_devtools/ai/skill/references/resource.md +301 -0
- resource_engine_devtools/ai/skill/references/state-machines.md +126 -0
- resource_engine_devtools/ai/skill/references/testing.md +80 -0
- resource_engine_devtools/ai/templates/agents_block.md +26 -0
- resource_engine_devtools/ai/templates/cursor_rule.mdc +7 -0
- resource_engine_devtools/check.py +67 -0
- resource_engine_devtools/cli.py +1112 -0
- resource_engine_devtools/consumer_scaffold.py +715 -0
- resource_engine_devtools/contracts.py +471 -0
- resource_engine_devtools/db.py +132 -0
- resource_engine_devtools/declarations_check.py +116 -0
- resource_engine_devtools/documentation/__init__.py +10 -0
- resource_engine_devtools/documentation/docstrings.py +72 -0
- resource_engine_devtools/documentation/generators/__init__.py +9 -0
- resource_engine_devtools/documentation/generators/ai_context.py +121 -0
- resource_engine_devtools/documentation/generators/api.py +63 -0
- resource_engine_devtools/documentation/generators/json.py +12 -0
- resource_engine_devtools/documentation/generators/markdown.py +687 -0
- resource_engine_devtools/documentation/generators/mermaid.py +22 -0
- resource_engine_devtools/documentation/generators/site.py +379 -0
- resource_engine_devtools/documentation/generators/site_assets/site.css +62 -0
- resource_engine_devtools/documentation/generators/site_assets/site.js +54 -0
- resource_engine_devtools/documentation/i18n.py +185 -0
- resource_engine_devtools/documentation/introspector.py +563 -0
- resource_engine_devtools/documentation/locale/ca/LC_MESSAGES/docs.po +628 -0
- resource_engine_devtools/documentation/locale/es/LC_MESSAGES/docs.po +627 -0
- resource_engine_devtools/documentation/model.py +311 -0
- resource_engine_devtools/documentation/validator.py +128 -0
- resource_engine_devtools/emails.py +172 -0
- resource_engine_devtools/events_check.py +124 -0
- resource_engine_devtools/gates_check.py +133 -0
- resource_engine_devtools/hooks_check.py +287 -0
- resource_engine_devtools/i18n.py +312 -0
- resource_engine_devtools/loader.py +93 -0
- resource_engine_devtools/logic_check.py +136 -0
- resource_engine_devtools/mcp_server.py +685 -0
- resource_engine_devtools/py.typed +0 -0
- resource_engine_devtools/scaffold.py +805 -0
- resource_engine_devtools/scaffold_parts.py +637 -0
- resource_engine_devtools/source_edit.py +424 -0
- resource_engine_devtools/templates_check.py +59 -0
- resource_engine_devtools/worker.py +68 -0
- resource_engine_devtools-0.1.0.dist-info/METADATA +38 -0
- resource_engine_devtools-0.1.0.dist-info/RECORD +88 -0
- resource_engine_devtools-0.1.0.dist-info/WHEEL +4 -0
- resource_engine_devtools-0.1.0.dist-info/entry_points.txt +2 -0
- resource_engine_devtools-0.1.0.dist-info/licenses/LICENSE +202 -0
|
@@ -0,0 +1,563 @@
|
|
|
1
|
+
"""Reads a registry into the documentation model."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import dataclasses
|
|
6
|
+
import inspect
|
|
7
|
+
import types
|
|
8
|
+
import typing
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
import resource_engine
|
|
12
|
+
from pydantic import BaseModel
|
|
13
|
+
from resource_engine import ResourceDefinition, ResourceRegistry
|
|
14
|
+
from resource_engine.integration import event_doc, has_external_effect
|
|
15
|
+
|
|
16
|
+
from resource_engine_devtools.documentation.docstrings import DocText, parse_doc
|
|
17
|
+
from resource_engine_devtools.documentation.model import (
|
|
18
|
+
ActionDoc,
|
|
19
|
+
DocumentationModel,
|
|
20
|
+
DocumentTemplateDoc,
|
|
21
|
+
DocumentUseDoc,
|
|
22
|
+
DocumentVariantDoc,
|
|
23
|
+
EmailTemplateDoc,
|
|
24
|
+
EmailUseDoc,
|
|
25
|
+
EmailVariantDoc,
|
|
26
|
+
EventDoc,
|
|
27
|
+
FieldDoc,
|
|
28
|
+
HookDoc,
|
|
29
|
+
OperationDoc,
|
|
30
|
+
PrintoutDoc,
|
|
31
|
+
ReactionDoc,
|
|
32
|
+
ReferenceDoc,
|
|
33
|
+
ResourceDoc,
|
|
34
|
+
SchemaDoc,
|
|
35
|
+
SlotDoc,
|
|
36
|
+
StateDoc,
|
|
37
|
+
StateMachineDoc,
|
|
38
|
+
SystemRoleDoc,
|
|
39
|
+
TimerDoc,
|
|
40
|
+
TransitionDoc,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
_CRUD_HTTP = {
|
|
44
|
+
"list": ("GET", ""),
|
|
45
|
+
"get": ("GET", "/{id}"),
|
|
46
|
+
"create": ("POST", ""),
|
|
47
|
+
"update": ("PATCH", "/{id}"),
|
|
48
|
+
"delete": ("DELETE", "/{id}"),
|
|
49
|
+
}
|
|
50
|
+
_CRUD_SCHEMAS = {
|
|
51
|
+
"list": (None, "read"),
|
|
52
|
+
"get": (None, "read"),
|
|
53
|
+
"create": ("create", "read"),
|
|
54
|
+
"update": ("update", "read"),
|
|
55
|
+
"delete": (None, None),
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _type_name(annotation: Any) -> str:
|
|
60
|
+
"""A type as it reads in Python 3.12+ source (``X | None``, ``Literal['a']``),
|
|
61
|
+
the same on every Python version (``str()`` of typing objects is not)."""
|
|
62
|
+
if annotation is type(None) or annotation is None:
|
|
63
|
+
return "None"
|
|
64
|
+
if annotation is Any:
|
|
65
|
+
return "Any"
|
|
66
|
+
if annotation is Ellipsis:
|
|
67
|
+
return "..."
|
|
68
|
+
origin, args = typing.get_origin(annotation), typing.get_args(annotation)
|
|
69
|
+
if origin is typing.Annotated:
|
|
70
|
+
return _type_name(args[0])
|
|
71
|
+
if origin in (typing.Union, types.UnionType):
|
|
72
|
+
parts = [_type_name(a) for a in args if a is not type(None)]
|
|
73
|
+
return " | ".join(parts + (["None"] if type(None) in args else []))
|
|
74
|
+
if origin is typing.Literal:
|
|
75
|
+
return f"Literal[{', '.join(repr(a) for a in args)}]"
|
|
76
|
+
if origin is not None:
|
|
77
|
+
name = getattr(origin, "__name__", None) or str(origin).replace("typing.", "")
|
|
78
|
+
return f"{name}[{', '.join(_type_name(a) for a in args)}]" if args else name
|
|
79
|
+
if inspect.isclass(annotation):
|
|
80
|
+
return annotation.__name__
|
|
81
|
+
if isinstance(annotation, typing.ForwardRef):
|
|
82
|
+
return annotation.__forward_arg__
|
|
83
|
+
return str(annotation).replace("typing.", "")
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _schema_doc(role: str, schema: type[BaseModel]) -> SchemaDoc:
|
|
87
|
+
fields = [
|
|
88
|
+
FieldDoc(
|
|
89
|
+
name=name,
|
|
90
|
+
type=_type_name(info.annotation),
|
|
91
|
+
required=info.is_required(),
|
|
92
|
+
description=info.description,
|
|
93
|
+
)
|
|
94
|
+
for name, info in schema.model_fields.items()
|
|
95
|
+
]
|
|
96
|
+
return SchemaDoc(name=schema.__name__, role=role, fields=fields, doc=parse_doc(schema))
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _event_doc(event: type) -> EventDoc:
|
|
100
|
+
hints = typing.get_type_hints(event)
|
|
101
|
+
fields = [
|
|
102
|
+
FieldDoc(
|
|
103
|
+
name=f.name,
|
|
104
|
+
type=_type_name(hints.get(f.name, f.type)),
|
|
105
|
+
required=f.default is dataclasses.MISSING and f.default_factory is dataclasses.MISSING,
|
|
106
|
+
)
|
|
107
|
+
for f in dataclasses.fields(event)
|
|
108
|
+
if f.name != "occurred_at"
|
|
109
|
+
]
|
|
110
|
+
text = event_doc(event)
|
|
111
|
+
return EventDoc(
|
|
112
|
+
name=event.__name__,
|
|
113
|
+
qualname=_qualname(event),
|
|
114
|
+
doc=parse_doc(inspect.cleandoc(text)) if text else DocText(),
|
|
115
|
+
fields=fields,
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _reaction_doc(on: Any) -> ReactionDoc:
|
|
120
|
+
when = [
|
|
121
|
+
f"{label} {value}"
|
|
122
|
+
for label, value in (
|
|
123
|
+
("resource", on.resource),
|
|
124
|
+
("transition", on.transition),
|
|
125
|
+
("action", on.action),
|
|
126
|
+
)
|
|
127
|
+
if value
|
|
128
|
+
]
|
|
129
|
+
text = on.description or inspect.getdoc(on.handler)
|
|
130
|
+
return ReactionDoc(
|
|
131
|
+
event=on.event_type,
|
|
132
|
+
when=", ".join(when),
|
|
133
|
+
key=on.key if isinstance(on.key, str) else getattr(on.key, "__name__", "custom"),
|
|
134
|
+
start=on.start,
|
|
135
|
+
handler=getattr(on.handler, "__qualname__", repr(on.handler)),
|
|
136
|
+
description=text.strip().split("\n\n")[0].replace("\n", " ") if text else None,
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def _reference_doc(ref: Any) -> ReferenceDoc:
|
|
141
|
+
return ReferenceDoc(
|
|
142
|
+
name=ref.name,
|
|
143
|
+
field=ref.field,
|
|
144
|
+
resource=ref.target if hasattr(ref, "target") else ref.resource,
|
|
145
|
+
on_delete=ref.on_delete,
|
|
146
|
+
description=ref.description,
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def _duration(value: Any) -> str:
|
|
151
|
+
"""``timedelta(days=30)`` → ``30d``; ``timedelta(hours=36)`` → ``1d 12h``."""
|
|
152
|
+
seconds = int(value.total_seconds())
|
|
153
|
+
parts = []
|
|
154
|
+
for unit, size in (("d", 86400), ("h", 3600), ("min", 60), ("s", 1)):
|
|
155
|
+
amount, seconds = divmod(seconds, size)
|
|
156
|
+
if amount:
|
|
157
|
+
parts.append(f"{amount}{unit}")
|
|
158
|
+
return " ".join(parts) or "0s"
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _qualname(obj: Any) -> str:
|
|
162
|
+
return f"{getattr(obj, '__module__', '?')}.{getattr(obj, '__qualname__', repr(obj))}"
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _resource_doc(d: ResourceDefinition) -> ResourceDoc:
|
|
166
|
+
base = f"/{d.name}"
|
|
167
|
+
operations: list[OperationDoc] = []
|
|
168
|
+
for op in d.operations:
|
|
169
|
+
method, suffix = _CRUD_HTTP[op]
|
|
170
|
+
inp, out = _CRUD_SCHEMAS[op]
|
|
171
|
+
operations.append(
|
|
172
|
+
OperationDoc(
|
|
173
|
+
name=op,
|
|
174
|
+
kind="crud",
|
|
175
|
+
method=method,
|
|
176
|
+
path=base + suffix,
|
|
177
|
+
doc=parse_doc(d.operation_docs[op]) if op in d.operation_docs else DocText(),
|
|
178
|
+
input_schema=d.schemas[inp].__name__ if inp else None,
|
|
179
|
+
output_schema=d.schemas[out].__name__ if out else None,
|
|
180
|
+
)
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
machine_docs: list[StateMachineDoc] = []
|
|
184
|
+
transition_text: dict[str, str] = {}
|
|
185
|
+
for m in d.machines.values():
|
|
186
|
+
machine_text = parse_doc(m.machine_cls)
|
|
187
|
+
state_text = machine_text.items.get("states", {})
|
|
188
|
+
own_text = machine_text.items.get("transitions", {})
|
|
189
|
+
transition_text.update({e: t for e, t in own_text.items() if e in m.info.events})
|
|
190
|
+
machine_docs.append(
|
|
191
|
+
StateMachineDoc(
|
|
192
|
+
name=m.machine_cls.__name__,
|
|
193
|
+
qualname=_qualname(m.machine_cls),
|
|
194
|
+
state_field=m.state_field,
|
|
195
|
+
doc=machine_text,
|
|
196
|
+
states=[
|
|
197
|
+
StateDoc(s.id, s.name, s.initial, s.final, state_text.get(s.id))
|
|
198
|
+
for s in m.info.states
|
|
199
|
+
],
|
|
200
|
+
transitions=[
|
|
201
|
+
TransitionDoc(t.event, t.source, t.target, own_text.get(t.event))
|
|
202
|
+
for t in m.info.transitions
|
|
203
|
+
],
|
|
204
|
+
timers=[
|
|
205
|
+
TimerDoc(
|
|
206
|
+
event=t.event,
|
|
207
|
+
after=_duration(t.after),
|
|
208
|
+
since=t.since,
|
|
209
|
+
states=[str(s) for s in t.states],
|
|
210
|
+
description=t.description,
|
|
211
|
+
)
|
|
212
|
+
for t in d.timers
|
|
213
|
+
if t.event in m.info.events
|
|
214
|
+
],
|
|
215
|
+
since_field=m.since_field,
|
|
216
|
+
machine=m.name,
|
|
217
|
+
)
|
|
218
|
+
)
|
|
219
|
+
if d.machine is not None:
|
|
220
|
+
for event in d.transitions:
|
|
221
|
+
schema = d.transition_schemas.get(event)
|
|
222
|
+
text = transition_text.get(event)
|
|
223
|
+
operations.append(
|
|
224
|
+
OperationDoc(
|
|
225
|
+
name=event,
|
|
226
|
+
kind="transition",
|
|
227
|
+
method="POST",
|
|
228
|
+
path=f"{base}/{{id}}/transitions/{event}",
|
|
229
|
+
doc=parse_doc(text) if text else DocText(),
|
|
230
|
+
input_schema=schema.__name__ if schema else None,
|
|
231
|
+
output_schema=d.schemas["read"].__name__ if "read" in d.schemas else None,
|
|
232
|
+
)
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
actions: list[ActionDoc] = []
|
|
236
|
+
for a in d.actions.values():
|
|
237
|
+
doc = parse_doc(a.action_cls)
|
|
238
|
+
actions.append(
|
|
239
|
+
ActionDoc(
|
|
240
|
+
name=a.name,
|
|
241
|
+
qualname=_qualname(a.action_cls),
|
|
242
|
+
instance=a.instance,
|
|
243
|
+
doc=doc,
|
|
244
|
+
input_schema=a.input_schema.__name__ if a.input_schema else None,
|
|
245
|
+
output_schema=a.output_schema.__name__ if a.output_schema else None,
|
|
246
|
+
)
|
|
247
|
+
)
|
|
248
|
+
operations.append(
|
|
249
|
+
OperationDoc(
|
|
250
|
+
name=a.name,
|
|
251
|
+
kind="action",
|
|
252
|
+
method="POST",
|
|
253
|
+
path=f"{base}/{{id}}/actions/{a.name}"
|
|
254
|
+
if a.instance
|
|
255
|
+
else f"{base}/actions/{a.name}",
|
|
256
|
+
doc=doc,
|
|
257
|
+
input_schema=a.input_schema.__name__ if a.input_schema else None,
|
|
258
|
+
output_schema=a.output_schema.__name__ if a.output_schema else None,
|
|
259
|
+
)
|
|
260
|
+
)
|
|
261
|
+
|
|
262
|
+
hooks = [
|
|
263
|
+
HookDoc(
|
|
264
|
+
point=str(key),
|
|
265
|
+
name=getattr(fn, "__name__", repr(fn)),
|
|
266
|
+
qualname=_qualname(fn),
|
|
267
|
+
external_effect=has_external_effect(fn),
|
|
268
|
+
doc=parse_doc(fn),
|
|
269
|
+
)
|
|
270
|
+
for key, fns in d.hooks.items()
|
|
271
|
+
for fn in fns
|
|
272
|
+
]
|
|
273
|
+
|
|
274
|
+
return ResourceDoc(
|
|
275
|
+
name=d.name,
|
|
276
|
+
qualname=_qualname(d.resource_cls),
|
|
277
|
+
module=d.resource_cls.__module__,
|
|
278
|
+
model=d.model.__name__,
|
|
279
|
+
doc=parse_doc(d.resource_cls),
|
|
280
|
+
operations=operations,
|
|
281
|
+
schemas=[_schema_doc(role, s) for role, s in d.schemas.items()],
|
|
282
|
+
hooks=hooks,
|
|
283
|
+
actions=actions,
|
|
284
|
+
state_machines=machine_docs,
|
|
285
|
+
discriminator=d.discriminator,
|
|
286
|
+
extends=d.parent.name if d.parent is not None else None,
|
|
287
|
+
identity=d.identity,
|
|
288
|
+
**_view_doc(d.view),
|
|
289
|
+
filterable=list(d.filterable),
|
|
290
|
+
sortable=list(d.sortable),
|
|
291
|
+
restricted_fields=list(d.restricted_fields),
|
|
292
|
+
tenant_field=d.tenant_field,
|
|
293
|
+
version_field=d.version_field,
|
|
294
|
+
require_version=d.require_version,
|
|
295
|
+
audit=d.audited,
|
|
296
|
+
audit_reads=d.audit_reads,
|
|
297
|
+
audit_exclude=list(d.audit_exclude),
|
|
298
|
+
events=[_event_doc(e) for e in d.events],
|
|
299
|
+
references=[_reference_doc(r) for r in d.references.values()],
|
|
300
|
+
correlation=getattr(d.resource_cls, "correlation", None)
|
|
301
|
+
if getattr(d.resource_cls, "reacts", None)
|
|
302
|
+
else None,
|
|
303
|
+
reacts=[_reaction_doc(on) for on in getattr(d.resource_cls, "reacts", ())],
|
|
304
|
+
documents=[
|
|
305
|
+
SlotDoc(
|
|
306
|
+
name=slot.name,
|
|
307
|
+
description=slot.description,
|
|
308
|
+
content_types=list(slot.content_types),
|
|
309
|
+
max_bytes=slot.max_bytes,
|
|
310
|
+
multiple=slot.multiple,
|
|
311
|
+
upload=slot.upload,
|
|
312
|
+
required_for=list(slot.required_for),
|
|
313
|
+
processing=slot.processing,
|
|
314
|
+
)
|
|
315
|
+
for slot in d.document_slots.values()
|
|
316
|
+
],
|
|
317
|
+
)
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def introspect(
|
|
321
|
+
registry: ResourceRegistry,
|
|
322
|
+
authz: Any = None,
|
|
323
|
+
*,
|
|
324
|
+
templates: Any = None,
|
|
325
|
+
email_prefix: str = "email/",
|
|
326
|
+
document_prefix: str = "documents/",
|
|
327
|
+
) -> DocumentationModel:
|
|
328
|
+
"""``authz``: an ``AuthzConfig`` to document system roles (permission matrix).
|
|
329
|
+
``templates``: the application's ``TemplateRenderer``, to show the emails
|
|
330
|
+
and the documents generated from templates."""
|
|
331
|
+
from resource_engine.authz import Catalog
|
|
332
|
+
from resource_engine.integration import FIELD_KINDS, names_field
|
|
333
|
+
|
|
334
|
+
catalog = Catalog.from_registry(registry)
|
|
335
|
+
# open fields follow their record: only restricted ones are worth a row
|
|
336
|
+
documented = [
|
|
337
|
+
name
|
|
338
|
+
for name, info in catalog.actions.items()
|
|
339
|
+
if info.kind not in FIELD_KINDS or info.restricted
|
|
340
|
+
]
|
|
341
|
+
roles = []
|
|
342
|
+
for role in getattr(authz, "system_roles", ()):
|
|
343
|
+
covered = sorted(
|
|
344
|
+
{
|
|
345
|
+
a
|
|
346
|
+
for g in role.grants
|
|
347
|
+
for a in catalog.matching(g, include_sensitive=role.include_sensitive)
|
|
348
|
+
if a in documented
|
|
349
|
+
and (not catalog.actions[a].restricted or role.include_sensitive or names_field(g))
|
|
350
|
+
}
|
|
351
|
+
)
|
|
352
|
+
roles.append(SystemRoleDoc(role.name, role.description, list(role.grants), covered))
|
|
353
|
+
resources = [_resource_doc(d) for d in registry]
|
|
354
|
+
by_name = {r.name: r for r in resources}
|
|
355
|
+
for d in registry:
|
|
356
|
+
if d.parent is not None and d.parent.name in by_name and d.identity:
|
|
357
|
+
by_name[d.parent.name].subtypes[d.identity] = d.name
|
|
358
|
+
by_name = {r.name: r for r in resources}
|
|
359
|
+
for d in registry:
|
|
360
|
+
for ref in d.references.values():
|
|
361
|
+
if ref.target in by_name:
|
|
362
|
+
by_name[ref.target].referenced_by.append(
|
|
363
|
+
ReferenceDoc(
|
|
364
|
+
name=ref.name,
|
|
365
|
+
field=f"{d.name}.{ref.field}",
|
|
366
|
+
resource=d.name,
|
|
367
|
+
on_delete=ref.on_delete,
|
|
368
|
+
description=ref.description,
|
|
369
|
+
related_name=ref.related_name if d.enabled("list") else None,
|
|
370
|
+
)
|
|
371
|
+
)
|
|
372
|
+
for d in registry:
|
|
373
|
+
if d.link is None:
|
|
374
|
+
continue
|
|
375
|
+
by_field = {r.field: r for r in d.references.values()}
|
|
376
|
+
a, b = (by_field[f] for f in d.link)
|
|
377
|
+
for source, target in ((a, b), (b, a)):
|
|
378
|
+
if source.target in by_name:
|
|
379
|
+
by_name[source.target].links.append(
|
|
380
|
+
ReferenceDoc(
|
|
381
|
+
name=d.link_names.get(source.field, target.target),
|
|
382
|
+
field=d.name,
|
|
383
|
+
resource=target.target,
|
|
384
|
+
on_delete="link",
|
|
385
|
+
)
|
|
386
|
+
)
|
|
387
|
+
emails = _emails(registry, by_name, templates, email_prefix)
|
|
388
|
+
documents = _documents(registry, by_name, templates, document_prefix)
|
|
389
|
+
return DocumentationModel(
|
|
390
|
+
resources=resources,
|
|
391
|
+
engine_version=resource_engine.__version__,
|
|
392
|
+
actions=documented,
|
|
393
|
+
audit_resource="audit_log" if "audit_log" in registry else None,
|
|
394
|
+
system_roles=roles,
|
|
395
|
+
emails=emails,
|
|
396
|
+
documents=documents,
|
|
397
|
+
)
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
def _emails(
|
|
401
|
+
registry: ResourceRegistry, by_name: dict[str, ResourceDoc], templates: Any, prefix: str
|
|
402
|
+
) -> list[EmailTemplateDoc]:
|
|
403
|
+
"""Each resource's requests, and every template set with a preview per locale."""
|
|
404
|
+
from resource_engine_devtools.emails import find_email_requests
|
|
405
|
+
from resource_engine_devtools.templates_check import missing_template
|
|
406
|
+
|
|
407
|
+
used: dict[str, list[str]] = {}
|
|
408
|
+
missing: dict[str, set[str]] = {}
|
|
409
|
+
seen: set[tuple[str, str, str | None]] = set()
|
|
410
|
+
for use in find_email_requests(registry):
|
|
411
|
+
resource = by_name.get(use.resource)
|
|
412
|
+
if resource is None or (use.point, use.function, use.template) in seen:
|
|
413
|
+
continue
|
|
414
|
+
seen.add((use.point, use.function, use.template))
|
|
415
|
+
resource.emails.append(
|
|
416
|
+
EmailUseDoc(use.template, use.point, use.function, use.locale, use.to)
|
|
417
|
+
)
|
|
418
|
+
if use.template is None:
|
|
419
|
+
continue
|
|
420
|
+
used.setdefault(use.template, []).append(f"{use.resource} {use.point}")
|
|
421
|
+
if templates is not None:
|
|
422
|
+
gap = missing_template(templates, prefix, use.template, use.locale)
|
|
423
|
+
if gap:
|
|
424
|
+
missing.setdefault(use.template, set()).add(gap)
|
|
425
|
+
names = set(used)
|
|
426
|
+
if templates is not None:
|
|
427
|
+
suffix = ".subject.txt"
|
|
428
|
+
names |= {
|
|
429
|
+
n[len(prefix) : -len(suffix)]
|
|
430
|
+
for n in templates.names()
|
|
431
|
+
if n.startswith(prefix) and n.endswith(suffix)
|
|
432
|
+
}
|
|
433
|
+
out = []
|
|
434
|
+
for name in sorted(names):
|
|
435
|
+
doc = EmailTemplateDoc(name, used.get(name, []), missing=sorted(missing.get(name, ())))
|
|
436
|
+
if templates is not None and hasattr(templates, "preview"):
|
|
437
|
+
doc.variants = _variants(templates, f"{prefix}{name}")
|
|
438
|
+
out.append(doc)
|
|
439
|
+
return out
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
DOCUMENT_EXTENSIONS = (".html", ".md", ".txt")
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
def _documents(
|
|
446
|
+
registry: ResourceRegistry, by_name: dict[str, ResourceDoc], templates: Any, prefix: str
|
|
447
|
+
) -> list[DocumentTemplateDoc]:
|
|
448
|
+
"""Each resource's printouts and generated documents, and every document
|
|
449
|
+
template with a preview per locale."""
|
|
450
|
+
from resource_engine_devtools.emails import find_document_renders
|
|
451
|
+
|
|
452
|
+
used: dict[str, list[str]] = {}
|
|
453
|
+
for d in registry:
|
|
454
|
+
resource = by_name.get(d.name)
|
|
455
|
+
for name, printout in getattr(d.resource_cls, "printouts", {}).items():
|
|
456
|
+
template = printout.template or name
|
|
457
|
+
if resource is not None:
|
|
458
|
+
resource.printouts.append(
|
|
459
|
+
PrintoutDoc(name, template, list(printout.formats), printout.description)
|
|
460
|
+
)
|
|
461
|
+
used.setdefault(template, []).append(f"{d.name} printout:{name}")
|
|
462
|
+
seen: set[tuple[str, str, str | None]] = set()
|
|
463
|
+
for use in find_document_renders(registry):
|
|
464
|
+
resource = by_name.get(use.resource)
|
|
465
|
+
if resource is None or (use.point, use.function, use.template) in seen:
|
|
466
|
+
continue
|
|
467
|
+
seen.add((use.point, use.function, use.template))
|
|
468
|
+
resource.generated.append(
|
|
469
|
+
DocumentUseDoc(use.template, use.slot, use.point, use.function, use.format)
|
|
470
|
+
)
|
|
471
|
+
if use.template:
|
|
472
|
+
used.setdefault(use.template, []).append(f"{use.resource} {use.point}")
|
|
473
|
+
names = set(used)
|
|
474
|
+
if templates is not None:
|
|
475
|
+
for name in templates.names():
|
|
476
|
+
if name.startswith(prefix) and name.endswith(DOCUMENT_EXTENSIONS):
|
|
477
|
+
names.add(name[len(prefix) :].rsplit(".", 1)[0])
|
|
478
|
+
out = []
|
|
479
|
+
for name in sorted(names):
|
|
480
|
+
doc = DocumentTemplateDoc(name, used.get(name, []))
|
|
481
|
+
if templates is not None and hasattr(templates, "preview"):
|
|
482
|
+
doc.variants = _document_variants(templates, f"{prefix}{name}")
|
|
483
|
+
out.append(doc)
|
|
484
|
+
return out
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
def _document_variants(templates: Any, base: str) -> list[DocumentVariantDoc]:
|
|
488
|
+
from resource_engine.errors import TemplateFailed, TemplateNotFound
|
|
489
|
+
|
|
490
|
+
files = [f"{base}{ext}" for ext in DOCUMENT_EXTENSIONS]
|
|
491
|
+
locales: list[str | None] = []
|
|
492
|
+
for name in files:
|
|
493
|
+
for locale in templates.variants(name):
|
|
494
|
+
if locale not in locales:
|
|
495
|
+
locales.append(locale)
|
|
496
|
+
default = getattr(templates, "default_locale", None)
|
|
497
|
+
locales.sort(key=lambda loc: (loc != default, loc is None, loc or ""))
|
|
498
|
+
|
|
499
|
+
def show(name: str, locale: str | None) -> str | None:
|
|
500
|
+
try:
|
|
501
|
+
preview: str = templates.preview(name, locale=locale)
|
|
502
|
+
return preview
|
|
503
|
+
except TemplateNotFound:
|
|
504
|
+
return None
|
|
505
|
+
except TemplateFailed as exc:
|
|
506
|
+
return f"⚠ {exc.message}"
|
|
507
|
+
|
|
508
|
+
out = []
|
|
509
|
+
for locale in locales:
|
|
510
|
+
html = show(files[0], locale)
|
|
511
|
+
text = show(files[1], locale) or show(files[2], locale)
|
|
512
|
+
if html or text:
|
|
513
|
+
out.append(DocumentVariantDoc(locale, html, text))
|
|
514
|
+
return out
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
def _variants(templates: Any, base: str) -> list[EmailVariantDoc]:
|
|
518
|
+
from resource_engine.errors import TemplateFailed, TemplateNotFound
|
|
519
|
+
|
|
520
|
+
files = (f"{base}.subject.txt", f"{base}.txt", f"{base}.html")
|
|
521
|
+
locales: list[str | None] = []
|
|
522
|
+
for name in files:
|
|
523
|
+
for locale in templates.variants(name):
|
|
524
|
+
if locale not in locales:
|
|
525
|
+
locales.append(locale)
|
|
526
|
+
default = getattr(templates, "default_locale", None)
|
|
527
|
+
# the default locale first, the unlocalised fallback last
|
|
528
|
+
locales.sort(key=lambda loc: (loc != default, loc is None, loc or ""))
|
|
529
|
+
|
|
530
|
+
def show(name: str, locale: str | None) -> str | None:
|
|
531
|
+
try:
|
|
532
|
+
preview: str = templates.preview(name, locale=locale)
|
|
533
|
+
return preview
|
|
534
|
+
except TemplateNotFound:
|
|
535
|
+
return None
|
|
536
|
+
except TemplateFailed as exc:
|
|
537
|
+
return f"⚠ {exc.message}"
|
|
538
|
+
|
|
539
|
+
return [
|
|
540
|
+
EmailVariantDoc(
|
|
541
|
+
locale,
|
|
542
|
+
" ".join((show(files[0], locale) or "").split()) or None,
|
|
543
|
+
show(files[1], locale),
|
|
544
|
+
show(files[2], locale),
|
|
545
|
+
)
|
|
546
|
+
for locale in locales
|
|
547
|
+
]
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
def _view_doc(view: Any) -> dict[str, Any]:
|
|
551
|
+
from resource_engine import Aggregate
|
|
552
|
+
|
|
553
|
+
if view is None:
|
|
554
|
+
return {}
|
|
555
|
+
if not isinstance(view, Aggregate):
|
|
556
|
+
return {"view": "custom"}
|
|
557
|
+
values = {}
|
|
558
|
+
for name, m in view.values.items():
|
|
559
|
+
text = f"{m.kind}({m.field or ''})"
|
|
560
|
+
if m.where:
|
|
561
|
+
text += " where " + ", ".join(f"{k}={v}" for k, v in m.where.items())
|
|
562
|
+
values[name] = text
|
|
563
|
+
return {"view": view.source, "view_group_by": list(view.group_by), "view_values": values}
|