-
Notifications
You must be signed in to change notification settings - Fork 8
/
gen_handler.py
executable file
·200 lines (162 loc) · 4.86 KB
/
gen_handler.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
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
#!/usr/bin/env python
import subprocess
import shutil
import string
import shlex
import sys
import re
import os
import os.path
HANDLER_TPL = """
package handler
import (
"gohive/internal/pb/msg"
)
{handlers}
"""
def gen_handlers(msgs, path):
for p in msgs:
cate = p[0]
items = p[1]
fullpath = os.path.join(path, cate) + ".go"
ctx = ""
for item in items:
msg_name = item[0]
msg_op = item[1]
if msg_op == "Req":
ctx += "func On%sReq(req *msg.%s_Req) *msg.%s_Res {\n" % (msg_name, msg_name, msg_name)
ctx += "res := &msg.%s_Res{}\n" % (msg_name)
ctx += "return res\n"
ctx += "}\n\n"
elif msg_op == "Res":
ctx += "func On%sRes(res *msg.%s_Res) {\n" % (msg_name, msg_name)
ctx += "}\n\n"
#print ctx
#print fullpath
if not os.path.exists(fullpath):
ctx = string.replace(HANDLER_TPL, "{handlers}", ctx)
with open(fullpath, "w") as fp:
fp.write(ctx)
fp.close()
# gofmt
args = shlex.split("gofmt -w %s" % (fullpath))
#subprocess.Popen(args).wait()
subprocess.call(args)
CASE_TPL = """
case "msg.{name}.{op}":
res = handler.On{name}{op}(m.(*msg.{name}_{op}))
break
"""
DISPATCHER_TPL = """
package game
import (
//log "github.com/Sirupsen/logrus"
//"gohive/server/game/entity"
"gohive/internal/pb/msg"
"gohive/server/game/handler"
"github.com/golang/protobuf/proto"
)
func Dispatch(api string, m proto.Message) (res proto.Message) {
switch api {
{case_ctx}
default:
break
}
return
}
"""
def _f(name):
strs = []
for s in name.split("_"):
strs.append(s.capitalize())
return "".join(strs)
def gen_dispatcher(msgs, dst_path):
case_ctx = ""
for p in msgs:
items = p[1]
for item in items:
msg_name = item[0]
msg_op = item[1]
if msg_op == "Req":
curr = string.replace(CASE_TPL, "{name}", msg_name)
case_ctx += string.replace(curr, "{op}", msg_op)
ctx = string.replace(DISPATCHER_TPL, "{case_ctx}", case_ctx)
fullpath = os.path.join(dst_path, "dispatcher") + ".go"
with open(fullpath, "w") as fp:
fp.write(ctx)
fp.close()
# gofmt
args = shlex.split("gofmt -w %s" % (fullpath))
subprocess.call(args)
def get_msgs(src):
proto_files = {}
msg_types = []
for path, dirs, files in os.walk(src, followlinks=True):
for f in files:
name, ext = os.path.splitext(f)
if ext != ".proto":
continue
fullpath = os.path.join(path, f)
protos = proto_files.get(path)
if protos == None:
protos = []
protos.append(fullpath)
proto_files[path] = protos
strs = path.split('/')
if strs[len(strs) - 1] == "msg":
msg_types.append((name, parse_msg(fullpath)))
return msg_types
#gen_msg_dispatcher(msg_types, dispatcher_path)
def usage():
print '%s [src_path] [dispatcher_path]' % (sys.argv[0])
MSG_BEGIN = r"\s*(message|enum)\s+(\w+)\s+\{"
MSG_END = r"\s*\}"
COMMENT_BEGIN = r"\s*\/\*"
COMMENT_END = r".*\*\/.*"
COMMENT = r"(\s*)\/\/.*"
def parse_msg(fullpath):
stacks = []
comment = False
handlers = ""
msgs = []
with open(fullpath, "r") as fp:
lines = fp.readlines()
for l in lines:
if re.compile(COMMENT).match(l) != None:
continue
if re.compile(COMMENT_BEGIN).match(l) != None:
comment = True
if re.compile(COMMENT_END).match(l) != None:
comment = False
continue
if comment:
continue
r = re.compile(MSG_BEGIN)
result = r.match(l)
if result != None:
stacks.append(l)
r = re.compile(MSG_END)
result = r.match(l)
if result != None:
s = stacks.pop()
if len(stacks) > 0:
b = stacks[0]
res1 = re.compile(r"\s*(\w+)\s+(\w+)\s+").match(s)
res2 = re.compile(r"\s*(\w+)\s+(\w+)\s+").match(b)
op = res1.group(2)
name = res2.group(2)
if op != "Req" and op != "Res":
continue
msgs.append((name, op))
fp.close()
return msgs
if __name__ == "__main__":
if len(sys.argv) != 4:
usage()
sys.exit(1)
proto_source = sys.argv[1]
dispatcher_path = sys.argv[2]
handler_path = sys.argv[3]
msgs = get_msgs(proto_source)
gen_dispatcher(msgs, dispatcher_path)
gen_handlers(msgs, handler_path)