dest_paths.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import os
  2. import pathlib
  3. import re
  4. from exception import FilterException
  5. import util
  6. import log
  7. def format_resolve(code, emoji, format):
  8. """
  9. Takes an instance of %whatever in a file path, and returns the actual intended value.
  10. """
  11. # %(param)
  12. if code[0] == '(':
  13. inside = code[1:-1]
  14. if inside not in emoji:
  15. raise ValueError('Missing property: ' + inside)
  16. return emoji[inside]
  17. # (%c) colormap
  18. if code == 'c':
  19. if 'color' not in emoji:
  20. raise ValueError('Cannot resolve %c - no colormap')
  21. return emoji['color']
  22. # (%d) replicating the directory structure to the image file as given in the input folder.
  23. if code == 'd':
  24. if 'src' not in emoji:
  25. raise ValueError('Cannot resolve %d - no emoji source file defined')
  26. return str(pathlib.Path(emoji['src']).parent)
  27. # (%f) export format (png-64, webp-128, etc.)
  28. if code == 'f':
  29. return format
  30. # (%z) export size (64, 128, 512, etc.)
  31. # will return 0 (as a string) if it's SVG.
  32. if code == 'z':
  33. if format.split("-")[0] in ["svg"]: # if there's no size...
  34. return "0"
  35. else:
  36. return format.split("-")[1]
  37. # (%i) image format, without size (png, webp, jxl, etc.)
  38. if code == 'i':
  39. return format.split("-")[0]
  40. # (%s) the emoji's shortcode
  41. if code == 's':
  42. if 'short' not in emoji:
  43. raise ValueError('Cannot resolve %s - no shortcode')
  44. return emoji['short']
  45. # (%s) the emoji's shortcode
  46. if code == 'b':
  47. if 'bundle' not in emoji:
  48. raise ValueError('Cannot resolve %b - no bundle')
  49. return emoji['bundle']
  50. # (%u) the emoji's unicode codepoint
  51. if code == 'u':
  52. if 'code' not in emoji:
  53. raise ValueError('Cannot resolve %u - no unicode codepoint defined')
  54. if '!' in emoji['code']:
  55. raise FilterException('Cannot resolve %u (unicode codepoint is explicitly undefined )')
  56. return util.uni_to_hex_filename(emoji['code'])
  57. raise ValueError('Cannot resolve format code: ' + code)
  58. def format_path(path, emoji, format):
  59. """
  60. Takes the requested output format and creates
  61. an actual usable path with it.
  62. """
  63. res = path
  64. # add file extensions to the path
  65. # (also acts as a format check)
  66. if format == 'svg':
  67. res = res + '.svg'
  68. elif format.startswith('png-'):
  69. res = res + '.png'
  70. elif format.startswith('pngc-'):
  71. res = res + '.png'
  72. elif format.startswith('webp-'):
  73. res = res + '.webp'
  74. elif format.startswith('jxl-'):
  75. res = res + '.jxl'
  76. else:
  77. raise ValueError('Invalid export format: ' + FilterException)
  78. # - catch instances of % in the output file path
  79. # - uses format_resolve() to figure out what it is
  80. # - replaces what it found
  81. for match, fcode in set(re.findall(r'(%(\(.*\)|.))', res)):
  82. repl = format_resolve(fcode, emoji, format)
  83. res = res.replace(match, repl)
  84. return res
  85. def make_dir_structure_for_file(path):
  86. try:
  87. dirname = os.path.dirname(path)
  88. if dirname:
  89. os.makedirs(dirname, exist_ok=True)
  90. except IOError:
  91. raise Exception('Could not create directory: ' + dirname)