ignis: improve patch
This commit is contained in:
parent
76c8faf26b
commit
e8743be3b6
1 changed files with 47 additions and 30 deletions
|
@ -49,57 +49,57 @@ index f3a2ed5..e803171 100644
|
|||
|
||||
|
||||
diff --git a/ignis/utils/sass.py b/ignis/utils/sass.py
|
||||
index 9bd829d..a25fb2e 100644
|
||||
index 9bd829d..a891d85 100644
|
||||
--- a/ignis/utils/sass.py
|
||||
+++ b/ignis/utils/sass.py
|
||||
@@ -1,14 +1,31 @@
|
||||
@@ -1,14 +1,26 @@
|
||||
import os
|
||||
+import shutil
|
||||
import subprocess
|
||||
-from ignis.exceptions import SassCompilationError, DartSassNotFoundError
|
||||
+import typing
|
||||
+from ignis.exceptions import SassCompilationError, SassNotFoundError
|
||||
|
||||
TEMP_DIR = "/tmp/ignis"
|
||||
COMPILED_CSS = f"{TEMP_DIR}/compiled.css"
|
||||
os.makedirs(TEMP_DIR, exist_ok=True)
|
||||
|
||||
+# pick a Sass compiler
|
||||
+sass_compiler = None
|
||||
+
|
||||
+sass_compilers = [
|
||||
+ "sass", # dart-sass
|
||||
+ "grass" # grass-sass
|
||||
+]
|
||||
+
|
||||
+for compiler in sass_compilers:
|
||||
+ sass_compiler = shutil.which(compiler)
|
||||
+
|
||||
+ if sass_compiler:
|
||||
+ break
|
||||
+# resolve Sass compiler paths and pick a default one
|
||||
+# "sass" (dart-sass) is the default,
|
||||
+# "grass" is an API-compatible drop-in replacement
|
||||
+known_compilers = typing.Literal["sass", "grass"]
|
||||
+sass_compilers = {}
|
||||
+for cmd in typing.get_args(known_compilers):
|
||||
+ path = shutil.which(cmd)
|
||||
+ if path:
|
||||
+ sass_compilers[cmd] = path
|
||||
+
|
||||
|
||||
def compile_file(path: str) -> str:
|
||||
-def compile_file(path: str) -> str:
|
||||
- result = subprocess.run(["sass", path, COMPILED_CSS], capture_output=True)
|
||||
+ assert sass_compiler
|
||||
+
|
||||
+ result = subprocess.run([sass_compiler, path, COMPILED_CSS], capture_output=True)
|
||||
+def compile_file(path: str, compiler_path: str) -> str:
|
||||
+ result = subprocess.run([compiler_path, path, COMPILED_CSS], capture_output=True)
|
||||
|
||||
if result.returncode != 0:
|
||||
raise SassCompilationError(result.stderr.decode())
|
||||
@@ -18,8 +35,10 @@ def compile_file(path: str) -> str:
|
||||
@@ -17,9 +29,9 @@ def compile_file(path: str) -> str:
|
||||
return file.read()
|
||||
|
||||
|
||||
def compile_string(string: str) -> str:
|
||||
+ assert sass_compiler
|
||||
+
|
||||
-def compile_string(string: str) -> str:
|
||||
+def compile_string(string: str, compiler_path: str) -> str:
|
||||
process = subprocess.Popen(
|
||||
- ["sass", "--stdin"],
|
||||
+ [sass_compiler, "--stdin"],
|
||||
+ [compiler_path, "--stdin"],
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
@@ -35,7 +54,8 @@ def compile_string(string: str) -> str:
|
||||
def sass_compile(path: str | None = None, string: str | None = None) -> str:
|
||||
@@ -32,28 +44,38 @@ def compile_string(string: str) -> str:
|
||||
return stdout.decode()
|
||||
|
||||
|
||||
-def sass_compile(path: str | None = None, string: str | None = None) -> str:
|
||||
+def sass_compile(path: str | None = None, string: str | None = None, compiler: known_compilers | None = None) -> str:
|
||||
"""
|
||||
Compile a SASS/SCSS file or string.
|
||||
- Requires `Dart Sass <https://sass-lang.com/dart-sass/>`_.
|
||||
|
@ -108,7 +108,8 @@ index 9bd829d..a25fb2e 100644
|
|||
|
||||
Args:
|
||||
path: The path to the SASS/SCSS file.
|
||||
@@ -43,11 +63,11 @@ def sass_compile(path: str | None = None, string: str | None = None) -> str:
|
||||
string: A string with SASS/SCSS style.
|
||||
+ compiler: The desired Sass compiler, either ``sass`` or ``grass``.
|
||||
|
||||
Raises:
|
||||
TypeError: If neither of the arguments is provided.
|
||||
|
@ -118,8 +119,24 @@ index 9bd829d..a25fb2e 100644
|
|||
"""
|
||||
- if not os.path.exists("/bin/sass"):
|
||||
- raise DartSassNotFoundError()
|
||||
+ if not sass_compiler:
|
||||
+ if not sass_compilers:
|
||||
+ raise SassNotFoundError()
|
||||
+
|
||||
+ if compiler and compiler not in sass_compilers:
|
||||
+ raise SassNotFoundError()
|
||||
+
|
||||
+ if compiler:
|
||||
+ compiler_path = sass_compilers[compiler]
|
||||
+ else:
|
||||
+ compiler_path = next(iter(sass_compilers.values()))
|
||||
|
||||
if string:
|
||||
return compile_string(string)
|
||||
- return compile_string(string)
|
||||
+ return compile_string(string, compiler_path)
|
||||
|
||||
elif path:
|
||||
- return compile_file(path)
|
||||
+ return compile_file(path, compiler_path)
|
||||
|
||||
else:
|
||||
raise TypeError("sass_compile() requires at least one positional argument")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue