Skip to content

Commit dbc4759

Browse files
committed
refactor: Static method for brand to sass variable helpers
1 parent f083519 commit dbc4759

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

shiny/ui/_theme_brand.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ def __str__(self):
2727
return self.message
2828

2929

30+
def warn_or_raise_unmapped_variable(unmapped: str):
31+
if os.environ.get("SHINY_BRAND_YML_RAISE_UNMAPPED") == "true":
32+
raise ThemeBrandUnmappedFieldError(unmapped)
33+
else:
34+
warnings.warn(
35+
f"Shiny's brand.yml theme does not yet support {unmapped}.",
36+
stacklevel=4,
37+
)
38+
39+
3040
color_map: dict[str, list[str]] = {
3141
# Bootstrap uses $gray-900 and $white for the body bg-color by default, and then
3242
# swaps them for $gray-100 and $gray-900 in dark mode. brand.yml may end up with
@@ -286,8 +296,8 @@ def __init__(
286296
self.brand = brand
287297

288298
# Prep Sass and CSS Variables -------------------------------------------------
289-
sass_vars_colors, css_vars_colors = self._prepare_color_vars()
290-
sass_vars_typography = self._prepare_typography_vars()
299+
sass_vars_colors, css_vars_colors = ThemeBrand._prepare_color_vars(brand)
300+
sass_vars_typography = ThemeBrand._prepare_typography_vars(brand)
291301

292302
# Theme -----------------------------------------------------------------------
293303
# Defaults are added in reverse order, so each chunk appears above the next
@@ -315,25 +325,26 @@ def _get_theme_name(self, brand: Brand) -> str:
315325

316326
return brand.meta.name.short or brand.meta.name.full or "brand"
317327

318-
def _prepare_color_vars(self) -> tuple[dict[str, str], list[str]]:
328+
@staticmethod
329+
def _prepare_color_vars(brand: Brand) -> tuple[dict[str, str], list[str]]:
319330
"""Colors: create a dictionary of Sass variables and a list of brand CSS variables"""
320-
if not self.brand.color:
331+
if not brand.color:
321332
return {}, []
322333

323334
mapped: dict[str, str] = {}
324335
brand_sass_vars: dict[str, str] = {}
325336
brand_css_vars: list[str] = []
326337

327338
# Map values in colors to their Sass variable counterparts
328-
for thm_name, thm_color in self.brand.color.to_dict(include="theme").items():
339+
for thm_name, thm_color in brand.color.to_dict(include="theme").items():
329340
if thm_name not in color_map:
330-
self._handle_unmapped_variable(f"color.{thm_name}")
341+
warn_or_raise_unmapped_variable(f"color.{thm_name}")
331342
continue
332343

333344
for sass_var in color_map[thm_name]:
334345
mapped[sass_var] = thm_color
335346

336-
brand_color_palette = self.brand.color.to_dict(include="palette")
347+
brand_color_palette = brand.color.to_dict(include="palette")
337348

338349
# Map the brand color palette to Bootstrap's named colors, e.g. $red, $blue.
339350
for pal_name, pal_color in brand_color_palette.items():
@@ -350,21 +361,22 @@ def _prepare_color_vars(self) -> tuple[dict[str, str], list[str]]:
350361

351362
return {**brand_sass_vars, **mapped}, brand_css_vars
352363

353-
def _prepare_typography_vars(self) -> dict[str, str]:
364+
@staticmethod
365+
def _prepare_typography_vars(brand: Brand) -> dict[str, str]:
354366
"""Typography: Create a list of Bootstrap Sass variables"""
355367
mapped: dict[str, str] = {}
356368

357-
if not self.brand.typography:
369+
if not brand.typography:
358370
return mapped
359371

360-
brand_typography = self.brand.typography.model_dump(
372+
brand_typography = brand.typography.model_dump(
361373
exclude={"fonts"},
362374
exclude_none=True,
363375
)
364376

365377
for field, prop in brand_typography.items():
366378
if field not in typography_map:
367-
self._handle_unmapped_variable(f"typography.{field}")
379+
warn_or_raise_unmapped_variable(f"typography.{field}")
368380
continue
369381

370382
for prop_key, prop_value in prop.items():
@@ -376,7 +388,7 @@ def _prepare_typography_vars(self) -> dict[str, str]:
376388
for typo_sass_var in typo_sass_vars:
377389
mapped[typo_sass_var] = prop_value
378390
else:
379-
self._handle_unmapped_variable(f"typography.{field}.{prop_key}")
391+
warn_or_raise_unmapped_variable(f"typography.{field}.{prop_key}")
380392

381393
return mapped
382394

@@ -545,15 +557,6 @@ def _add_brand_bootstrap_other(self, bootstrap: BrandBootstrapConfig):
545557
if bootstrap.rules:
546558
self.add_rules(bootstrap.rules)
547559

548-
def _handle_unmapped_variable(self, unmapped: str):
549-
if os.environ.get("SHINY_BRAND_YML_RAISE_UNMAPPED") == "true":
550-
raise ThemeBrandUnmappedFieldError(unmapped)
551-
else:
552-
warnings.warn(
553-
f"Shiny's brand.yml theme does not yet support {unmapped}.",
554-
stacklevel=4,
555-
)
556-
557560
def _html_dependencies(self) -> list[HTMLDependency]:
558561
theme_deps = super()._html_dependencies()
559562

0 commit comments

Comments
 (0)