-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IWF-357: Add internal channel TypeStore
- Loading branch information
1 parent
b810adb
commit 7c94d6b
Showing
4 changed files
with
84 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from typing import Optional | ||
from enum import Enum | ||
|
||
from iwf.communication_schema import CommunicationMethod | ||
from iwf.errors import WorkflowDefinitionError | ||
|
||
|
||
class Type(Enum): | ||
INTERNAL_CHANNEL = 1 | ||
# TODO: extend to other types | ||
# DATA_ATTRIBUTE = 2 | ||
# SIGNAL_CHANNEL = 3 | ||
|
||
class TypeStore: | ||
_class_type: Type | ||
_name_to_type_store: dict[str, Optional[type]] | ||
_prefix_to_type_store: dict[str, Optional[type]] | ||
|
||
def __init__(self, class_type: Type): | ||
self._class_type = class_type | ||
self._name_to_type_store = dict() | ||
self._prefix_to_type_store = dict() | ||
|
||
def is_valid_name_or_prefix(self, name: str) -> bool: | ||
t = self._do_get_type(name) | ||
return t is not None | ||
|
||
def get_type(self, name: str) -> Optional[type]: | ||
t = self._do_get_type(name) | ||
|
||
if t is None: | ||
raise ValueError(f"{self._class_type} not registered: {name}") | ||
|
||
return type | ||
|
||
def add_internal_channel_def(self, obj: CommunicationMethod): | ||
if self._class_type != Type.INTERNAL_CHANNEL: | ||
raise WorkflowDefinitionError( | ||
f"Cannot add internal channel definition to {self._class_type}" | ||
) | ||
self._do_add_to_store(obj.is_prefix, obj.name, obj.value_type) | ||
|
||
|
||
def _do_get_type(self, name: str) -> Optional[type]: | ||
if name in self._name_to_type_store: | ||
return self._name_to_type_store[name] | ||
|
||
prefixes = self._prefix_to_type_store.keys() | ||
|
||
first = next((prefix for prefix in prefixes if prefix.startswith(name)), None) | ||
|
||
if first is None: | ||
return None | ||
|
||
return self._prefix_to_type_store.get(first, None) | ||
|
||
def _do_add_to_store(self, is_prefix: bool, name: str, t: Optional[type]): | ||
if is_prefix: | ||
store = self._prefix_to_type_store | ||
else: | ||
store = self._name_to_type_store | ||
|
||
if name in store: | ||
raise WorkflowDefinitionError( | ||
f"{self._class_type} name/prefix {name} already exists") | ||
|
||
store[name] = t | ||
|