Skip to content

Commit 684318d

Browse files
committed
稍微修改模板代码位置;修正顶层结构为List时解析的错误取值
1 parent 8781c61 commit 684318d

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

formater.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
from mainwindow import *
99

10-
# 定义显示的json格式化字符串的缩进量为4个空格
1110
from tools import *
1211

12+
# 定义显示的json格式化字符串的缩进量为4个空格
1313
indent = ' '
1414

1515
# 临时存储
@@ -211,7 +211,7 @@ def init_view():
211211
init_table()
212212

213213

214-
def customUI():
214+
def custom_ui():
215215
init_view()
216216
init_event()
217217

@@ -224,6 +224,6 @@ def customUI():
224224
ui = Ui_MainWindow()
225225
ui.setupUi(widget)
226226
# 在生成代码的基础上再修改UI以及添加逻辑
227-
customUI()
227+
custom_ui()
228228
widget.show()
229229
sys.exit(app.exec_())

template_code.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,26 @@ class ${type} {
2323
2424
"""
2525

26+
template_list = r"""
27+
${list_count}${class_type}${>count} ${name}${current_child} = [];
28+
for (var ${name}${current_items} in ${name}${parent_items}){
29+
${loop}
30+
${name}${current_child}.add(${name}${child_child});
31+
}
32+
"""
33+
2634

2735
def get_top_code_dict(t):
2836
return template_dict.replace('${type}', t)
37+
38+
39+
def get_list_code_loop(list_count, ct, ab_count, n, current_child, child_child, current_items, parent_items):
40+
return template_list \
41+
.replace('${list_count}', list_count) \
42+
.replace('${class_type}', ct) \
43+
.replace('${>count}', ab_count) \
44+
.replace('${name}', n) \
45+
.replace('${current_child}', current_child) \
46+
.replace('${child_child}', child_child) \
47+
.replace('${current_items}', current_items) \
48+
.replace('${parent_items}', parent_items)

tools.py

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from PyQt5.QtWidgets import QLabel, QComboBox, QMessageBox
99

1010
# 验证json字符串是否合法
11-
from template_code import get_top_code_dict
11+
from template_code import get_top_code_dict, get_list_code_loop
1212

1313
msg_box_ui = None
1414

@@ -44,14 +44,6 @@ def check_level_type(t):
4444

4545

4646
def list_code_loop(code, count, total, n, ct):
47-
template = r"""
48-
${list_count}${class_type}${>count} ${name}${current_child} = [];
49-
for (var ${name}${current_items} in ${name}${parent_items}){
50-
${loop}
51-
${name}${current_child}.add(${name}${child_child});
52-
}
53-
"""
54-
5547
list_count = 'List<' * (total - count)
5648
ab_count = '>' * (total - count)
5749

@@ -61,15 +53,7 @@ def list_code_loop(code, count, total, n, ct):
6153
current_items = 'Item' * (count + 1)
6254
parent_items = 'Item' * count
6355

64-
fragment = template \
65-
.replace('${list_count}', list_count) \
66-
.replace('${class_type}', ct) \
67-
.replace('${>count}', ab_count) \
68-
.replace('${name}', n) \
69-
.replace('${current_child}', current_child) \
70-
.replace('${child_child}', child_child) \
71-
.replace('${current_items}', current_items) \
72-
.replace('${parent_items}', parent_items)
56+
fragment = get_list_code_loop(list_count, ct, ab_count, n, current_child, child_child, current_items, parent_items)
7357

7458
if code == '':
7559
return fragment
@@ -175,12 +159,15 @@ def build_level_code(level_bean):
175159

176160

177161
def generate_code(work_bean):
178-
# 如果顶级容器为list而不是dict,则先在外面包一层dict,并将list的别名传递为dict的类型,list则重命名为'list',
162+
is_list_top = False
163+
164+
# 如果顶级容器为list而不是dict,则先在外面包一层dict,并将list的别名传递为dict的类型,list则重命名为'list',并修改标志位供后面修改使用
179165
(l, f, t, n) = work_bean[0]
180166
if t.startswith('List<'):
181167
work_bean[0][3] = 'list'
182168
work_bean[0][1] = 'json_list'
183169
work_bean.insert(0, (-2, "", n, ""))
170+
is_list_top = True
184171

185172
build_level_code(work_bean)
186173

@@ -192,13 +179,15 @@ def generate_code(work_bean):
192179

193180
codes.clear()
194181

195-
# 最终修改,添加jsonStr解析为jsonRes代码
182+
# 如果顶级容器为list,需要修改第一次获取JSON对象的方式为直接获取
183+
if is_list_top:
184+
res = res.replace('jsonRes[\'list\']', 'jsonRes', 1)
196185

186+
# 最终修改,添加jsonStr解析为jsonRes代码
197187
bp = res.find('(jsonRes) {')
198188
return 'import \'dart:convert\';\n' + res[:bp] + '(jsonStr) {\n var jsonRes = JSON.decode(jsonStr);\n' + res[bp + 11:]
199189

200190

201-
202191
def check_and_generate_code(bean):
203192
work_bean = []
204193
global msg_box_ui

0 commit comments

Comments
 (0)