-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcli.py
78 lines (64 loc) · 2.08 KB
/
cli.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
import sys
import click
from flask import current_app
from flask.cli import with_appcontext
from flask_static_digest.digester import clean as _clean
from flask_static_digest.digester import compile as _compile
@click.group()
@click.pass_context
@with_appcontext
def digest(ctx):
"""md5 tag and compress static files."""
ctx.ensure_object(dict)
ctx.obj["gzip"] = False
ctx.obj["brotli"] = False
ctx.obj["blacklist_filter"] = current_app.config.get(
"FLASK_STATIC_DIGEST_BLACKLIST_FILTER"
)
compression = current_app.config.get("FLASK_STATIC_DIGEST_COMPRESSION")
for algo in compression:
if algo == "gzip":
ctx.obj["gzip"] = True
elif algo == "brotli":
try:
import brotli # noqa: F401
except ModuleNotFoundError:
click.echo("Error: Python package 'brotli' not installed.")
sys.exit(78) # sysexits.h 78 EX_CONFIG configuration error
else:
ctx.obj["brotli"] = True
else:
click.echo(
f"{algo} is not a supported compression value, it must be"
" 'gzip' or 'brotli'"
)
sys.exit(78)
@digest.command()
@click.pass_context
@with_appcontext
def compile(ctx):
"""Generate optimized static files and a cache manifest."""
for blueprint in [current_app, *current_app.blueprints.values()]:
if not blueprint.static_folder:
continue
_compile(
blueprint.static_folder,
blueprint.static_folder,
ctx.obj["blacklist_filter"],
ctx.obj["gzip"],
ctx.obj["brotli"],
)
@digest.command()
@click.pass_context
@with_appcontext
def clean(ctx):
"""Remove generated static files and cache manifest."""
for blueprint in [current_app, *current_app.blueprints.values()]:
if not blueprint.static_folder:
continue
_clean(
blueprint.static_folder,
ctx.obj["blacklist_filter"],
ctx.obj["gzip"],
ctx.obj["brotli"],
)