Skip to content

Commit

Permalink
feat: config override checking
Browse files Browse the repository at this point in the history
  • Loading branch information
castilloglenn committed Apr 14, 2024
1 parent e3c38a1 commit 57a21ac
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
24 changes: 14 additions & 10 deletions dataflow_animation/settings/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from dataclasses import dataclass, field
from typing import Tuple, Optional
from typing import Optional
import logging

from dataflow_animation.types import Color, ConfigOptions


# pylint: disable=R0902
@dataclass
Expand All @@ -23,20 +25,22 @@ class Config:
font_size: int = 20

# Colors
font_color: Tuple[int, int, int] = (255, 255, 255)
background_color: Tuple[int, int, int] = (0, 0, 0)
font_color: Color = (255, 255, 255)
background_color: Color = (0, 0, 0)

def __post_init__(self):
# Initialize `sdl_video_window_pos` based on `x` and `y`
self.sdl_video_window_pos = f"{self.x},{self.y}"

def update(self, **kwargs):
def update(self, **kwargs: ConfigOptions):
for key, value in kwargs.items():
if hasattr(self, key):
setattr(self, key, value)
if key in ("x", "y"):
self.sdl_video_window_pos = f"{self.x},{self.y}"
logging.info("Configuration updated: %s", kwargs)
if not hasattr(self, key):
raise ValueError(f"Invalid configuration option: {key}")

setattr(self, key, value)
if key in ("x", "y"):
self.sdl_video_window_pos = f"{self.x},{self.y}"

logging.info("Configuration updated: %s -> %s", key, value)


_singleton_instance: Optional[Config] = None
Expand Down
15 changes: 14 additions & 1 deletion dataflow_animation/types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
from typing import Any, Tuple
from typing import TypedDict, Any, Tuple


Dataflow = Any
"""Type alias for the Dataflow class."""

Color = Tuple[int, int, int]


class ConfigOptions(TypedDict, total=False):
width: int
height: int
x: int
y: int
sdl_video_window_pos: str
fps: int
font_name: str
font_size: int
font_color: Color
background_color: Color

0 comments on commit 57a21ac

Please sign in to comment.