-
Notifications
You must be signed in to change notification settings - Fork 4
/
crud.py
166 lines (133 loc) · 4.17 KB
/
crud.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import base
import fcntl
from ptsd.loader import Loader
from Cheetah.Template import Template
from os import path
if len(sys.argv) != 3:
print "usage: \n\tpython crud.py thrift_file_path output_folder_path"
sys.exit()
thrift_file = sys.argv[1]
out_path = sys.argv[2]
filename = ".".join(path.basename(thrift_file).split(".")[:-1])
if not out_path.endswith(path.sep):
out_path = out_path + path.sep
outDir = out_path.split(path.sep)[-2]
modelsNamespace = ""
def updateController(out_path):
subdirs = [o for o in os.listdir(out_path) if os.path.isdir(os.path.join(out_path, o))]
content = "package controller\n\nimport (\n"
for x in subdirs:
l = "\t_ \"controller/{0}\"\n".format(x)
content = content + l
content = content + ")\n"
f = open(out_path + "gen_init.go", "w")
fcntl.flock(f, fcntl.LOCK_EX)
f.write(content)
fcntl.flock(f, fcntl.LOCK_UN)
f.close()
def getControlDir(urlBase):
outdir = out_path + urlBase.split(path.sep)[-2] + path.sep
if not os.path.exists(outdir):
os.makedirs(outdir)
return outdir
def fieldElem(field, key):
for att in field.annotations:
if att.name.value.lower() == key:
return att.value.value
return ""
def fieldElems(field, key):
key = key.lower()
ret = []
for att in field.annotations:
if att.name.value.lower() == key:
ret.append(att.value.value)
return ret
def transform_tpl(obj, name):
idField = obj.fields[0]
tpl = fieldElem(idField, name)
namespace = tpl
if tpl != "":
name1 = ""
for item in obj.imports:
if isinstance(item, tuple):
if item[1] == namespace:
return item[0]
obj.imports.add((name, namespace))
return name
return None
def transform_tpls(obj):
obj.edittpl = transform_tpl(obj, "edittpl")
obj.viewtpl = transform_tpl(obj, "viewtpl")
obj.indextpl = transform_tpl(obj, "indextpl")
obj.createtpl = transform_tpl(obj, "createtpl")
def check_users_id(urlBase, obj):
for field in obj.fields:
if field.name.value == "UsersID":
return
print(thrift_file + " " + obj.name.value + " don't have field UsersID. urlBase=" + urlBase)
def transform_module(module):
for obj in module.structs:
urlBase = ""
obj.imports = set()
idField = obj.fields[0]
urlBase = fieldElem(idField, "baseurl")
transform_tpls(obj)
tplPackage = fieldElem(idField, "tplpackage")
if tplPackage == "":
tplPackage = "tpl/auto"
if urlBase == "":
continue
check_users_id(urlBase, obj)
if obj.label != obj.name.value:
obj.perm = obj.label
if hasattr(idField, "perm"):
obj.perm = idField.perm
obj.imports.add("admin/permission")
if obj.has_docnumber:
obj.imports.add("zfw/docnumber")
obj.hasUser = len([i for i in obj.fields if str(i.name) == "UsersID"]) > 0
if len(obj.relateObj) > 0:
obj.imports.add("encoding/json")
outDir = urlBase.split(path.sep)[-2]
crud = open('tmpl/crud.tmpl', 'r').read().decode("utf8")
res = Template(crud, searchList=[{"namespace": outDir,
"className": obj.name.value,
"urlBase": urlBase,
"tplPackage": tplPackage,
"modelsNamespace": modelsNamespace,
"obj": obj,
}])
writeDir = getControlDir(urlBase)
outfile = writeDir + "gen_" + obj.name.value.lower() + ".go"
with open(outfile, "w") as fp:
fp.write(str(res))
else:
for field in obj.fields:
if field.widget_type in ("relateSelect", "relateAjaxSelect"):
outDir = urlBase.split(path.sep)[-2]
tmpl = file("tmpl/relateOnly.tmpl").read().decode("u8")
ret = Template(tmpl, searchList=[{"namespace": outDir,
"className": obj.label,
"urlBase": urlBase,
"tplPackage": tplPackage,
"modelsNamespace": field.bindModels,
"obj": obj,
}])
writeDir = getControlDir(urlBase)
outfile = writeDir + "gen_relate_" + obj.name.value.lower() + ".go"
with open(outfile, "w") as fp:
fp.write(str(ret).strip())
break
def main(thrift_idl):
loader = base.load_thrift(thrift_idl)
global namespace, modelsNamespace
namespace = loader.namespace
modelsNamespace = namespace
for obj in loader.modules.values():
transform_module(obj)
updateController(out_path)
main(thrift_file)