Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

dbbasic-object-server/object_metadata.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

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
3from __future__ import annotations
4
5from pathlib import Path
6from typing import Any, Iterable
7
8import object_files
9import object_logs
10import object_source
11import object_state
12import object_versions
13from object_namespace import (
14 ObjectSource,
15 iter_object_sources,
16 parse_user_object_id,
17 validate_object_id,
18)
19from object_versions import DEFAULT_DATA_DIR, InvalidObjectIdError
20
21
22def 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
48def _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
59def _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)