default_theme_icons_builders.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. """Functions used to generate source files during build time"""
  2. import os
  3. from io import StringIO
  4. from methods import to_raw_cstring
  5. # See also `editor/icons/editor_icons_builders.py`.
  6. def make_default_theme_icons_action(target, source, env):
  7. dst = str(target[0])
  8. svg_icons = [str(x) for x in source]
  9. with StringIO() as icons_string, StringIO() as s:
  10. for svg in svg_icons:
  11. with open(svg, "r") as svgf:
  12. icons_string.write("\t%s,\n" % to_raw_cstring(svgf.read()))
  13. s.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n\n")
  14. s.write('#include "modules/modules_enabled.gen.h"\n\n')
  15. s.write("#ifndef _DEFAULT_THEME_ICONS_H\n")
  16. s.write("#define _DEFAULT_THEME_ICONS_H\n")
  17. s.write("static const int default_theme_icons_count = {};\n\n".format(len(svg_icons)))
  18. s.write("#ifdef MODULE_SVG_ENABLED\n")
  19. s.write("static const char *default_theme_icons_sources[] = {\n")
  20. s.write(icons_string.getvalue())
  21. s.write("};\n")
  22. s.write("#endif // MODULE_SVG_ENABLED\n\n")
  23. s.write("static const char *default_theme_icons_names[] = {\n")
  24. index = 0
  25. for f in svg_icons:
  26. fname = str(f)
  27. # Trim the `.svg` extension from the string.
  28. icon_name = os.path.basename(fname)[:-4]
  29. s.write('\t"{0}"'.format(icon_name))
  30. if fname != svg_icons[-1]:
  31. s.write(",")
  32. s.write("\n")
  33. index += 1
  34. s.write("};\n")
  35. s.write("#endif\n")
  36. with open(dst, "w", encoding="utf-8", newline="\n") as f:
  37. f.write(s.getvalue())