draft: rewrite of interface
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
import re
|
||||
from collections import defaultdict
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
from typing import (Any, Callable, Dict, Generator, Generic, Hashable, List,
|
||||
Optional, Tuple, TypeVar, Union)
|
||||
@@ -26,7 +28,6 @@ class HumanIO:
|
||||
def not_found(key: Any) -> None:
|
||||
return None
|
||||
|
||||
|
||||
class Option(Generic[P]):
|
||||
"""
|
||||
This could represent a data object, a string or a page.
|
||||
@@ -166,16 +167,59 @@ class Select(Generic[P]):
|
||||
return "\n".join(str(option) for option in self)
|
||||
|
||||
|
||||
class OptionGroup(Option[P], Select[P]):
|
||||
ALPHABET: str = "abcdefghijklmnopqrstuvwxyz"
|
||||
ATTRIBUTES_FORMATTING: Tuple[str, ...] = ("alphabetic_index", "value")
|
||||
class Node(Generator[P]):
|
||||
def __init__(
|
||||
self,
|
||||
value: Optional[P] = None,
|
||||
children: List[Node[P]] = None,
|
||||
parent: Node[P] = None,
|
||||
**kwargs
|
||||
):
|
||||
self.value = value
|
||||
self.depth = 0
|
||||
self.same_level_index: int = 0
|
||||
|
||||
TEXT_TEMPLATE: str = f"{BColors.HEADER.value}{{alphabetic_index}}) {{value}}{BColors.ENDC.value}"
|
||||
|
||||
self.children: List[Node[P]] = kwargs.get("children", [])
|
||||
self.parent: Optional[Node[P]] = kwargs.get("parent", None)
|
||||
|
||||
super(Node, self).__init__(**kwargs)
|
||||
|
||||
def hash_key(self, key: Any) -> int:
|
||||
try:
|
||||
key = int(key)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
if isinstance(key, str):
|
||||
return hash(unify(key))
|
||||
|
||||
return hash(key)
|
||||
|
||||
@property
|
||||
def alphabetic_index(self) -> str:
|
||||
return self.ALPHABET[self.index % len(self.ALPHABET)]
|
||||
def is_root(self) -> bool:
|
||||
return self.parent is None
|
||||
|
||||
def __init__(self, value: P, data: Generator[P, None, None] **kwargs):
|
||||
super(OptionGroup, self).__init__(value=value, data=data, **kwargs)
|
||||
|
||||
@property
|
||||
def is_leaf(self) -> bool:
|
||||
return not self.children
|
||||
|
||||
def __iter__(self, **kwargs) -> Generator[Node[P], None, None]:
|
||||
_level_index_map: Dict[int, int] = kwargs.get("level_index_map", defaultdict(lambda: 0))
|
||||
|
||||
self.same_level_index = _level_index_map[self.depth]
|
||||
yield self
|
||||
_level_index_map[self.depth] += 1
|
||||
|
||||
for child in self.children:
|
||||
child.depth = self.depth + 1
|
||||
|
||||
for node in child.__iter__(level_index_map=_level_index_map):
|
||||
yield node
|
||||
|
||||
def __getitem__(self, key: Any) -> Option[P]:
|
||||
pass
|
||||
|
||||
def __contains__(self, key: Any) -> bool:
|
||||
if key in self.option:
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user