Skip to content

Commit

Permalink
satisfied pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rayk committed Oct 19, 2023
1 parent d2b5c2c commit a27985f
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 117 deletions.
12 changes: 10 additions & 2 deletions src/labone/core/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,12 @@ async def get_with_expression(
for raw_result in response.result
]

async def subscribe(self, path: LabOneNodePath, value_packaging_function=None) -> DataQueue:
async def subscribe(
self,
path: LabOneNodePath,
value_packaging_function: t.Callable[[AnnotatedValue], AnnotatedValue]
| None = None,
) -> DataQueue:
"""Register a new subscription to a node.
Registers a new subscription to a node on the kernel/server. All
Expand All @@ -640,6 +645,9 @@ async def subscribe(self, path: LabOneNodePath, value_packaging_function=None) -
Args:
path: String representing the path of the node to be streamed.
Currently does not support wildcards in the path.
value_packaging_function: Function to bring values obtained from
data-queue into desired format. This may involve parsing
them or putting them into an enum.
Returns:
An instance of the DataQueue class. This async queue will receive
Expand Down Expand Up @@ -671,5 +679,5 @@ async def subscribe(self, path: LabOneNodePath, value_packaging_function=None) -
return DataQueue(
path=path,
register_function=streaming_handle.register_data_queue,
value_packaging_function=value_packaging_function
value_packaging_function=value_packaging_function,
)
40 changes: 23 additions & 17 deletions src/labone/core/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,23 @@ class DataQueue(asyncio.Queue):
"""

def __init__(
self,
*,
path: LabOneNodePath,
register_function: t.Callable[[weakref.ReferenceType[DataQueue]], None],
value_packaging_function: t.Callable[
['AnnotatedValue'], 'AnnotatedValue'] | None = None
self,
*,
path: LabOneNodePath,
register_function: t.Callable[[weakref.ReferenceType[DataQueue]], None],
value_packaging_function: t.Callable[[AnnotatedValue], AnnotatedValue]
| None = None,
) -> None:
super().__init__()
self._path = path
self._connection_state = _ConnectionState()
self._register_function = register_function

if value_packaging_function is None:
value_packaging_function = lambda x: x

def value_packaging_function(x: AnnotatedValue) -> AnnotatedValue:
return x

self._value_packaging_function = value_packaging_function
register_function(weakref.ref(self))

Expand Down Expand Up @@ -127,8 +130,11 @@ def fork(self) -> DataQueue:
"sense as it would never receive data.",
)
raise errors.StreamingError(msg)
return DataQueue(path=self._path, register_function=self._register_function,
value_packaging_function=self._value_packaging_function)
return DataQueue(
path=self._path,
register_function=self._register_function,
value_packaging_function=self._value_packaging_function,
)

def disconnect(self) -> None:
"""Disconnect the data queue.
Expand Down Expand Up @@ -244,9 +250,9 @@ def register_data_queue(self, data_queue: weakref.ReferenceType[DataQueue]) -> N
self._data_queues.append(data_queue)

def _add_to_data_queue(
self,
data_queue: DataQueue | None,
value: AnnotatedValue,
self,
data_queue: DataQueue | None,
value: AnnotatedValue,
) -> bool:
"""Add a value to the data queue.
Expand Down Expand Up @@ -279,8 +285,8 @@ def _add_to_data_queue(
return True

def _distribute_to_data_queues(
self,
value: session_protocol_capnp.AnnotatedValue,
self,
value: session_protocol_capnp.AnnotatedValue,
) -> None:
"""Add a value to all data queues.
Expand Down Expand Up @@ -312,9 +318,9 @@ def _distribute_to_data_queues(
)

async def sendValues( # noqa: N802 (function name is enforced through the schema)
self,
values: list[session_protocol_capnp.AnnotatedValue],
**_,
self,
values: list[session_protocol_capnp.AnnotatedValue],
**_,
) -> None:
"""Capnp Interface callback.
Expand Down
5 changes: 5 additions & 0 deletions src/labone/nodetree/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"""Subpackage providing the pythonic node-tree.
This subpackage delivers tree-like classes to navigate node-paths efficiently.
These nodes allow communication to server to get or set values accordingly.
"""
from labone.nodetree.node import Node, ResultNode, construct_nodetree

__all__ = ["construct_nodetree", "Node", "ResultNode"]
7 changes: 4 additions & 3 deletions src/labone/nodetree/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class NestedDict(t.Protocol[T]): # type: ignore[misc]
def __getitem__(self, key: str) -> T | NestedDict[T]:
...

#retyping dict method, because inheriting from non-protocal is prohibited
# retyping dict method, because inheriting from non-protocal is prohibited
def keys(self) -> t.KeysView[str]:
"""..."""
...
Expand Down Expand Up @@ -53,7 +53,8 @@ def __getitem__(self, _: object) -> UndefinedStructure:


def nested_dict_access(
key_chain: t.Iterable, nested_dict: NestedDict[T],
key_chain: t.Iterable,
nested_dict: NestedDict[T],
) -> NestedDict[T] | T:
"""Recursively going deeper in a nested dictionary.
Expand Down Expand Up @@ -101,7 +102,7 @@ def split_path(path: LabOneNodePath) -> list[NormalizedPathSegment]:
"""
path_segments = path.split(PATH_SEPERATOR)
first_item_index = 0
if path_segments[0] == "":
if path_segments[0] == "": #
# this happens if the path started with '/'
# ignore leading '/'
first_item_index = 1
Expand Down
10 changes: 5 additions & 5 deletions src/labone/nodetree/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ def __init__(
]

self._first_layer: NestedDict[
list[list[NormalizedPathSegment]] | dict] \
= build_prefix_dict(self._paths_as_segments)
list[list[NormalizedPathSegment]] | dict
] = build_prefix_dict(self._paths_as_segments)
self.structure_info: TreeProp[NodeInfoType] = TreeProp(self.path_to_info)

@classmethod
Expand Down Expand Up @@ -299,7 +299,7 @@ def __init__(self, node: LeafNode) -> None:
def __getattr__(
self,
item: str,
) -> (LabOneNodePath | str | NodeType | dict[str, str] | None):
) -> LabOneNodePath | str | NodeType | dict[str, str] | None:
return self._info[item] # type: ignore[literal-required]

def __contains__(self, item: str) -> bool:
Expand Down Expand Up @@ -634,7 +634,8 @@ def is_immediate_child_node(
Boolean if passed node is a child node
"""
path_segments = (
child_node.path_segments if isinstance(child_node, MetaNode)
child_node.path_segments
if isinstance(child_node, MetaNode)
else tuple(child_node)
)
return (
Expand Down Expand Up @@ -1362,7 +1363,6 @@ def _try_generate_subnode(
path_aliases=self._path_aliases,
)
except KeyError as e:

# wildcards are always legal
if next_path_segment == WILDCARD:
return self._tree_manager.path_segments_to_node(
Expand Down
6 changes: 4 additions & 2 deletions tests/nodetree/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ def test_build_structure(input_value, expected_output):
def test_result_nodes():
raw_response = [
AnnotatedValue(
value="abc", path="/zi/mds/xyz", timestamp=1696408914, extra_header=None,
value="abc",
path="/zi/mds/xyz",
timestamp=1696408914,
extra_header=None,
),
AnnotatedValue(
value=0,
Expand Down Expand Up @@ -87,4 +90,3 @@ def test_result_nodes_further():

for i in range(len(result.a.b)):
assert expected[i] == result.a.b[i]

11 changes: 7 additions & 4 deletions tests/nodetree/test_node2.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
example_info = {
"Node": "/ZI/DEBUG/LEVEL",
"Description": "Set the logging level (amount of detail) of the"
" LabOne Data Server.",
" LabOne Data Server.",
"Properties": "Read, Write, Setting",
"Type": "Integer (enumerated)",
"Unit": "None",
Expand Down Expand Up @@ -103,7 +103,7 @@ def get_result_node():
def test_tree_prop():
prop = TreeProp({"/alphabet/a": 1, "/alphabet/b": 2})
assert prop.get_with_path("/alphabet/a") == 1
assert prop.get_with_segments(("alphabet","b")) == 2
assert prop.get_with_segments(("alphabet", "b")) == 2


def test_tree_prop2():
Expand All @@ -128,6 +128,7 @@ def test_path_to_node():

assert node == zi.config[0]


def test_path_segments_to_node():
zi = get_serverless_tree()
manager = zi.tree_manager
Expand All @@ -150,10 +151,12 @@ def test_resolve_wildcard():
def test_nested_dict_access():
assert demo_structure == nested_dict_access((), demo_structure)
assert demo_structure["zi"]["about"] == nested_dict_access(
("zi", "about"), demo_structure,
("zi", "about"),
demo_structure,
)
assert demo_structure["zi"]["about"]["devices"]["all"] == nested_dict_access(
("zi", "about", "devices", "all"), demo_structure,
("zi", "about", "devices", "all"),
demo_structure,
)


Expand Down
11 changes: 7 additions & 4 deletions tests/nodetree/test_node_with_dataserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ async def kj_loop():

async def establish_connection_get_tree():
session = await KernelSession.create(
kernel_info=ZIKernelInfo(), server_info=ServerInfo(host="localhost", port=8004),
kernel_info=ZIKernelInfo(),
server_info=ServerInfo(host="localhost", port=8004),
)

return await construct_nodetree(session, hide_kernel_prefix=True)
Expand All @@ -40,8 +41,10 @@ def dataserver_available():
return True


only_with_dataserver = pytest.mark.skipif(not dataserver_available(),
reason="no dataserver available")
only_with_dataserver = pytest.mark.skipif(
not dataserver_available(),
reason="no dataserver available",
)


@only_with_dataserver
Expand Down Expand Up @@ -85,7 +88,7 @@ async def test_wait_for_state_change_wildcard():
async def test_subscribe():
zi = await establish_connection_get_tree()
node = zi.debug.level
await node(1) # set initial condition
await node(1) # set initial condition

queue_enum: DataQueue = await node.subscribe(enum=True)
queue_plain = await node.subscribe(enum=False)
Expand Down
80 changes: 0 additions & 80 deletions tests/nodetree/test_setup.py

This file was deleted.

0 comments on commit a27985f

Please sign in to comment.