-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathscope.py
More file actions
51 lines (38 loc) · 1.52 KB
/
Copy pathscope.py
File metadata and controls
51 lines (38 loc) · 1.52 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
50
51
from enum import StrEnum
CLIENT_PREFIX = "@"
PROFILE_PREFIX = "%"
class ScopeType(StrEnum):
CLIENT = "client"
PROFILE = "profile"
def normalize_scope(scope: str):
if not scope.startswith(CLIENT_PREFIX) and not scope.startswith(PROFILE_PREFIX):
return f"{CLIENT_PREFIX}{scope}"
return scope
def extract_from_scope(scope: str):
scope = normalize_scope(scope)
if scope.startswith(CLIENT_PREFIX):
return ScopeType.CLIENT, scope[1:]
if scope.startswith(PROFILE_PREFIX):
return ScopeType.PROFILE, scope[1:]
raise ValueError(f"Invalid scope: {scope}")
def parse_server(server: str):
"""
Parse a server string into its components.
Args:
server (str): The server string to parse.
Returns:
tuple: A tuple containing the context type, context name, and server name.
"""
client_type, server_name = extract_from_scope(server)
splitted = server_name.split("/", 1)
if len(splitted) == 1:
if server.startswith(CLIENT_PREFIX) or server.startswith(PROFILE_PREFIX):
return client_type, splitted[0], ""
return ScopeType.CLIENT, "", splitted[0]
return client_type, splitted[0], splitted[1]
def format_scope(scope_type: ScopeType, scope_name: str):
if scope_type == ScopeType.CLIENT and not scope_name.startswith(CLIENT_PREFIX):
return f"{CLIENT_PREFIX}{scope_name}"
if scope_type == ScopeType.PROFILE and not scope_name.startswith(PROFILE_PREFIX):
return f"{PROFILE_PREFIX}{scope_name}"
return scope_name