From bc203aaff171438b250a84f052c922e87cdbef83 Mon Sep 17 00:00:00 2001 From: Joscha Date: Thu, 16 May 2019 11:12:39 +0000 Subject: [PATCH] Create palette from config and add default config --- cheuph/euphoria/__init__.py | 4 +-- cheuph/euphoria/palette.py | 11 -------- cheuph/euphoria/util.py | 56 +++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 13 deletions(-) delete mode 100644 cheuph/euphoria/palette.py create mode 100644 cheuph/euphoria/util.py diff --git a/cheuph/euphoria/__init__.py b/cheuph/euphoria/__init__.py index 83b96a0..46b26d5 100644 --- a/cheuph/euphoria/__init__.py +++ b/cheuph/euphoria/__init__.py @@ -1,8 +1,8 @@ from typing import List -from .palette import * from .single_room_application import * +from .util import * __all__: List[str] = [] -__all__ += palette.__all__ __all__ += single_room_application.__all__ +__all__ += util.__all__ diff --git a/cheuph/euphoria/palette.py b/cheuph/euphoria/palette.py deleted file mode 100644 index a4c480e..0000000 --- a/cheuph/euphoria/palette.py +++ /dev/null @@ -1,11 +0,0 @@ -import enum - -__all__ = ["Style", "PALETTE"] - -@enum.unique -class Style(enum.Enum): - ROOM = "room" - -PALETTE = [ - (Style.ROOM, "light blue,bold", ""), -] diff --git a/cheuph/euphoria/util.py b/cheuph/euphoria/util.py new file mode 100644 index 0000000..480a16e --- /dev/null +++ b/cheuph/euphoria/util.py @@ -0,0 +1,56 @@ +from typing import List, Tuple, Union + +from ..config import Config + +__all__ = ["UtilException","Palette", "palette_from_config", "DEFAULT_CONFIG"] + +class UtilException(Exception): + pass + +Palette = List[Union[Tuple[str, str], Tuple[str, str, str], + Tuple[str, str, str, str]]] + +def palette_from_config(conf: Config) -> Palette: + palette: Palette = [] + + styles = conf.tree["style"] + for style, info in styles.items(): + # First, do the alias stuff + alias = info.get("alias") + if isinstance(alias, str): + if alias in styles: + palette.append((style, alias)) + continue + else: + raise UtilException((f"style.{style}.alias must be the name of" + " another style")) + elif alias is not None: + raise UtilException(f"style.{style}.alias must be a string") + + # Foreground/background + fg = info.get("fg") + bg = info.get("bg") + + if not isinstance(fg, str) and fg is not None: + raise TypeError(f"style.{style}.fg must be a string") + + if not isinstance(bg, str) and bg is not None: + raise TypeError(f"style.{style}.bg must be a string") + + fg = fg or "" + bg = bg or "" + + palette.append((style, fg, bg)) + + return palette + +DEFAULT_CONFIG = { + "element": { + "room": "room", + }, + "style": { + "room": { + "fg": "light blue, bold", + }, + }, +}