-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_handler.py
94 lines (82 loc) · 2.74 KB
/
generate_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
#
# generate model
#
import argparse
import tornado.template as template
import os.path
import timeit
import medium.powlib as lib
import medium.config as cfg
def camel_case(name):
"""
converts this_is_new to ThisIsNew
and this in This
"""
return "".join([x.capitalize() for x in name.split("_")])
def generate_handler(handler_name, model_type, rest, appname=None):
"""
generates a small handler
"""
#
# set some attributes
#
loader = template.Loader(cfg.templates["stubs_path"])
handler_class_name = camel_case(handler_name)
print(40*"-")
print(" generating handler: " + handler_class_name)
print(40*"-")
#
# create the handler
#
if rest:
# this is going to be a rest handler. Full rest actions and routing.
print("... REST Handler")
if model_type.lower() == "none":
template_file = "rest_handler_nodb_template.py"
else:
template_file = "rest_handler_template.py"
else:
print("... SIMPLE Handler")
# this will generate a simple handler with only two example routes and actions.
if model_type.lower() == "none":
template_file = "simple_handler_nodb_template.py"
else:
template_file = "simple_handler_template.py"
ofile_name = os.path.join(cfg.templates["handler_path"], handler_name+".py")
ofile = open(ofile_name, "wb")
res = loader.load(template_file).generate(
handler_name=handler_name,
handler_class_name=handler_class_name,
handler_model_class_name=handler_class_name,
model_type = model_type,
appname=appname,
)
ofile.write(res)
ofile.close()
print("... created: " + ofile_name)
print(40*"-")
return
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-n', "--name", action="store",
dest="name", help='-n handler name',
required=True)
#
# db type
#
parser.add_argument('-t', "--type", action="store",
dest="type", help="-t type (" + "|| ".join(cfg.database.keys()) + " || none) default=none",
default="none", required=False)
parser.add_argument('-r', "--rest", action="store_true",
dest="rest", help="-r | --rest to generate a handler with full rest routes and actions. ",
default=False, required=False)
args = parser.parse_args()
#
# show some args
#
#print("all args: ", args)
#print(dir(args))
print("CamelCased handler name: ", camel_case(args.name))
generate_handler(args.name, args.type, args.rest, appname="pow")
if __name__ == "__main__":
main()