Schließe die Connector-Kapselung ab, damit Paperless, IMAP und weitere Quellen über dieselbe Anbindung laufen.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
75a3949f7c
commit
d6b70d8f92
75 files changed
+9477
-414
No files matched your search
@@ -0,0 +1,182 @@
|
||||
"""Third source used only to prove that registration is enough.
|
||||
|
||||
This module is not a product connector and is not imported by the application.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from uuid import UUID
|
||||
|
||||
from app.access.policy import mediated_source_owner
|
||||
from app.core.errors import not_found, unprocessable
|
||||
from app.retrieval import FilePayload, RetrievalFailure
|
||||
from app.sourcebind import resolve_for_read
|
||||
from app.sourceregistry import SourceModule, register_source, unregister_source
|
||||
from app.sources import ORIGINAL_UNAVAILABLE, _audit_mediated, _connector_owner
|
||||
|
||||
KIND = "sample"
|
||||
|
||||
CATALOG = {
|
||||
"alpha": {
|
||||
"brief": {
|
||||
"id": "brief",
|
||||
"title": "Probenbrief",
|
||||
"text": "Der Probenbrief bleibt lesbar.",
|
||||
},
|
||||
"notiz": {
|
||||
"id": "notiz",
|
||||
"title": "Randnotiz",
|
||||
"text": "Die Randnotiz bleibt in der Quelle.",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
CAPABILITIES = {
|
||||
"kind": KIND,
|
||||
"navigate": False,
|
||||
"read_original": True,
|
||||
"read_preview": True,
|
||||
"read_attachment": False,
|
||||
"edit_content": False,
|
||||
"edit_metadata": False,
|
||||
"search": {
|
||||
"query": True,
|
||||
"title": False,
|
||||
"correspondent": False,
|
||||
"document_type": False,
|
||||
"tags": False,
|
||||
"document_range": False,
|
||||
"sort": False,
|
||||
"continuation": False,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def parse_config(payload: dict) -> tuple[str, dict, str]:
|
||||
config = payload.get("config") or {}
|
||||
catalog = str(config.get("catalog") or "").strip()
|
||||
name = str(payload.get("name") or "").strip() or catalog
|
||||
if catalog not in CATALOG:
|
||||
raise unprocessable("Der Katalog dieser Beispielquelle ist unbekannt.")
|
||||
return name, {"catalog": catalog}, ""
|
||||
|
||||
|
||||
def probe(row) -> dict:
|
||||
items = _items(row)
|
||||
return {"ok": True, "detail": f"Beispielquelle antwortet. {len(items)} Einträge sichtbar."}
|
||||
|
||||
|
||||
def find(conn, owner_id: UUID, connector_id: UUID | None, query: str, options: dict) -> dict:
|
||||
del options
|
||||
row = _connector(conn, owner_id, connector_id)
|
||||
needle = query.casefold()
|
||||
items = list(_items(row).values())
|
||||
if needle:
|
||||
items = [
|
||||
item for item in items
|
||||
if needle in item["title"].casefold() or needle in item["text"].casefold()
|
||||
]
|
||||
return {"items": items, "connector_id": str(row["id"])}
|
||||
|
||||
|
||||
def preview(conn, owner_id: UUID, connector_id: UUID, options: dict) -> dict:
|
||||
row = _connector(conn, owner_id, connector_id)
|
||||
item = _items(row).get(str(options.get("item") or ""))
|
||||
if item is None:
|
||||
raise unprocessable("Dieser Eintrag ist in der Beispielquelle nicht vorhanden.")
|
||||
return {"id": item["id"], "title": item["title"], "text": item["text"], "media": "text"}
|
||||
|
||||
|
||||
def open_original(conn, actor_id: UUID, element: dict, context, ref) -> tuple[bytes, str, str]:
|
||||
external_id = str((ref or {}).get("external_id") or "")
|
||||
stored = ref.get("connector_id") if ref else None
|
||||
stored_owner = _connector_owner(conn, stored)
|
||||
if element["owner_subject_id"] == actor_id or stored_owner == actor_id:
|
||||
result = resolve_for_read(conn, actor_id, KIND, ref, None)
|
||||
if result.row is None:
|
||||
raise unprocessable(result.message)
|
||||
return _bytes(result.row, external_id)
|
||||
owner_id = mediated_source_owner(conn, actor_id, element, context)
|
||||
if owner_id is None:
|
||||
raise unprocessable(ORIGINAL_UNAVAILABLE)
|
||||
result = resolve_for_read(conn, owner_id, KIND, ref, None)
|
||||
if result.row is None:
|
||||
_audit_mediated(conn, actor_id, element, context, KIND, stored, "unavailable")
|
||||
raise unprocessable(ORIGINAL_UNAVAILABLE)
|
||||
try:
|
||||
data, media, name = _bytes(result.row, external_id)
|
||||
except Exception:
|
||||
_audit_mediated(conn, actor_id, element, context, KIND, result.row["id"], "unavailable")
|
||||
raise unprocessable(ORIGINAL_UNAVAILABLE) from None
|
||||
_audit_mediated(conn, actor_id, element, context, KIND, result.row["id"], "ok")
|
||||
return data, media, name
|
||||
|
||||
|
||||
def export_original(conn, actor_id: UUID, external_id: str, detail: dict, assigned, ref):
|
||||
del detail
|
||||
result = resolve_for_read(conn, actor_id, KIND, ref, assigned)
|
||||
if result.row is None:
|
||||
return RetrievalFailure(result.code, result.message, result.choices)
|
||||
try:
|
||||
data, media, name = _bytes(result.row, str(external_id))
|
||||
except Exception as exc:
|
||||
return RetrievalFailure("missing", str(getattr(exc, "detail", exc)))
|
||||
return FilePayload(name, media, data, "")
|
||||
|
||||
|
||||
def install_sample_source() -> None:
|
||||
register_source(SourceModule(
|
||||
kind=KIND,
|
||||
label="Beispielquelle",
|
||||
representation="text",
|
||||
capabilities=CAPABILITIES,
|
||||
parse_config=parse_config,
|
||||
probe=probe,
|
||||
find=find,
|
||||
open_original=open_original,
|
||||
export_original=export_original,
|
||||
navigate=None,
|
||||
preview=preview,
|
||||
))
|
||||
|
||||
|
||||
def remove_sample_source() -> None:
|
||||
unregister_source(KIND)
|
||||
|
||||
|
||||
def _connector(conn, owner_id: UUID, connector_id: UUID | None):
|
||||
if connector_id is not None:
|
||||
row = conn.execute(
|
||||
"""
|
||||
SELECT id, owner_id, kind, name, settings, secret
|
||||
FROM connectors WHERE id = %s AND owner_id = %s
|
||||
""",
|
||||
(connector_id, owner_id),
|
||||
).fetchone()
|
||||
if row is None or row["kind"] != KIND:
|
||||
raise not_found()
|
||||
return row
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT id, owner_id, kind, name, settings, secret
|
||||
FROM connectors
|
||||
WHERE owner_id = %s AND kind = %s
|
||||
ORDER BY name, id
|
||||
""",
|
||||
(owner_id, KIND),
|
||||
).fetchall()
|
||||
if len(rows) != 1:
|
||||
raise unprocessable("Für diese Quelle ist kein eindeutiger Zugang zugeordnet.")
|
||||
return rows[0]
|
||||
|
||||
|
||||
def _items(row) -> dict:
|
||||
catalog = (row["settings"] or {}).get("catalog")
|
||||
return CATALOG.get(catalog) or {}
|
||||
|
||||
|
||||
def _bytes(row, external_id: str) -> tuple[bytes, str, str]:
|
||||
item = _items(row).get(external_id)
|
||||
if item is None:
|
||||
raise unprocessable("Dieser Eintrag ist in der Beispielquelle nicht vorhanden.")
|
||||
return item["text"].encode("utf-8"), "text/plain", f"{item['id']}.txt"
|
||||
Reference in new issue
Block a user