Move type parsers for config into own module

This commit is contained in:
Kovid Goyal
2021-05-24 22:29:28 +05:30
parent a4daa49f70
commit fe94f4cbb4
11 changed files with 392 additions and 372 deletions

View File

@@ -7,7 +7,7 @@ import re
import shlex
from typing import (
Any, Callable, Dict, FrozenSet, Generator, Iterable, Iterator, List,
NamedTuple, Optional, Sequence, Tuple, Type, TypeVar, Union
NamedTuple, Optional, Sequence, Tuple, Type, TypeVar, Union, Set
)
from ..rgb import Color, to_color as as_color
@@ -24,6 +24,14 @@ class BadLine(NamedTuple):
exception: Exception
def positive_int(x: ConvertibleToNumbers) -> int:
return max(0, int(x))
def positive_float(x: ConvertibleToNumbers) -> float:
return max(0, float(x))
def to_color(x: str) -> Color:
ans = as_color(x, validate=True)
if ans is None: # this is only for type-checking
@@ -342,3 +350,9 @@ def parse_kittens_key(
return None
ans = parse_kittens_func_args(action, funcs_with_args)
return ans, parse_shortcut(sc)
def uniq(vals: Iterable[T]) -> List[T]:
seen: Set[T] = set()
seen_add = seen.add
return [x for x in vals if x not in seen and not seen_add(x)]