-
Notifications
You must be signed in to change notification settings - Fork 7
/
music_config.py
95 lines (73 loc) · 2.58 KB
/
music_config.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
import json
import os.path
from typing import Dict, Union
class MusicConfig:
_config: Dict[str, Union[str, int]]
def __init__(self, file_path: str):
self._file_path = file_path
self._config = {}
# load config
self._load()
def _load(self):
if os.path.isfile(self._file_path):
try:
with open(self._file_path, 'r') as fp:
data = fp.read()
self._config = json.loads(data)
except:
pass
self._load_default()
def _load_default(self):
self._config.setdefault('music_source', 'local')
self._config.setdefault('play_mode', 'random')
self._config.setdefault('local_track_path', '')
self._config.setdefault('cloud_playlist_id', 0)
self._config.setdefault('cloud_track_id', 0)
def set_config(self, key: str, value: Union[str, int]):
self._config[key] = value
# real save
self._save()
def _save(self):
with open(self._file_path, 'w') as fp:
fp.write(json.dumps(self._config))
def get_config(self, key: str, default=None) -> Union[str, int]:
return self._config.get(key, default)
def is_local_source(self) -> bool:
return self.music_source == 'local'
def to_dict(self):
return self._config
@property
def music_source(self):
return self.get_config('music_source', 'local')
@music_source.setter
def music_source(self, val: str):
if self.music_source != val:
self.set_config('music_source', val)
@property
def play_mode(self):
return self.get_config('play_mode', '')
@play_mode.setter
def play_mode(self, val: str):
if self.play_mode != val:
self.set_config('play_mode', val)
@property
def local_track_path(self):
return self.get_config('local_track_path', '')
@local_track_path.setter
def local_track_path(self, val: str):
if self.local_track_path != val:
self.set_config('local_track_path', val)
@property
def cloud_playlist_id(self):
return self.get_config('cloud_playlist_id', 0)
@cloud_playlist_id.setter
def cloud_playlist_id(self, val: str):
if self.cloud_playlist_id != val:
self.set_config('cloud_playlist_id', val)
@property
def cloud_track_id(self):
return self.get_config('cloud_track_id', 0)
@cloud_track_id.setter
def cloud_track_id(self, val: int):
if self.cloud_track_id != val:
self.set_config('cloud_track_id', val)