config: number with unit for min contrast ratio

This commit is contained in:
arne314
2025-03-14 14:05:34 +01:00
parent a6fcdf1964
commit 40ef6b4f37
5 changed files with 52 additions and 20 deletions

View File

@@ -22,6 +22,7 @@ from ..typing import Protocol
from ..utils import expandvars, log_error, shlex_split
key_pat = re.compile(r'([a-zA-Z][a-zA-Z0-9_-]*)\s+(.+)$')
number_unit_pat = re.compile(r'\s*([-+]?\d+\.?\d*)\s*([^\d\s]*)?')
ItemParser = Callable[[str, str, dict[str, Any]], bool]
T = TypeVar('T')
@@ -66,6 +67,19 @@ def unit_float(x: ConvertibleToNumbers) -> float:
return max(0, min(float(x), 1))
def number_with_unit(x: str, default_unit: str = '') -> tuple[float, str]:
mat = number_unit_pat.match(x)
exception = None
if mat is not None:
try:
value, unit = float(mat.group(1)), mat.group(2)
unit = default_unit if not unit else unit
return value, unit
except Exception as e:
exception = e
raise ValueError(f'Invalid number with unit config option provided: {x if exception is None else exception}')
def to_bool(x: str) -> bool:
return x.lower() in ('y', 'yes', 'true')