askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
object_metadata.py · 2.0 KB · 64 lines
| 1 | """Read-only object metadata summaries.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | from pathlib import Path |
| 6 | from typing import Any, Iterable |
| 7 | |
| 8 | import object_files |
| 9 | import object_logs |
| 10 | import object_source |
| 11 | import object_state |
| 12 | import object_versions |
| 13 | from object_namespace import ( |
| 14 | ObjectSource, |
| 15 | iter_object_sources, |
| 16 | parse_user_object_id, |
| 17 | validate_object_id, |
| 18 | ) |
| 19 | from object_versions import DEFAULT_DATA_DIR, InvalidObjectIdError |
| 20 | |
| 21 | |
| 22 | def get_object_metadata( |
| 23 | object_id: str, |
| 24 | base_dir: Path | str = DEFAULT_DATA_DIR, |
| 25 | roots: Iterable[Path] | None = None, |
| 26 | ) -> dict[str, Any]: |
| 27 | """Return a conservative metadata summary for an existing object.""" |
| 28 | source = _find_object_source(object_id, roots=roots) |
| 29 | state = object_state.get_object_state(object_id, base_dir=base_dir) |
| 30 | logs = object_logs.get_object_logs(object_id, base_dir=base_dir) |
| 31 | files = object_files.list_object_files(object_id, base_dir=base_dir) |
| 32 | versions = object_versions.VersionManager(base_dir).get_history(object_id) |
| 33 | |
| 34 | return { |
| 35 | "object_id": object_id, |
| 36 | "source_path": source.relative_path.as_posix(), |
| 37 | "owner": _object_owner(object_id), |
| 38 | "kind": source.kind, |
| 39 | "last_modified": source.path.stat().st_mtime, |
| 40 | "state_count": len(state), |
| 41 | "state_keys": list(state.keys()), |
| 42 | "log_count": len(logs), |
| 43 | "file_count": len(files), |
| 44 | "version_count": len(versions), |
| 45 | } |
| 46 | |
| 47 | |
| 48 | def _find_object_source(object_id: str, roots: Iterable[Path] | None = None) -> ObjectSource: |
| 49 | if not validate_object_id(object_id): |
| 50 | raise InvalidObjectIdError(f"Invalid object ID: {object_id}") |
| 51 | |
| 52 | for source in iter_object_sources(roots): |
| 53 | if source.object_id == object_id: |
| 54 | return source |
| 55 | |
| 56 | raise object_source.ObjectSourceNotFoundError(f"Object source not found: {object_id}") |
| 57 | |
| 58 | |
| 59 | def _object_owner(object_id: str) -> str: |
| 60 | parsed = parse_user_object_id(object_id) |
| 61 | if parsed is None: |
| 62 | return "system" |
| 63 | user_id, _ = parsed |
| 64 | return str(user_id) |