-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_property.py
More file actions
215 lines (160 loc) · 6.15 KB
/
auto_property.py
File metadata and controls
215 lines (160 loc) · 6.15 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
import os
import re
import argparse
def code_create(path, is_swagger, is_wiki):
if is_swagger is True:
code_create_swagger(path)
elif is_wiki is True:
code_create_wiki(path)
else:
code_create_json_dict(path)
def code_create_swagger(path):
# 获取文件数据
data = open(path, "r", encoding="utf-8", errors="ignore")
# 读取第一行
each = data.readline()
# 属性列表
my_property_name_list = list()
my_property_annotation_list = list()
# 存储文件原内容
whole_string = ''
while each:
whole_string += each
if each.find("@interface") != -1:
each = data.readline()
continue
# (ex:hit (integer, optional): 0 未命中,1 命中) 类型默认为 NSString *
if each.find(":") != -1:
left = each.split(":")[0]
right = each.split(":")[1]
result = re.search(r"\b[a-zA-Z_0-9]+\b", left)
if result != None:
name = result.group(0)
name = name.strip()
my_property_name_list.append(name)
my_property_annotation_list.append("///" + right)
each = data.readline()
continue
# 都没匹配上则下一行继续
each = data.readline()
continue
# 开始拼接
property_string = ''
for i in range(len(my_property_name_list)):
each_property = my_property_name_list[i]
each_property = "@property (nonatomic, copy) NSString *%s;\n" % (each_property)
each_annotation = my_property_annotation_list[i]
property_string += each_annotation
property_string += each_property
property_string += "\n"
whole_string = whole_string.replace("@end", property_string)
whole_string += "\n@end"
with open(path, 'w', encoding='utf-8', errors='ignore') as f:
f.write(whole_string)
def code_create_wiki(path):
# 获取文件数据
data = open(path, "r", encoding="utf-8", errors="ignore")
# 读取第一行
each = data.readline()
# 属性列表
my_property_name_list = list()
my_property_annotation_list = list()
# 存储文件原内容
whole_string = ''
while each:
whole_string += each
# (each example: otherRank 其它排名 array) 类型默认为 NSString *
each = each.strip()
res = re.match(r"^[a-zA-Z].*?", each)
if res != None and each.find(" ") != -1:
name = each.split(" ")[0].strip()
right = each.split(" ")[1].strip()
my_property_name_list.append(name)
my_property_annotation_list.append("///" + right)
each = data.readline()
continue
# 都没匹配上则下一行继续
each = data.readline()
continue
# 开始拼接
property_string = ''
for i in range(len(my_property_name_list)):
each_property = my_property_name_list[i]
each_property = "@property (nonatomic, copy) NSString *%s;\n" % (each_property)
each_annotation = my_property_annotation_list[i]
property_string += each_annotation
property_string += "\n"
property_string += each_property
property_string += "\n"
whole_string = whole_string.replace("@end", property_string)
whole_string += "@end"
with open(path, 'w', encoding='utf-8', errors='ignore') as f:
f.write(whole_string)
def code_create_json_dict(path):
# 获取文件数据
data = open(path, "r", encoding="utf-8", errors="ignore")
# 读取第一行
each = data.readline()
# 属性列表
my_property_name_list = list()
# 存储文件原内容
whole_string = ''
while each:
whole_string += each
if each.find("import") != -1:
each = data.readline()
continue
# 这个if针对dict格式 (ex:matchTime = 1542713700000) 类型默认为 NSString *
if each.find("=") != -1:
name = each.split("=")[0]
name = name.strip()
my_property_name_list.append(name)
each = data.readline()
continue
# 这个if针对json格式 (ex: "teamName": "利物浦")
match = re.findall('"(.*?)"', each)
if len(match) > 0:
name = match[0]
my_property_name_list.append(name)
each = data.readline()
continue
# 都没匹配上则下一行继续
each = data.readline()
continue
# 开始拼接
property_string = ''
for i in range(len(my_property_name_list)):
each_property = my_property_name_list[i]
auto_property = "///\n@property (nonatomic, copy) NSString *%s;\n" % (each_property)
property_string += auto_property
property_string += "\n"
whole_string = whole_string.replace("@end", property_string)
whole_string += "\n@end"
with open(path, 'w', encoding='utf-8', errors='ignore') as f:
f.write(whole_string)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
# 路径参数
parser.add_argument("-path", "--path", help=".h文件的全路径")
# 选项参数
group = parser.add_mutually_exclusive_group()
group.add_argument("-swagger", "--swagger", help="如果数据来源于swagger,使用该选项", action="store_true")
group.add_argument("-jsonDict", "--jsonDict", help="如果数据来源于Xcode打印的json数据或则json装换的字典数据,使用该选项", action="store_true")
group.add_argument("-wiki", "--wiki", help="如果数据来源于wiki,使用该选项", action="store_true")
# 解析参数
args = parser.parse_args()
# 取值
swagger = args.swagger
jsonDict = args.jsonDict
wiki = args.wiki
h_path = args.path
# 选项不能为空
if swagger is False and jsonDict is False and wiki is False:
print("必须选择数据来源选项, -swagger 或者 -jsonDict 或者 -wiki")
exit(-1)
# 文件不能为空
if h_path:
code_create(h_path, swagger, wiki)
print("----Done----")
else:
print(".h路径为空, 必须使用 -path 后面带上.h文件全路径")