forked from mutantstandard/orxporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
paths.py
83 lines (67 loc) · 2.43 KB
/
paths.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import pathlib
import re
from exception import FilterException
import util
def format_resolve(code, emoji, format):
"""
Takes an instance of %whatever in a file path, and returns the actual intended value.
"""
# %(param)
if code[0] == '(':
inside = code[1:-1]
if inside not in emoji:
raise ValueError('Missing property: ' + inside)
return emoji[inside]
# (%c) colormap
if code == 'c':
if 'color' not in emoji:
raise ValueError('Cannot resolve %c - no colormap')
return emoji['color']
# (%d) replicating the directory structure to the image file as given in the input folder.
if code == 'd':
if 'src' not in emoji:
raise ValueError('Cannot resolve %d - no emoji source file defined')
return str(pathlib.Path(emoji['src']).parent)
# (%f) export format (png, webp, jpx, etc.)
if code == 'f':
return format
# (%s) the emoji's shortcode
if code == 's':
if 'short' not in emoji:
raise ValueError('Cannot resolve %s - no shortcode')
return emoji['short']
# (%u) the emoji's unicode codepoint
if code == 'u':
if 'code' not in emoji:
raise ValueError('Cannot resolve %u - no unicode codepoint defined')
if '!' in emoji['code']:
raise FilterException('Cannot resolve %u (unicode codepoint is explicitly undefined )')
return util.uni_to_hex_filename(emoji['code'])
raise ValueError('Cannot resolve format code: ' + code)
def format_path(path, emoji, format):
"""
Takes the requested output format and creates
an actual usable path with it.
"""
res = path
# add file extensions to the path
# (also acts as a format check)
if format == 'svg':
res = res + '.svg'
elif format.startswith('png-'):
res = res + '.png'
elif format.startswith('flif-'):
res = res + '.flif'
elif format.startswith('webp-'):
res = res + '.webp'
elif format.startswith('avif-'):
res = res + '.avif'
else:
raise ValueError('Invalid export format: ' + FilterException)
# - catch instances of % in the output file path
# - uses format_resolve() to figure out what it is
# - replaces what it found
for match, fcode in set(re.findall(r'(%(\(.*\)|.))', res)):
repl = format_resolve(fcode, emoji, format)
res = res.replace(match, repl)
return res