Skip to content

Commit 3338a55

Browse files
committed
获取所有字段层级,类型和名称用于后续处理
1 parent fbd850b commit 3338a55

File tree

5 files changed

+250
-21
lines changed

5 files changed

+250
-21
lines changed

code_construction_var.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env python
2+
# -*- coding:utf-8 -*-
3+
# @Filename : code_construction_var.py
4+
# @Date : 18-1-30 下午5:03
5+
# @Author : DebuggerX
6+
7+
8+
def get_code_construction_var():
9+
pass

code_top.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env python
2+
# -*- coding:utf-8 -*-
3+
# @Filename : code_top.py
4+
# @Date : 18-1-30 下午2:37
5+
# @Author : DebuggerX
6+
7+
8+
template_dict = r"""
9+
import 'dart:convert';
10+
11+
class ${type} {
12+
${properties}
13+
14+
${type}(jsonStr) {
15+
var json = JSON.decode(jsonStr);
16+
${construction}
17+
}
18+
19+
@override
20+
String toString() {
21+
return '{${toString}}';
22+
}
23+
}
24+
"""
25+
26+
template_list = r"""
27+
import 'dart:convert';
28+
29+
class ${name} {
30+
${type} list;
31+
32+
${type}(jsonStr) {
33+
var json = JSON.decode(jsonStr);
34+
list = new List();
35+
for (var item in json) {
36+
list.add(item);
37+
}
38+
}
39+
40+
@override
41+
String toString() {
42+
return '{"list": $list}';
43+
}
44+
}
45+
"""
46+
47+
48+
def get_top_code_dict(t):
49+
return template_dict.replace('${type}', t)
50+
51+
52+
def get_top_code_list(t, n):
53+
res = template_list.replace('${type}', t).replace('${name}', n)
54+
55+
if t[t.find('<') + 1: t.rfind('>')] not in ['int', 'double', 'bool', 'String']:
56+
res.replace('list.add(item);', )
57+
58+
59+
return res

formater.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,16 @@
66
from functools import partial
77

88
from mainwindow import *
9-
import json
109

1110
# 定义显示的json格式化字符串的缩进量为4个空格
11+
from tools import *
12+
1213
indent = ' '
1314

1415
# 临时存储
1516
last_list_com_box = None
1617

1718

18-
# 验证json字符串是否合法
19-
def is_json(myjson):
20-
try:
21-
json.loads(myjson)
22-
except ValueError:
23-
return False
24-
return True
25-
26-
27-
# 传入未格式化的单行json字符串,返回指定缩进的多行json字符串
28-
def jformat(inp):
29-
obj = json.loads(inp)
30-
outp = json.dumps(obj, skipkeys=False, ensure_ascii=False, check_circular=True, allow_nan=True, cls=None, indent=' ', separators=None,
31-
default=None, sort_keys=True)
32-
return outp
33-
34-
3519
def analyse_json_obj(json_obj, level=0, res=None, json_key=None):
3620
if res is None:
3721
res = []
@@ -65,7 +49,7 @@ def analyse_json_obj(json_obj, level=0, res=None, json_key=None):
6549
if json_key is None:
6650
res.append('%s<[list]>9' % (indent * level))
6751
else:
68-
res.append('%s <%s> : <[list]>9' % (indent * level, json_key))
52+
res.append('%s<%s> : <[list]>9' % (indent * level, json_key))
6953
if len(json_obj) > 0:
7054
analyse_json_obj(json_obj[0], level + 1, res)
7155

@@ -96,6 +80,7 @@ def get_type_combobox(line):
9680
com_box = QtWidgets.QComboBox()
9781
com_box.setEditable(True)
9882
obj_type = int(line[-1]) if line[-1].isdigit() else 0
83+
global last_list_com_box
9984

10085
if last_list_com_box is not None:
10186
com_box.currentTextChanged.connect(partial(change_text, last_list_com_box))
@@ -115,7 +100,6 @@ def get_type_combobox(line):
115100
elif obj_type == 9:
116101
com_box.setCurrentText('List<>')
117102
# 将该list字段的编辑框临时保存,用于与下一个字段的类型绑定
118-
global last_list_com_box
119103
last_list_com_box = com_box
120104

121105
return com_box
@@ -177,7 +161,17 @@ def json_format():
177161

178162

179163
def generate_bean():
180-
ui.te_json.setText("OK")
164+
bean = []
165+
for i in range(ui.tv_fields.rowCount()):
166+
var_field = ui.tv_fields.cellWidget(i, 0)
167+
var_type = ui.tv_fields.cellWidget(i, 1)
168+
var_name = ui.tv_fields.cellWidget(i, 2)
169+
170+
bean.append([var_field, var_type, var_name])
171+
172+
res = check_and_generate_code(bean)
173+
if res != '':
174+
ui.te_json.setText(res)
181175

182176

183177
def init_event():

template.dart

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import 'dart:convert';
2+
3+
class ${className} {
4+
$loop$
5+
${variableType} ${variableName}
6+
$loop$
7+
8+
9+
${className}(jsonStr) {
10+
var json = JSON.decode(jsonStr);
11+
$loop
12+
${variableName} json['${variableName}'];
13+
$loop
14+
}
15+
16+
@override
17+
String toString() {
18+
return '{"code": $code, "msg": ${msg != null?'"$msg"':'null'}, "rs": $rs}';
19+
}
20+
21+
}
22+
23+
class ActivityPage {
24+
int totalRow;
25+
int pageNumber;
26+
bool firstPage;
27+
bool lastPage;
28+
int totalPage;
29+
int pageSize;
30+
List<Activity> list;
31+
32+
ActivityPage(jsonRes) {
33+
totalRow = jsonRes['totalRow'];
34+
pageNumber = jsonRes['pageNumber'];
35+
firstPage = jsonRes['firstPage'];
36+
lastPage = jsonRes['lastPage'];
37+
totalPage = jsonRes['totalPage'];
38+
pageSize = jsonRes['pageSize'];
39+
list = new List();
40+
for (var activity in jsonRes['list']) {
41+
list.add(new Activity(activity));
42+
}
43+
}
44+
45+
@override
46+
String toString() {
47+
return '{"totalRow": $totalRow, "pageNumber": $pageNumber, "firstPage": $firstPage, "lastPage": $lastPage, "totalPage": $totalPage, "pageSize": $pageSize, "list": $list}';
48+
}
49+
50+
}
51+
52+
class Activity {
53+
int comment_num;
54+
String preface;
55+
String show_time;
56+
String create_time;
57+
String cover_pic;
58+
String end_time;
59+
String title;
60+
int type;
61+
int is_show;
62+
String recomm_time;
63+
int hasVote;
64+
int is_del;
65+
int id;
66+
int is_recomm;
67+
int approve_num;
68+
int is_approve;
69+
70+
Activity(jsonRes) {
71+
comment_num = jsonRes['comment_num'];
72+
preface = jsonRes['preface'];
73+
show_time = jsonRes['show_time'];
74+
create_time = jsonRes['create_time'];
75+
cover_pic = jsonRes['cover_pic'];
76+
end_time = jsonRes['end_time'];
77+
title = jsonRes['title'];
78+
type = jsonRes['type'];
79+
is_show = jsonRes['is_show'];
80+
recomm_time = jsonRes['recomm_time'];
81+
hasVote = jsonRes['hasVote'];
82+
is_del = jsonRes['is_del'];
83+
id = jsonRes['id'];
84+
is_recomm = jsonRes['is_recomm'];
85+
approve_num = jsonRes['approve_num'];
86+
is_approve = jsonRes['is_approve'];
87+
}
88+
89+
@override
90+
String toString() {
91+
92+
return '{"comment_num": $comment_num, "preface": ${preface != null?'"$preface"':'null'}, "show_time": ${show_time != null?'"$show_time"':'null'}, "create_time": ${create_time != null?'"$create_time"':'null'}, "cover_pic": ${cover_pic != null?'"$cover_pic"':'null'}, "end_time": ${end_time != null?'"$end_time"':'null'}, "title": ${title != null?'"$title"':'null'}, "type": $type, "is_show": $is_show, "recomm_time": ${recomm_time != null?'"$recomm_time"':'null'}, "hasVote": $hasVote, "is_del": $is_del, "id": $id, "is_recomm": $is_recomm, "approve_num": $approve_num, "is_approve": $is_approve}';
93+
}
94+
95+
}

tools.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env python
2+
# -*- coding:utf-8 -*-
3+
# @Filename : tools.py
4+
# @Date : 18-1-30 上午11:48
5+
# @Author : DebuggerX
6+
7+
import json
8+
from PyQt5.QtWidgets import QLabel, QComboBox, QMessageBox
9+
10+
# 验证json字符串是否合法
11+
from code_top import get_top_code_list
12+
13+
from code_top import get_top_code_dict
14+
15+
16+
def is_json(myjson):
17+
try:
18+
json.loads(myjson)
19+
except ValueError:
20+
return False
21+
return True
22+
23+
24+
# 传入未格式化的单行json字符串,返回指定缩进的多行json字符串
25+
def jformat(inp):
26+
obj = json.loads(inp)
27+
outp = json.dumps(obj, skipkeys=False, ensure_ascii=False, check_circular=True, allow_nan=True, cls=None, indent=' ', separators=None,
28+
default=None, sort_keys=True)
29+
return outp
30+
31+
32+
def generate_code(work_bean):
33+
level_bean = None
34+
for (l, f, t, n) in work_bean:
35+
if l == -1:
36+
if t.startswith('List<'):
37+
level_bean = []
38+
else:
39+
level_bean = {}
40+
41+
level_bean
42+
# todo 怎么搞。。。
43+
44+
45+
# l, f, t, n = work_bean.pop(0)
46+
#
47+
# if l == -1:
48+
# if t.startswith('List<'):
49+
# top = get_top_code_list(t, n)
50+
# else:
51+
# top = get_top_code_dict(t)
52+
# print(top)
53+
54+
55+
def check_and_generate_code(bean):
56+
work_bean = []
57+
58+
for f, t, n in bean:
59+
field_text = f.text()
60+
61+
level = field_text.find('※') // 4
62+
field_text = field_text[field_text.find("》") + 1: field_text.find(":")] if ":" in field_text else ''
63+
type_text = t.currentText() if type(t) is QComboBox else t.toPlainText()
64+
name_text = n.text() if type(n) is QLabel else n.toPlainText()
65+
66+
if type_text.strip() != '':
67+
work_bean.append([level, field_text, type_text, name_text])
68+
else:
69+
QMessageBox().information(f, "警告", "字段类型未设置", QMessageBox.Ok)
70+
return ''
71+
72+
return generate_code(work_bean)

0 commit comments

Comments
 (0)