-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathload_static.py
67 lines (57 loc) · 1.9 KB
/
load_static.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
"""
Helper class for loading generated file from webpack.
"""
import json
import logging
from urllib.parse import urlparse
from pkg_resources import resource_string
from django.conf import settings
logger = logging.getLogger(__name__) # pylint: disable=invalid-name
def urljoin(*args):
"""
Joining multiple url continuously. The default behavior from os.path.join
would completely reset the path if the second path start with '/'.
"""
begining_slash = '/' if args[0].startswith('/') else ''
trailing_slash = '/' if args[-1].endswith('/') else ''
joined_path = '/'.join(map(lambda x: str(x).strip('/'), args))
return begining_slash + joined_path + trailing_slash
class LoadStatic:
"""
Helper class for loading generated file from webpack.
"""
_manifest = {}
_is_dev_server = False
_is_loaded = False
@staticmethod
def reload_manifest():
"""
Reload from manifest file
"""
# comment this out while developing
if (LoadStatic._is_loaded):
return
try:
json_data = resource_string(__name__, 'static/dist/manifest.json').decode("utf8")
LoadStatic._manifest = json.loads(json_data)
LoadStatic._is_dev_server = LoadStatic._manifest.get('is_dev_server', False)
LoadStatic._is_loaded = True
except OSError:
logger.error('Cannot find static/dist/manifest.json')
@staticmethod
def get_url(key):
"""
get url from key
"""
LoadStatic.reload_manifest()
url = LoadStatic._manifest[key] if key in LoadStatic._manifest else key
if LoadStatic._is_dev_server:
return url
return urljoin(settings.STATIC_URL, 'dist', url)
@staticmethod
def get_is_dev_server():
"""
get is_dev_server
"""
LoadStatic.reload_manifest()
return LoadStatic._is_dev_server