generate_frontend_files.py 1.85 KB
Newer Older
1
2
#####
# This python file is used to generate js files for redux
Florent Chehab's avatar
Florent Chehab committed
3
4
from os import makedirs
from os.path import join, dirname, realpath, exists
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

############
# 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()
##########


Florent Chehab's avatar
Florent Chehab committed
26
current_dir = dirname(realpath(__file__))
27
templates_dir = current_dir + '/templates/'
Florent Chehab's avatar
Florent Chehab committed
28
saving_dir = realpath(current_dir + "/../src/generated/")
29

Florent Chehab's avatar
Florent Chehab committed
30
31
if not exists(saving_dir):
    makedirs(saving_dir)
32
33
34
35

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

40
API_BASE = "/api/"
Florent Chehab's avatar
Florent Chehab committed
41
with open(join(current_dir, '../../general/api/api_config.yml'), 'r') as f:
42
43
44
45
46
    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
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'])


66
for filename in templates:
Florent Chehab's avatar
Florent Chehab committed
67
68
69
70
71
    template_path = join(templates_dir, filename + '.tpl')
    with open(template_path, "r") as f:
        t = f.read()

    t = template.Template(t)
72

73
74
    c = template.Context({'data': contexts})
    output = t.render(c)
75

Florent Chehab's avatar
Florent Chehab committed
76
77
78
    output_path = join(saving_dir, filename + '.js')
    with open(output_path, 'w') as f:
        f.write(output)