This repository has been archived by the owner on Mar 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_manifest.py
85 lines (60 loc) · 2.17 KB
/
flask_manifest.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
import os
from flask import (json, send_from_directory, current_app,
url_for as flask_url_for)
__all__ = ['Manifest']
CONFIG_PREFIX = 'MANIFEST_'
DEFAULT_CONFIG = {
'MANIFEST_DEBUG': False,
'MANIFEST_ROOT': 'dist',
'MANIFEST_SERVE_ROOT': True,
'MANIFEST_FILENAME': 'rev-manifest.json',
'MANIFEST_ENDPOINT': 'manifest-dist',
'MANIFEST_URL_PATH': '/dist',
'MANIFEST_DOMAIN': None,
}
FOREVER = 10 * 365 * 24 * 60 * 60
EXTENSION_KEY = 'manifest'
def get_state():
return current_app.extensions[EXTENSION_KEY]
def send_static_file(filename):
return send_from_directory(get_state().root, filename,
cache_timeout=FOREVER)
def url_for(endpoint, **values):
if endpoint == 'static':
state = get_state()
try:
rev_file = state.manifest_contents[values['filename']]
except KeyError:
pass
else:
values['filename'] = rev_file
if state.serve_root:
endpoint = state.endpoint
else:
# TODO: Handle external domains
values['_external'] = True
return flask_url_for(endpoint, **values)
class ManifestState(object):
def __init__(self, app):
for key, value in DEFAULT_CONFIG.items():
attr_name = key.replace(CONFIG_PREFIX, '').lower()
setattr(self, attr_name, app.config.get(key, value))
self.manifest = {}
class Manifest(object):
def __init__(self, app=None):
self.app = app
if self.app is not None:
self.init_app(app)
def init_app(self, app):
state = ManifestState(app)
if app.debug and not state.debug:
return
app.extensions[EXTENSION_KEY] = state
path = os.path.join(state.root, state.filename)
with app.open_resource(path) as manifest:
state.manifest_contents = json.load(manifest)
if state.serve_root:
app.add_url_rule(state.url_path + '/<path:filename>',
endpoint=state.endpoint,
view_func=send_static_file)
app.jinja_env.globals['url_for'] = url_for