-
Notifications
You must be signed in to change notification settings - Fork 1
/
state_manager.py
100 lines (75 loc) · 2.58 KB
/
state_manager.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from os import path
from typing import Any
import json
from benedict import benedict
DEFAULT_STATE = {
"theme": "dark",
"currentWallpaper": None,
"representationMethods": {
"notifications": True,
"wallpaper": False,
"terminal": False
},
"wallpaperSettings": {
"fontSize": 2.5, # in percent of wallpaper width
"fontColor": "#FFC0CB", # hex color
"pickColorFromWallpaper": True, # if true, fontColor is ignored
"fontFamily": "Arial",
"fontStroke": 5, # in percent of font size (e.g 5% of 16px ~ 1px)
"xOffset": 50, # in percent of screen width
"yOffset": 80, # in percent of screen height
"width": 100, # in percent of screen width
"height": 20, # in percent of screen height
"quality": 100, # in percent
"scaling": 100, # in percent
}
}
state = None # memory cache for state to avoid reading from disk
def reset_state():
"""
This function resets the state to the default state.
"""
set_state(DEFAULT_STATE)
def set_state(new_state: dict):
"""
This function sets the state to the given state.
Args:
new_state (dict): The new state.
"""
global state
with open("state.json", "w") as f: json.dump(new_state, f, indent=4)
state = None # clear memory cache
def get_state() -> dict:
"""
This function returns the current state.
Returns:
dict: The current state.
"""
global state
if state is not None: return state # memory cache
if not path.exists("state.json"): reset_state()
with open("state.json", "r") as f: return json.load(f)
def set_attribute_js_notation(state: dict, attribute: str, value: Any) -> dict:
"""
This function sets the given attribute to the given value in the given state.
Args:
state (dict): The state to set the attribute in.
attribute (str): The attribute to set in js notation.
value (Any): The value to set the attribute to.
Returns:
dict: The state with the attribute set to the value.
"""
state = benedict(state, keypath_separator=".")
state[attribute] = value
return state.dict()
def get_attribute_js_notation(state: dict, attribute: str) -> Any:
"""
This function returns the value of the given attribute in the given state.
Args:
state (dict): The state to get the attribute from.
attribute (str): The attribute to get in js notation.
Returns:
Any: The value of the attribute.
"""
state = benedict(state, keypath_separator=".")
return state[attribute]