check.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import os
  2. from dest_paths import format_path, format_resolve
  3. from exception import FilterException
  4. import svg
  5. import log
  6. def emoji(m, filtered_emoji, input_path, formats, path, src_size,
  7. num_threads, renderer, max_batch, cache, license_enabled, verbose):
  8. """
  9. Checks all emoji in a very light validation as well as checking if emoji
  10. aren't filtered out by user choices.
  11. It only checks:
  12. - If the emoji has been filtered out by user exporting options.
  13. - If the source SVG file exists.
  14. (Will throw an Exception if not the case.)
  15. - If the shortcode ('short') attribute exists.
  16. (Will throw an Exception if not the case.)
  17. - If the svg size is consistent (if a -q flag is used).
  18. (Will throw an Exception if not the case.)
  19. It this doesn't result in an Exception, it returns dict containing
  20. a list of emoji that aren't filtered out, as well as a count
  21. of emoji that were skipped.
  22. """
  23. exporting_emoji = []
  24. cached_emoji = {
  25. 'exports': [],
  26. 'licensed_exports': []
  27. }
  28. cached_emoji_count = 0 # Required to give a correct count without overlap
  29. skipped_emoji_count = 0
  30. for i, e in enumerate(filtered_emoji):
  31. short = e.get("short", "<UNNAMED>") # to provide info on possible error printouts
  32. try:
  33. format_path(path, e, 'svg')
  34. except FilterException as ex:
  35. if verbose:
  36. log.out(f"- - Skipped emoji: {short} - {ex}", 34)
  37. skipped_emoji_count += 1
  38. continue # skip if filtered out
  39. if 'src' not in e:
  40. raise ValueError(f"The emoji '{short}' is missing an 'src' attribute. It needs to have one.")
  41. # try to see if the source SVG file exists
  42. srcpath = os.path.join(m.homedir, input_path, e['src'])
  43. try:
  44. emoji_svg = open(srcpath, 'r').read()
  45. except Exception:
  46. raise ValueError(f"This source image for emoji '{short}' could not be loaded: {srcpath}")
  47. # the SVG size check (-q)
  48. if src_size is not None:
  49. img_size = svg.get_viewbox_size(emoji_svg)
  50. if img_size != src_size:
  51. raise ValueError("""The source image size for emoji '{}' is not what
  52. was expected. It's supposed to be {}, but it's actually
  53. {}.""".format(
  54. short,
  55. str(src_size[0]) + 'x' + str(src_size[1]),
  56. str(img_size[0]) + 'x' + str(img_size[1])
  57. ))
  58. if cache:
  59. # prime the cache keys in the emoji for later
  60. emoji_cache_keys = cache.get_cache_keys(e, m, emoji_svg,
  61. license_enabled)
  62. e['cache_keys'] = emoji_cache_keys
  63. # check if the emoji is in cache
  64. formats_status = {
  65. 'licensed_export': [],
  66. 'export': [],
  67. 'no_cache': []
  68. }
  69. for f in formats:
  70. status = None
  71. # Attempt to find a licensed export in cache
  72. if license_enabled:
  73. status = cache.get_cache(e, f, license_enabled)
  74. if status:
  75. formats_status['licensed_export'].append(f)
  76. # Attempt to find a non-licensed export in cache
  77. if status is None:
  78. status = cache.get_cache(e, f, license_enabled=False)
  79. if status:
  80. formats_status['export'].append(f)
  81. else:
  82. formats_status['no_cache'].append(f)
  83. # Assign the formats to their cache status and export bins
  84. if formats_status['licensed_export']:
  85. cached_emoji['licensed_exports'].append((e, formats_status['licensed_export']))
  86. if formats_status['export']:
  87. cached_emoji['exports'].append((e, formats_status['export']))
  88. if formats_status['export'] or formats_status['licensed_export']:
  89. cached_emoji_count += 1
  90. if formats_status['no_cache']:
  91. exporting_emoji.append((e, formats_status['no_cache']))
  92. else:
  93. # add the emoji to exporting_emoji if it's passed all the tests.
  94. # Cache is not enabled; pass all formats to exporting.
  95. exporting_emoji.append((e, formats))
  96. return { "exporting_emoji" : exporting_emoji
  97. , "skipped_emoji_count" : skipped_emoji_count
  98. , "cached_emoji": cached_emoji
  99. , "cached_emoji_count": cached_emoji_count
  100. }