From 7125f287609f9eaf0f922251398743140a2bb5ac Mon Sep 17 00:00:00 2001 From: KV Date: Sat, 25 Sep 2021 01:01:07 +0200 Subject: [PATCH] Move color type aliases into wv_colors.py to avoid circular imports --- src/wireviz/DataClasses.py | 10 +++------- src/wireviz/wv_colors.py | 13 +++++++++---- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/wireviz/DataClasses.py b/src/wireviz/DataClasses.py index cb2aba1..898168d 100644 --- a/src/wireviz/DataClasses.py +++ b/src/wireviz/DataClasses.py @@ -6,7 +6,7 @@ from dataclasses import dataclass, field, InitVar 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 @@ -19,12 +19,8 @@ Designator = PlainText # Case insensitive unique name of connector or cable 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 @@ -284,9 +280,9 @@ class Cable: 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 diff --git a/src/wireviz/wv_colors.py b/src/wireviz/wv_colors.py index ad7f6bc..e49c81a 100644 --- a/src/wireviz/wv_colors.py +++ b/src/wireviz/wv_colors.py @@ -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', @@ -112,7 +110,14 @@ color_default = '#ffffff' _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] @@ -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)] -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()