forked from darknessomi/musicbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
247 lines (237 loc) · 9.24 KB
/
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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# encoding: UTF-8
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from builtins import open
from future import standard_library
standard_library.install_aliases()
import json
import os
from . import logger
from .singleton import Singleton
from .const import Constant
from .utils import utf8_data_to_file
log = logger.getLogger(__name__)
class Config(Singleton):
def __init__(self):
if hasattr(self, '_init'):
return
self._init = True
self.config_file_path = Constant.config_path
self.default_config = {
'version': 8,
'cache': {
'value': False,
'default': False,
'describe': ('A toggle to enable cache function or not. '
'Set value to true to enable it.')
},
'mpg123_parameters': {
'value': [],
'default': [],
'describe': 'The additional parameters when mpg123 start.'
},
'aria2c_parameters': {
'value': [],
'default': [],
'describe': ('The additional parameters when '
'aria2c start to download something.')
},
'music_quality': {
'value': 0,
'default': 0,
'describe': ('Select the quality of the music. '
'May be useful when network is terrible. '
'0 for high quality, 1 for medium and 2 for low.')
},
'global_play_pause': {
'value': '<ctrl><alt>p',
'default': '<ctrl><alt>p',
'describe': 'Global keybind for play/pause.'
'Uses gtk notation for keybinds.'
},
'global_next': {
'value': '<ctrl><alt>j',
'default': '<ctrl><alt>j',
'describe': 'Global keybind for next song.'
'Uses gtk notation for keybinds.'
},
'global_previous': {
'value': '<ctrl><alt>k',
'default': '<ctrl><alt>k',
'describe': 'Global keybind for previous song.'
'Uses gtk notation for keybinds.'
},
'notifier': {
'value': True,
'default': True,
'describe': 'Notifier when switching songs.'
},
'translation': {
'value': True,
'default': True,
'describe': 'Foreign language lyrics translation.'
},
'osdlyrics': {
'value': False,
'default': False,
'describe': 'Desktop lyrics for musicbox.'
},
'osdlyrics_transparent': {
'value': False,
'default': False,
'describe': 'Desktop lyrics transparent bg.'
},
'osdlyrics_color': {
'value': [225, 248, 113],
'default': [225, 248, 113],
'describe': 'Desktop lyrics RGB Color.'
},
'osdlyrics_size': {
'value': [600, 60],
'default': [600, 60],
'describe': 'Desktop lyrics area size.'
},
'osdlyrics_font': {
'value': ['Decorative', 16],
'default': ['Decorative', 16],
'describe': 'Desktop lyrics font-family and font-size.'
},
'osdlyrics_background': {
'value': 'rgba(100, 100, 100, 120)',
'default': 'rgba(100, 100, 100, 120)',
'describe': 'Desktop lyrics background color.'
},
'osdlyrics_on_top': {
'value': True,
'default': True,
'describe': 'Desktop lyrics OnTopHint.'
},
'curses_transparency': {
'value': False,
'default': False,
'describe': 'Set true to make curses transparency.'
}
}
self.config = {}
if not os.path.isfile(self.config_file_path):
self.generate_config_file()
try:
f = open(self.config_file_path, 'r')
except IOError:
log.debug('Read config file error.')
return
try:
self.config = json.loads(f.read())
except ValueError:
log.debug('Load config json data failed.')
return
f.close()
if not self.check_version():
self.save_config_file()
def generate_config_file(self):
f = open(self.config_file_path, 'w')
utf8_data_to_file(f, json.dumps(self.default_config, indent=2))
f.close()
def save_config_file(self):
f = open(self.config_file_path, 'w')
utf8_data_to_file(f, json.dumps(self.config, indent=2))
f.close()
def check_version(self):
if self.config['version'] == self.default_config['version']:
return True
else:
# Should do some update. Like
# if self.database['version'] == 2 : self.database.['version'] = 3
# update database form version 1 to version 2
if self.config['version'] == 1:
self.config['version'] = 2
self.config['global_play_pause'] = {
'value': '<ctrl><alt>p',
'default': '<ctrl><alt>p',
'describe': 'Global keybind for play/pause.'
'Uses gtk notation for keybinds.'
}
self.config['global_next'] = {
'value': '<ctrl><alt>j',
'default': '<ctrl><alt>j',
'describe': 'Global keybind for next song.'
'Uses gtk notation for keybinds.'
}
self.config['global_previous'] = {
'value': '<ctrl><alt>k',
'default': '<ctrl><alt>k',
'describe': 'Global keybind for previous song.'
'Uses gtk notation for keybinds.'
}
elif self.config['version'] == 2:
self.config['version'] = 3
self.config['notifier'] = {
'value': True,
'default': True,
'describe': 'Notifier when switching songs.'
}
elif self.config['version'] == 3:
self.config['version'] = 4
self.config['translation'] = {
'value': True,
'default': True,
'describe': 'Foreign language lyrics translation.'
}
elif self.config['version'] == 4:
self.config['version'] = 5
self.config['osdlyrics'] = {
'value': False,
'default': False,
'describe': 'Desktop lyrics for musicbox.'
}
self.config['osdlyrics_color'] = {
'value': [225, 248, 113],
'default': [225, 248, 113],
'describe': 'Desktop lyrics RGB Color.'
}
self.config['osdlyrics_font'] = {
'value': ['Decorative', 16],
'default': ['Decorative', 16],
'describe': 'Desktop lyrics font-family and font-size.'
}
self.config['osdlyrics_background'] = {
'value': 'rgba(100, 100, 100, 120)',
'default': 'rgba(100, 100, 100, 120)',
'describe': 'Desktop lyrics background color.'
}
self.config['osdlyrics_transparent'] = {
'value': False,
'default': False,
'describe': 'Desktop lyrics transparent bg.'
}
elif self.config['version'] == 5:
self.config['version'] = 6
self.config['osdlyrics_on_top'] = {
'value': True,
'default': True,
'describe': 'Desktop lyrics OnTopHint.'
}
elif self.config['version'] == 6:
self.config['version'] = 7
self.config['curses_transparency'] = {
'value': False,
'default': False,
'describe': 'Set true to make curses transparency.'
}
elif self.config['version'] == 7:
self.config['version'] = 8
self.config['osdlyrics_size'] = {
'value': [600, 60],
'default': [600, 60],
'describe': 'Desktop lyrics area size.'
}
self.check_version()
return False
def get_item(self, name):
if name not in self.config.keys():
if name not in self.default_config.keys():
return None
return self.default_config[name].get('value')
return self.config[name].get('value')