This commit is contained in:
Joscha 2019-06-21 11:49:32 +00:00
parent 68595dcb7a
commit 11bd7778cf
14 changed files with 58 additions and 68 deletions

View file

@ -1,7 +1,6 @@
from dataclasses import dataclass, field
from enum import Enum, auto
from typing import (Any, Callable, Dict, Iterable, List, Optional, Tuple,
TypeVar)
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple
__all__ = ["ConfigException", "ConfigValueException", "TransparentConfig",
"Kind", "Condition", "Option", "TreeLoader"]
@ -43,6 +42,7 @@ class TransparentConfig:
# Special config reading and writing classes
class Kind(Enum):
BOOL = auto()
DICT = auto()
FLOAT = auto()
@ -64,13 +64,15 @@ Condition = Callable[[Any], bool]
@dataclass
class Option:
kind: Kind
default: Any
conditions: Iterable[Tuple[Condition, str]] = field(default_factory=list)
def check_valid(self, value: Any) -> None:
if not self.kind.matches(value):
raise ConfigValueException(f"value {value!r} does not match {self.kind}")
raise ConfigValueException(
f"value {value!r} does not match {self.kind}")
self.apply_conditions(value)