oshipka/vm_gen/vm_gen.py
2020-06-04 18:02:34 +02:00

125 lines
4.5 KiB
Python

import os
import sys
import autopep8
import inflect
import yaml
from jinja2 import Environment, FileSystemLoader
from oshipka.util.strings import snake_case_to_camel_case, camel_case_to_snake_case
pep_options = {'max_line_length': 120}
def _process_choice(column):
column_name = column.get('name', '')
column_upper = column_name.upper()
types_name = "{}_TYPES".format(column_upper)
choices = column.get('choices', {})
return {
'name': types_name,
'choices': choices,
}
def enrich_view_model(view_model):
columns = []
for column in view_model.get('columns', {}):
column_name = column.get('name')
column_type = column.get('type')
if column_type in ['text', 'long_text', ]:
_column_type = 'db.UnicodeText'
elif column_type in ['bool', ] or column_name.startswith('is_'):
_column_type = 'db.Boolean'
elif column_type in ['relationship', ]:
_column_type = 'relationship'
secondary = column.get('secondary')
if secondary:
if '_secondaries' not in view_model:
view_model['_secondaries'] = []
view_model['_secondaries'].append(secondary)
elif column_type in ['choice', ]:
if '_choice_types' not in view_model:
view_model['_choice_types'] = []
_choices = _process_choice(column)
_column_type = 'ChoiceType({})'.format(_choices.get('name'))
view_model['_choice_types'].append(_choices)
else:
_column_type = 'db.UnicodeText'
column.update({'_type': _column_type})
columns.append(column)
view_model['columns'] = columns
return view_model
def process_model(view_model):
template = env.get_template('model_py')
model = autopep8.fix_code(template.render(**view_model), options=pep_options)
_model_name = view_model.get('name')
filename = "{}.py".format(camel_case_to_snake_case(_model_name.split('.yaml')[0]))
with open(os.path.join(MODELS_PATH, filename), 'w+') as f:
f.write(model)
def process_html_templates(view_model):
_model_name_snake = camel_case_to_snake_case(view_model.get('name'))
model_dir = os.path.join(HTML_TEMPLATES_PATH, _model_name_snake)
if not os.path.exists(model_dir):
os.makedirs(model_dir)
for filename in os.listdir(os.path.join(VM_TEMPLATES_PATH, "html")):
template = env.get_template(os.path.join('html', filename))
rv = template.render(**view_model)
with open(os.path.join(model_dir, filename), 'w+') as f:
f.write(rv)
def main():
view_model_names = os.listdir(VIEW_MODELS_PATH)
all_imports = ['from oshipka.persistance import db']
for view_model_name in view_model_names:
with open(os.path.join(VIEW_MODELS_PATH, view_model_name), 'r') as stream:
try:
view_models = yaml.safe_load_all(stream)
for view_model in view_models:
view_model = enrich_view_model(view_model)
process_model(view_model)
view_model_name = view_model.get('name', '')
all_imports.append('from webapp.models.{} import {}'.format(
camel_case_to_snake_case(view_model_name), view_model_name))
process_html_templates(view_model)
except yaml.YAMLError as e:
breakpoint()
all_imports = autopep8.fix_code('\n'.join(all_imports), options=pep_options)
with open(os.path.join(MODELS_PATH, "__init__.py"), 'w+') as f:
f.write(all_imports)
if __name__ == "__main__":
basepath = sys.argv[1]
oshipka_path = os.environ.get('OSHIPKA_PATH')
VM_TEMPLATES_PATH = os.path.join(oshipka_path, "vm_gen", "templates")
WEBAPP_PATH = os.path.join(basepath, "webapp")
VIEW_MODELS_PATH = os.path.join(WEBAPP_PATH, "view_models")
MODELS_PATH = os.path.join(WEBAPP_PATH, "models")
if not os.path.exists(MODELS_PATH):
os.makedirs(MODELS_PATH)
HTML_TEMPLATES_PATH = os.path.join(WEBAPP_PATH, "templates")
env = Environment(
loader=FileSystemLoader(searchpath=VM_TEMPLATES_PATH),
block_start_string='[%',
block_end_string='%]',
variable_start_string='[[',
variable_end_string=']]'
)
env.filters['snake_to_camel'] = snake_case_to_camel_case
env.filters['camel_to_snake'] = camel_case_to_snake_case
p = inflect.engine()
env.filters['pluralize'] = p.plural
main()