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,116 @@
|
|
|
1
|
+
"""Declaration lint (``resource-engine check``): RE-C008.
|
|
2
|
+
|
|
3
|
+
A resource class says what the resource is; behaviour belongs in plain
|
|
4
|
+
functions and classes it lists (hooks marked with ``@hook``, ``Action``
|
|
5
|
+
classes, processes, domain services). The lint reports:
|
|
6
|
+
|
|
7
|
+
- methods defined in the class body, other than the mapping overrides
|
|
8
|
+
(``build_entity``, ``apply_update``, ``to_read``), and lambdas in its
|
|
9
|
+
settings;
|
|
10
|
+
- declarations longer than ``MAX_LINES`` lines (the docstring aside): move
|
|
11
|
+
long values (document slots, timers, printouts) to modules next to it.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import ast
|
|
17
|
+
import inspect
|
|
18
|
+
import textwrap
|
|
19
|
+
from typing import Any
|
|
20
|
+
|
|
21
|
+
from resource_engine import ResourceRegistry
|
|
22
|
+
|
|
23
|
+
from resource_engine_devtools.check import Violation
|
|
24
|
+
|
|
25
|
+
#: Lines a resource class body may take, its docstring aside.
|
|
26
|
+
MAX_LINES = 80
|
|
27
|
+
#: Methods a resource may override: how schemas map to entities.
|
|
28
|
+
MAPPING_OVERRIDES = frozenset({"build_entity", "apply_update", "to_read"})
|
|
29
|
+
|
|
30
|
+
_HINT_BEHAVIOUR = (
|
|
31
|
+
"Keep the declaration to what the resource is: move the code to a hook marked with "
|
|
32
|
+
"@hook.before(...) / @hook.after(...), an Action, or a plain function, and list it."
|
|
33
|
+
)
|
|
34
|
+
_HINT_LENGTH = (
|
|
35
|
+
"Move long values (document slots, timers, printouts, operation docs) to modules next "
|
|
36
|
+
"to the resource and refer to them; behaviour goes to hooks, actions and plain code."
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def check_declarations(registry: ResourceRegistry) -> list[Violation]:
|
|
41
|
+
"""RE-C008: resource declarations that hold behaviour or grew too long."""
|
|
42
|
+
violations: list[Violation] = []
|
|
43
|
+
seen: set[tuple[str, int]] = set()
|
|
44
|
+
for definition in registry:
|
|
45
|
+
cls = definition.resource_cls
|
|
46
|
+
if cls.__module__.startswith("resource_engine."):
|
|
47
|
+
continue # the engine's built-in resources
|
|
48
|
+
found = _source(cls)
|
|
49
|
+
if found is None:
|
|
50
|
+
continue
|
|
51
|
+
path, first, node = found
|
|
52
|
+
if (path, first) in seen:
|
|
53
|
+
continue # the same class registered twice (a tenant-scoped copy)
|
|
54
|
+
seen.add((path, first))
|
|
55
|
+
name = cls.__name__
|
|
56
|
+
for stmt in node.body:
|
|
57
|
+
if isinstance(stmt, ast.FunctionDef | ast.AsyncFunctionDef):
|
|
58
|
+
if stmt.name not in MAPPING_OVERRIDES:
|
|
59
|
+
violations.append(
|
|
60
|
+
Violation(
|
|
61
|
+
code="RE-C008",
|
|
62
|
+
path=path,
|
|
63
|
+
line=first + stmt.lineno - 1,
|
|
64
|
+
message=f"{name} defines the method {stmt.name}() in its declaration",
|
|
65
|
+
hint=_HINT_BEHAVIOUR,
|
|
66
|
+
)
|
|
67
|
+
)
|
|
68
|
+
elif isinstance(stmt, ast.Assign | ast.AnnAssign) and stmt.value is not None:
|
|
69
|
+
for sub in ast.walk(stmt.value):
|
|
70
|
+
if isinstance(sub, ast.Lambda):
|
|
71
|
+
violations.append(
|
|
72
|
+
Violation(
|
|
73
|
+
code="RE-C008",
|
|
74
|
+
path=path,
|
|
75
|
+
line=first + sub.lineno - 1,
|
|
76
|
+
message=f"{name} has a lambda in its declaration",
|
|
77
|
+
hint=_HINT_BEHAVIOUR,
|
|
78
|
+
)
|
|
79
|
+
)
|
|
80
|
+
body = node.body[1:] if _has_docstring(node) else node.body
|
|
81
|
+
if body:
|
|
82
|
+
length = body[-1].end_lineno - body[0].lineno + 1 # type: ignore[operator]
|
|
83
|
+
if length > MAX_LINES:
|
|
84
|
+
violations.append(
|
|
85
|
+
Violation(
|
|
86
|
+
code="RE-C008",
|
|
87
|
+
path=path,
|
|
88
|
+
line=first + node.lineno - 1,
|
|
89
|
+
message=f"{name}'s declaration is {length} lines long "
|
|
90
|
+
f"(more than {MAX_LINES})",
|
|
91
|
+
hint=_HINT_LENGTH,
|
|
92
|
+
)
|
|
93
|
+
)
|
|
94
|
+
return violations
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _source(cls: type[Any]) -> tuple[str, int, ast.ClassDef] | None:
|
|
98
|
+
try:
|
|
99
|
+
lines, first = inspect.getsourcelines(cls)
|
|
100
|
+
path = inspect.getsourcefile(cls)
|
|
101
|
+
except (OSError, TypeError):
|
|
102
|
+
return None
|
|
103
|
+
tree = ast.parse(textwrap.dedent("".join(lines)))
|
|
104
|
+
node = tree.body[0] if tree.body else None
|
|
105
|
+
if path is None or not isinstance(node, ast.ClassDef):
|
|
106
|
+
return None
|
|
107
|
+
return path, first, node
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _has_docstring(node: ast.ClassDef) -> bool:
|
|
111
|
+
head = node.body[0] if node.body else None
|
|
112
|
+
return (
|
|
113
|
+
isinstance(head, ast.Expr)
|
|
114
|
+
and isinstance(head.value, ast.Constant)
|
|
115
|
+
and isinstance(head.value.value, str)
|
|
116
|
+
)
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"""Documentation Engine: introspection → model → generators / validator.
|
|
2
|
+
|
|
3
|
+
It never participates in executing operations.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from resource_engine_devtools.documentation.introspector import introspect
|
|
7
|
+
from resource_engine_devtools.documentation.model import DocumentationModel
|
|
8
|
+
from resource_engine_devtools.documentation.validator import Finding, validate
|
|
9
|
+
|
|
10
|
+
__all__ = ["DocumentationModel", "Finding", "introspect", "validate"]
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""Docstring convention (Google style with functional sections), parsed with docstring-parser."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import inspect
|
|
6
|
+
from dataclasses import dataclass, field
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from docstring_parser.google import GoogleParser, Section, SectionType
|
|
10
|
+
|
|
11
|
+
#: Functional sections understood by the engine, by docstring title → key.
|
|
12
|
+
FUNCTIONAL_SECTIONS: dict[str, str] = {
|
|
13
|
+
"Business area": "business_area",
|
|
14
|
+
"Actors": "actors",
|
|
15
|
+
"Preconditions": "preconditions",
|
|
16
|
+
"Postconditions": "postconditions",
|
|
17
|
+
"Business Rules": "business_rules",
|
|
18
|
+
"Effects": "effects",
|
|
19
|
+
"Exceptions": "exceptions",
|
|
20
|
+
"Examples": "examples",
|
|
21
|
+
}
|
|
22
|
+
#: Sections whose body is a ``name: description`` list.
|
|
23
|
+
KEYED_SECTIONS: dict[str, str] = {"States": "states", "Transitions": "transitions"}
|
|
24
|
+
|
|
25
|
+
_PARSER = GoogleParser(
|
|
26
|
+
sections=[Section(t, k, SectionType.SINGULAR) for t, k in FUNCTIONAL_SECTIONS.items()]
|
|
27
|
+
+ [Section(t, k, SectionType.MULTIPLE) for t, k in KEYED_SECTIONS.items()],
|
|
28
|
+
title_colon=True,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@dataclass
|
|
33
|
+
class DocText:
|
|
34
|
+
"""A docstring split into its summary, description and named sections."""
|
|
35
|
+
|
|
36
|
+
summary: str | None = None
|
|
37
|
+
description: str | None = None
|
|
38
|
+
sections: dict[str, str] = field(default_factory=dict)
|
|
39
|
+
items: dict[str, dict[str, str]] = field(default_factory=dict)
|
|
40
|
+
|
|
41
|
+
@property
|
|
42
|
+
def present(self) -> bool:
|
|
43
|
+
"""Whether the docstring has a summary."""
|
|
44
|
+
return bool(self.summary)
|
|
45
|
+
|
|
46
|
+
def list_section(self, key: str) -> list[str]:
|
|
47
|
+
"""The items of section ``key``, one per line, without bullets."""
|
|
48
|
+
raw = self.sections.get(key, "")
|
|
49
|
+
return [line.strip().lstrip("-*").strip() for line in raw.splitlines() if line.strip()]
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def parse_doc(obj_or_text: Any) -> DocText:
|
|
53
|
+
"""The parts of an object's own docstring (or of a text)."""
|
|
54
|
+
text = obj_or_text if isinstance(obj_or_text, str) else _own_doc(obj_or_text)
|
|
55
|
+
if not text:
|
|
56
|
+
return DocText()
|
|
57
|
+
parsed = _PARSER.parse(inspect.cleandoc(text))
|
|
58
|
+
doc = DocText(summary=parsed.short_description, description=parsed.long_description)
|
|
59
|
+
for meta in parsed.meta:
|
|
60
|
+
key = meta.args[0]
|
|
61
|
+
if len(meta.args) > 1:
|
|
62
|
+
doc.items.setdefault(key, {})[meta.args[1]] = (meta.description or "").strip()
|
|
63
|
+
else:
|
|
64
|
+
doc.sections[key] = (meta.description or "").strip()
|
|
65
|
+
return doc
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _own_doc(obj: Any) -> str | None:
|
|
69
|
+
"""Docstring defined on the object itself, not inherited from a base class."""
|
|
70
|
+
if inspect.isclass(obj):
|
|
71
|
+
return obj.__dict__.get("__doc__")
|
|
72
|
+
return getattr(obj, "__doc__", None)
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"""Renderers of the documentation model: Markdown, JSON, Mermaid, the AI context and the HTML
|
|
2
|
+
site."""
|
|
3
|
+
|
|
4
|
+
from resource_engine_devtools.documentation.generators.ai_context import render_ai_context
|
|
5
|
+
from resource_engine_devtools.documentation.generators.json import render_json
|
|
6
|
+
from resource_engine_devtools.documentation.generators.markdown import render_markdown
|
|
7
|
+
from resource_engine_devtools.documentation.generators.mermaid import render_state_machine
|
|
8
|
+
|
|
9
|
+
__all__ = ["render_ai_context", "render_json", "render_markdown", "render_state_machine"]
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"""Compact project catalogue meant to be read by AI coding agents."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from resource_engine_devtools.documentation.model import DocumentationModel
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def render_ai_context(model: DocumentationModel) -> str:
|
|
9
|
+
"""The documentation model as the compact Markdown context AI assistants read."""
|
|
10
|
+
out = [
|
|
11
|
+
f"<!-- generated by resource-engine {model.engine_version}; do not edit -->",
|
|
12
|
+
"## Resources",
|
|
13
|
+
]
|
|
14
|
+
for r in model.resources:
|
|
15
|
+
crud = [o.name for o in r.operations if o.kind == "crud"]
|
|
16
|
+
out.append(f"- {r.name} ({r.module}) — {r.doc.summary or 'undocumented'}")
|
|
17
|
+
out.append(f" ops: {', '.join(crud) or '-'}")
|
|
18
|
+
if r.filterable:
|
|
19
|
+
out.append(f" filterable: {', '.join(r.filterable)}")
|
|
20
|
+
if r.tenant_field:
|
|
21
|
+
out.append(f" tenant_field: {r.tenant_field}")
|
|
22
|
+
if r.version_field:
|
|
23
|
+
strict = " (required)" if r.require_version else ""
|
|
24
|
+
out.append(f" version_field: {r.version_field}{strict}")
|
|
25
|
+
if r.restricted_fields:
|
|
26
|
+
out.append(f" restricted_fields: {', '.join(r.restricted_fields)}")
|
|
27
|
+
if model.audit_resource and not r.audit:
|
|
28
|
+
out.append(" audit: off")
|
|
29
|
+
elif model.audit_resource and (r.audit_reads or r.audit_exclude):
|
|
30
|
+
flags = ["reads"] if r.audit_reads else []
|
|
31
|
+
flags += [f"exclude={'+'.join(r.audit_exclude)}"] if r.audit_exclude else []
|
|
32
|
+
out.append(f" audit: {', '.join(flags)}")
|
|
33
|
+
for sm in r.state_machines:
|
|
34
|
+
edges = "; ".join(f"{t.source}-{t.event}->{t.target}" for t in sm.transitions)
|
|
35
|
+
out.append(f" state_machine: {sm.qualname} field={sm.state_field}: {edges}")
|
|
36
|
+
if sm.timers:
|
|
37
|
+
out.append(
|
|
38
|
+
" timers: "
|
|
39
|
+
+ ", ".join(
|
|
40
|
+
f"{t.event} after {t.after} in {'|'.join(t.states)} (since {t.since})"
|
|
41
|
+
for t in sm.timers
|
|
42
|
+
)
|
|
43
|
+
)
|
|
44
|
+
if r.actions:
|
|
45
|
+
out.append(
|
|
46
|
+
" actions: " + ", ".join(f"{a.name}({a.input_schema or ''})" for a in r.actions)
|
|
47
|
+
)
|
|
48
|
+
if r.references:
|
|
49
|
+
out.append(
|
|
50
|
+
" references: "
|
|
51
|
+
+ ", ".join(
|
|
52
|
+
f"{x.name}={x.field}->{x.resource}(on_delete={x.on_delete})"
|
|
53
|
+
for x in r.references
|
|
54
|
+
)
|
|
55
|
+
)
|
|
56
|
+
reverse = [x for x in r.referenced_by if x.related_name]
|
|
57
|
+
if reverse:
|
|
58
|
+
out.append(" reverse: " + ", ".join(f"{x.related_name}<-{x.field}" for x in reverse))
|
|
59
|
+
if r.links:
|
|
60
|
+
out.append(
|
|
61
|
+
" links: " + ", ".join(f"{x.name}->{x.resource}(via {x.field})" for x in r.links)
|
|
62
|
+
)
|
|
63
|
+
if r.reacts:
|
|
64
|
+
out.append(
|
|
65
|
+
f" process (correlation={r.correlation}): "
|
|
66
|
+
+ ", ".join(
|
|
67
|
+
f"{x.event}{'[' + x.when + ']' if x.when else ''}->{x.handler}"
|
|
68
|
+
+ ("(start)" if x.start else "")
|
|
69
|
+
for x in r.reacts
|
|
70
|
+
)
|
|
71
|
+
)
|
|
72
|
+
if r.events:
|
|
73
|
+
out.append(
|
|
74
|
+
" events: "
|
|
75
|
+
+ ", ".join(f"{e.name}({', '.join(f.name for f in e.fields)})" for e in r.events)
|
|
76
|
+
)
|
|
77
|
+
if r.documents:
|
|
78
|
+
out.append(
|
|
79
|
+
" documents: "
|
|
80
|
+
+ ", ".join(
|
|
81
|
+
f"{s.name}[{'|'.join(s.content_types)}"
|
|
82
|
+
f"{', many' if s.multiple else ''}"
|
|
83
|
+
f"{', required_for=' + '+'.join(s.required_for) if s.required_for else ''}"
|
|
84
|
+
f"{', processing' if s.processing else ''}]"
|
|
85
|
+
for s in r.documents
|
|
86
|
+
)
|
|
87
|
+
)
|
|
88
|
+
if r.hooks:
|
|
89
|
+
out.append(" hooks: " + ", ".join(f"{h.point}={h.name}" for h in r.hooks))
|
|
90
|
+
if model.system_roles:
|
|
91
|
+
out += ["", "## Permissions", "Action names: <resource>:<operation>[:<qualifier>]."]
|
|
92
|
+
out += [f"- system role {r.name}: {', '.join(r.grants)}" for r in model.system_roles]
|
|
93
|
+
out.append("- other permissions are ABAC policies stored in authz_policies (data)")
|
|
94
|
+
out.append(
|
|
95
|
+
"- fields: <resource>:read|write:<field>; hidden fields are omitted, writing a "
|
|
96
|
+
"protected field fails with RE-E706, filtering/sorting by one with RE-E707"
|
|
97
|
+
)
|
|
98
|
+
if any(r.tenant_field for r in model.resources):
|
|
99
|
+
out += [
|
|
100
|
+
"",
|
|
101
|
+
"## Tenancy",
|
|
102
|
+
"- resources with tenant_field are confined to Principal.tenant (other tenants' "
|
|
103
|
+
"records are 404); no tenant -> RE-E708, another tenant -> RE-E709; admin and "
|
|
104
|
+
"system_principal act across tenants and pass tenant= to create",
|
|
105
|
+
]
|
|
106
|
+
if any(r.version_field for r in model.resources):
|
|
107
|
+
out += [
|
|
108
|
+
"",
|
|
109
|
+
"## Concurrency",
|
|
110
|
+
"- resources with version_field: responses carry ETag; update/transition/action/"
|
|
111
|
+
"delete take expected_version= (HTTP If-Match) and a stale one fails with RE-E105 "
|
|
112
|
+
"(409); require_version makes it mandatory (RE-E106, 428); create starts at 1",
|
|
113
|
+
]
|
|
114
|
+
if model.audit_resource:
|
|
115
|
+
out += [
|
|
116
|
+
"",
|
|
117
|
+
"## Audit",
|
|
118
|
+
f"- every committed change and refused attempt is stored in {model.audit_resource} "
|
|
119
|
+
"(read-only, sensitive); restricted and audit_exclude fields are logged without values",
|
|
120
|
+
]
|
|
121
|
+
return "\n".join(out) + "\n"
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"""The application's machine-readable API documents next to the functional
|
|
2
|
+
documentation: ``openapi.json`` (HTTP), ``asyncapi.json`` (events) and
|
|
3
|
+
``schema.graphql``, linked from the home page. The documentation explains
|
|
4
|
+
the application; to look at the API or try it, use these files or the
|
|
5
|
+
explorer (``mount_explorer``, docs/explorer.md).
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import json
|
|
11
|
+
from collections.abc import Mapping
|
|
12
|
+
from typing import Any
|
|
13
|
+
|
|
14
|
+
from resource_engine_devtools.documentation.i18n import t
|
|
15
|
+
|
|
16
|
+
#: The documents, in the order they are listed.
|
|
17
|
+
DOCUMENTS = ("openapi.json", "asyncapi.json", "schema.graphql")
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _label(name: str) -> str:
|
|
21
|
+
return {
|
|
22
|
+
"openapi.json": t("OpenAPI: the HTTP API"),
|
|
23
|
+
"asyncapi.json": t("AsyncAPI: the published and consumed events"),
|
|
24
|
+
"schema.graphql": t("GraphQL schema"),
|
|
25
|
+
}[name]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def api_pages(
|
|
29
|
+
asyncapi: Mapping[str, Any] | None = None,
|
|
30
|
+
openapi: Mapping[str, Any] | None = None,
|
|
31
|
+
graphql: str | None = None,
|
|
32
|
+
) -> tuple[dict[str, str], dict[str, str]]:
|
|
33
|
+
"""``(pages, documents)``: the events page, and the API documents to
|
|
34
|
+
write next to the pages."""
|
|
35
|
+
from resource_engine_devtools.documentation.generators.site import render_events
|
|
36
|
+
|
|
37
|
+
pages: dict[str, str] = {}
|
|
38
|
+
documents: dict[str, str] = {}
|
|
39
|
+
if asyncapi and asyncapi.get("channels"):
|
|
40
|
+
pages["events.md"] = render_events(asyncapi)
|
|
41
|
+
documents["asyncapi.json"] = _json(asyncapi)
|
|
42
|
+
if openapi:
|
|
43
|
+
documents["openapi.json"] = _json(openapi)
|
|
44
|
+
if graphql:
|
|
45
|
+
documents["schema.graphql"] = graphql.rstrip("\n") + "\n"
|
|
46
|
+
return pages, documents
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def documents_section(documents: Mapping[str, str]) -> str:
|
|
50
|
+
"""The home page's list of the API documents ("" without any)."""
|
|
51
|
+
names = [name for name in DOCUMENTS if name in documents]
|
|
52
|
+
if not names:
|
|
53
|
+
return ""
|
|
54
|
+
lines = ["", f"## {t('API documents')}", "", t("Machine-readable descriptions of the API:"), ""]
|
|
55
|
+
lines += [f"- [`{name}`]({name}): {_label(name)}" for name in names]
|
|
56
|
+
return "\n".join(lines) + "\n"
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _json(document: Mapping[str, Any]) -> str:
|
|
60
|
+
return json.dumps(document, indent=2, ensure_ascii=False) + "\n"
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
__all__ = ["DOCUMENTS", "api_pages", "documents_section"]
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""The documentation model as JSON."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
|
|
7
|
+
from resource_engine_devtools.documentation.model import DocumentationModel
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def render_json(model: DocumentationModel) -> str:
|
|
11
|
+
"""The documentation model as indented JSON."""
|
|
12
|
+
return json.dumps(model.to_dict(), indent=2, ensure_ascii=False, default=str)
|