This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
make_aliases.py
111 lines (91 loc) · 2.91 KB
/
make_aliases.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Copyright © 2019 Adobe, Inc.
# Author: Miguel Sousa
"""
Creates aliases of SVG or PNG files in the same directory.
"""
import argparse
import io
import logging
import os
import sys
from make_bw_font import validate_dir_path, validate_file_path
FILE_PREFIX = 'u'
FILE_EXTENSIONS = ('svg', 'png')
log = logging.getLogger('make_aliases')
def sniff_file_extension(src_name):
for ext in FILE_EXTENSIONS:
src_filename = '{}{}.{}'.format(FILE_PREFIX, src_name, ext)
if os.path.exists(src_filename):
return src_filename, ext
# no file was found
return None, None
def make_aliases(aliases_list, in_dir):
os.chdir(in_dir)
for src_name, dst_name in aliases_list:
src_filename, ext = sniff_file_extension(src_name)
if not src_filename:
log.warning("File named '{}{}' not found in '{}'".format(
FILE_PREFIX, src_name, in_dir))
continue
dst_filename = '{}{}.{}'.format(FILE_PREFIX, dst_name, ext)
if os.path.exists(dst_filename):
os.remove(dst_filename)
try:
os.symlink(src_filename, dst_filename)
except OSError as err:
if err.args == ('symbolic link privilege not held',):
log.error('On Windows this script must be run in Admin mode.')
else:
log.error('Failure while trying to create alias/symlink.')
return 1
def parse_aliases_file(file_path):
"""
Parses an emoji aliases text file.
Returns a list of tuples in the form ('src_name', 'dst_name').
"""
with io.open(file_path, encoding='utf-8') as fp:
lines = fp.read().splitlines()
aliases_list = []
for line in lines:
line = line.strip()
if not line or line.startswith('#'):
continue
# strip in-line comments
comment_idx = line.find('#')
if comment_idx > 0:
line = line[:comment_idx].strip()
aliases_list.append(tuple(line.split(';')))
return aliases_list
def main(args=None):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
'-v',
'--verbose',
help='verbose mode. Use -vv for debug mode',
action='count',
default=0
)
parser.add_argument(
'in_file',
help='text file containing a listing of the aliases',
metavar='FILE',
type=validate_file_path,
)
parser.add_argument(
'in_dir',
help='input directory containing SVG files',
metavar='DIR',
type=validate_dir_path,
)
opts = parser.parse_args(args)
if not opts.verbose:
level = "WARNING"
elif opts.verbose == 1:
level = "INFO"
else:
level = "DEBUG"
logging.basicConfig(level=level)
aliases_list = parse_aliases_file(opts.in_file)
make_aliases(aliases_list, opts.in_dir)
if __name__ == "__main__":
sys.exit(main())