1INITIALISED = False
2babel = None
3babelPresetEs2015 = None
4
5
6def js6_to_js5(code):
7    global INITIALISED, babel, babelPresetEs2015
8    if not INITIALISED:
9        import signal, warnings, time
10        warnings.warn(
11            '\nImporting babel.py for the first time - this can take some time. \nPlease note that currently Javascript 6 in Js2Py is unstable and slow. Use only for tiny scripts!'
12        )
13
14        from .babel import babel as _babel
15        babel = _babel.Object.babel
16        babelPresetEs2015 = _babel.Object.babelPresetEs2015
17
18        # very weird hack. Somehow this helps babel to initialise properly!
19        try:
20            babel.transform('warmup', {'presets': {}})
21            signal.alarm(2)
22
23            def kill_it(a, b):
24                raise KeyboardInterrupt('Better work next time!')
25
26            signal.signal(signal.SIGALRM, kill_it)
27            babel.transform('stuckInALoop', {
28                'presets': babelPresetEs2015
29            }).code
30            for n in range(3):
31                time.sleep(1)
32        except:
33            print("Initialised babel!")
34        INITIALISED = True
35    return babel.transform(code, {'presets': babelPresetEs2015}).code
36
37
38if __name__ == '__main__':
39    print(js6_to_js5('obj={}; obj.x = function() {return () => this}'))
40    print()
41    print(js6_to_js5('const a = 1;'))
42