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.
Files changed (88) hide show
  1. resource_engine_devtools/__init__.py +6 -0
  2. resource_engine_devtools/ai/__init__.py +1 -0
  3. resource_engine_devtools/ai/example.py +66 -0
  4. resource_engine_devtools/ai/installer.py +424 -0
  5. resource_engine_devtools/ai/skill/SKILL.md +130 -0
  6. resource_engine_devtools/ai/skill/examples/README.md +7 -0
  7. resource_engine_devtools/ai/skill/examples/invoices/__init__.py +0 -0
  8. resource_engine_devtools/ai/skill/examples/invoices/app.py +245 -0
  9. resource_engine_devtools/ai/skill/examples/invoices/application/__init__.py +0 -0
  10. resource_engine_devtools/ai/skill/examples/invoices/application/actions.py +61 -0
  11. resource_engine_devtools/ai/skill/examples/invoices/application/balances.py +51 -0
  12. resource_engine_devtools/ai/skill/examples/invoices/application/customers.py +35 -0
  13. resource_engine_devtools/ai/skill/examples/invoices/application/hooks.py +61 -0
  14. resource_engine_devtools/ai/skill/examples/invoices/application/processes.py +80 -0
  15. resource_engine_devtools/ai/skill/examples/invoices/application/resource.py +112 -0
  16. resource_engine_devtools/ai/skill/examples/invoices/application/schemas.py +164 -0
  17. resource_engine_devtools/ai/skill/examples/invoices/application/tags.py +77 -0
  18. resource_engine_devtools/ai/skill/examples/invoices/authz.py +126 -0
  19. resource_engine_devtools/ai/skill/examples/invoices/bootstrap.py +80 -0
  20. resource_engine_devtools/ai/skill/examples/invoices/domain/__init__.py +0 -0
  21. resource_engine_devtools/ai/skill/examples/invoices/domain/entities.py +112 -0
  22. resource_engine_devtools/ai/skill/examples/invoices/domain/events.py +22 -0
  23. resource_engine_devtools/ai/skill/examples/invoices/domain/state_machine.py +88 -0
  24. resource_engine_devtools/ai/skill/examples/invoices/infrastructure/__init__.py +0 -0
  25. resource_engine_devtools/ai/skill/examples/invoices/infrastructure/persistence.py +121 -0
  26. resource_engine_devtools/ai/skill/examples/invoices/locale/en/LC_MESSAGES/messages.po +682 -0
  27. resource_engine_devtools/ai/skill/examples/invoices/locale/es/LC_MESSAGES/messages.po +685 -0
  28. resource_engine_devtools/ai/skill/examples/invoices/notifications.py +47 -0
  29. resource_engine_devtools/ai/skill/examples/invoices/templates/email/base.html +7 -0
  30. resource_engine_devtools/ai/skill/examples/invoices/templates/email/invoice_approved.html +5 -0
  31. resource_engine_devtools/ai/skill/examples/invoices/templates/email/invoice_approved.subject.txt +1 -0
  32. resource_engine_devtools/ai/skill/examples/invoices/templates/email/invoice_approved.txt +5 -0
  33. resource_engine_devtools/ai/skill/examples/invoices/tests/conftest.py +40 -0
  34. resource_engine_devtools/ai/skill/examples/invoices/tests/test_invoices_engine.py +241 -0
  35. resource_engine_devtools/ai/skill/examples/invoices/worker.py +53 -0
  36. resource_engine_devtools/ai/skill/llms.txt +25 -0
  37. resource_engine_devtools/ai/skill/references/anti-patterns.md +209 -0
  38. resource_engine_devtools/ai/skill/references/docstrings.md +97 -0
  39. resource_engine_devtools/ai/skill/references/errors.md +81 -0
  40. resource_engine_devtools/ai/skill/references/lifecycle.md +118 -0
  41. resource_engine_devtools/ai/skill/references/permissions.md +111 -0
  42. resource_engine_devtools/ai/skill/references/resource.md +301 -0
  43. resource_engine_devtools/ai/skill/references/state-machines.md +126 -0
  44. resource_engine_devtools/ai/skill/references/testing.md +80 -0
  45. resource_engine_devtools/ai/templates/agents_block.md +26 -0
  46. resource_engine_devtools/ai/templates/cursor_rule.mdc +7 -0
  47. resource_engine_devtools/check.py +67 -0
  48. resource_engine_devtools/cli.py +1112 -0
  49. resource_engine_devtools/consumer_scaffold.py +715 -0
  50. resource_engine_devtools/contracts.py +471 -0
  51. resource_engine_devtools/db.py +132 -0
  52. resource_engine_devtools/declarations_check.py +116 -0
  53. resource_engine_devtools/documentation/__init__.py +10 -0
  54. resource_engine_devtools/documentation/docstrings.py +72 -0
  55. resource_engine_devtools/documentation/generators/__init__.py +9 -0
  56. resource_engine_devtools/documentation/generators/ai_context.py +121 -0
  57. resource_engine_devtools/documentation/generators/api.py +63 -0
  58. resource_engine_devtools/documentation/generators/json.py +12 -0
  59. resource_engine_devtools/documentation/generators/markdown.py +687 -0
  60. resource_engine_devtools/documentation/generators/mermaid.py +22 -0
  61. resource_engine_devtools/documentation/generators/site.py +379 -0
  62. resource_engine_devtools/documentation/generators/site_assets/site.css +62 -0
  63. resource_engine_devtools/documentation/generators/site_assets/site.js +54 -0
  64. resource_engine_devtools/documentation/i18n.py +185 -0
  65. resource_engine_devtools/documentation/introspector.py +563 -0
  66. resource_engine_devtools/documentation/locale/ca/LC_MESSAGES/docs.po +628 -0
  67. resource_engine_devtools/documentation/locale/es/LC_MESSAGES/docs.po +627 -0
  68. resource_engine_devtools/documentation/model.py +311 -0
  69. resource_engine_devtools/documentation/validator.py +128 -0
  70. resource_engine_devtools/emails.py +172 -0
  71. resource_engine_devtools/events_check.py +124 -0
  72. resource_engine_devtools/gates_check.py +133 -0
  73. resource_engine_devtools/hooks_check.py +287 -0
  74. resource_engine_devtools/i18n.py +312 -0
  75. resource_engine_devtools/loader.py +93 -0
  76. resource_engine_devtools/logic_check.py +136 -0
  77. resource_engine_devtools/mcp_server.py +685 -0
  78. resource_engine_devtools/py.typed +0 -0
  79. resource_engine_devtools/scaffold.py +805 -0
  80. resource_engine_devtools/scaffold_parts.py +637 -0
  81. resource_engine_devtools/source_edit.py +424 -0
  82. resource_engine_devtools/templates_check.py +59 -0
  83. resource_engine_devtools/worker.py +68 -0
  84. resource_engine_devtools-0.1.0.dist-info/METADATA +38 -0
  85. resource_engine_devtools-0.1.0.dist-info/RECORD +88 -0
  86. resource_engine_devtools-0.1.0.dist-info/WHEEL +4 -0
  87. resource_engine_devtools-0.1.0.dist-info/entry_points.txt +2 -0
  88. resource_engine_devtools-0.1.0.dist-info/licenses/LICENSE +202 -0
@@ -0,0 +1,185 @@
1
+ """Languages of the generated documentation.
2
+
3
+ Two catalogues meet here:
4
+
5
+ - **The generator's own texts** (headings, table columns, explanations),
6
+ written in English and translated by catalogues shipped with devtools
7
+ (``documentation/locale/<locale>/LC_MESSAGES/docs.po``: Catalan, Spanish).
8
+ - **The application's texts** (docstrings, descriptions, state names), written
9
+ in the application's source language and translated by its own catalogue
10
+ (``resource-engine i18n``): ``translate_model`` returns a copy of the model
11
+ in another locale.
12
+
13
+ ``render_markdown`` and ``render_site`` render in the locale set with
14
+ ``docs_locale``; without one the texts stay as written.
15
+ """
16
+
17
+ from __future__ import annotations
18
+
19
+ import copy
20
+ import dataclasses
21
+ from collections.abc import Callable, Iterator
22
+ from contextlib import contextmanager
23
+ from contextvars import ContextVar
24
+ from functools import cache
25
+ from importlib.resources import files
26
+ from typing import Any
27
+
28
+ from resource_engine.templates import locale_candidates
29
+
30
+ DOMAIN = "docs"
31
+
32
+ _locale: ContextVar[str | None] = ContextVar("resource_engine_docs_locale", default=None)
33
+
34
+
35
+ @cache
36
+ def _catalogue(locale: str) -> dict[str, str]:
37
+ """The generator's catalogue for one locale (``{}`` when there is none)."""
38
+ from babel.messages.pofile import read_po
39
+
40
+ path = files(__package__) / "locale" / locale / "LC_MESSAGES" / f"{DOMAIN}.po"
41
+ if not path.is_file():
42
+ return {}
43
+ with path.open("rb") as handle:
44
+ catalog = read_po(handle)
45
+ return {
46
+ str(m.id): str(m.string)
47
+ for m in catalog
48
+ if m.id and m.string and not m.fuzzy and isinstance(m.id, str)
49
+ }
50
+
51
+
52
+ def generator_locales() -> list[str]:
53
+ """Locales the generator's own texts are translated into (plus English)."""
54
+ root = files(__package__) / "locale"
55
+ found = [p.name for p in root.iterdir() if (p / "LC_MESSAGES" / f"{DOMAIN}.po").is_file()]
56
+ return sorted({"en", *found})
57
+
58
+
59
+ @contextmanager
60
+ def docs_locale(locale: str | None) -> Iterator[None]:
61
+ """Render documentation in ``locale`` inside the block."""
62
+ token = _locale.set(locale)
63
+ try:
64
+ yield
65
+ finally:
66
+ _locale.reset(token)
67
+
68
+
69
+ def current() -> str | None:
70
+ """The locale documentation is being rendered in, if any."""
71
+ return _locale.get()
72
+
73
+
74
+ def t(message: str, **values: Any) -> str:
75
+ """A generator text in the current docs locale, with ``{name}`` values."""
76
+ locale = _locale.get()
77
+ text = message
78
+ if locale:
79
+ for candidate in locale_candidates(locale)[:-1]:
80
+ found = _catalogue(candidate).get(message)
81
+ if found:
82
+ text = found
83
+ break
84
+ return text.format(**values) if values else text
85
+
86
+
87
+ # ------------------------------------------------- the application's texts
88
+
89
+ #: Model fields that hold the application's own prose.
90
+ TEXT_FIELDS = {"summary", "description", "name", "body"}
91
+
92
+
93
+ def translate_model(model: Any, gettext: Callable[[str], str]) -> Any:
94
+ """A copy of ``model`` with every docstring, description and state name
95
+ passed through ``gettext`` (msgid: the text as written in the code)."""
96
+ return _translate(copy.deepcopy(model), gettext)
97
+
98
+
99
+ def _translate(value: Any, gettext: Callable[[str], str]) -> Any:
100
+ from resource_engine_devtools.documentation.docstrings import DocText
101
+ from resource_engine_devtools.documentation.model import StateDoc
102
+
103
+ if isinstance(value, DocText):
104
+ value.summary = _tr(value.summary, gettext)
105
+ value.description = _tr(value.description, gettext)
106
+ value.sections = {k: _tr(v, gettext) or v for k, v in value.sections.items()}
107
+ value.items = {
108
+ k: {name: _tr(text, gettext) or text for name, text in items.items()}
109
+ for k, items in value.items.items()
110
+ }
111
+ return value
112
+ if isinstance(value, StateDoc):
113
+ value.name = _tr(value.name, gettext) or value.name
114
+ value.description = _tr(value.description, gettext)
115
+ return value
116
+ if dataclasses.is_dataclass(value) and not isinstance(value, type):
117
+ for f in dataclasses.fields(value):
118
+ current = getattr(value, f.name)
119
+ if f.name == "description" and isinstance(current, str):
120
+ setattr(value, f.name, _tr(current, gettext))
121
+ elif isinstance(current, list | dict) or dataclasses.is_dataclass(current):
122
+ setattr(value, f.name, _translate(current, gettext))
123
+ return value
124
+ if isinstance(value, list):
125
+ return [_translate(v, gettext) for v in value]
126
+ if isinstance(value, dict):
127
+ return {k: _translate(v, gettext) for k, v in value.items()}
128
+ return value
129
+
130
+
131
+ def _tr(text: str | None, gettext: Callable[[str], str]) -> str | None:
132
+ if not text:
133
+ return text
134
+ return gettext(text) or text
135
+
136
+
137
+ def model_texts(model: Any) -> Iterator[tuple[str, str]]:
138
+ """``(text, where)`` for every application text of the model, for extraction."""
139
+ seen: set[str] = set()
140
+
141
+ def visit(value: Any, where: str) -> Iterator[tuple[str, str]]:
142
+ from resource_engine_devtools.documentation.docstrings import DocText
143
+ from resource_engine_devtools.documentation.model import StateDoc
144
+
145
+ if isinstance(value, DocText):
146
+ texts = [value.summary, value.description, *value.sections.values()]
147
+ texts += [t for items in value.items.values() for t in items.values()]
148
+ for text in texts:
149
+ if text and text not in seen:
150
+ seen.add(text)
151
+ yield text, where
152
+ return
153
+ if isinstance(value, StateDoc):
154
+ for text in (value.name if value.name != value.id else None, value.description):
155
+ if text and text not in seen:
156
+ seen.add(text)
157
+ yield text, f"{where} state {value.id}"
158
+ return
159
+ if dataclasses.is_dataclass(value) and not isinstance(value, type):
160
+ label = getattr(value, "name", None) or getattr(value, "template", None)
161
+ here = f"{where} {label}".strip() if isinstance(label, str) else where
162
+ for f in dataclasses.fields(value):
163
+ current = getattr(value, f.name)
164
+ if f.name == "description" and isinstance(current, str) and current:
165
+ if current not in seen:
166
+ seen.add(current)
167
+ yield current, here
168
+ elif isinstance(current, list | dict) or dataclasses.is_dataclass(current):
169
+ yield from visit(current, here)
170
+ elif isinstance(value, list):
171
+ for item in value:
172
+ yield from visit(item, where)
173
+ elif isinstance(value, dict):
174
+ for item in value.values():
175
+ yield from visit(item, where)
176
+
177
+ resources = [r for r in getattr(model, "resources", []) if not _engine(r)]
178
+ yield from visit(resources, "")
179
+ yield from visit(getattr(model, "system_roles", []), "role")
180
+
181
+
182
+ def _engine(resource: Any) -> bool:
183
+ """The engine's built-in resources (authz_*, audit_log, documents) keep
184
+ their own texts: they aren't the application's to translate."""
185
+ return str(getattr(resource, "module", "")).startswith("resource_engine.")