resource-engine 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 (128) hide show
  1. resource_engine/__init__.py +96 -0
  2. resource_engine/application/__init__.py +1 -0
  3. resource_engine/application/actions.py +48 -0
  4. resource_engine/application/context.py +111 -0
  5. resource_engine/application/events.py +54 -0
  6. resource_engine/application/hooks.py +272 -0
  7. resource_engine/application/idempotency.py +277 -0
  8. resource_engine/application/manager.py +1987 -0
  9. resource_engine/application/query.py +47 -0
  10. resource_engine/application/registry.py +137 -0
  11. resource_engine/application/relations.py +470 -0
  12. resource_engine/application/resource.py +984 -0
  13. resource_engine/application/searching.py +162 -0
  14. resource_engine/application/settings.py +169 -0
  15. resource_engine/application/slots.py +67 -0
  16. resource_engine/application/state_machines.py +177 -0
  17. resource_engine/application/timers.py +347 -0
  18. resource_engine/application/transaction.py +100 -0
  19. resource_engine/application/views.py +239 -0
  20. resource_engine/audit/__init__.py +40 -0
  21. resource_engine/audit/export.py +280 -0
  22. resource_engine/audit/history.py +165 -0
  23. resource_engine/audit/janitor.py +257 -0
  24. resource_engine/audit/model.py +98 -0
  25. resource_engine/audit/recorder.py +145 -0
  26. resource_engine/audit/resource.py +68 -0
  27. resource_engine/audit/seal.py +327 -0
  28. resource_engine/audit/sink.py +69 -0
  29. resource_engine/authz/__init__.py +72 -0
  30. resource_engine/authz/apikeys.py +300 -0
  31. resource_engine/authz/base.py +143 -0
  32. resource_engine/authz/catalog.py +291 -0
  33. resource_engine/authz/conditions.py +416 -0
  34. resource_engine/authz/config.py +77 -0
  35. resource_engine/authz/evaluator.py +382 -0
  36. resource_engine/authz/external.py +249 -0
  37. resource_engine/authz/fields.py +44 -0
  38. resource_engine/authz/model.py +145 -0
  39. resource_engine/authz/resources.py +289 -0
  40. resource_engine/authz/roles.py +101 -0
  41. resource_engine/cache.py +144 -0
  42. resource_engine/calls.py +100 -0
  43. resource_engine/capabilities.py +145 -0
  44. resource_engine/composite.py +295 -0
  45. resource_engine/documents/__init__.py +69 -0
  46. resource_engine/documents/content.py +57 -0
  47. resource_engine/documents/events.py +49 -0
  48. resource_engine/documents/janitor.py +93 -0
  49. resource_engine/documents/machine.py +38 -0
  50. resource_engine/documents/manager.py +704 -0
  51. resource_engine/documents/model.py +91 -0
  52. resource_engine/documents/pipeline.py +98 -0
  53. resource_engine/documents/processors.py +320 -0
  54. resource_engine/documents/resource.py +39 -0
  55. resource_engine/domain/__init__.py +10 -0
  56. resource_engine/domain/events.py +50 -0
  57. resource_engine/domain/exceptions.py +23 -0
  58. resource_engine/email.py +417 -0
  59. resource_engine/errors.py +562 -0
  60. resource_engine/explorer.py +133 -0
  61. resource_engine/exports.py +687 -0
  62. resource_engine/generation.py +233 -0
  63. resource_engine/i18n.py +251 -0
  64. resource_engine/identity.py +46 -0
  65. resource_engine/integration.py +80 -0
  66. resource_engine/jobs.py +390 -0
  67. resource_engine/locale/ca/LC_MESSAGES/resource_engine.mo +0 -0
  68. resource_engine/locale/ca/LC_MESSAGES/resource_engine.po +206 -0
  69. resource_engine/locale/es/LC_MESSAGES/resource_engine.mo +0 -0
  70. resource_engine/locale/es/LC_MESSAGES/resource_engine.po +206 -0
  71. resource_engine/logs.py +201 -0
  72. resource_engine/messaging/__init__.py +73 -0
  73. resource_engine/messaging/asyncapi.py +529 -0
  74. resource_engine/messaging/cloudevents.py +98 -0
  75. resource_engine/messaging/consumer.py +165 -0
  76. resource_engine/messaging/delivery.py +115 -0
  77. resource_engine/messaging/envelope.py +193 -0
  78. resource_engine/messaging/hub.py +148 -0
  79. resource_engine/messaging/inbound.py +76 -0
  80. resource_engine/messaging/inspection.py +113 -0
  81. resource_engine/messaging/janitor.py +136 -0
  82. resource_engine/messaging/live.py +179 -0
  83. resource_engine/messaging/processing.py +357 -0
  84. resource_engine/messaging/publishing.py +318 -0
  85. resource_engine/messaging/relay.py +84 -0
  86. resource_engine/messaging/worker.py +166 -0
  87. resource_engine/nosql.py +394 -0
  88. resource_engine/ports/__init__.py +13 -0
  89. resource_engine/ports/activity.py +171 -0
  90. resource_engine/ports/audit.py +42 -0
  91. resource_engine/ports/authz.py +132 -0
  92. resource_engine/ports/cache.py +41 -0
  93. resource_engine/ports/email.py +118 -0
  94. resource_engine/ports/event_publisher.py +34 -0
  95. resource_engine/ports/idempotency.py +69 -0
  96. resource_engine/ports/identity.py +112 -0
  97. resource_engine/ports/locks.py +28 -0
  98. resource_engine/ports/messaging.py +197 -0
  99. resource_engine/ports/observability.py +60 -0
  100. resource_engine/ports/ratelimit.py +59 -0
  101. resource_engine/ports/repository.py +56 -0
  102. resource_engine/ports/storage.py +93 -0
  103. resource_engine/ports/templates.py +54 -0
  104. resource_engine/ports/tracing.py +61 -0
  105. resource_engine/ports/unit_of_work.py +46 -0
  106. resource_engine/processes.py +408 -0
  107. resource_engine/py.typed +0 -0
  108. resource_engine/querying.py +319 -0
  109. resource_engine/ratelimit.py +217 -0
  110. resource_engine/reporting.py +230 -0
  111. resource_engine/schema_examples.py +51 -0
  112. resource_engine/templates.py +143 -0
  113. resource_engine/tenancy.py +68 -0
  114. resource_engine/testing/__init__.py +34 -0
  115. resource_engine/testing/activity.py +123 -0
  116. resource_engine/testing/contracts.py +1416 -0
  117. resource_engine/testing/email.py +36 -0
  118. resource_engine/testing/engine.py +40 -0
  119. resource_engine/testing/memory.py +603 -0
  120. resource_engine/testing/storage.py +110 -0
  121. resource_engine/testing/tracing.py +100 -0
  122. resource_engine/tracing.py +156 -0
  123. resource_engine/translatable.py +80 -0
  124. resource_engine/versions.py +189 -0
  125. resource_engine-0.1.0.dist-info/METADATA +36 -0
  126. resource_engine-0.1.0.dist-info/RECORD +128 -0
  127. resource_engine-0.1.0.dist-info/WHEEL +4 -0
  128. resource_engine-0.1.0.dist-info/licenses/LICENSE +202 -0
@@ -0,0 +1,93 @@
1
+ """Clean-up job for documents, the storage counterpart of ``OutboxRelay``.
2
+
3
+ Rollback compensations and post-commit purges are best effort; a crash can skip
4
+ them. Run ``DocumentJanitor.run_once()`` periodically (cron, worker loop).
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import dataclasses
10
+ from dataclasses import dataclass
11
+ from datetime import UTC, datetime, timedelta
12
+ from typing import Any
13
+
14
+ from resource_engine.application.query import ListQuery
15
+ from resource_engine.documents.manager import aware
16
+
17
+
18
+ @dataclass
19
+ class JanitorStats:
20
+ """What one run of the documents janitor did."""
21
+
22
+ expired: int = 0
23
+ purged: int = 0
24
+ orphans: int = 0
25
+
26
+
27
+ class DocumentJanitor:
28
+ """Expires stale uploads, purges deleted content and removes orphan blobs.
29
+
30
+ Args:
31
+ documents: ``ResourceManager.documents``.
32
+ grace: Blobs younger than this are never treated as orphans (an upload
33
+ may be committing right now).
34
+ batch: Documents handled per run and category.
35
+ """
36
+
37
+ def __init__(
38
+ self, documents: Any, *, grace: timedelta = timedelta(hours=1), batch: int = 500
39
+ ) -> None:
40
+ self.documents = documents
41
+ self.grace = grace
42
+ self.batch = batch
43
+
44
+ async def _with_status(self, status: str) -> list[Any]:
45
+ async with self.documents.manager.uow_factory() as uow:
46
+ page = await uow.repository(self.documents.resource).list(
47
+ ListQuery(filters={"status": status}, limit=self.batch)
48
+ )
49
+ return [dataclasses.replace(d) for d in page.items]
50
+
51
+ async def run_once(self, now: datetime | None = None) -> JanitorStats:
52
+ """Expire stale uploads, purge deleted content and remove orphan blobs; returns the
53
+ counts."""
54
+ now = now or datetime.now(UTC)
55
+ stats = JanitorStats()
56
+ manager = self.documents.manager
57
+ machine = self.documents.machine
58
+
59
+ expired = [
60
+ d
61
+ for d in await self._with_status("pending")
62
+ if (expires := aware(d.expires_at)) is not None and expires < now
63
+ ]
64
+ if expired:
65
+ async with manager.uow_factory() as uow:
66
+ repo = uow.repository(self.documents.resource)
67
+ for stale in expired:
68
+ doc = await repo.get(stale.id)
69
+ await machine.fire(doc, "discard", {})
70
+ doc.error = "upload expired"
71
+ await repo.update(doc)
72
+ await uow.commit()
73
+ stats.expired = len(expired)
74
+
75
+ deleted = [d.id for d in await self._with_status("deleted") if d.purged_at is None]
76
+ stats.purged = await self.documents.purge(deleted)
77
+
78
+ cutoff = now - self.grace
79
+ async with manager.uow_factory() as uow:
80
+ repo = uow.repository(self.documents.resource)
81
+ orphans = []
82
+ async for info in self.documents.store.list(""):
83
+ modified = aware(info.last_modified)
84
+ if modified is None or modified > cutoff:
85
+ continue
86
+ if await repo.get(info.key.rsplit("/", 1)[-1]) is None:
87
+ orphans.append(info.key)
88
+ if len(orphans) >= self.batch:
89
+ break
90
+ if orphans:
91
+ await self.documents.store.delete(orphans)
92
+ stats.orphans = len(orphans)
93
+ return stats
@@ -0,0 +1,38 @@
1
+ """The state machine of documents."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from statemachine import State, StateMachine
6
+
7
+
8
+ class DocumentStateMachine(StateMachine):
9
+ """Life of a document from upload to deletion.
10
+
11
+ States:
12
+ pending: Upload requested; the content is not confirmed yet.
13
+ processing: Stored and waiting for the processing pipeline.
14
+ available: Stored, verified and processed; it can be downloaded.
15
+ rejected: The pipeline refused it (see ``error``).
16
+ deleted: Detached; the content is purged from the store.
17
+
18
+ Transitions:
19
+ confirm: The content is stored and needs no processing.
20
+ start_processing: The content is stored and goes to the pipeline.
21
+ complete: The pipeline accepted the document.
22
+ reject: The pipeline or the upload check refused it.
23
+ discard: The document is detached or its upload expired.
24
+ """
25
+
26
+ pending = State("Pending", initial=True)
27
+ processing = State("Processing")
28
+ available = State("Available")
29
+ rejected = State("Rejected")
30
+ deleted = State("Deleted", final=True)
31
+
32
+ confirm = pending.to(available)
33
+ start_processing = pending.to(processing)
34
+ complete = processing.to(available)
35
+ reject = pending.to(rejected) | processing.to(rejected)
36
+ discard = (
37
+ pending.to(deleted) | processing.to(deleted) | available.to(deleted) | rejected.to(deleted)
38
+ )