-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_format.py
More file actions
496 lines (411 loc) · 20.7 KB
/
Copy pathjson_format.py
File metadata and controls
496 lines (411 loc) · 20.7 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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QFrame, QWidget, QVBoxLayout, QHBoxLayout, QLabel
from qfluentwidgets import SubtitleLabel, setFont, ComboBox, PushButton, ProgressBar, PrimaryPushButton, IndeterminateProgressBar
from PyQt6.QtCore import QThread, pyqtSignal
import os
import orjson
from functions import format_json_file, show_message_bar
from config import cfg
from found import scan_packs
import shared # 正确导入shared模块
class JsonFormatInterface(QFrame):
""" JSON 文件规范化界面 """
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setObjectName('JsonFormatInterface')
self.vBoxLayout = QVBoxLayout(self)
self.behavior_packs = [] # 存储行为包信息
self.selected_pack_path = None # 存储选中的包路径
# 创建标题
self.titleLabel = SubtitleLabel('JSON 文件规范化', self)
setFont(self.titleLabel, 24)
self.titleLabel.setAlignment(Qt.AlignmentFlag.AlignCenter)
# 创建下拉框、按钮和进度条的水平布局
self.controlLayout = QHBoxLayout()
self.packComboBox = ComboBox(self)
self.packComboBox.setPlaceholderText('没有行为包')
self.refreshButton = PushButton('刷新', self)
self.refreshButton.clicked.connect(self.load_behavior_packs)
self.packComboBox.currentIndexChanged.connect(self.on_pack_selected)
# 创建开始按钮
self.startButton = PrimaryPushButton('开始', self)
self.startButton.setEnabled(False) # 初始禁用
self.startButton.clicked.connect(self.start_json_formatting)
# 创建全部规范化按钮
self.formatAllButton = PrimaryPushButton('全部规范化', self)
self.formatAllButton.setEnabled(False) # 初始禁用
self.formatAllButton.clicked.connect(self.start_format_all_json)
# 创建不确定进度条
self.indeterminateProgressBar = IndeterminateProgressBar(self)
self.indeterminateProgressBar.setFixedWidth(200)
self.indeterminateProgressBar.hide() # 默认隐藏
# 创建确定进度条
self.progressBar = ProgressBar(self)
self.progressBar.setValue(0) # 默认为空
self.progressBar.setFixedWidth(200)
self.progressBar.hide() # 默认隐藏
# 添加控件到水平布局
self.controlLayout.addWidget(self.packComboBox)
self.controlLayout.addWidget(self.refreshButton)
self.controlLayout.addWidget(self.startButton)
self.controlLayout.addWidget(self.formatAllButton)
self.controlLayout.addWidget(self.indeterminateProgressBar)
self.controlLayout.addWidget(self.progressBar)
self.controlLayout.addStretch()
# 添加处理文件标签
self.processingLabel = QLabel(self)
self.processingLabel.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.processingLabel.setVisible(False) # 默认隐藏
# 添加组件到主布局
self.vBoxLayout.addWidget(self.titleLabel)
self.vBoxLayout.addLayout(self.controlLayout)
self.vBoxLayout.addWidget(self.processingLabel, 0, Qt.AlignmentFlag.AlignCenter) # 添加标签到布局
self.vBoxLayout.addStretch()
self.vBoxLayout.setContentsMargins(36, 10, 36, 36)
self.vBoxLayout.setSpacing(20)
self.load_behavior_packs() # 初始化时加载行为包
def load_behavior_packs(self):
self.packComboBox.clear()
self.behavior_packs = []
app_folder = cfg.appFolder.value
if not app_folder or not os.path.exists(app_folder):
show_message_bar(title='错误', content='未配置AppFolder路径或路径不存在', bar_type='error', duration=5000, parent=self)
self.packComboBox.setPlaceholderText('没有行为包')
self.startButton.setEnabled(False)
self.formatAllButton.setEnabled(False)
return
# 使用scan_packs函数获取行为包和资源包
behavior_packs, _ = scan_packs()
self.behavior_packs = behavior_packs
if self.behavior_packs:
default_index = -1 # 默认不选中
error_json_pack_index = -1 # 存储包含错误JSON文件的包的索引
# 添加包到下拉框
for i, pack in enumerate(self.behavior_packs):
self.packComboBox.addItem(pack.name, userData=pack.path)
# 检查是否是包含错误JSON文件的包
if shared.error_json_pack_path and shared.error_json_pack_path == pack.path:
error_json_pack_index = i
# 优先选择包含错误JSON文件的包,如果没有则选择第一个包
if error_json_pack_index >= 0:
self.packComboBox.setCurrentIndex(error_json_pack_index)
self.startButton.setEnabled(True)
self.formatAllButton.setEnabled(True)
elif len(self.behavior_packs) > 0:
self.packComboBox.setCurrentIndex(0) # 选择第一个包
self.startButton.setEnabled(True)
self.formatAllButton.setEnabled(True)
else:
self.packComboBox.setCurrentIndex(-1) # 不选中任何包
self.packComboBox.setPlaceholderText('选择一个行为包')
self.startButton.setEnabled(False)
self.formatAllButton.setEnabled(False)
else:
self.packComboBox.setPlaceholderText('没有行为包')
self.startButton.setEnabled(False)
self.formatAllButton.setEnabled(False)
def on_pack_selected(self, index):
if index >= 0:
self.selected_pack_path = self.packComboBox.itemData(index)
self.startButton.setEnabled(True)
self.formatAllButton.setEnabled(True)
self.progressBar.setValue(0) # 重置进度条
else:
self.selected_pack_path = None
self.startButton.setEnabled(False)
self.formatAllButton.setEnabled(False)
def start_json_formatting(self):
if not self.selected_pack_path:
show_message_bar(title='提示', content='请先选择一个行为包', bar_type='warning', duration=5000, parent=self)
return
# 禁用UI元素
self.startButton.setEnabled(False)
self.formatAllButton.setEnabled(False)
self.refreshButton.setEnabled(False)
self.packComboBox.setEnabled(False)
# 显示不确定进度条
self.indeterminateProgressBar.show()
self.processingLabel.setText("正在扫描JSON文件...")
self.processingLabel.setVisible(True)
# 找到对应的 PackInfo 对象
selected_pack_name = self.packComboBox.currentText()
current_pack_info = next((pack for pack in self.behavior_packs if pack.name == selected_pack_name and pack.path == self.selected_pack_path), None)
if not current_pack_info:
show_message_bar(title='错误', content='无法找到选定包的信息。', bar_type='error', duration=5000, parent=self)
self.reset_ui_state()
return
# 创建并启动扫描线程
self.scanning_thread = JsonScanningThread(current_pack_info.path)
self.scanning_thread.scanning_completed.connect(self.on_scanning_completed)
self.scanning_thread.start()
def start_format_all_json(self):
"""启动全部JSON文件规范化流程"""
if not self.selected_pack_path:
show_message_bar(title='提示', content='请先选择一个行为包', bar_type='warning', duration=5000, parent=self)
return
# 禁用UI元素
self.startButton.setEnabled(False)
self.formatAllButton.setEnabled(False)
self.refreshButton.setEnabled(False)
self.packComboBox.setEnabled(False)
# 显示不确定进度条
self.indeterminateProgressBar.show()
self.processingLabel.setText("正在扫描所有JSON文件,准备进行规范化...")
self.processingLabel.setVisible(True)
# 找到对应的 PackInfo 对象
selected_pack_name = self.packComboBox.currentText()
current_pack_info = next((pack for pack in self.behavior_packs if pack.name == selected_pack_name and pack.path == self.selected_pack_path), None)
if not current_pack_info:
show_message_bar(title='错误', content='无法找到选定包的信息。', bar_type='error', duration=5000, parent=self)
self.reset_ui_state()
return
# 创建并启动全部JSON扫描线程
self.all_json_scanning_thread = AllJsonScanningThread(current_pack_info.path)
self.all_json_scanning_thread.json_files_found.connect(self.on_all_json_files_found)
self.all_json_scanning_thread.start()
def on_scanning_completed(self, failed_json_files):
# 隐藏不确定进度条
self.indeterminateProgressBar.hide()
if not failed_json_files:
show_message_bar(title='成功', content='所有行为包内的JSON文件均可被解析!', bar_type='success', duration=5000, parent=self)
self.reset_ui_state()
return
# 显示确定进度条
self.progressBar.show()
self.progressBar.setValue(0)
# 创建并启动格式化线程
self.formatting_thread = JsonFormattingThread(self.selected_pack_path, failed_json_files)
self.formatting_thread.progress_updated.connect(self.update_progress)
self.formatting_thread.file_processing.connect(self.update_processing_file)
self.formatting_thread.formatting_completed.connect(self.on_formatting_completed)
self.formatting_thread.start()
def on_all_json_files_found(self, json_files):
"""处理找到的所有JSON文件"""
# 隐藏不确定进度条
self.indeterminateProgressBar.hide()
if not json_files:
show_message_bar(title='提示', content='未找到任何JSON文件', bar_type='info', duration=5000, parent=self)
self.reset_ui_state()
return
# 显示确定进度条
self.progressBar.show()
self.progressBar.setValue(0)
# 更新处理标签,说明优化的解析策略
self.processingLabel.setText(f"正在规范化 {len(json_files)} 个JSON文件 (优先使用高效orjson解析)")
# 创建并启动格式化所有JSON线程
self.format_all_thread = FormatAllJsonThread(json_files)
self.format_all_thread.progress_updated.connect(self.update_progress)
self.format_all_thread.file_processing.connect(self.update_processing_file)
self.format_all_thread.formatting_completed.connect(self.on_all_formatting_completed)
self.format_all_thread.start()
def update_progress(self, value):
self.progressBar.setValue(value)
def update_processing_file(self, file_name):
"""更新正在处理的文件标签"""
self.processingLabel.setText(f"正在处理: {file_name}")
self.processingLabel.setVisible(True)
def on_formatting_completed(self, failed_files):
"""格式化完成的处理"""
self.progressBar.setValue(100)
self.reset_ui_state()
# 清除全局变量中的错误包路径
shared.error_json_pack_path = None
if failed_files:
failed_files_str = "\n".join([f"{os.path.basename(path)}: {error}" for path, error in failed_files])
show_message_bar(
title='JSON格式化完成',
content=f'有 {len(failed_files)} 个文件无法使用json5解析,已跳过。\n{failed_files_str}',
bar_type='warning',
duration=10000,
parent=self
)
else:
show_message_bar(
title='成功',
content='所有不规范的JSON文件已成功格式化!',
bar_type='success',
duration=5000,
parent=self
)
def on_all_formatting_completed(self, failed_files):
"""全部格式化完成的处理"""
self.progressBar.setValue(100)
self.reset_ui_state()
# 清除全局变量中的错误包路径
shared.error_json_pack_path = None
if failed_files:
failed_files_str = "\n".join([f"{os.path.basename(path)}: {error}" for path, error in failed_files[:10]])
if len(failed_files) > 10:
failed_files_str += f"\n... 以及其他 {len(failed_files) - 10} 个文件"
show_message_bar(
title='全部JSON格式化完成',
content=f'有 {len(failed_files)} 个文件无法使用json5解析,已跳过。\n{failed_files_str}',
bar_type='warning',
duration=10000,
parent=self
)
else:
show_message_bar(
title='成功',
content='所有JSON文件已成功规范化!',
bar_type='success',
duration=5000,
parent=self
)
def reset_ui_state(self):
"""重置UI状态"""
self.processingLabel.setVisible(False)
self.startButton.setEnabled(True)
self.formatAllButton.setEnabled(True)
self.refreshButton.setEnabled(True)
self.packComboBox.setEnabled(True)
self.indeterminateProgressBar.hide()
self.progressBar.hide()
self.progressBar.setValue(0)
class JsonScanningThread(QThread):
scanning_completed = pyqtSignal(list) # 扫描完成信号,传递解析失败的文件列表
def __init__(self, pack_path):
super().__init__()
self.pack_path = pack_path
def run(self):
try:
failed_json_files = []
# 检查items文件夹
items_dir = os.path.join(self.pack_path, 'items')
if os.path.isdir(items_dir):
for root, _, files in os.walk(items_dir):
for file in files:
if file.endswith('.json'):
file_path = os.path.join(root, file)
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
orjson.loads(content)
except Exception:
failed_json_files.append(file_path)
# 检查entities文件夹
entities_dir = os.path.join(self.pack_path, 'entities')
if os.path.isdir(entities_dir):
for root, _, files in os.walk(entities_dir):
for file in files:
if file.endswith('.json'):
file_path = os.path.join(root, file)
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
orjson.loads(content)
except Exception:
failed_json_files.append(file_path)
# 发送扫描结果
self.scanning_completed.emit(failed_json_files)
except Exception as e:
import traceback
print(f"Error in JsonScanningThread: {e}\n{traceback.format_exc()}")
self.scanning_completed.emit([])
class AllJsonScanningThread(QThread):
json_files_found = pyqtSignal(list) # 找到所有JSON文件的信号
def __init__(self, pack_path):
super().__init__()
self.pack_path = pack_path
def run(self):
try:
all_json_files = []
# 遍历整个包文件夹查找所有JSON文件
for root, _, files in os.walk(self.pack_path):
for file in files:
if file.endswith('.json'):
file_path = os.path.join(root, file)
all_json_files.append(file_path)
# 发送找到的所有JSON文件
self.json_files_found.emit(all_json_files)
except Exception as e:
import traceback
print(f"Error in AllJsonScanningThread: {e}\n{traceback.format_exc()}")
self.json_files_found.emit([])
class JsonFormattingThread(QThread):
progress_updated = pyqtSignal(int) # 进度更新信号
file_processing = pyqtSignal(str) # 正在处理的文件信号
formatting_completed = pyqtSignal(list) # 格式化完成信号,传递失败文件列表
def __init__(self, pack_path, failed_json_files):
super().__init__()
self.pack_path = pack_path
self.failed_json_files = failed_json_files
self.is_running = True
def run(self):
try:
if not self.failed_json_files:
self.formatting_completed.emit([])
return
total_files = len(self.failed_json_files)
processed_files = 0
failed_files = []
for file_path in self.failed_json_files:
if not self.is_running:
break
# 发送正在处理的文件名
file_name = os.path.basename(file_path)
self.file_processing.emit(file_name)
# 使用format_json_file函数处理文件
success, message = format_json_file(file_path)
if not success:
failed_files.append((file_path, message))
# 更新进度
processed_files += 1
progress = int((processed_files / total_files) * 100) # 0%到100%的范围
self.progress_updated.emit(progress)
# 完成
self.formatting_completed.emit(failed_files)
except Exception as e:
import traceback
print(f"Error in JsonFormattingThread: {e}\n{traceback.format_exc()}")
self.formatting_completed.emit([("Unknown error", str(e))])
def stop(self):
self.is_running = False
class FormatAllJsonThread(QThread):
progress_updated = pyqtSignal(int) # 进度更新信号
file_processing = pyqtSignal(str) # 正在处理的文件信号
formatting_completed = pyqtSignal(list) # 格式化完成信号,传递失败文件列表
def __init__(self, json_files):
super().__init__()
self.json_files = json_files
self.is_running = True
def run(self):
try:
if not self.json_files:
self.formatting_completed.emit([])
return
total_files = len(self.json_files)
processed_files = 0
failed_files = []
for file_path in self.json_files:
if not self.is_running:
break
# 发送正在处理的文件名
file_name = os.path.basename(file_path)
self.file_processing.emit(file_name)
# 先尝试使用orjson解析(效率高)
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
orjson.loads(content)
# orjson解析成功,使用format_json_file规范化
success, message = format_json_file(file_path)
if not success:
failed_files.append((file_path, message))
except Exception:
# orjson解析失败,尝试使用json5库解析(性能较低但更宽松)
success, message = format_json_file(file_path)
if not success:
failed_files.append((file_path, message))
# 更新进度
processed_files += 1
progress = int((processed_files / total_files) * 100) # 0%到100%的范围
self.progress_updated.emit(progress)
# 完成
self.formatting_completed.emit(failed_files)
except Exception as e:
import traceback
print(f"Error in FormatAllJsonThread: {e}\n{traceback.format_exc()}")
self.formatting_completed.emit([("Unknown error", str(e))])
def stop(self):
self.is_running = False