Skip to content

Commit

Permalink
Add theme color attrs for stronger typing; v0.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
schloerke committed Mar 7, 2024
1 parent 11c3d0a commit 1f13e89
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to `shinyswatch` will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.5.1] - 2024-03-07

* Add typed attributes in the theme's color class for stronger type checking.

## [0.5.0] - 2024-03-07

### Breaking changes
Expand Down
2 changes: 1 addition & 1 deletion shinyswatch/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Bootswatch + Bootstrap 5 themes for Shiny"""

__version__ = "0.5.0"
__version__ = "0.5.1"

from . import theme
from ._get_theme import get_theme
Expand Down
40 changes: 29 additions & 11 deletions shinyswatch/_theme_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,45 @@


class ThemeColors:
def __init__(self, name: BSW5_THEME_NAME) -> None:
self._name = name
self._colors: dict[str, str] = bsw5_theme_colors[name]

def __getattr__(self, key: str) -> str:
return self._colors[key]
# Leave type definitions for pyright type stubs; Do not set values here
# Leave type definintions here for completions w/ static type checker
primary: str
secondary: str
success: str
danger: str
warning: str
info: str
light: str
dark: str
body_color: str
body_bg: str

def __getitem__(self, key: str) -> str:
return self._colors[key]
def __init__(self, name: BSW5_THEME_NAME) -> None:
colors = bsw5_theme_colors[name]
_color_names: list[str] = list(colors.keys())
_color_name_len = max(len(x) for x in _color_names)

def __dir__(self) -> list[str]:
return list(self._colors.keys())
self._name = name
self._color_names = _color_names
self._color_name_len = _color_name_len
for k, v in colors.items():
setattr(self, k, v)

def __repr__(self) -> str:
colors = [f"{k:11}: {v}" for k, v in self._colors.items()]
colors = [
f"{k:{self._color_name_len + 1}}: {getattr(self, k)}"
for k in self._color_names
]
colors = "\n ".join(colors)
ret = [f"<ThemeColors({self._name!r}):", " " + colors, ">"]
return "\n".join(ret)


class ShinyswatchTheme(Tagifiable):
# Leave type definitions for pyright type stubs; Do not set values here
name: BSW5_THEME_NAME
colors: ThemeColors

def __init__(self, name: BSW5_THEME_NAME) -> None:
assert_theme(name=name)
self.name: BSW5_THEME_NAME = name
Expand Down

0 comments on commit 1f13e89

Please sign in to comment.