exporter_utils.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from os.path import join
  2. from pathlib import Path
  3. from shutil import copytree, rmtree
  4. from jinja2 import Environment, FileSystemLoader
  5. import jogai._globals as _globals
  6. from jogai.settings import paths, settings
  7. from jogai.translations import gettext as _
  8. from jogai.videogame import load_environment, prepare
  9. CLASSNAMES = {
  10. 'character': _('character').capitalize(),
  11. 'scene': _('scene').capitalize(),
  12. }
  13. DIRS = {
  14. 'html': Path(f'{paths.EXPORT_MAIN_DIR}'), # /{paths.EXPORT_HTML_SUBDIR}'),
  15. 'css': Path(f'{paths.EXPORT_MAIN_DIR}/{paths.EXPORT_CSS_SUBDIR}'),
  16. 'js': Path(f'{paths.EXPORT_MAIN_DIR}/{paths.EXPORT_JS_SUBDIR}'),
  17. }
  18. def create_dirs():
  19. for dir_path in DIRS.values():
  20. dir_path.mkdir(parents=True)
  21. copytree(paths.IMAGES_DIR, paths.EXPORT_MAIN_DIR / paths.IMAGES_DIR)
  22. def write_file(filename: str, content: str):
  23. filename = Path(filename)
  24. file_path = DIRS[filename.suffix.removeprefix('.')] / filename
  25. with file_path.open(mode='w', encoding='utf-8') as f:
  26. f.write(content)
  27. def export(environment: str):
  28. load_environment(environment, to_export=True)
  29. window_size = (settings.WINDOW_WIDTH, settings.WINDOW_HEIGHT)
  30. jinja_env = Environment(
  31. loader=FileSystemLoader(
  32. join(
  33. settings.BASE_DIR,
  34. paths.EXPORTER_TEMPLATES_PATH,
  35. )
  36. )
  37. )
  38. template = jinja_env.get_template('base.html')
  39. html_content = template.render(
  40. characters=_globals.vg_characters,
  41. classnames=CLASSNAMES,
  42. css_path=f'{paths.EXPORT_CSS_SUBDIR}/{environment}.css',
  43. current_scene=_globals.vg_scene,
  44. language=settings.LANGUAGE_CODE,
  45. js_dir=f'{paths.EXPORT_JS_SUBDIR}',
  46. title=_globals.vg_title,
  47. )
  48. template = jinja_env.get_template('base.css')
  49. css_content = template.render(
  50. classnames=CLASSNAMES,
  51. window_size=window_size,
  52. )
  53. template = jinja_env.get_template('videogame.js')
  54. js_videogame_content = template.render(
  55. characters=_globals.vg_characters,
  56. classnames=CLASSNAMES,
  57. window_size=window_size,
  58. )
  59. template = jinja_env.get_template('scene.js')
  60. js_scenes_content = template.render(
  61. classnames=CLASSNAMES,
  62. scene=_globals.vg_scene,
  63. settings=settings,
  64. )
  65. template = jinja_env.get_template('character.js')
  66. js_characters_content = template.render(
  67. classnames=CLASSNAMES,
  68. settings=settings,
  69. paths=paths,
  70. )
  71. rmtree(paths.EXPORT_MAIN_DIR)
  72. create_dirs()
  73. write_file(f'{environment}.html', html_content)
  74. write_file(f'{environment}.css', css_content)
  75. write_file('videogame.js', js_videogame_content)
  76. write_file('scenes.js', js_scenes_content)
  77. write_file('characters.js', js_characters_content)