forked from janusnic/flask-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scaffold.py
413 lines (356 loc) · 19.7 KB
/
scaffold.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/usr/bin/env python
import os
import shutil
import sys
import subprocess
import json
import yaml
import inflect
from scaffold.custom_fields import *
from scaffold.modules.replace_string import replace_string, \
new_route_string, menu_string, js_src_string, test_script_string, conf_js_string
from scaffold.modules.errors import BlueprintError
blueprint_file = 'app/__init__.py'
test_script = 'tests.bash'
yaml_file = sys.argv[1]
app_js_file = "app/templates/static/js/app.js"
main_index_file = "app/templates/index.html"
conf_js_file = "conf.js"
# Error classes
def make_plural(resource):
# https://pypi.python.org/pypi/inflect
p = inflect.engine()
if p.singular_noun(resource):
resources = resource
resource = p.singular_noun(resource)
return resource, resources
else:
resources = p.plural(resource)
return resource, resources
def generate_files(module_path, angular_dir):
app_files = ['views.py', 'models.py', '__init__.py', '_form.html', 'add.html', 'update.html',
'index.html', 'controllers.js', 'tests.py', 'conf.js', 'spec.js']
for file in app_files:
# Generate App files
if file == "views.py":
with open(os.path.join(module_path, 'views.py'), "w") as new_file:
with open("scaffold/app/views.py", "r") as old_file:
for line in old_file:
new_file.write(line.format(resource=resource,
resources=resources,
Resources=resources.title(),
Resource=resource.title(),
add_fields=add_fields))
elif file == "models.py":
with open(os.path.join(module_path, 'models.py'), "w") as new_file:
with open("scaffold/app/models.py", "r") as old_file:
for line in old_file:
new_file.write(line.format(resource=resource, resources=resources,
Resources=resources.title(),
db_rows=db_rows,
schema=schema, meta=meta,
init_self_vars=init_self_vars,
init_args=init_args))
elif file == "__init__.py":
with open(os.path.join(module_path, '__init__.py'), "w") as new_file:
with open("scaffold/app/__init__.py", "r") as old_file:
for line in old_file:
new_file.write(line)
# Tests
elif file == "tests.py":
with open(os.path.join(module_path, 'test_{}.py'.format(resources)), "w") as new_file:
with open("scaffold/app/tests.py", "r") as old_file:
for line in old_file:
new_file.write(line.format(resource=resource, resources=resources,
Resources=resources.title(),
test_add_fields=json.dumps(
test_add_fields),
test_update_fields=json.dumps(
test_update_fields)))
# Generate Template Files
# Need to add template resource path
elif file == "_form.html":
with open(os.path.join(angular_dir, '_form.html'), "w") as new_file:
with open("scaffold/app/_form.html", "r") as old_file:
for line in old_file:
new_file.write(line.format(resource=resource,
resources=resources,
Resources=resources.title(),
Resource=resource.title(),
form_args=','.join(
form_args),
form_fields=form_fields))
elif file == "add.html":
with open(os.path.join(angular_dir, 'add.html'), "w") as new_file:
with open("scaffold/app/add.html", "r") as old_file:
for line in old_file:
new_file.write(line.format(resource=resource, resources=resources,
Resource=resource.title()))
elif file == "update.html":
with open(os.path.join(angular_dir, 'update.html'), "w") as new_file:
with open("scaffold/app/update.html", "r") as old_file:
for line in old_file:
new_file.write(line.format(resource=resource, resources=resources,
Resource=resource.title()))
elif file == "index.html":
with open(os.path.join(angular_dir, 'index.html'), "w") as new_file:
with open("scaffold/app/index.html", "r") as old_file:
for line in old_file:
new_file.write(line.format(resource=resource, resources=resources,
Resource=resource.title()))
elif file == "controllers.js":
with open(os.path.join(angular_dir, 'controllers.js'), "w") as new_file:
with open("scaffold/app/controller.js", "r") as old_file:
for line in old_file:
new_file.write(line.format(resource=resource, resources=resources,
Resource=resource.title(), controller_fields=controller_fields
) )
# Protractor
elif file == "conf.js":
with open(os.path.join(angular_dir, 'conf.js'), "w") as new_file:
with open("scaffold/app/protractor-conf.js", "r") as old_file:
for line in old_file:
new_file.write(line.format(resource=resource))
elif file == "spec.js":
with open(os.path.join(angular_dir, 'spec.js'), "w") as new_file:
with open("scaffold/app/protractor-spec.js", "r") as old_file:
for line in old_file:
new_file.write(line.format(resource=resource, resources=resources,
Resource=resource.title(), Resources=resources.title(),
protractor_page_objects=protractor_page_objects,
protractor_edit_elments=protractor_edit_elments,
protractor_add_elments=protractor_add_elments
))
def register_blueprints():
string_to_insert_after = '# Blueprints'
new_blueprint = """
# Blueprints
from app.{resources}.views import {resources}
app.register_blueprint({resources}, url_prefix='/api/v1/{resources}')""".format(resources=resources)
with open(blueprint_file, 'r+') as old_file:
filedata = old_file.read()
if string_to_insert_after in filedata:
# replace the first occurrence
new_filedata = filedata.replace(
string_to_insert_after, new_blueprint, 1)
with open(blueprint_file, 'w') as new_file:
new_file.write(new_filedata)
print("Registered Blueprints for ", resources)
else:
raise BlueprintError()
def clean_up(module_path):
if os.path.isdir(module_path):
shutil.rmtree(module_path)
if os.path.isdir(angular_dir):
shutil.rmtree(angular_dir)
def run_autopep8():
try:
cmd_output = subprocess.check_output(
['autopep8', '--in-place', '--recursive', 'app'])
print("Ran autopep8")
except subprocess.CalledProcessError:
print("autopep8 failed")
raise
# Main Code Start
#
# Parse YAML file
with open(yaml_file, "r") as file:
yaml_data = yaml.load(file)
for module, fields in yaml_data.items():
# make module name plural
resource, resources = make_plural(module)
# Start strings to insert into models
db_rows = ""
schema = ""
meta = ""
init_self_vars = ""
init_args = ""
# End strings to insert into models
# Start strings to insert into views
add_fields = ""
# strings to insert into _form.html
form_args = []
form_fields = ""
# strings to insert into update.html
update_form_args = ""
# strings to insert into index.html
field_table_headers = ""
index_fields = ""
# strings to insert into tests.py
test_add_fields = {}
test_update_fields = {}
# Fields to insert into controller.js
controller_fields = ""
radio_button_default =""
# Fields to add to protractor spec.js
protractor_page_objects = ""
protractor_edit_elments = ""
protractor_add_elments = ""
for f in fields:
field, field_type = f.split(':')
if field_type == "string":
db_rows += """
{} = db.Column(db.String(250), nullable=False)""".format(field)
schema += """
{} = fields.String(validate=not_blank)""".format(field)
test_add_fields[field] = string_test
test_update_fields[field] = update_string_test
protractor_page_objects += pro_po_string.format(field=field, Field=field.title())
protractor_edit_elments += update_pro_string.format(field=field, resource=resource, Field=field.title())
protractor_add_elments += pro_string.format(field=field, resource=resource, Field=field.title())
form_fields += form_field.format(field=field, Field=field.title(
), field_type="text", resource=resource, Resource=resource.title())
elif field_type == "boolean":
db_rows += """
{} = db.Column(db.Boolean, nullable=False)""".format(field)
schema += """
{} = fields.Boolean(required=True)""".format(field)
form_fields += boolean_form_string.format(Field=field.title(),
field=field, resource=resource, field_type=field_type)
test_add_fields[field] = boolean_test
test_update_fields[field] = update_boolean_test
protractor_edit_elments += update_pro_boolean.format(field=field)
protractor_add_elments += pro_boolean.format(field=field)
radio_button_default += radio_button_string.format(resource=resource, field=field)
elif field_type == "integer":
db_rows += """
{} = db.Column(db.Integer, nullable=False)""".format(field)
schema += """
{} = fields.Integer(required=True)""".format(field)
form_fields += form_field.format(field=field, Field=field.title(
), field_type="number", resource=resource, Resource=resource.title())
test_add_fields[field] = integer_test
test_update_fields[field] = update_integer_test
protractor_page_objects += pro_po_string.format(field=field, Field=field.title())
protractor_edit_elments += update_pro_int.format(field=field, resource=resource, Field=field.title())
protractor_add_elments += pro_int.format(field=field, resource=resource, Field=field.title())
elif field_type == "biginteger":
db_rows += """
{} = db.Column(db.BigInteger, nullable=False)""".format(field)
schema += """
{} = fields.Integer(required=True)""".format(field)
form_fields += form_field.format(field=field, Field=field.title(
), field_type="number", resource=resource, Resource=resource.title())
test_add_fields[field] = big_integer_test
test_update_fields[field] = update_big_integer_test
protractor_page_objects += pro_po_string.format(field=field, Field=field.title())
protractor_edit_elments += update_pro_big_int.format(field=field, resource=resource, Field=field.title())
protractor_add_elments += pro_big_int.format(field=field, resource=resource, Field=field.title())
elif field_type == "email":
db_rows += """
{} = db.Column(db.String(250), nullable=False)""".format(field)
schema += """
{} = fields.Email(validate=not_blank)""".format(field)
form_fields += form_field.format(field=field, Field=field.title(
), field_type=field_type, resource=resource, Resource=resource.title())
test_add_fields[field] = email_test
test_update_fields[field] = update_email_test
protractor_page_objects += pro_po_string.format(field=field, Field=field.title())
protractor_edit_elments += update_pro_email.format(field=field, resource=resource, Field=field.title())
protractor_add_elments += pro_email.format(field=field, resource=resource, Field=field.title())
elif field_type == "url":
db_rows += """
{} = db.Column(db.String(250), nullable=False)""".format(field)
schema += """
{} = fields.URL(validate=not_blank)""".format(field)
form_fields += form_field.format(field=field, Field=field.title(
), field_type=field_type, resource=resource, Resource=resource.title())
test_add_fields[field] = url_test
test_update_fields[field] = update_url_test
protractor_page_objects += pro_po_string.format(field=field, Field=field.title())
protractor_edit_elments += update_pro_url.format(field=field, resource=resource, Field=field.title())
protractor_add_elments += pro_url.format(field=field, resource=resource, Field=field.title())
elif field_type == "datetime":
db_rows += """
{} = db.Column(db.TIMESTAMP,server_default=db.func.current_timestamp(),nullable=False)""".format(field)
schema += """
{} = fields.DateTime(required=True)""".format(field)
form_fields += form_field.format(field=field, Field=field.title(
), field_type=field_type, resource=resource, Resource=resource.title())
test_add_fields[field] = date_time_test
test_update_fields[field] = update_date_time_test
protractor_page_objects += pro_po_string.format(field=field, Field=field.title())
protractor_edit_elments += update_pro_timestamp.format(field=field, resource=resource, Field=field.title())
protractor_add_elments += pro_timestamp.format(field=field, resource=resource, Field=field.title())
elif field_type == "date":
db_rows += """
{} = db.Column(db.Date, nullable=False)""".format(field)
schema += """
{} = fields.Date(required=True)""".format(field)
form_fields += date_field_string.format(field=field, Field=field.title(
), field_type=field_type, resource=resource, Resource=resource.title())
test_add_fields[field] = date_test
test_update_fields[field] = update_date_test
protractor_edit_elments += update_pro_date.format(field=field)
protractor_add_elments += pro_date.format(field=field)
elif field_type == "decimal":
db_rows += """
{} = db.Column(db.Numeric, nullable=False)""".format(field)
schema += """
{} = fields.Decimal(as_string=True)""".format(field)
form_fields += decimal_form_string.format(Field=field.title(),
field=field, resource=resource)
test_add_fields[field] = decimal_test
test_update_fields[field] = update_decimal_test
protractor_page_objects += pro_po_string.format(field=field, Field=field.title())
protractor_edit_elments += update_pro_decimal.format(field=field, resource=resource, Field=field.title())
protractor_add_elments += pro_decimal.format(field=field, resource=resource, Field=field.title())
elif field_type == "text":
db_rows += """
{} = db.Column(db.Text, nullable=False)""".format(field)
schema += """
{} = fields.String(validate=not_blank)""".format(field)
form_fields += text_form_string.format(Field=field.title(),
field=field, resource=resource)
test_add_fields[field] = text_test
test_update_fields[field] = update_text_test
protractor_page_objects += pro_po_string.format(field=field, Field=field.title())
protractor_edit_elments += update_pro_text.format(field=field, resource=resource, Field=field.title())
protractor_add_elments += pro_text.format(field=field, resource=resource, Field=field.title())
# models
meta += """ '{}', """.format(field)
init_args += """ {}, """.format(field)
init_self_vars += """
self.{field} = {field}""".format(field=field)
# Views
add_fields += add_string.format(field)
#_form.html
form_args.append(
"""{resource}_{field} = ''""".format(resource=resource, field=field))
field_table_headers += """ <th>{field}</th> """.format(field=field)
index_fields += """<td>{{{{ result['{field}'] }}}}</td>""".format(
field=field)
update_form_args += """{resource}_{field} = {resource}.{field}, """.format(resource=resource, field=field)
# controller.js
controller_fields += controller_field.format(field=field)
# Generate files with the new fields
module_dir = os.path.join('app', resources)
angular_dir = os.path.join('app/templates/', resources)
try:
os.mkdir(module_dir)
try:
os.makedirs(angular_dir)
generate_files(module_dir, angular_dir)
print('{} created successfully'.format(module_dir))
register_blueprints()
# Update routes in app.js
replace_string(
resource, resources, app_js_file, "// States", new_route_string)
# Add js files to index.html
replace_string(
resource, resources, main_index_file, "<!-- Controllers -->", js_src_string)
# Add menus to the main index.html
replace_string(
resource, resources, main_index_file, "<!-- menu -->", menu_string)
# Add tests to test.bash
replace_string(
resource, resources, test_script, "#TESTS", test_script_string)
# Add tests to conf.js
replace_string(
resource, resources, conf_js_file, "//Specs", conf_js_string)
run_autopep8()
except:
clean_up(module_dir)
raise
except:
raise