-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_model.py
82 lines (76 loc) · 2.56 KB
/
generate_model.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
#
# generate model script
#
import argparse
import tornado.template as template
import os
from medium.config import templates
from medium.powlib import pluralize
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_model(model_name=None, model_type=None, appname=None):
""" generates a small model with the given modelname
also sets the right db and table settings and further boilerplate configuration.
Template engine = tornado.templates
"""
#
# set some attributes
#
print(40*"-")
print(" generating model: " + model_name)
print(40*"-")
try:
loader = template.Loader(templates["stubs_path"])
model_class_name = camel_case(model_name)
print("model_class_name: " + model_class_name)
model_name_plural = pluralize(model_name)
print("model_name_plural: " + model_name_plural)
#
# create the model
#
ofilePath = os.path.join(templates["model_path"], model_type)
ofile = open(os.path.join(ofilePath, model_name+".py"), "wb")
res = loader.load(model_type + "_model_template.py").generate(
model_name=model_name,
model_name_plural=model_name_plural,
model_class_name=model_class_name,
appname=appname,
model_type=model_type
)
ofile.write(res)
ofile.close()
except:
return False
print("... generated: " + model_type + " DB Model")
print(40*"-")
print("... in : " + ofile.name)
print(40*"-")
return True
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-n', "--name", action="store",
dest="name", help='-n modelname',
required=True)
parser.add_argument('-t', "--type", action="store",
dest="type", help='-d dbtype -> Example: -d sql OR -d tinydb ...',
default="sql", required=True)
#
# db type
#
# parser.add_argument('-d', "--db", action="store",
# dest="db", help='-d which_db (mongo || tiny || peewee_sqlite) default = tiny',
# default="tiny", required=True)
args = parser.parse_args()
#
# show some args
#
#print("all args: ", args)
#print(dir(args))
#print("pluralized model name: ", pluralize(args.name))
generate_model(args.name, args.type, appname="pow")
if __name__ == "__main__":
main()