generate_frontend_files.py 1.95 KB
Newer Older
1
2
3
#####
# This python file is used to generate js files for redux
import os
4
from os.path import join
5
from django import template
6
import re
7
import yaml
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

############
# Need to do this first so that Django template engine is working
import django
from django.conf import settings

settings.configure(TEMPLATES=[
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['.'],  # if you want the templates from a file
        'APP_DIRS': False,  # we have no apps
    },
])

django.setup()
##########


current_dir = os.path.dirname(os.path.realpath(__file__))
templates_dir = current_dir + '/templates/'
saving_dir = os.path.realpath(current_dir + "/../src/generated/")

if not os.path.exists(saving_dir):
    os.makedirs(saving_dir)

templates = [
    'action-types',
    'actions',
36
37
    'reducers',
    'combinedReducers'
38
39
]

40
41
42
43
44
45
46
API_BASE = "/api/"
with open(join(current_dir, '../../backend/generate/api_config.yml'), 'r') as f:
    data = f.read()
    api_config = yaml.load(data)

contexts = []
for api in api_config:
47
48
    if "requires_testing" in api and api["requires_testing"]:
        continue
49
50
51
    name = api['viewset'].split('ViewSet')[0]
    name = name[0].lower() + name[1:]
    contexts.append({
52
53
        "name": api["api_end_point"],
        "api_end_point": API_BASE + api["api_end_point"] + '/',
54
    })
55
56
57
58
59
60
61
62
63
64
65
66


def read_file(file):
    with open(file, "r") as myfile:
        return myfile.read()


def write_file(file, data):
    with open(file, 'w') as the_file:
        the_file.write(data)


67
68
69
70
71
72
73
74
75
76
def convert(name):
    s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
    return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).upper()


for c in contexts:
    c['NAME'] = convert(c['name'])

# print(contexts)

77
78
79
80
for filename in templates:
    t = template.Template(
        read_file(os.path.join(templates_dir, filename + '.tpl')))

81
82
    c = template.Context({'data': contexts})
    output = t.render(c)
83
84

    write_file(os.path.join(saving_dir, filename + '.js'), output)