Debian Codegraph @

src/globus_sdk/services/gcs/data/_common.py

raw
1 from __future__ import annotations
2
3 import typing as t
4
5 from globus_sdk._missing import MISSING
6
7 VersionTuple = t.Tuple[int, int, int]
8
9 DatatypeCallback = t.Callable[["DocumentWithInducedDatatype"], t.Optional[VersionTuple]]
10
11
12 class DocumentWithInducedDatatype(t.Protocol):
13 DATATYPE_BASE: str
14 DATATYPE_VERSION_IMPLICATIONS: dict[str, VersionTuple]
15 DATATYPE_VERSION_CALLBACKS: tuple[DatatypeCallback, ...]
16
17 def __contains__(self, key: str) -> bool: # pragma: no cover
18 ...
19
20 def __setitem__(self, key: str, value: t.Any) -> None: # pragma: no cover
21 ...
22
23 def __getitem__(self, key: str) -> t.Any: # pragma: no cover
24 ...
25
26
27 def deduce_datatype_version(obj: DocumentWithInducedDatatype) -> str:
28 max_deduced_version = (1, 0, 0)
29 for fieldname, version in obj.DATATYPE_VERSION_IMPLICATIONS.items():
30 if fieldname not in obj or obj[fieldname] is MISSING:
31 continue
32 if version > max_deduced_version:
33 max_deduced_version = version
34 for callback in obj.DATATYPE_VERSION_CALLBACKS:
35 opt_version = callback(obj)
36 if opt_version is not None and opt_version > max_deduced_version:
37 max_deduced_version = opt_version
38 return ".".join(str(x) for x in max_deduced_version)
39
40
41 def ensure_datatype(obj: DocumentWithInducedDatatype) -> None:
42 if "DATA_TYPE" not in obj or obj["DATA_TYPE"] is MISSING:
43 obj["DATA_TYPE"] = f"{obj.DATATYPE_BASE}#{deduce_datatype_version(obj)}"