Skip to content

Commit

Permalink
refactor: Improve/simplify .to_css(compile_args=)
Browse files Browse the repository at this point in the history
Co-authored-by: Carson Sievert <carson@posit.co>
  • Loading branch information
gadenbuie and cpsievert committed Jun 26, 2024
1 parent e6819f6 commit 95cc53e
Showing 1 changed file with 16 additions and 29 deletions.
45 changes: 16 additions & 29 deletions shiny/ui/_theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,7 @@
import re
import tempfile
import textwrap
from typing import (
Any,
Callable,
Iterable,
Literal,
Optional,
Sequence,
TypeVar,
Union,
cast,
)
from typing import Any, Literal, Optional, Sequence, TypeVar

from htmltools import HTMLDependency

Expand All @@ -32,13 +22,6 @@
T = TypeVar("T", bound="Theme")


SassImporterReturnValue = Union["tuple[str]", "tuple[str, str]", "tuple[str, str, str]"]
SassImporterFunction = Union[
Callable[[str], SassImporterReturnValue],
Callable[[str, str], SassImporterReturnValue],
]


class SassCompileArgs(TypedDict):
output_style: NotRequired[Literal["nested", "expanded", "compact", "compressed"]]
source_comments: NotRequired[bool]
Expand All @@ -50,7 +33,7 @@ class SassCompileArgs(TypedDict):
precision: NotRequired[int]
custom_functions: NotRequired[Any] # not worth the effort, it's a complicated type
indented: NotRequired[bool]
importers: NotRequired[Iterable[tuple[int, SassImporterFunction]] | None]
importers: NotRequired[Any] # not worth the effort, it's a complicated type


theme_temporary_directories: set[tempfile.TemporaryDirectory[str]] = set()
Expand Down Expand Up @@ -416,17 +399,21 @@ def to_css(
check_libsass_installed()
import sass

if compile_args is None:
compile_args = {"output_style": "compressed"}
args: SassCompileArgs = {} if compile_args is None else compile_args

self._css = cast(
str,
sass.compile(
string=self.to_sass(),
include_paths=self._include_paths,
**compile_args, # type: ignore
),
)
if "include_paths" in args:
raise ValueError(
"The 'include_paths' argument is not allowed in 'compile_args'. "
"Use the 'include_paths' argument of the Theme constructor instead.",
)

args: SassCompileArgs = {
"output_style": "compressed",
"include_paths": self._include_paths,
**args,
}

self._css = sass.compile(string=self.to_sass(), **args)

return self._css

Expand Down

0 comments on commit 95cc53e

Please sign in to comment.