FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
StreamController/src/backend/SettingsManager.py at main · StreamController/StreamController · GitHub
StreamController
/
StreamController
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
120
Star
1.1k
Code
Issues
19
Pull requests
14
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
StreamController
/
src
/
backend
/
SettingsManager.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
122 lines (97 loc) · 4.26 KB
Breadcrumbs
StreamController
/
src
/
backend
/
SettingsManager.py
Copy path
File metadata and controls
122 lines (97 loc) · 4.26 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""
Author: Core447
Year: 2023
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This programm comes with ABSOLUTELY NO WARRANTY!
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
# Import Python modules
import
os
,
json
,
copy
from
loguru
import
logger
as
log
from
functools
import
lru_cache
# Import own modules
import
globals
as
gl
from
src
.
backend
.
Utils
.
AtomicSaveUtils
import
atomic_save_json
class
SettingsManager
:
def
__init__
(
self
):
self
.
font_defaults
:
dict
=
{}
# Used by the LabelManager to get the default font settings
self
.
load_font_defaults
()
def
invalidate_all_caches
(
self
):
for
name
in
dir
(
self
):
attr
=
getattr
(
self
,
name
)
if
hasattr
(
attr
,
"cache_clear"
):
attr
.
cache_clear
()
@
staticmethod
def
load_settings_from_file
(
file_path
:
str
)
->
dict
:
if
not
os
.
path
.
exists
(
file_path
):
return
{}
try
:
with
open
(
file_path
)
as
f
:
return
json
.
load
(
f
)
except
json
.
decoder
.
JSONDecodeError
as
e
:
log
.
error
(
f"Invalid json in
{
file_path
}
:
{
e
}
"
)
return
{}
@
staticmethod
def
save_settings_to_file
(
file_path
:
str
,
settings
:
dict
)
->
None
:
atomic_save_json
(
file_path
,
settings
)
gl
.
settings_manager
.
invalidate_all_caches
()
@
lru_cache
def
_load_deck_settings_cached
(
self
,
deck_serial_number
:
str
)
->
dict
:
path
=
os
.
path
.
join
(
gl
.
DATA_PATH
,
"settings"
,
"decks"
,
f"
{
deck_serial_number
}
.json"
)
return
self
.
load_settings_from_file
(
path
)
def
get_deck_settings
(
self
,
deck_serial_number
:
str
)
->
dict
:
"""
Retrieves the deck settings for a given deck serial number.
Cached (invalidated on save) and deep-copied per call, so callers can
still freely mutate the result without touching the cache.
Args:
deck_serial_number (str): The serial number of the deck.
Returns:
dict: The deck settings loaded from the file.
"""
return
copy
.
deepcopy
(
self
.
_load_deck_settings_cached
(
deck_serial_number
))
def
save_deck_settings
(
self
,
deck_serial_number
:
str
,
settings
:
dict
)
->
None
:
"""
Saves the settings for a deck.
This is just a wrapper around save_settings_to_file()
Args:
deck_serial_number (str): The serial number of the deck.
settings (dict): The settings to save.
Returns:
None
"""
path
=
os
.
path
.
join
(
gl
.
DATA_PATH
,
"settings"
,
"decks"
,
f"
{
deck_serial_number
}
.json"
)
self
.
save_settings_to_file
(
path
,
settings
)
self
.
invalidate_all_caches
()
@
lru_cache
def
get_app_settings
(
self
)
->
dict
:
path
=
os
.
path
.
join
(
gl
.
DATA_PATH
,
"settings"
,
"settings.json"
)
settings
=
self
.
load_settings_from_file
(
path
)
if
settings
==
None
:
settings
=
{}
self
.
save_settings_to_file
(
path
,
settings
)
return
settings
def
save_app_settings
(
self
,
settings
:
dict
)
->
None
:
path
=
os
.
path
.
join
(
gl
.
DATA_PATH
,
"settings"
,
"settings.json"
)
self
.
save_settings_to_file
(
path
,
settings
)
# Invalidate get_app_settings cache
self
.
get_app_settings
.
cache_clear
()
def
get_static_settings
(
self
)
->
dict
:
"""
Returns always the same settings, no matter what the data path is set to
"""
return
self
.
load_settings_from_file
(
gl
.
STATIC_SETTINGS_FILE_PATH
)
def
save_static_settings
(
self
,
settings
:
dict
)
->
None
:
self
.
save_settings_to_file
(
gl
.
STATIC_SETTINGS_FILE_PATH
,
settings
)
def
load_font_defaults
(
self
)
->
None
:
app_settings
=
self
.
get_app_settings
()
self
.
font_defaults
=
app_settings
.
get
(
"general"
, {}).
get
(
"default-font"
, {})
def
save_font_defaults
(
self
)
->
None
:
app_settings
=
self
.
get_app_settings
()
# Only replace default-font, every other key under general has to survive
app_settings
.
setdefault
(
"general"
, {})[
"default-font"
]
=
self
.
font_defaults
self
.
save_app_settings
(
app_settings
)
Back
|
FazBrowse Home
|
New Git URL