-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
105 lines (75 loc) · 2.97 KB
/
main.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
from nenwfont.nodes import nodes_map
import os
class Program:
def __init__(self, config):
self.config = config
self.pipeline = config['pipeline']
self.temp_files = []
self.callbacks = {}
self.nodes = {
node_key: nodes_map[node_key](self)
for node_key in set([ node['name'] for node in self.pipeline ])
}
def add_callback(self, name, callback):
if name not in self.callbacks:
self.callbacks[name] = []
self.callbacks[name].append(callback)
def run_callback(self, name, args=tuple()):
if name not in self.callbacks:
return
for callback in self.callbacks[name]:
callback(*args)
def process(self):
fonts = []
self.run_callback('before_process')
for pipe in self.pipeline:
self.run_callback('before_transform', (pipe, fonts))
node = self.nodes[pipe['name']]
fonts = node.transform(fonts, pipe)
self.run_callback('after_transform', (pipe, fonts))
self.run_callback('after_process', (fonts, ))
def cleanup(self):
for temp_file in self.temp_files:
print("Removing temporary files: %s" % temp_file)
os.remove(temp_file)
if __name__ == '__main__':
from datetime import datetime
import yaml
transform_start = 0
def before_transform_hook(pipe, fonts):
global transform_start
index = program.pipeline.index(pipe)
print("=" * 30 + " (Stage %d / %d, with %d Fonts)" % (index, len(program.pipeline), len(fonts)))
for key, value in pipe.items():
print("%s: %s" % (key, repr(value)))
print()
transform_start = datetime.now().timestamp()
def after_transform_hook(pipe, fonts):
print()
print("Done in %.3fs" % (datetime.now().timestamp() - transform_start))
print("Emitted %d fonts" % len(fonts))
def after_process_hook(fonts):
print("=" * 30 + " (with %d Fonts)" % len(fonts))
print("Cleaning Up!")
for font in fonts:
font.font.close()
program.cleanup()
print("Finished!")
print("Fonts:")
for font in fonts:
print("\t%s" % font['file_name'])
with open('pipeline.yml') as f:
config = yaml.load(f, Loader=yaml.SafeLoader)
program = Program(config)
program.add_callback('before_transform', before_transform_hook)
program.add_callback('after_transform', after_transform_hook)
program.add_callback('after_process', after_process_hook)
if config.get('debug_pipeline', False):
def debug_after_transform_hook(pipe, fonts):
for font in fonts:
print("[Font %s]" % font['file_name'])
for key, value in font.attributes.items():
print("\t%s: %s" % (key, value))
print()
program.add_callback('after_transform', debug_after_transform_hook)
program.process()