-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_lib.py
More file actions
72 lines (57 loc) · 2.55 KB
/
Copy path_lib.py
File metadata and controls
72 lines (57 loc) · 2.55 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
import subprocess
import typing as t
from types import SimpleNamespace
def namespace_to_dict(
obj: t.Union[SimpleNamespace, t.List[SimpleNamespace]],
) -> t.Union[t.Dict, t.List[t.Dict], SimpleNamespace, t.List[SimpleNamespace]]:
"""
Recursively convert a SimpleNamespace object and any nested namespaces into a dictionary.
:param obj: The object to convert. It can be a SimpleNamespace, list, dictionary, or any other type.
:type obj: Union[SimpleNamespace, List[SimpleNamespace]]
:return: A dictionary (or list, or primitive type) suitable for JSON serialization.
:rtype: Union[Dict, List[Dict], SimpleNamespace, List[SimpleNamespace]]
"""
if isinstance(obj, SimpleNamespace):
return {key: namespace_to_dict(value) for key, value in vars(obj).items()}
elif isinstance(obj, list):
return [namespace_to_dict(item) for item in obj]
elif isinstance(obj, dict):
return {key: namespace_to_dict(value) for key, value in obj.items()}
else:
return obj
def dict_to_namespace(
obj: t.Union[t.List[t.Dict], t.Dict],
) -> t.Union[t.List[SimpleNamespace], SimpleNamespace, t.List[t.Dict], t.Dict]:
"""
Recursively converts the API response into a SimpleNamespace object(s).
:param obj: The object to convert, either a dictionary or a list of dictionaries.
:type obj: Union[List[Dict], Dict]
:return: A SimpleNamespace object or list of SimpleNamespace objects.
:rtype: Union[List[SimpleNamespace], SimpleNamespace, List[Dict], Dict]
"""
if isinstance(obj, t.Dict):
return SimpleNamespace(
**{key: dict_to_namespace(obj=value) for key, value in obj.items()}
)
elif isinstance(obj, t.List):
return [dict_to_namespace(obj=item) for item in obj]
else:
return obj
def update_window_title(text: str):
"""
Update the current window title with the specified text.
:param text: Text to update the window with.
"""
from . import __pkg__, __version__
from ._cli.panels import console
console.set_window_title(f"{__pkg__.capitalize()} v{__version__} - {text}")
def clear_screen():
"""
Clear the screen.
Not using console.clear() because it doesn't really clear the screen.
It instead creates a space between the items on top and below,
then moves the cursor to the items on the bottom, thus creating the illusion of a "cleared screen".
Using subprocess might be a bad idea, but I'm yet to see how bad of an idea that is.
"""
subprocess.run(["cls" if os.name == "nt" else "clear"])