aboutsummaryrefslogtreecommitdiff
path: root/Info/9/3/Planet/tests/generate-scorm.py
blob: 31cb3ffbb9cea08582789a0392ac0e500ad8f347 (plain)
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
#!/usr/bin/env python3
"""Create a SCORM package checking test code using the Online-IDE"""
import argparse
from pathlib import Path
import os
import tomllib
import zipfile


def main():
    """entry point to the SCORM package creation"""

    parser = argparse.ArgumentParser()
    parser.add_argument('config', help='the projects config file')
    parser.add_argument('embedded_online_ide_files',
                        help='the path to the embeded online-ide files')
    parser.add_argument('--outfile', help='path of the output scorm package')
    parser.add_argument('--template-dir',
                        help='path to the scorm-template directory')
    parser.add_argument('--index-template', help='an index.html template file')
    parser.add_argument('--manifest-template',
                        help='an imsmanifest.xml template file')
    parser.add_argument('--scorm-api',
                        help='the path to the scorm api js code')
    parser.add_argument('--exercise',
                        default='exercise.java',
                        help='the path to the exercise code')
    parser.add_argument('--test',
                        default='test.java',
                        help='the path to the test code')
    parser.add_argument('--context', help='the path to the context code')
    args = parser.parse_args()

    with open(args.config, 'rb') as config_file:
        project = tomllib.load(config_file)

    substiutions = {
        'if_spritesheet_file_tag':
        f'<file href="{project["spritesheet"]}"/>'
        if project['spritesheet'] else '',
        'if_spritesheet_data_member':
        f"'spritesheetURL': '{project['spritesheet']}',"
        if project['spritesheet'] else '',
    }

    scorm_api_path = args.scorm_api or Path(args.template_dir
                                            or '') / 'scorm-api.js'
    with open(scorm_api_path, encoding='utf-8') as scorm_api_file:
        substiutions['scorm_api_js'] = scorm_api_file.read()

    for code_type in ['exercise', 'test']:
        code_path = vars(args)[code_type] or Path(args.template_dir
                                                  or '') / f'{code_type}.java'
        with open(code_path, encoding='utf-8') as code_file:
            substiutions[f'{code_type}_code'] = code_file.read()

    context_path = args.context or Path(args.template_dir
                                        or '') / 'context.java'
    if Path(context_path).exists():
        with open(context_path, encoding='utf-8') as f:
            context_code = f.read()
        substiutions[
            'if_context_script_tag'] = f'<script type="text/plain" title="Context.java" data-type="java" data-online-ide="{{\'readOnly\': true}}">{context_code}</script>'

    index_html_path = args.index_template or Path(args.template_dir
                                                  or '') / 'index.html'
    with open(index_html_path, encoding='utf-8') as templ:
        index_html = templ.read().format(**project, **substiutions)

    manifest_path = args.manifest_template or Path(args.template_dir
                                                   or '') / 'imsmanifest.xml'
    with open(manifest_path, encoding='utf-8') as templ:
        manifest = templ.read().format(**project, **substiutions)

    output_name = args.outfile or f'{project["id"]}.zip'
    with zipfile.ZipFile(output_name, 'w', zipfile.ZIP_DEFLATED) as output_zip:
        output_zip.writestr('index.html', index_html)
        output_zip.writestr('imsmanifest.xml', manifest)

        oide_files = Path(args.embedded_online_ide_files)
        for name in ['online-ide-embedded.js', 'online-ide-embedded.css']:
            output_zip.write(oide_files / name, arcname=name)

        for dirname, _, files in os.walk(oide_files / 'assets'):
            for filename in files:
                abspath = Path(dirname) / filename
                relpath = abspath.relative_to(oide_files)
                output_zip.write(abspath, arcname=relpath)

        if project['spritesheet']:
            output_zip.write(project['spritesheet'])


if __name__ == '__main__':
    main()