#!/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''
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''
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()