about summary refs log tree commit diff
path: root/gn_auth/auth/authorisation/resources/data.py
diff options
context:
space:
mode:
Diffstat (limited to 'gn_auth/auth/authorisation/resources/data.py')
-rw-r--r--gn_auth/auth/authorisation/resources/data.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/gn_auth/auth/authorisation/resources/data.py b/gn_auth/auth/authorisation/resources/data.py
new file mode 100644
index 0000000..8f5e625
--- /dev/null
+++ b/gn_auth/auth/authorisation/resources/data.py
@@ -0,0 +1,29 @@
+"""
+Utilities for handling data on resources.
+
+These are mostly meant for internal use.
+"""
+from uuid import UUID
+from typing import Sequence
+from functools import reduce
+
+import sqlite3
+
+from .base import Resource
+
+def __attach_data__(
+        data_rows: Sequence[sqlite3.Row],
+        resources: Sequence[Resource]) -> Sequence[Resource]:
+    def __organise__(acc, row):
+        resource_id = UUID(row["resource_id"])
+        return {
+            **acc,
+            resource_id: acc.get(resource_id, tuple()) + (dict(row),)
+        }
+    organised: dict[UUID, tuple[dict, ...]] = reduce(__organise__, data_rows, {})
+    return tuple(
+        Resource(
+            resource.group, resource.resource_id, resource.resource_name,
+            resource.resource_category, resource.public,
+            organised.get(resource.resource_id, tuple()))
+        for resource in resources)