Make adding config values easier
This commit is contained in:
parent
ae63d3e704
commit
671b5ce2c7
1 changed files with 13 additions and 2 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from enum import Enum, auto
|
from enum import Enum, auto
|
||||||
from typing import Any, Callable, Dict, List, Optional, Tuple, TypeVar
|
from typing import (Any, Callable, Dict, Iterable, List, Optional, Tuple,
|
||||||
|
TypeVar)
|
||||||
|
|
||||||
__all__ = ["ConfigException", "ConfigValueException", "TransparentConfig",
|
__all__ = ["ConfigException", "ConfigValueException", "TransparentConfig",
|
||||||
"Kind", "Condition", "Option", "TreeLoader"]
|
"Kind", "Condition", "Option", "TreeLoader"]
|
||||||
|
|
@ -65,7 +66,7 @@ Condition = Callable[[Any], bool]
|
||||||
class Option:
|
class Option:
|
||||||
kind: Kind
|
kind: Kind
|
||||||
default: Any
|
default: Any
|
||||||
conditions: List[Tuple[Condition, str]] = field(default_factory=list)
|
conditions: Iterable[Tuple[Condition, str]] = field(default_factory=list)
|
||||||
|
|
||||||
def check_valid(self, value: Any) -> None:
|
def check_valid(self, value: Any) -> None:
|
||||||
if not self.kind.matches(value):
|
if not self.kind.matches(value):
|
||||||
|
|
@ -83,6 +84,16 @@ class TreeLoader:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self._options: Dict[str, Any] = {}
|
self._options: Dict[str, Any] = {}
|
||||||
|
|
||||||
|
def add(self,
|
||||||
|
name: str,
|
||||||
|
kind: Kind,
|
||||||
|
default: Any,
|
||||||
|
*conditions: Tuple[Condition, str],
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
option = Option(kind, default, conditions)
|
||||||
|
self.add_option(name, option)
|
||||||
|
|
||||||
def add_option(self, name: str, option: Option) -> None:
|
def add_option(self, name: str, option: Option) -> None:
|
||||||
self._options[name] = option
|
self._options[name] = option
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue