dest_paths.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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", "svgo"]: # if there's no size...
  34. return "0"
  35. else:
  36. return format.split("-")[1]
  37. # (%i) image format, without size (png, webp, avif, 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. # (%u) the emoji's unicode codepoint
  46. if code == 'u':
  47. if 'code' not in emoji:
  48. raise ValueError('Cannot resolve %u - no unicode codepoint defined')
  49. if '!' in emoji['code']:
  50. raise FilterException('Cannot resolve %u (unicode codepoint is explicitly undefined )')
  51. return util.uni_to_hex_filename(emoji['code'])
  52. raise ValueError('Cannot resolve format code: ' + code)
  53. def format_path(path, emoji, format):
  54. """
  55. Takes the requested output format and creates
  56. an actual usable path with it.
  57. """
  58. res = path
  59. # add file extensions to the path
  60. # (also acts as a format check)
  61. if format == 'svg':
  62. res = res + '.svg'
  63. elif format == 'svgo':
  64. res = res + '.svg'
  65. elif format.startswith('png-'):
  66. res = res + '.png'
  67. elif format.startswith('pngc-'):
  68. res = res + '.png'
  69. elif format.startswith('flif-'):
  70. res = res + '.flif'
  71. elif format.startswith('webp-'):
  72. res = res + '.webp'
  73. elif format.startswith('avif-'):
  74. res = res + '.avif'
  75. else:
  76. raise ValueError('Invalid export format: ' + FilterException)
  77. # - catch instances of % in the output file path
  78. # - uses format_resolve() to figure out what it is
  79. # - replaces what it found
  80. for match, fcode in set(re.findall(r'(%(\(.*\)|.))', res)):
  81. repl = format_resolve(fcode, emoji, format)
  82. res = res.replace(match, repl)
  83. return res
  84. def make_dir_structure_for_file(path):
  85. try:
  86. dirname = os.path.dirname(path)
  87. if dirname:
  88. os.makedirs(dirname, exist_ok=True)
  89. except IOError:
  90. raise Exception('Could not create directory: ' + dirname)