-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathversioning.py
More file actions
49 lines (39 loc) · 1.46 KB
/
versioning.py
File metadata and controls
49 lines (39 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from __future__ import annotations
from collections.abc import Callable, Sequence
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from .func.session import ComputeSession
from .session import BaseSession
naming_profile = {
"path": ("kernel", "session"),
"session_events_path": ("/stream/kernel/_/events", "/events/session"),
"name_arg": ("clientSessionToken", "name"),
"event_name_arg": ("sessionId", "name"),
"name_gql_field": ("sess_id", "name"),
"type_gql_field": ("sess_type", "type"),
}
def get_naming(api_version: tuple[int, str], key: str) -> str:
if api_version[0] <= 4:
return naming_profile[key][0]
return naming_profile[key][1]
def get_id_or_name(api_version: tuple[int, str], obj: ComputeSession) -> str:
if api_version[0] <= 4:
if obj.name is None:
raise ValueError("Session name is required for API version <= 4")
return obj.name
if obj.id:
return str(obj.id)
if obj.name is None:
raise ValueError("Session must have either id or name")
return obj.name
def apply_version_aware_fields(
api_session: BaseSession,
fields: Sequence[tuple[str, Callable[[BaseSession], str] | str]],
) -> Sequence[tuple[str, str]]:
version_aware_fields = []
for f in fields:
if callable(f[1]):
version_aware_fields.append((f[0], f[1](api_session)))
else:
version_aware_fields.append((f[0], f[1]))
return version_aware_fields