| 1 |
from typing import Dict, List |
| 2 |
import os |
| 3 |
|
| 4 |
from django.contrib.staticfiles.finders import get_finders |
| 5 |
import sass |
| 6 |
|
| 7 |
|
| 8 |
def find_static_paths() -> List[str]: |
| 9 |
""" |
| 10 |
Finds all static paths available in this Django project. |
| 11 |
|
| 12 |
:returns: |
| 13 |
List of paths containing static files. |
| 14 |
""" |
| 15 |
found_paths = [] |
| 16 |
for finder in get_finders(): |
| 17 |
if hasattr(finder, "storages"): |
| 18 |
for appname in finder.storages: |
| 19 |
if hasattr(finder.storages[appname], "location"): |
| 20 |
abspath = finder.storages[appname].location |
| 21 |
found_paths.append(abspath) |
| 22 |
return found_paths |
| 23 |
|
| 24 |
|
| 25 |
def find_static_scss() -> List[str]: |
| 26 |
""" |
| 27 |
Finds all static scss/sass files available in this Django project. |
| 28 |
|
| 29 |
:returns: |
| 30 |
List of paths of static scss/sass files. |
| 31 |
""" |
| 32 |
scss_files = [] |
| 33 |
for finder in get_finders(): |
| 34 |
for path, storage in finder.list([]): |
| 35 |
if path.endswith(".scss") or path.endswith(".sass"): |
| 36 |
fullpath = finder.find(path) |
| 37 |
scss_files.append(fullpath) |
| 38 |
return scss_files |
| 39 |
|
| 40 |
|
| 41 |
def compile_sass( |
| 42 |
inpath: str, |
| 43 |
outpath: str, |
| 44 |
output_style: str = None, |
| 45 |
precision: int = None, |
| 46 |
source_map: bool = False, |
| 47 |
include_paths: List[str] = None, |
| 48 |
) -> None: |
| 49 |
""" |
| 50 |
Calls sass.compile() within context of Django's known static file paths, |
| 51 |
and writes output CSS and/or sourcemaps to file. |
| 52 |
|
| 53 |
:param str inpath: |
| 54 |
Path to SCSS/Sass file or directory of SCSS/Sass files. |
| 55 |
:param str outpath: |
| 56 |
Path to a CSS file or directory in which to write output. The path will |
| 57 |
be created if it does not exist. |
| 58 |
:param str output_style: |
| 59 |
Corresponds to `output_style` from sass package. |
| 60 |
:param int precision: |
| 61 |
Corresponds to `precision` from sass package. |
| 62 |
:param bool source_map: |
| 63 |
If True, write a source map along with the output CSS file. |
| 64 |
Only valid when `inpath` is a file. |
| 65 |
:returns: |
| 66 |
None |
| 67 |
""" |
| 68 |
|
| 69 |
|
| 70 |
include_paths = include_paths or find_static_paths() |
| 71 |
|
| 72 |
|
| 73 |
sassargs = {} |
| 74 |
|
| 75 |
|
| 76 |
if os.path.isdir(inpath): |
| 77 |
|
| 78 |
if not os.path.exists(outpath): |
| 79 |
os.makedirs(outpath) |
| 80 |
if os.path.isdir(outpath): |
| 81 |
sassargs.update({"dirname": (inpath, outpath)}) |
| 82 |
else: |
| 83 |
raise NotADirectoryError( |
| 84 |
"Output path must also be a directory when input path is a directory." |
| 85 |
) |
| 86 |
|
| 87 |
|
| 88 |
outfile = None |
| 89 |
if os.path.isfile(inpath): |
| 90 |
|
| 91 |
sassargs.update({"filename": inpath}) |
| 92 |
|
| 93 |
|
| 94 |
if not os.path.exists(outpath): |
| 95 |
if not outpath.endswith(".css"): |
| 96 |
os.makedirs(outpath) |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
if os.path.exists(outpath) and os.path.isdir(outpath): |
| 101 |
outfile = os.path.join( |
| 102 |
outpath, |
| 103 |
os.path.basename( |
| 104 |
inpath.replace(".scss", ".css").replace(".sass", ".css") |
| 105 |
), |
| 106 |
) |
| 107 |
else: |
| 108 |
outfile = outpath |
| 109 |
|
| 110 |
|
| 111 |
if source_map: |
| 112 |
sassargs.update({"source_map_filename": outfile + ".map"}) |
| 113 |
|
| 114 |
|
| 115 |
rval = sass.compile( |
| 116 |
output_style=output_style, |
| 117 |
precision=precision, |
| 118 |
include_paths=include_paths, |
| 119 |
**sassargs, |
| 120 |
) |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
if rval and outfile: |
| 126 |
|
| 127 |
if isinstance(rval, tuple): |
| 128 |
map_outfile = outfile + ".map" |
| 129 |
outfile_dir = os.path.dirname(map_outfile) |
| 130 |
if not os.path.exists(outfile_dir): |
| 131 |
os.makedirs(outfile_dir, exist_ok=True) |
| 132 |
file = open(map_outfile, "w", encoding="utf8") |
| 133 |
file.write(rval[1]) |
| 134 |
file.close() |
| 135 |
rval = rval[0] |
| 136 |
|
| 137 |
|
| 138 |
outfile_dir = os.path.dirname(outfile) |
| 139 |
if not os.path.exists(outfile_dir): |
| 140 |
os.makedirs(outfile_dir, exist_ok=True) |
| 141 |
file = open(outfile, "w", encoding="utf8") |
| 142 |
file.write(rval) |
| 143 |
file.close() |