-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
67 lines (54 loc) · 2.14 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
from flask import Flask, render_template, send_from_directory
import os
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from flask_frozen import Freezer
import shutil
app = Flask(__name__)
app.config['PROPAGATE_EXCEPTIONS'] = True
app.config['DEBUG'] = True
freezer = Freezer(app)
class FontEventHandler(FileSystemEventHandler):
def __init__(self, path):
self.path = path
def on_created(self, event):
if not event.is_directory and (event.src_path.endswith('.ttf')
or event.src_path.endswith('.otf')):
if os.path.dirname(event.src_path) == self.path:
generate_stylesheet()
def on_deleted(self, event):
if not event.is_directory and (event.src_path.endswith('.ttf')
or event.src_path.endswith('.otf')):
if os.path.dirname(event.src_path) == self.path:
generate_stylesheet()
def generate_stylesheet():
fonts_path = os.path.join(os.getcwd(), 'fonts')
fonts = [
f for f in os.listdir(fonts_path)
if f.endswith('.ttf') or f.endswith('.otf')
]
with open('static/style.css', 'w') as f:
for font in fonts:
font_name = os.path.splitext(font)[0]
font_ext = os.path.splitext(font)[1][1:]
font_format = 'opentype' if font_ext == 'otf' else 'truetype'
f.write('@font-face {\n')
f.write(f'\tfont-family: "{font_name}";\n')
f.write(f'\tsrc: url("/fonts/{font}") format("{font_format}");\n')
f.write('}\n')
observer = Observer()
observer.schedule(FontEventHandler('fonts'), path='fonts', recursive=True)
observer.start()
@app.route('/')
def index():
fonts_path = os.path.join(os.getcwd(), 'fonts')
fonts = sorted([
f for f in os.listdir(fonts_path)
if f.endswith('.ttf') or f.endswith('.otf')
])
return render_template('index.html', fonts=fonts)
if __name__ == '__main__':
generate_stylesheet()
freezer.freeze()
shutil.copytree('fonts', 'build/static/fonts', dirs_exist_ok=True)
print("Build Complete.")