WIP: feature/cleanup_programming_interface #40

Draft
Hazel wants to merge 35 commits from feature/cleanup_programming_interface into experimental
Showing only changes of commit d4fe99ffc7 - Show all commits

View File

@ -36,11 +36,13 @@ class Option:
keys: List[Any] = None,
hidden: bool = False,
parse_key: Callable[[Any], Any] = lambda x: x,
index: int = None,
):
self._parse_key: Callable[[Any], Any] = parse_key
self._index = index
self.value = value
self.text = text or str(value)
self._text = text or str(value)
self.hidden = hidden
self._raw_keys = set(keys or [])
@ -50,7 +52,28 @@ class Option:
except TypeError:
pass
self._raw_keys.add(str(self.value))
self._raw_keys.add(self._index)
self.keys = set(self.parse_key(key) for key in self._raw_keys)
@property
def text(self) -> str:
return self._text.replace("{index}", str(self.index))
@text.setter
def text(self, value: str):
self._text = value
@property
def index(self) -> int:
return self._index
@index.setter
def index(self, value: int):
p = self._parse_key(self._index)
if p in self.keys:
self.keys.remove(p)
self._index = value
self.keys.add(p)
def register_key(self, key: Any):
self._raw_keys.add(key)
@ -110,6 +133,12 @@ class Select:
for key in option.keys:
self._key_to_option[key] = option
def _remap(self):
self._key_to_option = dict()
for option in self._options:
for key in option.keys:
self._key_to_option[key] = option
def extend(self, options: List[Option]):
for option in options:
self.append(option)
@ -126,7 +155,8 @@ class Select:
def __getitem__(self, key: Any) -> Option:
r = self._key_to_option[self._parse_option_key(key)]
# check if is callable
if callable(r):
r = r()
if callable(r.value):
r.value = r.value()
return r
@ -223,7 +253,8 @@ class DataObjectSelect(Select):
return Option(
value=data_object,
keys=[index, data_object.option_string, data_object.title_string],
text=f"{BColors.BOLD.value}{index: >2}{BColors.ENDC.value}: {data_object.option_string}",
text=f"{BColors.BOLD.value}{{index}}{BColors.ENDC.value}: {data_object.option_string}",
index=index,
)
def option_from_source_type(self, source_type: SourceType) -> Option:
@ -256,6 +287,7 @@ class DataObjectSelect(Select):
source_types = list(sorted(self._source_type_to_data_objects.keys(), key=lambda x: x.name))
single_source = len(source_types) > 1
j = 0
for st in source_types:
if single_source:
yield self._source_type_to_option[st]
@ -263,4 +295,9 @@ class DataObjectSelect(Select):
limit = min(15, len(self._source_type_to_data_objects[st])) if single_source else len(self._source_type_to_data_objects[st])
for i in range(limit):
yield self._source_type_to_data_objects[st][i]
o = self._source_type_to_data_objects[st][i]
o.index = j
yield o
j += 1
self._remap()