-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfigurator.py
433 lines (350 loc) · 12.9 KB
/
configurator.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# -*-coding:utf-8-*-
"""
相关配置配置读写解析
@author Myles Yang
"""
import configparser
import os
import re
import urllib.parse
import win32con
import const_config
import mylogger
import utils
from utils import create_dialog_w
log = mylogger.default_loguru
def get_bg_abspaths():
"""
获取壁纸目录下的绝对路径列表
不包括子目录
:return: 壁纸绝对路径列表
"""
bg_srcpath = const_config.bg_srcpath
bg_paths = []
if not os.path.isdir(bg_srcpath):
return bg_paths
bg_src_abspath = os.path.abspath(bg_srcpath)
for df in os.listdir(bg_src_abspath):
df_abspath = os.path.join(bg_src_abspath, df)
if os.path.isfile(df_abspath):
bg_paths.append(df_abspath)
return bg_paths
def parse_config():
"""
解析配置文件解析
:return: configparser.RawConfigParser() 配置对象
"""
config = configparser.RawConfigParser()
config_srcpath = const_config.config_srcpath
res = config.read([os.path.join(config_srcpath, 'config.ini')])
if not res:
# http://timgolden.me.uk/pywin32-docs/win32api__MessageBox_meth.html
btn_val = create_dialog_w("配置文件丢失,是否使用默认配置启动?", const_config.dialog_title,
style=win32con.MB_YESNO)
if btn_val != win32con.IDYES:
os._exit(1)
log.info("配置文件读取失败,加载默认配置")
config = get_default_config()
update_config_file(config)
else:
if not config.has_option('Api', 'url') and \
not (config.has_section('Params') and not config.items('Params')):
btn_val = create_dialog_w("配置文件未指定请求URL或参数,是否使用默认配置启动?", const_config.dialog_title,
style=win32con.MB_YESNO)
if btn_val != win32con.IDYES:
os._exit(1)
default_url = get_default_config().get('Api', 'url')
__get_option(config, 'Api', 'url', default=default_url)
return config
"""============================ [APi] ============================"""
def get_request_params(config: configparser.RawConfigParser = None):
"""
从配置文件中获取请求参数
:param config: configparser.RawConfigParser,如果为空则读取配置文件
:return: 参数元组列表 list of (name, value) tuples
"""
config = config if config else parse_config()
if hasattr(config, 'req_params'):
return config.req_params
query = {}
if config.has_option('Api', 'url'):
url = config.get('Api', 'url')
query = urllib.parse.urlsplit(url).query
query = dict(urllib.parse.parse_qsl(query))
params = {}
if config.has_section('Params'):
params = dict(config.items('Params'))
# 合并两个字典,并移除空值
dict_res = dict(query, **params)
dict_res = {k: v for k, v in dict_res.items() if v}
config.req_params = dict_res
return dict_res
"""============================ [Task] ============================"""
def get_current(config: configparser.RawConfigParser = None):
"""
获取配置中当前壁纸数组的下标
:return: 值不存在或小于0,返回0
"""
config = config if config else parse_config()
return __get_int_option(config, 'Task', 'current', default=0, lt=0)
def set_current(current, config: configparser.RawConfigParser = None):
"""
更新配置文件中当前壁纸数组的下标
"""
config = config if config else parse_config()
config.set('Task', 'current', str(current))
update_config_file(config)
def get_seconds(config: configparser.RawConfigParser = None):
"""
获取桌面背景更换的频率
:return: 值不存在或小于10,返回300
"""
config = config if config else parse_config()
return __get_int_option(config, 'Task', 'seconds', default=300, lt=10)
def get_dwn_threads(config: configparser.RawConfigParser = None):
"""
获取下载壁纸时的线程数
"""
config = config if config else parse_config()
default_threads = min(32, (os.cpu_count() or 1) + 4)
return __get_int_option(config, 'Task', 'threads', default=2,
lt=1, lt_default=default_threads,
rt=32, rt_default=32)
def get_random_sleep(config: configparser.RawConfigParser = None):
"""
获取在下载壁纸前的随机睡眠时间
:return tuple: 返回两个元素的元组,生成两个数间的随机值
"""
config = config if config else parse_config()
if hasattr(config, 'rndsleep'):
return config.rndsleep
str_vals = __get_option(config, 'Task', 'rndsleep')
rnd_vals = __get_multivalued_option(str_vals, ',', opt_type=float)
need_update = False
# 数据错误,返回默认值
if not rnd_vals or len(rnd_vals) != 2:
str_vals = get_default_config().get('Task', 'rndsleep')
rnd_vals = __get_multivalued_option(str_vals, ',', opt_type=float)
need_update = True
# 检验每个值是否小于0
for i in range(len(rnd_vals)):
if rnd_vals[i] < 0:
rnd_vals[i] = 0.0
need_update = True
# 判断两个值大小是否反了
if rnd_vals[0] > rnd_vals[1]:
tmp = rnd_vals[1]
rnd_vals[1] = rnd_vals[0]
rnd_vals[0] = tmp
need_update = True
if need_update:
config.set('Task', 'rndsleep', '{},{}'.format(rnd_vals[0], rnd_vals[1]))
update_config_file(config)
config.rndsleep = tuple(rnd_vals)
return config.rndsleep
def is_retain_bg_files(config: configparser.RawConfigParser = None):
"""
在拉取新的壁纸前,是否删除旧的壁纸
"""
return __get_bool_option(config, 'Task', 'retainbgs')
"""============================ [Hotkey] ============================"""
def is_hotkey_enabled(config: configparser.RawConfigParser):
"""
是否启用热键
"""
return __get_bool_option(config, 'Hotkey', 'enabled')
def get_hotkey_prev(config: configparser.RawConfigParser):
"""
获取热键:上一个桌面背景
"""
return __get_hotkey(config, 'Hotkey', 'hk_prev')
def get_hotkey_next(config: configparser.RawConfigParser):
"""
获取热键:下一个桌面背景
"""
return __get_hotkey(config, 'Hotkey', 'hk_next')
def get_hotkey_locate(config: configparser.RawConfigParser):
"""
获取热键:定位到当前桌面背景文件
"""
return __get_hotkey(config, 'Hotkey', 'hk_locate')
def __get_hotkey(config: configparser.RawConfigParser, section: str, option: str):
"""
获取热键通用方法
"""
if not (config or config.has_option(section, option)):
return None
hk_str = config.get(section, option)
hk = __get_multivalued_option(hk_str, '+', opt_type=str)
return utils.list_deduplication(hk)
"""============================ ============================"""
def __get_multivalued_option(opt_val: str, delimiter: str, opt_type: type = str):
"""
获取多值配置项,通常由分隔符分割每一个值
:param opt_val: 配置项字符值
:param delimiter: 分割符
:param opt_type: 配置的项每个值的数据类型,作为类型转换
:return list: 配置项的每个值
"""
if not (opt_val or delimiter):
return []
opt_vals = opt_val.split(delimiter)
# 去除空白字符
opt_vals = list(map(lambda s: s.strip(), opt_vals))
# 去除空白项
opt_vals = list(filter(lambda s: s, opt_vals))
# 类型转换
try:
opt_vals = list(map(lambda s: opt_type(s), opt_vals))
except:
return []
return opt_vals
def __get_int_option(config: configparser.RawConfigParser, section: str, option: str,
default: int = 0,
lt: int = None, lt_default: int = None,
rt: int = None, rt_default: int = None):
"""
获取整数的配置项,
注意:使用到默认值,必定会创建该值然后写入文件
:param config: configparser.RawConfigParser 对象
:param section: 配置项所属section
:param option: 配置项key
:param default: 解析失败或...时使用的默认值
:param lt: if 配置值 < lt then (res = lt_default if lt_default else default)
:param lt_default: 指定 lt 且不符合 lt 条件时返回的值
:param rt: if 配置值 < rt then (res = rt_default if rt_default else default)
:param rt_default: 指定 rt 且不符合 rt 条件时返回的值
:return: 配置项的整数值
"""
if not config.has_option(section, option):
if not config.has_section(section):
config.add_section(section)
config.set(section, option, str(default))
update_config_file(config)
return default
try:
seconds = config.getint(section, option)
except:
config.set(section, option, str(default))
update_config_file(config)
return default
need_update = False
if lt and seconds < lt:
seconds = lt_default if (lt_default or lt_default == 0) else default
need_update = True
if rt and seconds > rt:
seconds = rt_default if (rt_default or rt_default == 0) else default
need_update = True
if need_update:
config.set(section, option, str(default))
update_config_file(config)
return seconds
def __get_option(config: configparser.RawConfigParser, section: str, option: str, default: str = ''):
"""
获取配置项的值
:param config: configparser.RawConfigParser 对象
:param section: 配置项所属section
:param option: 配置项key
:param default: 获取失败时的默认值
:return str: 配置项的值
"""
if not config.has_option(section, option):
if not config.has_section(section):
config.add_section(section)
config.set(section, option, str(default))
update_config_file(config)
return default
return config.get(section, option)
def __get_bool_option(config: configparser.RawConfigParser, section: str, option: str):
"""
判断某项设置的波尔值
:param config: configparser.RawConfigParser 对象
:param section: 配置项所属section
:param option: 配置项key
:return: True or False
"""
if (not config) or (not config.has_section(section)) \
or (not config.has_option(section, option)):
return False
opt_val = config.get(section, option)
if opt_val and not re.match('^(0|off|false)$', opt_val.lower()):
return True
return False
"""============================ ============================"""
def get_default_config():
"""
读取默认配置并写入配置文件
:return: configparser.RawConfigParser() 配置对象
"""
config = configparser.RawConfigParser()
# 配置默认属性
config['Api'] = {
'name': 'Default',
'url': 'https://wallhaven.cc/api/v1/search'
}
config['Params'] = {
'categories': '111',
'purity': '100',
'sorting': 'random',
'order': 'desc',
'resolutions': '1920x1080',
}
config['Task'] = {
'seconds': '600',
'current': '0',
'threads': '2',
'rndsleep': '0.0,5.0',
'retainbgs': '0',
}
config['Hotkey'] = {
'enable': '1',
'hk_prev': 'control+alt+left',
'hk_next': 'control+alt+right',
'hk_locate': 'control+alt+up',
}
config['App'] = {
'version': '1.2021.07.07',
'author': 'Myles Yang',
'github': 'https://github.com/snwjas/RandomDesktopBackground',
'gitee': 'https://gitee.com/snwjas/random-desktop-background',
}
return config
def update_config_file(config: configparser.RawConfigParser):
"""
更新配置文件
"""
if not config:
return False
config_srcpath = const_config.config_srcpath
with open(os.path.join(config_srcpath, 'config.ini'), 'w') as configfile:
config.write(configfile)
return True
def record_pid(running: bool = True):
"""
记录程序运行的PID
"""
pid = os.getpid() if running else -1
pid_path = os.path.join(const_config.pid_srcpath, 'pid')
with open(pid_path, 'w') as f:
f.write(str(pid))
# 锁定PID文件
# 共享锁:所有进程没有写访问权限,加锁进程也没有,但所有进程有读访问权限。
return pid
def get_pid_from_file():
"""
从文件获取PID
:return int: 返回PID,获取失败返回None
"""
pid_filepath = os.path.join(const_config.pid_srcpath, 'pid')
if not os.path.isfile(pid_filepath):
log.error('PID文件丢失,读取失败')
return None
try:
with open(pid_filepath, 'r') as f:
str_pid = f.read(20)
str_pid = str_pid.strip()
int_pid = int(str_pid)
except Exception as e:
log.error('读取PID失败: {}'.format(e))
return None
return int_pid