-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathconfig.py
81 lines (56 loc) · 1.82 KB
/
config.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
import re
__all__ = ("load_config", "get_setting_value")
def load_from_django():
from django.conf import settings
DEFAULT_CONFIG = {
"CACHE": not settings.DEBUG,
"IGNORE": [r".+\.hot-update.js", r".+\.map"],
"LOADER_CLASS": "webpack_boilerplate.loader.WebpackLoader",
}
user_config = dict(DEFAULT_CONFIG, **getattr(settings, "WEBPACK_LOADER", {}))
user_config["ignores"] = [re.compile(I) for I in user_config["IGNORE"]]
user_config["web_framework"] = "django"
return user_config
def load_from_flask():
from flask import current_app
DEFAULT_CONFIG = {
"CACHE": not current_app.config["DEBUG"],
"IGNORE": [r".+\.hot-update.js", r".+\.map"],
"LOADER_CLASS": "webpack_boilerplate.loader.WebpackLoader",
}
user_config = dict(DEFAULT_CONFIG, **current_app.config["WEBPACK_LOADER"])
user_config["ignores"] = [re.compile(I) for I in user_config["IGNORE"]]
user_config["web_framework"] = "flask"
return user_config
def load_config(name):
try:
import django
return load_from_django()
except ImportError:
pass
try:
import flask
return load_from_flask()
except ImportError:
pass
raise Exception("can not load config from this project")
def get_setting_value(key):
try:
import django
from django.conf import settings
return settings.get(key, None)
except ImportError:
pass
try:
import flask
from flask import current_app
map = {"STATIC_URL": "STATIC_URL"}
return current_app.config.get(map[key], None)
except ImportError:
pass
def setup_jinja2_ext(app):
"""
Run by flask app
"""
from .contrib.jinja2ext import WebpackExtension
app.jinja_env.add_extension(WebpackExtension)