Skip to content

Commit

Permalink
Move color type aliases into wv_colors.py to avoid circular imports
Browse files Browse the repository at this point in the history
  • Loading branch information
kvid authored and formatc1702 committed Sep 28, 2021
1 parent c349461 commit 7125f28
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
10 changes: 3 additions & 7 deletions src/wireviz/DataClasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pathlib import Path

from wireviz.wv_helper import int2tuple, aspect_ratio
from wireviz import wv_colors
from wireviz.wv_colors import Color, Colors, ColorMode, ColorScheme, COLOR_CODES


# Each type alias have their legal values described in comments - validation might be implemented in the future
Expand All @@ -19,12 +19,8 @@
ConnectorMultiplier = PlainText # = Literal['pincount', 'populated']
CableMultiplier = PlainText # = Literal['wirecount', 'terminations', 'length', 'total_length']
ImageScale = PlainText # = Literal['false', 'true', 'width', 'height', 'both']
Color = PlainText # Two-letter color name = Literal[wv_colors._color_hex.keys()]
ColorMode = PlainText # = Literal['full', 'FULL', 'hex', 'HEX', 'short', 'SHORT', 'ger', 'GER']
ColorScheme = PlainText # Color scheme name = Literal[wv_colors.COLOR_CODES.keys()]

# Type combinations
Colors = PlainText # One or more two-letter color names (Color) concatenated into one string
Pin = Union[int, PlainText] # Pin identifier
PinIndex = int # Zero-based pin index
Wire = Union[int, PlainText] # Wire number or Literal['s'] for shield
Expand Down Expand Up @@ -284,9 +280,9 @@ def __post_init__(self) -> None:
if self.colors: # use custom color palette (partly or looped if needed)
pass
elif self.color_code: # use standard color palette (partly or looped if needed)
if self.color_code not in wv_colors.COLOR_CODES:
if self.color_code not in COLOR_CODES:
raise Exception('Unknown color code')
self.colors = wv_colors.COLOR_CODES[self.color_code]
self.colors = COLOR_CODES[self.color_code]
else: # no colors defined, add dummy colors
self.colors = [''] * self.wirecount

Expand Down
13 changes: 9 additions & 4 deletions src/wireviz/wv_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

from typing import Dict, List

from wireviz.DataClasses import Color, Colors

COLOR_CODES = {
'DIN': ['WH', 'BN', 'GN', 'YE', 'GY', 'PK', 'BU', 'RD', 'BK', 'VT', 'GYPK', 'RDBU', 'WHGN', 'BNGN', 'WHYE', 'YEBN',
'WHGY', 'GYBN', 'WHPK', 'PKBN', 'WHBU', 'BNBU', 'WHRD', 'BNRD', 'WHBK', 'BNBK', 'GYGN', 'YEGY', 'PKGN',
Expand Down Expand Up @@ -112,7 +110,14 @@
_hex_digits = set('0123456789abcdefABCDEF')


def get_color_hex(input, pad=False):
# Literal type aliases below are commented to avoid requiring python 3.8
Color = str # Two-letter color name = Literal[_color_hex.keys()]
Colors = str # One or more two-letter color names (Color) concatenated into one string
ColorMode = str # = Literal['full', 'FULL', 'hex', 'HEX', 'short', 'SHORT', 'ger', 'GER']
ColorScheme = str # Color scheme name = Literal[COLOR_CODES.keys()]


def get_color_hex(input: Colors, pad: bool = False) -> List[str]:
"""Return list of hex colors from either a string of color names or :-separated hex colors."""
if input is None or input == '':
return [color_default]
Expand Down Expand Up @@ -156,7 +161,7 @@ def from_hex(hex_input: str) -> str:
[translate.get(input[i:i+2], '??') for i in range(0, len(input), 2)]


def translate_color(input, color_mode):
def translate_color(input: Colors, color_mode: ColorMode) -> str:
if input == '' or input is None:
return ''
upper = color_mode.isupper()
Expand Down

0 comments on commit 7125f28

Please sign in to comment.