Skip to content

Commit

Permalink
Refactor to improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GDYendell committed Jun 17, 2024
1 parent 1f525d6 commit 91ba168
Show file tree
Hide file tree
Showing 6 changed files with 499 additions and 31 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dev = [
"pre-commit",
"pydata-sphinx-theme>=0.12",
"pytest",
"pytest-asyncio",
"pytest-cov",
"ruff",
"sphinx-autobuild",
Expand Down
59 changes: 31 additions & 28 deletions src/odin_fastcs/odin_controller.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import logging
from collections.abc import Sequence
from dataclasses import dataclass
from typing import Any

Expand Down Expand Up @@ -87,11 +88,21 @@ def __init__(
self._parameters = parameters
self._api_prefix = api_prefix

def create_attributes(self):
"""Create ``Attributes`` from Odin server parameter tree."""
parameters = self.process_odin_parameters(self._parameters)
async def initialise(self):
self._process_parameters()
self._create_attributes()

def _process_parameters(self):
"""Hook to process ``OdinParameters`` before creating ``Attributes``.
For example, renaming or removing a section of the parameter path.
for parameter in parameters:
"""
pass

Check warning on line 101 in src/odin_fastcs/odin_controller.py

View check run for this annotation

Codecov / codecov/patch

src/odin_fastcs/odin_controller.py#L101

Added line #L101 was not covered by tests

def _create_attributes(self):
"""Create controller ``Attributes`` from ``OdinParameters``."""
for parameter in self._parameters:
if "writeable" in parameter.metadata and parameter.metadata["writeable"]:
attr_class = AttrRW
else:
Expand Down Expand Up @@ -123,18 +134,6 @@ def create_attributes(self):

setattr(self, parameter.name.replace(".", ""), attr)

async def initialise(self):
pass

def process_odin_parameters(
self, parameters: list[OdinParameter]
) -> list[OdinParameter]:
"""Hook for child classes to process parameters before creating attributes."""
return parameters

def create_odin_parameters(self):
return create_odin_parameters(self._parameter_tree)


class OdinController(Controller):
"""A root ``Controller`` for an Odin control server."""
Expand Down Expand Up @@ -176,7 +175,6 @@ async def initialise(self) -> None:
self._connection, create_odin_parameters(response), adapter
)
await adapter_controller.initialise()
adapter_controller.create_attributes()
self.register_sub_controller(adapter_controller)

Check warning on line 178 in src/odin_fastcs/odin_controller.py

View check run for this annotation

Codecov / codecov/patch

src/odin_fastcs/odin_controller.py#L177-L178

Added lines #L177 - L178 were not covered by tests

await self._connection.close()
Expand Down Expand Up @@ -226,7 +224,6 @@ async def initialise(self):
[f"FP{idx}"],
)
await adapter_controller.initialise()
adapter_controller.create_attributes()
self.register_sub_controller(adapter_controller)

Check warning on line 227 in src/odin_fastcs/odin_controller.py

View check run for this annotation

Codecov / codecov/patch

src/odin_fastcs/odin_controller.py#L226-L227

Added lines #L226 - L227 were not covered by tests


Expand All @@ -245,38 +242,44 @@ async def initialise(self):
f"Did not find valid plugins in response:\n{plugins_response}"
)

self._process_parameters()
await self._create_plugin_sub_controllers(plugins)
self._create_attributes()

Check warning on line 247 in src/odin_fastcs/odin_controller.py

View check run for this annotation

Codecov / codecov/patch

src/odin_fastcs/odin_controller.py#L245-L247

Added lines #L245 - L247 were not covered by tests

def _process_parameters(self):
for parameter in self._parameters:
# Remove duplicate index from uri
parameter.uri = parameter.uri[1:]
# Remove redundant status/config from parameter path
parameter.set_path(parameter.uri[1:])

async def _create_plugin_sub_controllers(self, plugins: Sequence[str]):
for plugin in plugins:

def __parameter_in_plugin(
parameter: OdinParameter, plugin: str = plugin
) -> bool:
return parameter.path[0] == plugin

plugin_parameters, self._parameters = partition(
self._parameters, lambda p, plugin=plugin: p.path[0] == plugin
self._parameters, __parameter_in_plugin
)
plugin_controller = OdinFPPluginController(
self._connection,
plugin_parameters,
f"{self._api_prefix}",
self.path + [plugin],
)
plugin_controller.create_attributes()
await plugin_controller.initialise()
self.register_sub_controller(plugin_controller)


class OdinFPPluginController(OdinSubController):
# TODO: Just use initialise?
def process_odin_parameters(
self, parameters: list[OdinParameter]
) -> list[OdinParameter]:
for parameter in parameters:
def _process_parameters(self):
for parameter in self._parameters:
# Remove plugin name included in controller base path
parameter.set_path(parameter.path[1:])

# TODO: Make a copy?
return parameters


class FROdinController(OdinSubController):
def __init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,4 @@
}
}
}
}
}
Loading

0 comments on commit 91ba168

Please sign in to comment.