Move color type aliases into wv_colors.py to avoid circular imports

This commit is contained in:
KV 2021-09-25 01:01:07 +02:00 committed by Daniel Rojas
parent c34946183e
commit 7125f28760
2 changed files with 12 additions and 11 deletions

View File

@ -6,7 +6,7 @@ from dataclasses import dataclass, field, InitVar
from pathlib import Path from pathlib import Path
from wireviz.wv_helper import int2tuple, aspect_ratio 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 # Each type alias have their legal values described in comments - validation might be implemented in the future
@ -19,12 +19,8 @@ Designator = PlainText # Case insensitive unique name of connector or cable
ConnectorMultiplier = PlainText # = Literal['pincount', 'populated'] ConnectorMultiplier = PlainText # = Literal['pincount', 'populated']
CableMultiplier = PlainText # = Literal['wirecount', 'terminations', 'length', 'total_length'] CableMultiplier = PlainText # = Literal['wirecount', 'terminations', 'length', 'total_length']
ImageScale = PlainText # = Literal['false', 'true', 'width', 'height', 'both'] 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 # Type combinations
Colors = PlainText # One or more two-letter color names (Color) concatenated into one string
Pin = Union[int, PlainText] # Pin identifier Pin = Union[int, PlainText] # Pin identifier
PinIndex = int # Zero-based pin index PinIndex = int # Zero-based pin index
Wire = Union[int, PlainText] # Wire number or Literal['s'] for shield Wire = Union[int, PlainText] # Wire number or Literal['s'] for shield
@ -284,9 +280,9 @@ class Cable:
if self.colors: # use custom color palette (partly or looped if needed) if self.colors: # use custom color palette (partly or looped if needed)
pass pass
elif self.color_code: # use standard color palette (partly or looped if needed) 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') 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 else: # no colors defined, add dummy colors
self.colors = [''] * self.wirecount self.colors = [''] * self.wirecount

View File

@ -3,8 +3,6 @@
from typing import Dict, List from typing import Dict, List
from wireviz.DataClasses import Color, Colors
COLOR_CODES = { COLOR_CODES = {
'DIN': ['WH', 'BN', 'GN', 'YE', 'GY', 'PK', 'BU', 'RD', 'BK', 'VT', 'GYPK', 'RDBU', 'WHGN', 'BNGN', 'WHYE', 'YEBN', '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', 'WHGY', 'GYBN', 'WHPK', 'PKBN', 'WHBU', 'BNBU', 'WHRD', 'BNRD', 'WHBK', 'BNBK', 'GYGN', 'YEGY', 'PKGN',
@ -112,7 +110,14 @@ color_default = '#ffffff'
_hex_digits = set('0123456789abcdefABCDEF') _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.""" """Return list of hex colors from either a string of color names or :-separated hex colors."""
if input is None or input == '': if input is None or input == '':
return [color_default] return [color_default]
@ -156,7 +161,7 @@ def get_color_translation(translate: Dict[Color, str], input: Colors) -> List[st
[translate.get(input[i:i+2], '??') for i in range(0, len(input), 2)] [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: if input == '' or input is None:
return '' return ''
upper = color_mode.isupper() upper = color_mode.isupper()