forked from Class-Widgets/Class-Widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconf.py
More file actions
244 lines (208 loc) · 8.52 KB
/
Copy pathconf.py
File metadata and controls
244 lines (208 loc) · 8.52 KB
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
import configparser as config
import json
import os
import time
from pathlib import Path
from typing import Any, Dict, List, Union
from dateutil import parser
from loguru import logger
from PyQt5.QtCore import QCoreApplication
import list_
from basic_dirs import CONFIG_HOME, CW_HOME, PLUGIN_HOME, THEME_DIRS
from data_model import ThemeConfig, ThemeInfo
from file import config_center
from utils import TimeManagerFactory
conf = config.ConfigParser()
name = 'Class Widgets'
# app 图标
app_icon = (
CW_HOME
/ 'img'
/ (
'favicon.ico'
if os.name == 'nt'
else 'favicon.icns'
if os.name == 'darwin'
else 'favicon.png'
)
)
update_countdown_custom_last = 0
countdown_cnt = 0
def __load_json(path: Path) -> ThemeConfig:
with open(path, encoding='utf-8') as file:
return ThemeConfig.model_validate_json(file.read())
# 此函数在 i18n_manager.py 有重复定义,更新时需同步
def load_theme_config(theme: str) -> ThemeInfo:
default_path = CW_HOME / 'ui' / 'default' / 'theme.json'
try:
config_path = next(
(
dir
for theme_dir in THEME_DIRS
if (dir := (theme_dir / theme / 'theme.json')).exists()
),
default_path,
)
return ThemeInfo(path=config_path.parent, config=__load_json(config_path))
except Exception as e:
logger.error(f"加载主题数据时出错: {e!r},返回默认主题")
return ThemeInfo(path=default_path.parent, config=__load_json(default_path))
def load_plugin_config() -> Dict[str, List[str]]:
try:
plugin_config_path = CONFIG_HOME / 'plugin.json'
if plugin_config_path.exists():
with open(plugin_config_path, encoding='utf-8') as file:
data: Dict[str, List[str]] = json.load(file)
else:
with open(plugin_config_path, 'w', encoding='utf-8') as file:
data = {"enabled_plugins": []}
json.dump(data, file, ensure_ascii=False, indent=4)
return data
except Exception as e:
logger.error(f"加载启用插件数据时出错: {e}")
return {"enabled_plugins": []}
def save_plugin_config(data: Dict[str, Any]) -> bool:
data_dict = load_plugin_config()
data_dict.update(data)
try:
with open(CONFIG_HOME / 'plugin.json', 'w', encoding='utf-8') as file:
json.dump(data_dict, file, ensure_ascii=False, indent=4)
return True
except Exception as e:
logger.error(f"保存启用插件数据时出错: {e}")
return False
def save_installed_plugin(raw_data: List[Any]) -> bool:
data = {"plugins": raw_data}
try:
with open(PLUGIN_HOME / 'plugins_from_pp.json', 'w', encoding='utf-8') as file:
json.dump(data, file, ensure_ascii=False, indent=4)
return True
except Exception as e:
logger.error(f"保存已安装插件数据时出错: {e}")
return False
def is_temp_week() -> Union[bool, str]:
if (
config_center.read_conf('Temp', 'set_week') is None
or config_center.read_conf('Temp', 'set_week') == ''
):
return False
return config_center.read_conf('Temp', 'set_week')
def is_temp_schedule() -> bool:
return config_center.read_conf('Temp', 'temp_schedule') not in [None, '']
def update_countdown(cnt: int) -> None:
global update_countdown_custom_last
global countdown_cnt
if (length := len(config_center.read_conf('Date', 'cd_text_custom').split(','))) == 0:
countdown_cnt = -1
elif config_center.read_conf('Date', 'countdown_custom_mode') == '1':
countdown_cnt = cnt
elif (nowtime := time.time()) - update_countdown_custom_last > int(
config_center.read_conf('Date', 'countdown_upd_cd')
):
update_countdown_custom_last = nowtime
countdown_cnt += 1
if countdown_cnt >= length:
countdown_cnt = 0 if length != 0 else -1
def get_cd_text_custom() -> str:
global countdown_cnt
if countdown_cnt == -1:
return QCoreApplication.translate("conf", '未设置')
if countdown_cnt >= len(li := config_center.read_conf('Date', 'cd_text_custom').split(',')):
return QCoreApplication.translate("conf", '未设置')
return li[countdown_cnt] if countdown_cnt >= 0 else ''
def get_custom_countdown() -> str:
global countdown_cnt
if countdown_cnt == -1:
return QCoreApplication.translate("conf", '未设置')
li = config_center.read_conf('Date', 'countdown_date').split(',')
if countdown_cnt == -1 or countdown_cnt >= len(li):
return QCoreApplication.translate("conf", '未设置') # 获取自定义倒计时
custom_countdown = li[countdown_cnt]
if custom_countdown == '':
return QCoreApplication.translate("conf", '未设置')
try:
custom_countdown = parser.parse(custom_countdown)
except Exception as e:
logger.error(f"解析日期时出错: {custom_countdown}, 错误: {e}")
return '解析失败'
current_time = TimeManagerFactory.get_instance().get_current_time()
if custom_countdown < current_time:
return '0 天'
cd_text = custom_countdown - current_time
return f'{cd_text.days + 1} 天'
# return (
# f"{cd_text.days} 天 {cd_text.seconds // 3600} 小时 {cd_text.seconds // 60 % 60} 分"
# )
def get_week_type() -> int:
"""
获取单双周类型
:return: 0 - 单周, 1 - 双周
"""
if (temp_schedule := config_center.read_conf('Temp', 'set_schedule')) not in (
'',
None,
): # 获取单双周
return int(temp_schedule)
start_date_str = config_center.read_conf('Date', 'start_date')
if start_date_str not in ('', None):
try:
start_date = parser.parse(start_date_str)
except (ValueError, TypeError):
logger.error(f"解析日期时出错: {start_date_str}")
return 0 # 解析失败默认单周
today = TimeManagerFactory.get_instance().get_current_time()
week_num = (today - start_date).days // 7 + 1
if week_num % 2 == 0:
return 1 # 双周
return 0 # 单周
return 0 # 默认单周
def get_is_widget_in(widget: str = 'example.ui') -> bool:
widgets_list = list_.get_widget_config()
return widget in widgets_list
def save_widget_conf_to_json(new_data: Dict[str, Any]) -> bool:
# 初始化 data_dict 为一个空字典
data_dict = {}
widget_json_path = CONFIG_HOME / 'widget.json'
if widget_json_path.exists():
try:
with open(widget_json_path, encoding='utf-8') as file:
data_dict = json.load(file)
except Exception as e:
print(f"读取现有数据时出错: {e}")
return False
data_dict.update(new_data)
try:
with open(widget_json_path, 'w', encoding='utf-8') as file:
json.dump(data_dict, file, ensure_ascii=False, indent=4)
return True
except Exception as e:
print(f"保存数据时出错: {e}")
return False
def load_plugins() -> Dict[str, Dict[str, str]]: # 加载插件配置文件
plugin_dict = {}
for folder in PLUGIN_HOME.iterdir():
if folder.is_dir() and (folder / 'plugin.json').exists():
try:
with open(PLUGIN_HOME / folder.name / "plugin.json", encoding='utf-8') as file:
data = json.load(file)
except Exception as e:
logger.error(f"加载插件配置文件数据时出错,将跳过: {e}") # 跳过奇怪的文件夹
plugin_dict[str(folder.name)] = {}
plugin_dict[str(folder.name)]['name'] = data['name'] # 名称
plugin_dict[str(folder.name)]['version'] = data['version'] # 插件版本号
plugin_dict[str(folder.name)]['author'] = data['author'] # 作者
plugin_dict[str(folder.name)]['description'] = data['description'] # 描述
plugin_dict[str(folder.name)]['plugin_ver'] = data['plugin_ver'] # 插件架构版本
plugin_dict[str(folder.name)]['settings'] = data['settings'] # 设置
plugin_dict[str(folder.name)]['url'] = data.get('url', '') # 插件URL
return plugin_dict
if __name__ == '__main__':
print('AL_1S')
print(get_week_type())
print(load_plugins())
# save_data_to_json(test_data_dict, 'schedule-1.json')
# loaded_data = load_from_json('schedule-1.json')
# print(loaded_data)
# schedule = loaded_data.get('schedule')
# print(schedule['0'])
# add_shortcut_to_startmenu('Settings.exe', 'img/favicon.ico')