Skip to content

Commit

Permalink
feat(core): add ability to validate across config options, starting w…
Browse files Browse the repository at this point in the history
…ith tab_bar config
  • Loading branch information
aravinda0 committed Jan 22, 2024
1 parent e1cc05e commit 1eb500f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
33 changes: 33 additions & 0 deletions src/qtile_bonsai/core/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from qtile_bonsai.core.geometry import (
Axis,
AxisParam,
Box,
Direction,
DirectionParam,
PerimieterParams,
Expand Down Expand Up @@ -110,6 +111,15 @@ def set_config(self, key: str, value: Any, *, level: int | None = None):

self._config[level][key] = value

def validate_config(self):
"""Validate config across config keys.
Raises:
`ValueError`: if there are any validation errors.
"""
for level in self._config:
self._validate_tab_bar_config(level)

def get_config(
self,
key: str,
Expand Down Expand Up @@ -1136,6 +1146,29 @@ def _notify_subscribers(self, event: TreeEvent, nodes: list[Node]):
for callback in self._event_subscribers[event].values():
callback(nodes)

def _validate_tab_bar_config(self, level: int):
bar_height = self.get_config(
"tab_bar.height", level=level, fall_back_to_default=True
)
bar_margin = self.get_config(
"tab_bar.margin", level=level, fall_back_to_default=True
)
bar_border_size = self.get_config(
"tab_bar.border_size", level=level, fall_back_to_default=True
)
bar_padding = self.get_config(
"tab_bar.padding", level=level, fall_back_to_default=True
)
try:
Box(
Rect(0, 0, self.width, bar_height),
margin=bar_margin,
border=bar_border_size,
padding=bar_padding,
).validate()
except ValueError as err:
raise ValueError(f"Error in tab_bar config. {err}") from err


def tree_matches_repr(tree: Tree, test_str: str) -> bool:
"""Tests if the provided `Tree` instance has a str representation that matches the
Expand Down
3 changes: 1 addition & 2 deletions src/qtile_bonsai/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,7 @@ def _reset(self):
# We initialize the tree with arbitrary dimensions. These get reset soon as this
# layout's group is assigned to a screen.
self._tree = BonsaiTree(100, 100, config=config)
self._tree.validate_config()

self._tree.subscribe(
TreeEvent.node_added, lambda nodes: self._handle_added_tree_nodes(nodes)
Expand Down Expand Up @@ -836,8 +837,6 @@ def parse_multi_level_config(self) -> BonsaiTree.MultiLevelConfig:

multi_level_config[level][key] = value

validation.validate_across_options(multi_level_config)

return multi_level_config

def _handle_added_tree_nodes(self, nodes: list[BonsaiNodeMixin]):
Expand Down
5 changes: 0 additions & 5 deletions src/qtile_bonsai/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
from qtile_bonsai.tree import BonsaiTree


def validate_across_options(multi_level_config: BonsaiTree.MultiLevelConfig):
for level, config in multi_level_config.items():
pass


def validate_border_size(key: str, value: Any) -> tuple[bool, str | None]:
if not isinstance(value, int):
err_msg = (
Expand Down

0 comments on commit 1eb500f

Please sign in to comment.