Exemple #1
0
def normalize_hex_code(value: Union[int, str]) -> str:
    if type(value) == int:
        if value == 0:
            return "000000"
        else:
            value = str(value)
    if isinstance(value, str) and re.search(HEX_REGEX, value):
        return value if len(value) == 6 else "".join(c * 2 for c in value)
    raise InputValueError("hex code", f"a string matching {HEX_REGEX}")
def test_input_error() -> None:
    error = InputValueError("A", "B")
    assert str(error) == "Bad A (expecting B)"
Exemple #3
0
def validate_indent_width(value: int) -> int:
    if type(value) == int and 0 <= value <= 8:
        return value
    raise InputValueError("indent width", "an integer between 0 and 8")
Exemple #4
0
def normalize_percent_value(value: Union[float, int]) -> float:
    if (type(value) in (float, int)) and 0 <= value <= 100:
        return value / 100
    raise InputValueError("percent value", "a float between 0.0 and 100.0")
Exemple #5
0
def validate_rgb_value(value: int) -> int:
    if type(value) == int and 0 <= value <= 255:
        return value
    raise InputValueError("RGB value", "an integer between 0 and 255")
Exemple #6
0
def normalize_degree_angle(value: Union[float, int]) -> float:
    if (type(value) in (float, int)) and 0 <= value <= 360:
        return value / 360
    raise InputValueError("degree angle", "a float between 0.0 and 360.0")
Exemple #7
0
def validate_editor(value: Optional[str]) -> Optional[str]:
    if value is None or (type(value) == str and len(value) > 0 and " " not in value):
        return value
    raise InputValueError("editor", "a shell-executable command without whitespaces")
Exemple #8
0
def validate_shades_count(value: Union[bool, int]) -> int:
    if (type(value) in (bool, int)) and 0 <= value <= 100:
        return value
    raise InputValueError("shades count", "an integer between 0 and 100")
Exemple #9
0
def validate_boolean_flag(value: Optional[bool]) -> Optional[bool]:
    if value is True or value is False or value is None:
        return value
    raise InputValueError("boolean flag", "True, False or no value")
Exemple #10
0
def normalize_hex_code(value: str) -> str:
    if isinstance(value, str) and re.search(HEX_REGEX, value):
        return value if len(value) == 6 else "".join(c * 2 for c in value)
    raise InputValueError("hex code", f"a string matching {HEX_REGEX}")
Exemple #11
0
def validate_editor(value: Optional[str]):
    if value is None or (type(value) == str and len(value.split()) == 1):
        return value
    raise InputValueError("editor", "a shell-executable command without args")