-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.py
117 lines (97 loc) · 3.25 KB
/
gen.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
from sys import argv
import model
def get_objects(input):
module = __import__(input.split('.')[0])
data = dir(module)
end = data.index('__builtins__')
objs = [getattr(module, obj) for obj in data[:end]]
return objs
def get_attributes(inst):
start = dir(inst).index('__weakref__')
attrs = dir(inst)[start+1:]
return attrs
def gen_form(objs):
data_frase = '''
<label for="VAR_NAME">SHOW_NAME:</label>
<input type="text" id="VAR_NAME" name="VAR_NAME"><br><br>'''
data_texto = '''
<label for="VAR_NAME">SHOW_NAME:</label><br>
<textarea id="VAR_NAME" name="VAR_NAME" rows="4" cols="50"></textarea><br><br>'''
data_data = '''
<label for="VAR_NAME">SHOW_NAME:</label>
<input type="date" id="VAR_NAME" name="VAR_NAME"><br><br>'''
data_html = {
model.Frase: data_frase,
model.Texto: data_texto,
model.Data: data_data,
}
for inst in objs:
content = '<form method="POST" action="/processar_formulario">'
for attr in get_attributes(inst()):
obj = getattr(inst, attr)
field = data_html[type(obj)]
field = field.replace('VAR_NAME', attr)
if obj.verboso:
field = field.replace('SHOW_NAME', obj.verboso.title())
else:
label = attr.replace('_', ' ').title()
field = field.replace('SHOW_NAME', label)
content += field
content += '\n\t<input type="submit" value="Enviar">\n</form>'
for obj in objs:
with open(f'{obj.__name__.lower()}.html', 'w') as form_arq:
form_arq.write(content)
def create_conn_db(objs):
data_sql = {
model.Frase: 'VARCHAR(255)',
model.Texto: 'TEXT',
model.Data: 'DATE',
}
content = ''
for inst in objs:
content += f'''
from flask import Flask, render_template, request
import sqlite3
app = Flask(__name__, template_folder='./')
@app.route('/')
def formulario():
return render_template('{inst.__name__.lower()}.html')
@app.route('/processar_formulario', methods=['POST'])
def processar_formulario():
conn = sqlite3.connect('formularios.db')\n
'''
for attr in get_attributes(inst()):
obj = getattr(inst, attr)
content += f'''
{attr} = request.form['{attr}']'''
content += f'''
conn.execute(\'''CREATE TABLE IF NOT EXISTS {inst.__name__.lower()}
(id INTEGER PRIMARY KEY AUTOINCREMENT,
'''
cols = list()
for item in get_attributes(inst()):
cols.append(item)
key = type(getattr(inst, item))
content += f'''
{item} {data_sql[key]},'''
content = content[:-1] + ");\'\'\')"
cols = ', '.join(cols)
content += f'''
conn.execute("INSERT INTO {inst.__name__.lower()} ({cols}) VALUES (?, ?, ?)",
({cols}))
conn.commit()
conn.close()
return 'Dados salvos com sucesso!'
if __name__ == '__main__':
app.run(debug=True)
'''
for obj in objs:
with open(f'{obj.__name__.lower()}.py', 'w') as app_arq:
app_arq.write(content)
def main():
input = argv[1]
objs = get_objects(input)
gen_form(objs)
create_conn_db(objs)
if __name__ == '__main__':
main()