mirror of
https://github.com/tiqi-group/pydase.git
synced 2025-12-18 12:11:20 +01:00
Compare commits
233 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bcabd2dc48 | ||
|
|
7ac9c557c2 | ||
|
|
656529d1fb | ||
|
|
14601105a7 | ||
|
|
484b5131e9 | ||
|
|
616a5cea21 | ||
|
|
300bd6ca9a | ||
|
|
3e1517e905 | ||
|
|
0ecaeac3fb | ||
|
|
0e9832e2f1 | ||
|
|
0343abd0b0 | ||
|
|
0c149b85b5 | ||
|
|
0e331e58ff | ||
|
|
45135927e6 | ||
|
|
d3866010a8 | ||
|
|
3c0f019af8 | ||
|
|
8aa7fd31f8 | ||
|
|
c9ff3db9e9 | ||
|
|
9e77bae5e7 | ||
|
|
6a6d1b27aa | ||
|
|
2d3e7d8c1b | ||
|
|
c7b039beb7 | ||
|
|
62e647c667 | ||
|
|
6382be5735 | ||
|
|
ea158bf8de | ||
|
|
63ad6d7b93 | ||
|
|
b8e758e479 | ||
|
|
a12a708385 | ||
|
|
edb24f5439 | ||
|
|
2a2b7b800d | ||
|
|
b6b20c21e4 | ||
|
|
53be794a3c | ||
|
|
a303ba7f0b | ||
|
|
2461f85ef0 | ||
|
|
ca41e12014 | ||
|
|
f69723dd58 | ||
|
|
c733026522 | ||
|
|
316ce5c7e7 | ||
|
|
43c3f746fa | ||
|
|
fea96c044c | ||
|
|
6543bc6b39 | ||
|
|
ef36c01407 | ||
|
|
9d90fd2b81 | ||
|
|
9fc6d6f910 | ||
|
|
805e270107 | ||
|
|
8e3a1694ce | ||
|
|
32a1d14a40 | ||
|
|
8940a61d4e | ||
|
|
393bde3280 | ||
|
|
eb2da1c5dc | ||
|
|
e7b73a99da | ||
|
|
392831e0fd | ||
|
|
32bda8d910 | ||
|
|
e106cc4927 | ||
|
|
464478cda9 | ||
|
|
97c026afe0 | ||
|
|
2f5c415cd5 | ||
|
|
728eea09f6 | ||
|
|
e3eaf5ffe2 | ||
|
|
1dc3b62060 | ||
|
|
8214faf5cb | ||
|
|
232eb53249 | ||
|
|
439f514ea5 | ||
|
|
c7d63f5139 | ||
|
|
f64b5c35ab | ||
|
|
bb4de988e9 | ||
|
|
36a8e916f6 | ||
|
|
1a00f37372 | ||
|
|
6630173cec | ||
|
|
08a62b2119 | ||
|
|
37ae34ecc0 | ||
|
|
8b78099178 | ||
|
|
3186e04cc1 | ||
|
|
055acbe591 | ||
|
|
0d08c2ce0d | ||
|
|
68cc5b693e | ||
|
|
4fcd5b4d44 | ||
|
|
9cbc639d0f | ||
|
|
a48cce32e4 | ||
|
|
8c24f5dd67 | ||
|
|
1c4a878aa8 | ||
|
|
31967d0d43 | ||
|
|
b4edc31030 | ||
|
|
ff7c92547e | ||
|
|
fab91f3221 | ||
|
|
bd77995d96 | ||
|
|
729f375901 | ||
|
|
e643dd6f5c | ||
|
|
53f4cf6690 | ||
|
|
c0c8591fc4 | ||
|
|
13fba6d3d6 | ||
|
|
dc4c9ff58f | ||
|
|
83cd07feee | ||
|
|
09f73a2b1d | ||
|
|
88886e3fd6 | ||
|
|
49984b7c2e | ||
|
|
39270561b9 | ||
|
|
8ac2c39908 | ||
|
|
0694a3d1ee | ||
|
|
c15ad54e2d | ||
|
|
71721b1286 | ||
|
|
74ceb7f05c | ||
|
|
06d11fff49 | ||
|
|
6d23151d32 | ||
|
|
0faf347376 | ||
|
|
a5fddf7e45 | ||
|
|
83c763bd20 | ||
|
|
9778541ee4 | ||
|
|
8e641c1b84 | ||
|
|
f6bf229c8c | ||
|
|
5a76d76d2b | ||
|
|
3169531a24 | ||
|
|
4bd0092fbf | ||
|
|
569e343e89 | ||
|
|
f2b2ef8dcd | ||
|
|
f70ac05df6 | ||
|
|
e3367efda1 | ||
|
|
3d2de7109b | ||
|
|
534ff4c149 | ||
|
|
0e47f6c4d3 | ||
|
|
b4ef8201f3 | ||
|
|
a97a55712e | ||
|
|
e8a0a7c000 | ||
|
|
6f0d43aa5a | ||
|
|
0e210b8ba6 | ||
|
|
329e0acd81 | ||
|
|
f97cd7eb4e | ||
|
|
3c168243bb | ||
|
|
0944a404dc | ||
|
|
a9c6070ca3 | ||
|
|
75ee71cbf8 | ||
|
|
1e55a4d914 | ||
|
|
aab2b4ee77 | ||
|
|
52d571e551 | ||
|
|
bb415af460 | ||
|
|
c3c1669cf9 | ||
|
|
5378396958 | ||
|
|
b66e964155 | ||
|
|
4fc25c6752 | ||
|
|
44cd9597cb | ||
|
|
e48a7067ec | ||
|
|
8919f6106a | ||
|
|
89b5a9cc9e | ||
|
|
0aa1595da4 | ||
|
|
8f8b3e3bcf | ||
|
|
43e6adcb2e | ||
|
|
3992f491c9 | ||
|
|
df571a8260 | ||
|
|
53713794d6 | ||
|
|
06e642972f | ||
|
|
a7ec7c1536 | ||
|
|
c891642bda | ||
|
|
cc105106ee | ||
|
|
7c7bb193e4 | ||
|
|
92e79579ff | ||
|
|
5d2d34bea3 | ||
|
|
3497962fca | ||
|
|
114a1c6fdc | ||
|
|
1d2ac57ba7 | ||
|
|
99dea381a3 | ||
|
|
e6e5ac84b4 | ||
|
|
246148f513 | ||
|
|
eb0c819037 | ||
|
|
f5d8775141 | ||
|
|
1ec034a62e | ||
|
|
93f0627534 | ||
|
|
ad2ae704e9 | ||
|
|
de5340d6fd | ||
|
|
b80a3ec6a1 | ||
|
|
f3853ef836 | ||
|
|
56ae9086b5 | ||
|
|
5a2371353a | ||
|
|
09a55f50bd | ||
|
|
abafd1a2b2 | ||
|
|
145ff89072 | ||
|
|
ba5b4e7be4 | ||
|
|
8ee49469d6 | ||
|
|
6997c4a842 | ||
|
|
598449e893 | ||
|
|
4802f19720 | ||
|
|
a04bd14e50 | ||
|
|
c60730f21b | ||
|
|
d5cd97ea57 | ||
|
|
0136885207 | ||
|
|
c04e048e21 | ||
|
|
9e9d3f17bc | ||
|
|
e576f6eb80 | ||
|
|
e57fe10c9e | ||
|
|
f27f513bf8 | ||
|
|
de4e4ed178 | ||
|
|
cb2687a4b9 | ||
|
|
ab794d780b | ||
|
|
617eed4d96 | ||
|
|
d517bd0489 | ||
|
|
d0869b707b | ||
|
|
eab99df9d1 | ||
|
|
9d36f99404 | ||
|
|
7b7ef0eb97 | ||
|
|
92f14c6788 | ||
|
|
4746470aee | ||
|
|
f5627e6a2f | ||
|
|
a769f4eb3b | ||
|
|
3970d5a17b | ||
|
|
a89db46d5e | ||
|
|
f67591c7ac | ||
|
|
fdcaa1c1ed | ||
|
|
613b1dd6a4 | ||
|
|
914997cc6b | ||
|
|
667bb949cc | ||
|
|
acaac6f0a6 | ||
|
|
e9df89765d | ||
|
|
123edb9e86 | ||
|
|
69328d6f68 | ||
|
|
0cd3a7e8a8 | ||
|
|
abd77e053d | ||
|
|
ebb8b4be8b | ||
|
|
a83e0c6c7f | ||
|
|
64dc09faf7 | ||
|
|
e2fb9ebae5 | ||
|
|
4a43bda5e2 | ||
|
|
f693fa9ba2 | ||
|
|
9820bda4b5 | ||
|
|
f5116607b9 | ||
|
|
0ea997384c | ||
|
|
28410a97f5 | ||
|
|
f6eef7085e | ||
|
|
a76035f443 | ||
|
|
2ab4d1c00a | ||
|
|
a9d577820f | ||
|
|
f5e6dca16a | ||
|
|
4a45d0d438 | ||
|
|
3cc6399f60 | ||
|
|
dc1c7e80f4 |
8
.flake8
8
.flake8
@@ -1,8 +0,0 @@
|
||||
[flake8]
|
||||
ignore = E501,W503,FS003,F403,F405,E203
|
||||
include = src
|
||||
max-line-length = 88
|
||||
max-doc-length = 88
|
||||
max-complexity = 7
|
||||
max-expression-complexity = 7
|
||||
use_class_attributes_order_strict_mode=True
|
||||
25
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
25
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
---
|
||||
name: Bug report
|
||||
about: Create a report to help us improve
|
||||
title: ''
|
||||
labels: 'bug'
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
## Describe the bug
|
||||
A clear and concise description of what the bug is.
|
||||
|
||||
## To Reproduce
|
||||
Provide steps to reproduce the behaviour, including a minimal code snippet (if applicable):
|
||||
```python
|
||||
# Minimal code snippet that reproduces the error
|
||||
```
|
||||
## Expected behaviour
|
||||
A clear and concise description of what you expected to happen.
|
||||
|
||||
## Screenshot/Video
|
||||
If applicable, add visual content that helps explain your problem.
|
||||
|
||||
## Additional context
|
||||
Add any other context about the problem here.
|
||||
14
.github/workflows/python-package.yml
vendored
14
.github/workflows/python-package.yml
vendored
@@ -20,6 +20,9 @@ jobs:
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: chartboost/ruff-action@v1
|
||||
with:
|
||||
src: "./src"
|
||||
- name: Set up Python ${{ matrix.python-version }}
|
||||
uses: actions/setup-python@v3
|
||||
with:
|
||||
@@ -28,14 +31,13 @@ jobs:
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
python -m pip install poetry
|
||||
poetry install
|
||||
- name: Lint with flake8
|
||||
run: |
|
||||
# stop the build if there are Python syntax errors or undefined names
|
||||
poetry run flake8 src/pydase --count --show-source --statistics
|
||||
poetry install --with dev
|
||||
- name: Test with pytest
|
||||
run: |
|
||||
poetry run pytest
|
||||
- name: Test with pyright
|
||||
run: |
|
||||
poetry run pyright src/pydase
|
||||
poetry run pyright
|
||||
- name: Test with mypy
|
||||
run: |
|
||||
poetry run mypy src
|
||||
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -128,6 +128,9 @@ venv.bak/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# ruff
|
||||
.ruff_cache/
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
||||
|
||||
|
||||
8
.vscode/extensions.json
vendored
Normal file
8
.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"recommendations": [
|
||||
"charliermarsh.ruff",
|
||||
"ms-python.python",
|
||||
"ms-python.vscode-pylance",
|
||||
"ms-python.mypy-type-checker"
|
||||
]
|
||||
}
|
||||
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
@@ -6,7 +6,7 @@
|
||||
"type": "python",
|
||||
"request": "launch",
|
||||
"module": "foo",
|
||||
"justMyCode": true,
|
||||
"justMyCode": false,
|
||||
"env": {
|
||||
"ENVIRONMENT": "development"
|
||||
}
|
||||
|
||||
31
.vscode/settings.json
vendored
31
.vscode/settings.json
vendored
@@ -1,25 +1,15 @@
|
||||
{
|
||||
"autoDocstring.docstringFormat": "google",
|
||||
"autoDocstring.startOnNewLine": true,
|
||||
"autoDocstring.generateDocstringOnEnter": true,
|
||||
"editor.formatOnSave": true,
|
||||
"editor.codeActionsOnSave": {
|
||||
"source.fixAll.eslint": true
|
||||
},
|
||||
"editor.rulers": [
|
||||
88
|
||||
],
|
||||
"python.defaultInterpreterPath": ".venv/bin/python",
|
||||
"python.formatting.provider": "black",
|
||||
"python.linting.lintOnSave": true,
|
||||
"python.linting.enabled": true,
|
||||
"python.linting.flake8Enabled": true,
|
||||
"python.linting.mypyEnabled": true,
|
||||
"[python]": {
|
||||
"editor.defaultFormatter": "charliermarsh.ruff",
|
||||
"editor.rulers": [
|
||||
88
|
||||
],
|
||||
"editor.tabSize": 4,
|
||||
"editor.detectIndentation": false,
|
||||
"editor.codeActionsOnSave": {
|
||||
"source.organizeImports": true
|
||||
"source.organizeImports": "explicit",
|
||||
"source.fixAll": "explicit"
|
||||
}
|
||||
},
|
||||
"[yaml]": {
|
||||
@@ -29,12 +19,11 @@
|
||||
"[typescript][javascript][vue][typescriptreact]": {
|
||||
"editor.tabSize": 2,
|
||||
"editor.defaultFormatter": "rvest.vs-code-prettier-eslint",
|
||||
"editor.formatOnPaste": false, // required
|
||||
"editor.formatOnType": false, // required
|
||||
"editor.formatOnSave": true, // optional
|
||||
"editor.formatOnSaveMode": "file", // required to format on save
|
||||
"editor.formatOnPaste": false,
|
||||
"editor.formatOnType": false,
|
||||
"editor.formatOnSaveMode": "file",
|
||||
"editor.codeActionsOnSave": {
|
||||
"source.fixAll.eslint": true
|
||||
"source.fixAll.eslint": "explicit"
|
||||
}
|
||||
}
|
||||
}
|
||||
316
README.md
316
README.md
@@ -21,11 +21,16 @@
|
||||
- [`NumberSlider`](#numberslider)
|
||||
- [`ColouredEnum`](#colouredenum)
|
||||
- [Extending with New Components](#extending-with-new-components)
|
||||
- [Customizing Web Interface Style](#customizing-web-interface-style)
|
||||
- [Understanding Service Persistence](#understanding-service-persistence)
|
||||
- [Controlling Property State Loading with `@load_state`](#controlling-property-state-loading-with-load_state)
|
||||
- [Understanding Tasks in pydase](#understanding-tasks-in-pydase)
|
||||
- [Understanding Units in pydase](#understanding-units-in-pydase)
|
||||
- [Changing the Log Level](#changing-the-log-level)
|
||||
- [Configuring pydase via Environment Variables](#configuring-pydase-via-environment-variables)
|
||||
- [Customizing the Web Interface](#customizing-the-web-interface)
|
||||
- [Enhancing the Web Interface Style with Custom CSS](#enhancing-the-web-interface-style-with-custom-css)
|
||||
- [Tailoring Frontend Component Layout](#tailoring-frontend-component-layout)
|
||||
- [Logging in pydase](#logging-in-pydase)
|
||||
- [Changing the Log Level](#changing-the-log-level)
|
||||
- [Documentation](#documentation)
|
||||
- [Contributing](#contributing)
|
||||
- [License](#license)
|
||||
@@ -41,8 +46,7 @@
|
||||
- [Saving and restoring the service state for service persistence](#understanding-service-persistence)
|
||||
- [Automated task management with built-in start/stop controls and optional autostart](#understanding-tasks-in-pydase)
|
||||
- [Support for units](#understanding-units-in-pydase)
|
||||
<!-- * Event-based callback functionality for real-time updates
|
||||
- Support for additional servers for specific use-cases -->
|
||||
<!-- Support for additional servers for specific use-cases -->
|
||||
|
||||
## Installation
|
||||
|
||||
@@ -285,26 +289,172 @@ if __name__ == "__main__":
|
||||
|
||||
#### `NumberSlider`
|
||||
|
||||
This component provides an interactive slider interface for adjusting numerical values on the frontend. It supports both floats and integers. The values adjusted on the frontend are synchronized with the backend in real-time, ensuring consistent data representation.
|
||||
The `NumberSlider` component in the `pydase` package provides an interactive slider interface for adjusting numerical values on the frontend. It is designed to support both numbers and quantities and ensures that values adjusted on the frontend are synchronized with the backend.
|
||||
|
||||
The slider can be customized with initial values, minimum and maximum limits, and step sizes to fit various use cases.
|
||||
To utilize the `NumberSlider`, users should implement a class that derives from `NumberSlider`. This class can then define the initial values, minimum and maximum limits, step sizes, and additional logic as needed.
|
||||
|
||||
Here's an example of how to implement and use a custom slider:
|
||||
|
||||
```python
|
||||
import pydase
|
||||
from pydase.components import NumberSlider
|
||||
import pydase.components
|
||||
|
||||
|
||||
class MySlider(pydase.components.NumberSlider):
|
||||
def __init__(
|
||||
self,
|
||||
value: float = 0.0,
|
||||
min_: float = 0.0,
|
||||
max_: float = 100.0,
|
||||
step_size: float = 1.0,
|
||||
) -> None:
|
||||
super().__init__(value, min_, max_, step_size)
|
||||
|
||||
@property
|
||||
def min(self) -> float:
|
||||
return self._min
|
||||
|
||||
@min.setter
|
||||
def min(self, value: float) -> None:
|
||||
self._min = value
|
||||
|
||||
@property
|
||||
def max(self) -> float:
|
||||
return self._max
|
||||
|
||||
@max.setter
|
||||
def max(self, value: float) -> None:
|
||||
self._max = value
|
||||
|
||||
@property
|
||||
def step_size(self) -> float:
|
||||
return self._step_size
|
||||
|
||||
@step_size.setter
|
||||
def step_size(self, value: float) -> None:
|
||||
self._step_size = value
|
||||
|
||||
@property
|
||||
def value(self) -> float:
|
||||
return self._value
|
||||
|
||||
@value.setter
|
||||
def value(self, value: float) -> None:
|
||||
if value < self._min or value > self._max:
|
||||
raise ValueError("Value is either below allowed min or above max value.")
|
||||
self._value = value
|
||||
|
||||
|
||||
class MyService(pydase.DataService):
|
||||
slider = NumberSlider(value=3.5, min=0, max=10, step_size=0.1, type="float")
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.voltage = MySlider()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
service = MyService()
|
||||
pydase.Server(service).run()
|
||||
service_instance = MyService()
|
||||
service_instance.voltage.value = 5
|
||||
print(service_instance.voltage.value) # Output: 5
|
||||
pydase.Server(service_instance).run()
|
||||
```
|
||||
|
||||
In this example, `MySlider` overrides the `min`, `max`, `step_size`, and `value` properties. Users can make any of these properties read-only by omitting the corresponding setter method.
|
||||
|
||||

|
||||
|
||||
- Accessing parent class resources in `NumberSlider`
|
||||
|
||||
In scenarios where you need the slider component to interact with or access resources from its parent class, you can achieve this by passing a callback function to it. This method avoids directly passing the entire parent class instance (`self`) and offers a more encapsulated approach. The callback function can be designed to utilize specific attributes or methods of the parent class, allowing the slider to perform actions or retrieve data in response to slider events.
|
||||
|
||||
Here's an illustrative example:
|
||||
|
||||
```python
|
||||
from collections.abc import Callable
|
||||
|
||||
import pydase
|
||||
import pydase.components
|
||||
|
||||
|
||||
class MySlider(pydase.components.NumberSlider):
|
||||
def __init__(
|
||||
self,
|
||||
value: float,
|
||||
on_change: Callable[[float], None],
|
||||
) -> None:
|
||||
super().__init__(value=value)
|
||||
self._on_change = on_change
|
||||
|
||||
# ... other properties ...
|
||||
|
||||
@property
|
||||
def value(self) -> float:
|
||||
return self._value
|
||||
|
||||
@value.setter
|
||||
def value(self, new_value: float) -> None:
|
||||
if new_value < self._min or new_value > self._max:
|
||||
raise ValueError("Value is either below allowed min or above max value.")
|
||||
self._value = new_value
|
||||
self._on_change(new_value)
|
||||
|
||||
|
||||
class MyService(pydase.DataService):
|
||||
def __init__(self) -> None:
|
||||
self.voltage = MySlider(
|
||||
5,
|
||||
on_change=self.handle_voltage_change,
|
||||
)
|
||||
|
||||
def handle_voltage_change(self, new_voltage: float) -> None:
|
||||
print(f"Voltage changed to: {new_voltage}")
|
||||
# Additional logic here
|
||||
|
||||
if __name__ == "__main__":
|
||||
service_instance = MyService()
|
||||
my_service.voltage.value = 7 # Output: "Voltage changed to: 7"
|
||||
pydase.Server(service_instance).run()
|
||||
```
|
||||
|
||||
- Incorporating units in `NumberSlider`
|
||||
|
||||
The `NumberSlider` is capable of displaying units alongside values, enhancing its usability in contexts where unit representation is crucial. When utilizing `pydase.units`, you can specify units for the slider's value, allowing the component to reflect these units in the frontend.
|
||||
|
||||
Here's how to implement a `NumberSlider` with unit display:
|
||||
|
||||
```python
|
||||
import pydase
|
||||
import pydase.components
|
||||
import pydase.units as u
|
||||
|
||||
class MySlider(pydase.components.NumberSlider):
|
||||
def __init__(
|
||||
self,
|
||||
value: u.Quantity = 0.0 * u.units.V,
|
||||
) -> None:
|
||||
super().__init__(value)
|
||||
|
||||
@property
|
||||
def value(self) -> u.Quantity:
|
||||
return self._value
|
||||
|
||||
@value.setter
|
||||
def value(self, value: u.Quantity) -> None:
|
||||
if value.m < self._min or value.m > self._max:
|
||||
raise ValueError("Value is either below allowed min or above max value.")
|
||||
self._value = value
|
||||
|
||||
class MyService(pydase.DataService):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.voltage = MySlider()
|
||||
|
||||
if __name__ == "__main__":
|
||||
service_instance = MyService()
|
||||
service_instance.voltage.value = 5 * u.units.V
|
||||
print(service_instance.voltage.value) # Output: 5 V
|
||||
pydase.Server(service_instance).run()
|
||||
```
|
||||
|
||||
#### `ColouredEnum`
|
||||
|
||||
This component provides a way to visually represent different states or categories in a data service using colour-coded options. It behaves similarly to a standard `Enum`, but the values encode colours in a format understood by CSS. The colours can be defined using various methods like Hexadecimal, RGB, HSL, and more.
|
||||
@@ -351,31 +501,6 @@ Users can also extend the library by creating custom components. This involves d
|
||||
|
||||
<!-- Component User Guide End -->
|
||||
|
||||
## Customizing Web Interface Style
|
||||
|
||||
`pydase` allows you to enhance the user experience by customizing the web interface's appearance. You can apply your own styles globally across the web interface by passing a custom CSS file to the server during initialization.
|
||||
|
||||
Here's how you can use this feature:
|
||||
|
||||
1. Prepare your custom CSS file with the desired styles.
|
||||
|
||||
2. When initializing your server, use the `css` parameter of the `Server` class to specify the path to your custom CSS file.
|
||||
|
||||
```python
|
||||
from pydase import Server, DataService
|
||||
|
||||
class Device(DataService):
|
||||
# ... your service definition ...
|
||||
|
||||
if __name__ == "__main__":
|
||||
service = MyService()
|
||||
server = Server(service, css="path/to/your/custom.css").run()
|
||||
```
|
||||
|
||||
This will apply the styles defined in `custom.css` to the web interface, allowing you to maintain branding consistency or improve visual accessibility.
|
||||
|
||||
Please ensure that the CSS file path is accessible from the server's running location. Relative or absolute paths can be used depending on your setup.
|
||||
|
||||
## Understanding Service Persistence
|
||||
|
||||
`pydase` allows you to easily persist the state of your service by saving it to a file. This is especially useful when you want to maintain the service's state across different runs.
|
||||
@@ -530,31 +655,118 @@ if __name__ == "__main__":
|
||||
|
||||
For more information about what you can do with the units, please consult the documentation of [`pint`](https://pint.readthedocs.io/en/stable/).
|
||||
|
||||
## Changing the Log Level
|
||||
## Configuring pydase via Environment Variables
|
||||
|
||||
You can change the log level of the logger by either
|
||||
Configuring `pydase` through environment variables enhances flexibility, security, and reusability. This approach allows for easy adaptation of services across different environments without code changes, promoting scalability and maintainability. With that, it simplifies deployment processes and facilitates centralized configuration management. Moreover, environment variables enable separation of configuration from code, aiding in secure and collaborative development.
|
||||
|
||||
1. (RECOMMENDED) setting the `ENVIRONMENT` environment variable to "production" or "development"
|
||||
`pydase` offers various configurable options:
|
||||
|
||||
- **`ENVIRONMENT`**: Sets the operation mode to either "development" or "production". Affects logging behaviour (see [logging section](#logging-in-pydase)).
|
||||
- **`SERVICE_CONFIG_DIR`**: Specifies the directory for service configuration files, like `web_settings.json`. This directory can also be used to hold user-defined configuration files. Default is the `config` folder in the service root folder. The variable can be accessed through:
|
||||
|
||||
```python
|
||||
import pydase.config
|
||||
pydase.config.ServiceConfig().config_dir
|
||||
```
|
||||
|
||||
- **`SERVICE_WEB_PORT`**: Defines the port number for the web server. This has to be different for each services running on the same host. Default is 8001.
|
||||
- **`SERVICE_RPC_PORT`**: Defines the port number for the rpc server. This has to be different for each services running on the same host. Default is 18871.
|
||||
- **`GENERATE_WEB_SETTINGS`**: When set to true, generates / updates the `web_settings.json` file. If the file already exists, only new entries are appended.
|
||||
|
||||
Some of those settings can also be altered directly in code when initializing the server:
|
||||
|
||||
```python
|
||||
import pathlib
|
||||
|
||||
from pydase import Server
|
||||
from your_service_module import YourService
|
||||
|
||||
|
||||
server = Server(
|
||||
YourService(),
|
||||
web_port=8080,
|
||||
rpc_port=18880,
|
||||
config_dir=pathlib.Path("other_config_dir"), # note that you need to provide an argument of type pathlib.Path
|
||||
generate_web_settings=True
|
||||
).run()
|
||||
```
|
||||
|
||||
## Customizing the Web Interface
|
||||
|
||||
### Enhancing the Web Interface Style with Custom CSS
|
||||
|
||||
`pydase` allows you to enhance the user experience by customizing the web interface's appearance. You can apply your own styles globally across the web interface by passing a custom CSS file to the server during initialization.
|
||||
|
||||
Here's how you can use this feature:
|
||||
|
||||
1. Prepare your custom CSS file with the desired styles.
|
||||
|
||||
2. When initializing your server, use the `css` parameter of the `Server` class to specify the path to your custom CSS file.
|
||||
|
||||
```python
|
||||
from pydase import Server, DataService
|
||||
|
||||
class MyService(DataService):
|
||||
# ... your service definition ...
|
||||
|
||||
if __name__ == "__main__":
|
||||
service = MyService()
|
||||
server = Server(service, css="path/to/your/custom.css").run()
|
||||
```
|
||||
|
||||
This will apply the styles defined in `custom.css` to the web interface, allowing you to maintain branding consistency or improve visual accessibility.
|
||||
|
||||
Please ensure that the CSS file path is accessible from the server's running location. Relative or absolute paths can be used depending on your setup.
|
||||
|
||||
### Tailoring Frontend Component Layout
|
||||
|
||||
`pydase` enables users to customize the frontend layout via the `web_settings.json` file. Each key in the file corresponds to the full access path of public attributes, properties, and methods of the exposed service, using dot-notation.
|
||||
|
||||
- **Custom Display Names**: Modify the `"displayName"` value in the file to change how each component appears in the frontend.
|
||||
<!-- - **Adjustable Component Order**: The `"index"` values determine the order of components. Alter these values to rearrange the components as desired. -->
|
||||
|
||||
The `web_settings.json` file will be stored in the directory specified by `SERVICE_CONFIG_DIR`. You can generate a `web_settings.json` file by setting the `GENERATE_WEB_SETTINGS` to `True`. For more information, see the [configuration section](#configuring-pydase-via-environment-variables).
|
||||
|
||||
## Logging in pydase
|
||||
|
||||
The `pydase` library organizes its loggers on a per-module basis, mirroring the Python package hierarchy. This structured approach allows for granular control over logging levels and behaviour across different parts of the library.
|
||||
|
||||
### Changing the Log Level
|
||||
|
||||
You have two primary ways to adjust the log levels in `pydase`:
|
||||
|
||||
1. directly targeting `pydase` loggers
|
||||
|
||||
You can set the log level for any `pydase` logger directly in your code. This method is useful for fine-tuning logging levels for specific modules within `pydase`. For instance, if you want to change the log level of the main `pydase` logger or target a submodule like `pydase.data_service`, you can do so as follows:
|
||||
|
||||
```python
|
||||
# <your_script.py>
|
||||
import logging
|
||||
|
||||
# Set the log level for the main pydase logger
|
||||
logging.getLogger("pydase").setLevel(logging.INFO)
|
||||
|
||||
# Optionally, target a specific submodule logger
|
||||
# logging.getLogger("pydase.data_service").setLevel(logging.DEBUG)
|
||||
|
||||
# Your logger for the current script
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.info("My info message.")
|
||||
```
|
||||
|
||||
This approach allows for specific control over different parts of the `pydase` library, depending on your logging needs.
|
||||
|
||||
2. using the `ENVIRONMENT` environment variable
|
||||
|
||||
For a more global setting that affects the entire `pydase` library, you can utilize the `ENVIRONMENT` environment variable. Setting this variable to "production" will configure all `pydase` loggers to only log messages of level "INFO" and above, filtering out more verbose logging. This is particularly useful for production environments where excessive logging can be overwhelming or unnecessary.
|
||||
|
||||
```bash
|
||||
ENVIRONMENT="production" python -m <module_using_pydase>
|
||||
```
|
||||
|
||||
The production environment will only log messages above "INFO", the development environment (default) logs everything above "DEBUG".
|
||||
In the absence of this setting, the default behavior is to log everything of level "DEBUG" and above, suitable for development environments where more detailed logs are beneficial.
|
||||
|
||||
2. calling the `pydase.utils.logging.setup_logging` function with the desired log level
|
||||
|
||||
```python
|
||||
# <your_script.py>
|
||||
import logging
|
||||
from pydase.utils.logging import setup_logging
|
||||
|
||||
setup_logging("INFO") # or setup_logging(logging.INFO)
|
||||
logger = logging.getLogger()
|
||||
|
||||
# ... and your log
|
||||
logger.info("My info message.")
|
||||
```
|
||||
**Note**: It is recommended to avoid calling the `pydase.utils.logging.setup_logging` function directly, as this may result in duplicated logging messages.
|
||||
|
||||
## Documentation
|
||||
|
||||
|
||||
@@ -111,17 +111,19 @@ import { setAttribute, runMethod } from '../socket'; // use this when your comp
|
||||
// or runs a method, respectively
|
||||
import { DocStringComponent } from './DocStringComponent';
|
||||
import React, { useEffect, useRef, useState } from 'react';
|
||||
import { WebSettingsContext } from '../WebSettings';
|
||||
import { Card, Collapse, Image } from 'react-bootstrap';
|
||||
import { DocStringComponent } from './DocStringComponent';
|
||||
import { ChevronDown, ChevronRight } from 'react-bootstrap-icons';
|
||||
import { getIdFromFullAccessPath } from '../utils/stringUtils';
|
||||
import { LevelName } from './NotificationsComponent';
|
||||
|
||||
interface ImageComponentProps {
|
||||
type ImageComponentProps = {
|
||||
name: string;
|
||||
parentPath: string;
|
||||
parentPath?: string;
|
||||
readOnly: boolean;
|
||||
docString: string;
|
||||
addNotification: (message: string) => void;
|
||||
addNotification: (message: string, levelname?: LevelName) => void;
|
||||
// Define your component specific props here
|
||||
value: string;
|
||||
format: string;
|
||||
@@ -132,9 +134,17 @@ export const ImageComponent = React.memo((props: ImageComponentProps) => {
|
||||
|
||||
const renderCount = useRef(0);
|
||||
const [open, setOpen] = useState(true); // add this if you want to expand/collapse your component
|
||||
const fullAccessPath = parentPath.concat('.' + name);
|
||||
const fullAccessPath = [parentPath, name].filter((element) => element).join('.');
|
||||
const id = getIdFromFullAccessPath(fullAccessPath);
|
||||
|
||||
// Web settings contain the user-defined display name of the components (and possibly more later)
|
||||
const webSettings = useContext(WebSettingsContext);
|
||||
let displayName = name;
|
||||
|
||||
if (webSettings[fullAccessPath] && webSettings[fullAccessPath].displayName) {
|
||||
displayName = webSettings[fullAccessPath].displayName;
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
renderCount.current++;
|
||||
});
|
||||
@@ -155,14 +165,15 @@ export const ImageComponent = React.memo((props: ImageComponentProps) => {
|
||||
onClick={() => setOpen(!open)}
|
||||
style={{ cursor: 'pointer' }} // Change cursor style on hover
|
||||
>
|
||||
{name} {open ? <ChevronDown /> : <ChevronRight />}
|
||||
{displayName}
|
||||
<DocStringComponent docString={docString} />
|
||||
{open ? <ChevronDown /> : <ChevronRight />}
|
||||
</Card.Header>
|
||||
<Collapse in={open}>
|
||||
<Card.Body>
|
||||
{process.env.NODE_ENV === 'development' && (
|
||||
<p>Render count: {renderCount.current}</p>
|
||||
)}
|
||||
<DocStringComponent docString={docString} />
|
||||
{/* Your component TSX here */}
|
||||
</Card.Body>
|
||||
</Collapse>
|
||||
@@ -205,6 +216,7 @@ React components in the frontend often need to send updates to the backend, part
|
||||
export const ButtonComponent = React.memo((props: ButtonComponentProps) => {
|
||||
// ...
|
||||
const { name, parentPath, value } = props;
|
||||
let displayName = ... // to access the user-defined display name
|
||||
|
||||
const setChecked = (checked: boolean) => {
|
||||
setAttribute(name, parentPath, checked);
|
||||
@@ -216,7 +228,7 @@ React components in the frontend often need to send updates to the backend, part
|
||||
value={parentPath}
|
||||
// ... other props
|
||||
onChange={(e) => setChecked(e.currentTarget.checked)}>
|
||||
<p>{name}</p>
|
||||
{displayName}
|
||||
</ToggleButton>
|
||||
);
|
||||
});
|
||||
@@ -299,6 +311,7 @@ useEffect(() => {
|
||||
```
|
||||
|
||||
However, you might want to use the `addNotification` at different places. For an example, see the [MethodComponent](../../frontend/src/components/MethodComponent.tsx).
|
||||
**Note**: you can specify the notification level by passing a string of type `LevelName` (one of 'CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG'). The default value is 'DEBUG'.
|
||||
|
||||
### Step 6: Write Tests for the Component (TODO)
|
||||
|
||||
|
||||
27
docs/dev-guide/Observer_Pattern_Implementation.md
Normal file
27
docs/dev-guide/Observer_Pattern_Implementation.md
Normal file
@@ -0,0 +1,27 @@
|
||||
# Observer Pattern Implementation in Pydase
|
||||
|
||||
## Overview
|
||||
|
||||
The Observer Pattern is a fundamental design pattern in the `pydase` package, serving as the central communication mechanism for state updates to clients connected to a service.
|
||||
|
||||
## How it Works
|
||||
|
||||
### The Observable Class
|
||||
|
||||
The `Observable` class is at the core of the pattern. It maintains a list of observers and is responsible for notifying them about state changes. It does so by overriding the following methods:
|
||||
|
||||
- `__setattr__`: This function emits a notification before and after a new value is set. These two notifications are important to track which attributes are being set to avoid endless recursion (e.g. when accessing a property within another property). Moreover, when setting an attribute to another observable, the former class will add itself as an observer to the latter class, ensuring that nested classes are properly observed.
|
||||
- `__getattribute__`: This function notifies the observers when a property getter is called, allowing for monitoring state changes in remote devices, as opposed to local instance attributes.
|
||||
|
||||
### Custom Collection Classes
|
||||
|
||||
To handle collections (like lists and dictionaries), the `Observable` class converts them into custom collection classes `_ObservableList` and `_ObservableDict` that notify observers of any changes in their state. For this, they have to override the methods changing the state, e.g., `__setitem__` or `append` for lists.
|
||||
|
||||
### The Observer Class
|
||||
|
||||
The `Observer` is the final element in the chain of observers. The notifications of attribute changes it receives include the full access path (in dot-notation) and the new value. It implements logic to handle state changes, like caching, error logging for type changes, etc. This can be extended by custom notification callbacks (implemented using `add_notification_callback` in `DataServiceObserver`). This enables the user to perform specific actions in response to changes. In `pydase`, the web server adds an additional notification callback that emits the websocket events (`sio_callback`).
|
||||
|
||||
Furthermore, the `DataServiceObserver` implements logic to reload the values of properties when an attribute change occurs that a property depends on.
|
||||
|
||||
- **Dynamic Inspection**: The observer dynamically inspects the observable object (recursively) to create a mapping of properties and their dependencies. This mapping is constructed based on the class or instance attributes used within the source code of the property getters.
|
||||
- **Dependency Management**: When a change in an attribute occurs, `DataServiceObserver` updates any properties that depend on this attribute. This ensures that the overall state remains consistent and up-to-date, especially in complex scenarios where properties depend on other instance attribute or properties.
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 20 KiB |
1523
frontend/package-lock.json
generated
1523
frontend/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -5,6 +5,7 @@
|
||||
"dependencies": {
|
||||
"@emotion/react": "^11.11.1",
|
||||
"@emotion/styled": "^11.11.0",
|
||||
"@fsouza/prettierd": "^0.25.1",
|
||||
"@mui/material": "^5.14.1",
|
||||
"@testing-library/jest-dom": "^5.16.5",
|
||||
"@testing-library/react": "^13.4.0",
|
||||
@@ -46,7 +47,7 @@
|
||||
"@types/node": "^20.0.0",
|
||||
"@types/react": "^18.0.0",
|
||||
"@types/react-dom": "^18.0.0",
|
||||
"@typescript-eslint/eslint-plugin": "^6.9.0",
|
||||
"@typescript-eslint/eslint-plugin": "^6.11.0",
|
||||
"@typescript-eslint/parser": "^6.9.0",
|
||||
"eslint": "^8.52.0",
|
||||
"eslint-config-prettier": "^9.0.0",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
body {
|
||||
min-width: 576px;
|
||||
max-width: 1200px;
|
||||
max-width: 2000px;
|
||||
}
|
||||
input.instantUpdate {
|
||||
background-color: rgba(255, 0, 0, 0.1);
|
||||
@@ -12,14 +12,31 @@ input.instantUpdate {
|
||||
}
|
||||
.navbarOffset {
|
||||
padding-top: 60px !important;
|
||||
right: 20;
|
||||
}
|
||||
/* .toastContainer {
|
||||
position: fixed;
|
||||
} */
|
||||
.notificationToast {
|
||||
.toastContainer {
|
||||
position: fixed !important;
|
||||
padding: 5px;
|
||||
}
|
||||
.debugToast, .infoToast {
|
||||
background-color: rgba(114, 214, 253, 0.5) !important;
|
||||
}
|
||||
.exceptionToast {
|
||||
.warningToast {
|
||||
background-color: rgba(255, 181, 44, 0.603) !important;
|
||||
}
|
||||
.errorToast, .criticalToast {
|
||||
background-color: rgba(216, 41, 18, 0.678) !important;
|
||||
}
|
||||
}
|
||||
.buttonComponent {
|
||||
float: left !important;
|
||||
margin-right: 10px !important;
|
||||
}
|
||||
.stringComponent {
|
||||
float: left !important;
|
||||
margin-right: 10px !important;
|
||||
}
|
||||
.numberComponent {
|
||||
float: left !important;
|
||||
margin-right: 10px !important;
|
||||
width: 270px !important;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useCallback, useEffect, useReducer, useRef, useState } from 'react';
|
||||
import { useCallback, useEffect, useReducer, useState } from 'react';
|
||||
import { Navbar, Form, Offcanvas, Container } from 'react-bootstrap';
|
||||
import { hostname, port, socket } from './socket';
|
||||
import {
|
||||
@@ -6,98 +6,36 @@ import {
|
||||
DataServiceJSON
|
||||
} from './components/DataServiceComponent';
|
||||
import './App.css';
|
||||
import { Notifications } from './components/NotificationsComponent';
|
||||
import {
|
||||
Notifications,
|
||||
Notification,
|
||||
LevelName
|
||||
} from './components/NotificationsComponent';
|
||||
import { ConnectionToast } from './components/ConnectionToast';
|
||||
import { SerializedValue, setNestedValueByPath, State } from './utils/stateUtils';
|
||||
import { WebSettingsContext, WebSetting } from './WebSettings';
|
||||
|
||||
type ValueType = boolean | string | number | object;
|
||||
|
||||
type State = DataServiceJSON | null;
|
||||
type Action =
|
||||
| { type: 'SET_DATA'; data: DataServiceJSON }
|
||||
| { type: 'UPDATE_ATTRIBUTE'; parentPath: string; name: string; value: ValueType };
|
||||
type UpdateMessage = {
|
||||
data: { parent_path: string; name: string; value: object };
|
||||
};
|
||||
type ExceptionMessage = {
|
||||
data: { exception: string; type: string };
|
||||
};
|
||||
|
||||
/**
|
||||
* A function to update a specific property in a deeply nested object.
|
||||
* The property to be updated is specified by a path array.
|
||||
*
|
||||
* Each path element can be a regular object key or an array index of the
|
||||
* form "attribute[index]", where "attribute" is the key of the array in
|
||||
* the object and "index" is the index of the element in the array.
|
||||
*
|
||||
* For array indices, the element at the specified index in the array is
|
||||
* updated.
|
||||
*
|
||||
* If the property to be updated is an object or an array, it is updated
|
||||
* recursively.
|
||||
*/
|
||||
function updateNestedObject(path: Array<string>, obj: object, value: ValueType) {
|
||||
// Base case: If the path is empty, return the new value.
|
||||
// This means we've reached the nested property to be updated.
|
||||
if (path.length === 0) {
|
||||
return value;
|
||||
}
|
||||
|
||||
// Recursive case: If the path is not empty, split it into the first key and the rest
|
||||
// of the path.
|
||||
const [first, ...rest] = path;
|
||||
|
||||
// Check if 'first' is an array index.
|
||||
const indexMatch = first.match(/^(\w+)\[(\d+)\]$/);
|
||||
|
||||
// If 'first' is an array index of the form "attribute[index]", then update the
|
||||
// element at the specified index in the array. Otherwise, update the property
|
||||
// specified by 'first' in the object.
|
||||
if (indexMatch) {
|
||||
const attribute = indexMatch[1];
|
||||
const index = parseInt(indexMatch[2]);
|
||||
|
||||
if (Array.isArray(obj[attribute]?.value)) {
|
||||
return {
|
||||
...obj,
|
||||
[attribute]: {
|
||||
...obj[attribute],
|
||||
value: obj[attribute].value.map((item, i) =>
|
||||
i === index
|
||||
? {
|
||||
...item,
|
||||
value: updateNestedObject(rest, item.value || {}, value)
|
||||
}
|
||||
: item
|
||||
)
|
||||
}
|
||||
};
|
||||
} else {
|
||||
throw new Error(
|
||||
`Expected ${attribute}.value to be an array, but received ${typeof obj[
|
||||
attribute
|
||||
]?.value}`
|
||||
);
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
...obj,
|
||||
[first]: {
|
||||
...obj[first],
|
||||
value: updateNestedObject(rest, obj[first]?.value || {}, value)
|
||||
}
|
||||
| { type: 'SET_DATA'; data: State }
|
||||
| {
|
||||
type: 'UPDATE_ATTRIBUTE';
|
||||
fullAccessPath: string;
|
||||
newValue: SerializedValue;
|
||||
};
|
||||
}
|
||||
}
|
||||
type UpdateMessage = {
|
||||
data: { full_access_path: string; value: SerializedValue };
|
||||
};
|
||||
type LogMessage = {
|
||||
levelname: LevelName;
|
||||
message: string;
|
||||
};
|
||||
|
||||
const reducer = (state: State, action: Action): State => {
|
||||
switch (action.type) {
|
||||
case 'SET_DATA':
|
||||
return action.data;
|
||||
case 'UPDATE_ATTRIBUTE': {
|
||||
const path = action.parentPath.split('.').slice(1).concat(action.name);
|
||||
|
||||
return updateNestedObject(path, state, action.value);
|
||||
return setNestedValueByPath(state, action.fullAccessPath, action.newValue);
|
||||
}
|
||||
default:
|
||||
throw new Error();
|
||||
@@ -105,19 +43,13 @@ const reducer = (state: State, action: Action): State => {
|
||||
};
|
||||
const App = () => {
|
||||
const [state, dispatch] = useReducer(reducer, null);
|
||||
const stateRef = useRef(state); // Declare a reference to hold the current state
|
||||
const [webSettings, setWebSettings] = useState<Record<string, WebSetting>>({});
|
||||
const [isInstantUpdate, setIsInstantUpdate] = useState(false);
|
||||
const [showSettings, setShowSettings] = useState(false);
|
||||
const [showNotification, setShowNotification] = useState(false);
|
||||
const [notifications, setNotifications] = useState([]);
|
||||
const [exceptions, setExceptions] = useState([]);
|
||||
const [notifications, setNotifications] = useState<Notification[]>([]);
|
||||
const [connectionStatus, setConnectionStatus] = useState('connecting');
|
||||
|
||||
// Keep the state reference up to date
|
||||
useEffect(() => {
|
||||
stateRef.current = state;
|
||||
}, [state]);
|
||||
|
||||
useEffect(() => {
|
||||
// Allow the user to add a custom css file
|
||||
fetch(`http://${hostname}:${port}/custom.css`)
|
||||
@@ -137,7 +69,10 @@ const App = () => {
|
||||
// Fetch data from the API when the client connects
|
||||
fetch(`http://${hostname}:${port}/service-properties`)
|
||||
.then((response) => response.json())
|
||||
.then((data: DataServiceJSON) => dispatch({ type: 'SET_DATA', data }));
|
||||
.then((data: State) => dispatch({ type: 'SET_DATA', data }));
|
||||
fetch(`http://${hostname}:${port}/web-settings`)
|
||||
.then((response) => response.json())
|
||||
.then((data: Record<string, WebSetting>) => setWebSettings(data));
|
||||
setConnectionStatus('connected');
|
||||
});
|
||||
socket.on('disconnect', () => {
|
||||
@@ -152,70 +87,55 @@ const App = () => {
|
||||
});
|
||||
|
||||
socket.on('notify', onNotify);
|
||||
socket.on('exception', onException);
|
||||
socket.on('log', onLogMessage);
|
||||
|
||||
return () => {
|
||||
socket.off('notify', onNotify);
|
||||
socket.off('exception', onException);
|
||||
socket.off('log', onLogMessage);
|
||||
};
|
||||
}, []);
|
||||
|
||||
// Adding useCallback to prevent notify to change causing a re-render of all
|
||||
// components
|
||||
const addNotification = useCallback((text: string) => {
|
||||
// Getting the current time in the required format
|
||||
const timeString = new Date().toISOString().substring(11, 19);
|
||||
// Adding an id to the notification to provide a way of removing it
|
||||
const id = Math.random();
|
||||
const addNotification = useCallback(
|
||||
(message: string, levelname: LevelName = 'DEBUG') => {
|
||||
// Getting the current time in the required format
|
||||
const timeStamp = new Date().toISOString().substring(11, 19);
|
||||
// Adding an id to the notification to provide a way of removing it
|
||||
const id = Math.random();
|
||||
|
||||
// Custom logic for notifications
|
||||
setNotifications((prevNotifications) => [
|
||||
{ id, text, time: timeString },
|
||||
...prevNotifications
|
||||
]);
|
||||
}, []);
|
||||
// Custom logic for notifications
|
||||
setNotifications((prevNotifications) => [
|
||||
{ levelname, id, message, timeStamp },
|
||||
...prevNotifications
|
||||
]);
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
const notifyException = (text: string) => {
|
||||
// Getting the current time in the required format
|
||||
const timeString = new Date().toISOString().substring(11, 19);
|
||||
// Adding an id to the notification to provide a way of removing it
|
||||
const id = Math.random();
|
||||
|
||||
// Custom logic for notifications
|
||||
setExceptions((prevNotifications) => [
|
||||
{ id, text, time: timeString },
|
||||
...prevNotifications
|
||||
]);
|
||||
};
|
||||
const removeNotificationById = (id: number) => {
|
||||
setNotifications((prevNotifications) =>
|
||||
prevNotifications.filter((n) => n.id !== id)
|
||||
);
|
||||
};
|
||||
|
||||
const removeExceptionById = (id: number) => {
|
||||
setExceptions((prevNotifications) => prevNotifications.filter((n) => n.id !== id));
|
||||
};
|
||||
|
||||
const handleCloseSettings = () => setShowSettings(false);
|
||||
const handleShowSettings = () => setShowSettings(true);
|
||||
|
||||
function onNotify(value: UpdateMessage) {
|
||||
// Extracting data from the notification
|
||||
const { parent_path: parentPath, name, value: newValue } = value.data;
|
||||
const { full_access_path: fullAccessPath, value: newValue } = value.data;
|
||||
|
||||
// Dispatching the update to the reducer
|
||||
dispatch({
|
||||
type: 'UPDATE_ATTRIBUTE',
|
||||
parentPath,
|
||||
name,
|
||||
value: newValue
|
||||
fullAccessPath,
|
||||
newValue
|
||||
});
|
||||
}
|
||||
|
||||
function onException(value: ExceptionMessage) {
|
||||
const newException = `${value.data.type}: ${value.data.exception}.`;
|
||||
notifyException(newException);
|
||||
function onLogMessage(value: LogMessage) {
|
||||
addNotification(value.message, value.levelname);
|
||||
}
|
||||
|
||||
// While the data is loading
|
||||
@@ -234,9 +154,7 @@ const App = () => {
|
||||
<Notifications
|
||||
showNotification={showNotification}
|
||||
notifications={notifications}
|
||||
exceptions={exceptions}
|
||||
removeNotificationById={removeNotificationById}
|
||||
removeExceptionById={removeExceptionById}
|
||||
/>
|
||||
|
||||
<Offcanvas
|
||||
@@ -265,12 +183,14 @@ const App = () => {
|
||||
</Offcanvas>
|
||||
|
||||
<div className="App navbarOffset">
|
||||
<DataServiceComponent
|
||||
name={''}
|
||||
props={state as DataServiceJSON}
|
||||
isInstantUpdate={isInstantUpdate}
|
||||
addNotification={addNotification}
|
||||
/>
|
||||
<WebSettingsContext.Provider value={webSettings}>
|
||||
<DataServiceComponent
|
||||
name={''}
|
||||
props={state as DataServiceJSON}
|
||||
isInstantUpdate={isInstantUpdate}
|
||||
addNotification={addNotification}
|
||||
/>
|
||||
</WebSettingsContext.Provider>
|
||||
</div>
|
||||
<ConnectionToast connectionStatus={connectionStatus} />
|
||||
</>
|
||||
|
||||
8
frontend/src/WebSettings.tsx
Normal file
8
frontend/src/WebSettings.tsx
Normal file
@@ -0,0 +1,8 @@
|
||||
import { createContext } from 'react';
|
||||
|
||||
export const WebSettingsContext = createContext<Record<string, WebSetting>>({});
|
||||
|
||||
export type WebSetting = {
|
||||
displayName: string;
|
||||
index: number;
|
||||
};
|
||||
@@ -1,24 +1,33 @@
|
||||
import React, { useEffect, useRef } from 'react';
|
||||
import React, { useContext, useEffect, useRef } from 'react';
|
||||
import { runMethod } from '../socket';
|
||||
import { InputGroup, Form, Button } from 'react-bootstrap';
|
||||
import { DocStringComponent } from './DocStringComponent';
|
||||
import { getIdFromFullAccessPath } from '../utils/stringUtils';
|
||||
import { LevelName } from './NotificationsComponent';
|
||||
import { WebSettingsContext } from '../WebSettings';
|
||||
|
||||
interface AsyncMethodProps {
|
||||
type AsyncMethodProps = {
|
||||
name: string;
|
||||
parentPath: string;
|
||||
parameters: Record<string, string>;
|
||||
value: Record<string, string>;
|
||||
docString?: string;
|
||||
hideOutput?: boolean;
|
||||
addNotification: (message: string) => void;
|
||||
}
|
||||
addNotification: (message: string, levelname?: LevelName) => void;
|
||||
};
|
||||
|
||||
export const AsyncMethodComponent = React.memo((props: AsyncMethodProps) => {
|
||||
const { name, parentPath, docString, value: runningTask, addNotification } = props;
|
||||
const renderCount = useRef(0);
|
||||
const formRef = useRef(null);
|
||||
const id = getIdFromFullAccessPath(parentPath.concat('.' + name));
|
||||
const fullAccessPath = [parentPath, name].filter((element) => element).join('.');
|
||||
const id = getIdFromFullAccessPath(fullAccessPath);
|
||||
const webSettings = useContext(WebSettingsContext);
|
||||
let displayName = name;
|
||||
|
||||
if (webSettings[fullAccessPath] && webSettings[fullAccessPath].displayName) {
|
||||
displayName = webSettings[fullAccessPath].displayName;
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
renderCount.current++;
|
||||
@@ -91,16 +100,14 @@ export const AsyncMethodComponent = React.memo((props: AsyncMethodProps) => {
|
||||
return (
|
||||
<div className="align-items-center asyncMethodComponent" id={id}>
|
||||
{process.env.NODE_ENV === 'development' && (
|
||||
<p>Render count: {renderCount.current}</p>
|
||||
<div>Render count: {renderCount.current}</div>
|
||||
)}
|
||||
<h5>
|
||||
Function: {name}
|
||||
<DocStringComponent docString={docString} />
|
||||
</h5>
|
||||
<h5>Function: {displayName}</h5>
|
||||
<Form onSubmit={execute} ref={formRef}>
|
||||
{args}
|
||||
<Button id={`button-${id}`} name={name} value={parentPath} type="submit">
|
||||
{runningTask ? 'Stop' : 'Start'}
|
||||
{runningTask ? 'Stop ' : 'Start '}
|
||||
<DocStringComponent docString={docString} />
|
||||
</Button>
|
||||
</Form>
|
||||
</div>
|
||||
|
||||
@@ -1,24 +1,32 @@
|
||||
import React, { useEffect, useRef } from 'react';
|
||||
import React, { useContext, useEffect, useRef } from 'react';
|
||||
import { WebSettingsContext } from '../WebSettings';
|
||||
import { ToggleButton } from 'react-bootstrap';
|
||||
import { setAttribute } from '../socket';
|
||||
import { DocStringComponent } from './DocStringComponent';
|
||||
import { getIdFromFullAccessPath } from '../utils/stringUtils';
|
||||
import { LevelName } from './NotificationsComponent';
|
||||
|
||||
interface ButtonComponentProps {
|
||||
type ButtonComponentProps = {
|
||||
name: string;
|
||||
parentPath?: string;
|
||||
value: boolean;
|
||||
readOnly: boolean;
|
||||
docString: string;
|
||||
mapping?: [string, string]; // Enforce a tuple of two strings
|
||||
addNotification: (message: string) => void;
|
||||
}
|
||||
addNotification: (message: string, levelname?: LevelName) => void;
|
||||
};
|
||||
|
||||
export const ButtonComponent = React.memo((props: ButtonComponentProps) => {
|
||||
const { name, parentPath, value, readOnly, docString, mapping, addNotification } =
|
||||
props;
|
||||
const buttonName = mapping ? (value ? mapping[0] : mapping[1]) : name;
|
||||
const id = getIdFromFullAccessPath(parentPath.concat('.' + name));
|
||||
const { name, parentPath, value, readOnly, docString, addNotification } = props;
|
||||
// const buttonName = props.mapping ? (value ? props.mapping[0] : props.mapping[1]) : name;
|
||||
const fullAccessPath = [parentPath, name].filter((element) => element).join('.');
|
||||
const id = getIdFromFullAccessPath(fullAccessPath);
|
||||
const webSettings = useContext(WebSettingsContext);
|
||||
let displayName = name;
|
||||
|
||||
if (webSettings[fullAccessPath] && webSettings[fullAccessPath].displayName) {
|
||||
displayName = webSettings[fullAccessPath].displayName;
|
||||
}
|
||||
|
||||
const renderCount = useRef(0);
|
||||
|
||||
@@ -37,10 +45,9 @@ export const ButtonComponent = React.memo((props: ButtonComponentProps) => {
|
||||
return (
|
||||
<div className={'buttonComponent'} id={id}>
|
||||
{process.env.NODE_ENV === 'development' && (
|
||||
<p>Render count: {renderCount.current}</p>
|
||||
<div>Render count: {renderCount.current}</div>
|
||||
)}
|
||||
|
||||
<DocStringComponent docString={docString} />
|
||||
<ToggleButton
|
||||
id={`toggle-check-${id}`}
|
||||
type="checkbox"
|
||||
@@ -49,7 +56,8 @@ export const ButtonComponent = React.memo((props: ButtonComponentProps) => {
|
||||
value={parentPath}
|
||||
disabled={readOnly}
|
||||
onChange={(e) => setChecked(e.currentTarget.checked)}>
|
||||
<p>{buttonName}</p>
|
||||
{displayName}
|
||||
<DocStringComponent docString={docString} />
|
||||
</ToggleButton>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
import React, { useEffect, useRef } from 'react';
|
||||
import React, { useContext, useEffect, useRef } from 'react';
|
||||
import { WebSettingsContext } from '../WebSettings';
|
||||
import { InputGroup, Form, Row, Col } from 'react-bootstrap';
|
||||
import { setAttribute } from '../socket';
|
||||
import { DocStringComponent } from './DocStringComponent';
|
||||
import { getIdFromFullAccessPath } from '../utils/stringUtils';
|
||||
import { LevelName } from './NotificationsComponent';
|
||||
|
||||
interface ColouredEnumComponentProps {
|
||||
type ColouredEnumComponentProps = {
|
||||
name: string;
|
||||
parentPath: string;
|
||||
value: string;
|
||||
docString?: string;
|
||||
readOnly: boolean;
|
||||
enumDict: Record<string, string>;
|
||||
addNotification: (message: string) => void;
|
||||
}
|
||||
addNotification: (message: string, levelname?: LevelName) => void;
|
||||
};
|
||||
|
||||
export const ColouredEnumComponent = React.memo((props: ColouredEnumComponentProps) => {
|
||||
const {
|
||||
@@ -25,7 +27,14 @@ export const ColouredEnumComponent = React.memo((props: ColouredEnumComponentPro
|
||||
addNotification
|
||||
} = props;
|
||||
const renderCount = useRef(0);
|
||||
const id = getIdFromFullAccessPath(parentPath.concat('.' + name));
|
||||
const fullAccessPath = [parentPath, name].filter((element) => element).join('.');
|
||||
const id = getIdFromFullAccessPath(fullAccessPath);
|
||||
const webSettings = useContext(WebSettingsContext);
|
||||
let displayName = name;
|
||||
|
||||
if (webSettings[fullAccessPath] && webSettings[fullAccessPath].displayName) {
|
||||
displayName = webSettings[fullAccessPath].displayName;
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
renderCount.current++;
|
||||
@@ -42,12 +51,14 @@ export const ColouredEnumComponent = React.memo((props: ColouredEnumComponentPro
|
||||
return (
|
||||
<div className={'enumComponent'} id={id}>
|
||||
{process.env.NODE_ENV === 'development' && (
|
||||
<p>Render count: {renderCount.current}</p>
|
||||
<div>Render count: {renderCount.current}</div>
|
||||
)}
|
||||
<DocStringComponent docString={docString} />
|
||||
<Row>
|
||||
<Col className="d-flex align-items-center">
|
||||
<InputGroup.Text>{name}</InputGroup.Text>
|
||||
<InputGroup.Text>
|
||||
{displayName}
|
||||
<DocStringComponent docString={docString} />
|
||||
</InputGroup.Text>
|
||||
{readOnly ? (
|
||||
// Display the Form.Control when readOnly is true
|
||||
<Form.Control
|
||||
|
||||
@@ -68,7 +68,7 @@ export const ConnectionToast = React.memo(
|
||||
const { message, bg, delay } = getToastContent();
|
||||
|
||||
return (
|
||||
<ToastContainer position="bottom-center">
|
||||
<ToastContainer position="bottom-center" className="toastContainer">
|
||||
<Toast
|
||||
show={show}
|
||||
onClose={handleClose}
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
import { useState } from 'react';
|
||||
import { useContext, useState } from 'react';
|
||||
import React from 'react';
|
||||
import { Card, Collapse } from 'react-bootstrap';
|
||||
import { ChevronDown, ChevronRight } from 'react-bootstrap-icons';
|
||||
import { Attribute, GenericComponent } from './GenericComponent';
|
||||
import { getIdFromFullAccessPath } from '../utils/stringUtils';
|
||||
import { LevelName } from './NotificationsComponent';
|
||||
import { WebSettingsContext } from '../WebSettings';
|
||||
|
||||
type DataServiceProps = {
|
||||
name: string;
|
||||
props: DataServiceJSON;
|
||||
parentPath?: string;
|
||||
isInstantUpdate: boolean;
|
||||
addNotification: (message: string) => void;
|
||||
addNotification: (message: string, levelname?: LevelName) => void;
|
||||
};
|
||||
|
||||
export type DataServiceJSON = Record<string, Attribute>;
|
||||
@@ -19,17 +21,24 @@ export const DataServiceComponent = React.memo(
|
||||
({
|
||||
name,
|
||||
props,
|
||||
parentPath = 'DataService',
|
||||
parentPath = '',
|
||||
isInstantUpdate,
|
||||
addNotification
|
||||
}: DataServiceProps) => {
|
||||
const [open, setOpen] = useState(true);
|
||||
let fullAccessPath = parentPath;
|
||||
if (name) {
|
||||
fullAccessPath = parentPath.concat('.' + name);
|
||||
fullAccessPath = [parentPath, name].filter((element) => element).join('.');
|
||||
}
|
||||
const id = getIdFromFullAccessPath(fullAccessPath);
|
||||
|
||||
const webSettings = useContext(WebSettingsContext);
|
||||
let displayName = fullAccessPath;
|
||||
|
||||
if (webSettings[fullAccessPath] && webSettings[fullAccessPath].displayName) {
|
||||
displayName = webSettings[fullAccessPath].displayName;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="dataServiceComponent" id={id}>
|
||||
<Card className="mb-3">
|
||||
@@ -37,7 +46,7 @@ export const DataServiceComponent = React.memo(
|
||||
onClick={() => setOpen(!open)}
|
||||
style={{ cursor: 'pointer' }} // Change cursor style on hover
|
||||
>
|
||||
{fullAccessPath} {open ? <ChevronDown /> : <ChevronRight />}
|
||||
{displayName} {open ? <ChevronDown /> : <ChevronRight />}
|
||||
</Card.Header>
|
||||
<Collapse in={open}>
|
||||
<Card.Body>
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { Badge, Tooltip, OverlayTrigger } from 'react-bootstrap';
|
||||
import React from 'react';
|
||||
|
||||
interface DocStringProps {
|
||||
type DocStringProps = {
|
||||
docString?: string;
|
||||
}
|
||||
};
|
||||
|
||||
export const DocStringComponent = React.memo((props: DocStringProps) => {
|
||||
const { docString } = props;
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
import React, { useEffect, useRef } from 'react';
|
||||
import React, { useContext, useEffect, useRef } from 'react';
|
||||
import { WebSettingsContext } from '../WebSettings';
|
||||
import { InputGroup, Form, Row, Col } from 'react-bootstrap';
|
||||
import { setAttribute } from '../socket';
|
||||
import { getIdFromFullAccessPath } from '../utils/stringUtils';
|
||||
import { DocStringComponent } from './DocStringComponent';
|
||||
import { LevelName } from './NotificationsComponent';
|
||||
|
||||
interface EnumComponentProps {
|
||||
type EnumComponentProps = {
|
||||
name: string;
|
||||
parentPath: string;
|
||||
value: string;
|
||||
docString?: string;
|
||||
enumDict: Record<string, string>;
|
||||
addNotification: (message: string) => void;
|
||||
}
|
||||
addNotification: (message: string, levelname?: LevelName) => void;
|
||||
};
|
||||
|
||||
export const EnumComponent = React.memo((props: EnumComponentProps) => {
|
||||
const {
|
||||
@@ -23,6 +26,14 @@ export const EnumComponent = React.memo((props: EnumComponentProps) => {
|
||||
} = props;
|
||||
|
||||
const renderCount = useRef(0);
|
||||
const fullAccessPath = [parentPath, name].filter((element) => element).join('.');
|
||||
const id = getIdFromFullAccessPath(fullAccessPath);
|
||||
const webSettings = useContext(WebSettingsContext);
|
||||
let displayName = name;
|
||||
|
||||
if (webSettings[fullAccessPath] && webSettings[fullAccessPath].displayName) {
|
||||
displayName = webSettings[fullAccessPath].displayName;
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
renderCount.current++;
|
||||
@@ -37,14 +48,16 @@ export const EnumComponent = React.memo((props: EnumComponentProps) => {
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={'enumComponent'} id={parentPath.concat('.' + name)}>
|
||||
<div className={'enumComponent'} id={id}>
|
||||
{process.env.NODE_ENV === 'development' && (
|
||||
<p>Render count: {renderCount.current}</p>
|
||||
<div>Render count: {renderCount.current}</div>
|
||||
)}
|
||||
<DocStringComponent docString={docString} />
|
||||
<Row>
|
||||
<Col className="d-flex align-items-center">
|
||||
<InputGroup.Text>{name}</InputGroup.Text>
|
||||
<InputGroup.Text>
|
||||
{displayName}
|
||||
<DocStringComponent docString={docString} />
|
||||
</InputGroup.Text>
|
||||
<Form.Select
|
||||
aria-label="Default select example"
|
||||
value={value}
|
||||
|
||||
@@ -10,6 +10,7 @@ import { ListComponent } from './ListComponent';
|
||||
import { DataServiceComponent, DataServiceJSON } from './DataServiceComponent';
|
||||
import { ImageComponent } from './ImageComponent';
|
||||
import { ColouredEnumComponent } from './ColouredEnumComponent';
|
||||
import { LevelName } from './NotificationsComponent';
|
||||
|
||||
type AttributeType =
|
||||
| 'str'
|
||||
@@ -26,7 +27,7 @@ type AttributeType =
|
||||
| 'ColouredEnum';
|
||||
|
||||
type ValueType = boolean | string | number | object;
|
||||
export interface Attribute {
|
||||
export type Attribute = {
|
||||
type: AttributeType;
|
||||
value?: ValueType | ValueType[];
|
||||
readonly: boolean;
|
||||
@@ -34,13 +35,13 @@ export interface Attribute {
|
||||
parameters?: Record<string, string>;
|
||||
async?: boolean;
|
||||
enum?: Record<string, string>;
|
||||
}
|
||||
};
|
||||
type GenericComponentProps = {
|
||||
attribute: Attribute;
|
||||
name: string;
|
||||
parentPath: string;
|
||||
isInstantUpdate: boolean;
|
||||
addNotification: (message: string) => void;
|
||||
addNotification: (message: string, levelname?: LevelName) => void;
|
||||
};
|
||||
|
||||
export const GenericComponent = React.memo(
|
||||
@@ -94,12 +95,12 @@ export const GenericComponent = React.memo(
|
||||
<SliderComponent
|
||||
name={name}
|
||||
parentPath={parentPath}
|
||||
docString={attribute.doc}
|
||||
docString={attribute.value['value'].doc}
|
||||
readOnly={attribute.readonly}
|
||||
value={attribute.value['value']['value']}
|
||||
min={attribute.value['min']['value']}
|
||||
max={attribute.value['max']['value']}
|
||||
stepSize={attribute.value['step_size']['value']}
|
||||
value={attribute.value['value']}
|
||||
min={attribute.value['min']}
|
||||
max={attribute.value['max']}
|
||||
stepSize={attribute.value['step_size']}
|
||||
isInstantUpdate={isInstantUpdate}
|
||||
addNotification={addNotification}
|
||||
/>
|
||||
@@ -178,14 +179,13 @@ export const GenericComponent = React.memo(
|
||||
parentPath={parentPath}
|
||||
value={attribute.value['value']['value'] as string}
|
||||
readOnly={attribute.readonly}
|
||||
docString={attribute.doc}
|
||||
docString={attribute.value['value'].doc}
|
||||
// Add any other specific props for the ImageComponent here
|
||||
format={attribute.value['format']['value'] as string}
|
||||
addNotification={addNotification}
|
||||
/>
|
||||
);
|
||||
} else if (attribute.type === 'ColouredEnum') {
|
||||
console.log(attribute);
|
||||
return (
|
||||
<ColouredEnumComponent
|
||||
name={name}
|
||||
|
||||
@@ -1,25 +1,34 @@
|
||||
import React, { useEffect, useRef, useState } from 'react';
|
||||
import React, { useContext, useEffect, useRef, useState } from 'react';
|
||||
import { WebSettingsContext } from '../WebSettings';
|
||||
import { Card, Collapse, Image } from 'react-bootstrap';
|
||||
import { DocStringComponent } from './DocStringComponent';
|
||||
import { ChevronDown, ChevronRight } from 'react-bootstrap-icons';
|
||||
import { getIdFromFullAccessPath } from '../utils/stringUtils';
|
||||
import { LevelName } from './NotificationsComponent';
|
||||
|
||||
interface ImageComponentProps {
|
||||
type ImageComponentProps = {
|
||||
name: string;
|
||||
parentPath: string;
|
||||
value: string;
|
||||
readOnly: boolean;
|
||||
docString: string;
|
||||
format: string;
|
||||
addNotification: (message: string) => void;
|
||||
}
|
||||
addNotification: (message: string, levelname?: LevelName) => void;
|
||||
};
|
||||
|
||||
export const ImageComponent = React.memo((props: ImageComponentProps) => {
|
||||
const { name, parentPath, value, docString, format, addNotification } = props;
|
||||
|
||||
const renderCount = useRef(0);
|
||||
const [open, setOpen] = useState(true);
|
||||
const id = getIdFromFullAccessPath(parentPath.concat('.' + name));
|
||||
const fullAccessPath = [parentPath, name].filter((element) => element).join('.');
|
||||
const id = getIdFromFullAccessPath(fullAccessPath);
|
||||
const webSettings = useContext(WebSettingsContext);
|
||||
let displayName = name;
|
||||
|
||||
if (webSettings[fullAccessPath] && webSettings[fullAccessPath].displayName) {
|
||||
displayName = webSettings[fullAccessPath].displayName;
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
renderCount.current++;
|
||||
@@ -31,19 +40,23 @@ export const ImageComponent = React.memo((props: ImageComponentProps) => {
|
||||
|
||||
return (
|
||||
<div className={'imageComponent'} id={id}>
|
||||
{process.env.NODE_ENV === 'development' && (
|
||||
<div>Render count: {renderCount.current}</div>
|
||||
)}
|
||||
<Card>
|
||||
<Card.Header
|
||||
onClick={() => setOpen(!open)}
|
||||
style={{ cursor: 'pointer' }} // Change cursor style on hover
|
||||
>
|
||||
{name} {open ? <ChevronDown /> : <ChevronRight />}
|
||||
{displayName}
|
||||
<DocStringComponent docString={docString} />
|
||||
{open ? <ChevronDown /> : <ChevronRight />}
|
||||
</Card.Header>
|
||||
<Collapse in={open}>
|
||||
<Card.Body>
|
||||
{process.env.NODE_ENV === 'development' && (
|
||||
<p>Render count: {renderCount.current}</p>
|
||||
)}
|
||||
<DocStringComponent docString={docString} />
|
||||
{/* Your component JSX here */}
|
||||
{format === '' && value === '' ? (
|
||||
<p>No image set in the backend.</p>
|
||||
|
||||
@@ -2,22 +2,24 @@ import React, { useEffect, useRef } from 'react';
|
||||
import { DocStringComponent } from './DocStringComponent';
|
||||
import { Attribute, GenericComponent } from './GenericComponent';
|
||||
import { getIdFromFullAccessPath } from '../utils/stringUtils';
|
||||
import { LevelName } from './NotificationsComponent';
|
||||
|
||||
interface ListComponentProps {
|
||||
type ListComponentProps = {
|
||||
name: string;
|
||||
parentPath?: string;
|
||||
value: Attribute[];
|
||||
docString: string;
|
||||
isInstantUpdate: boolean;
|
||||
addNotification: (message: string) => void;
|
||||
}
|
||||
addNotification: (message: string, levelname?: LevelName) => void;
|
||||
};
|
||||
|
||||
export const ListComponent = React.memo((props: ListComponentProps) => {
|
||||
const { name, parentPath, value, docString, isInstantUpdate, addNotification } =
|
||||
props;
|
||||
|
||||
const renderCount = useRef(0);
|
||||
const id = getIdFromFullAccessPath(parentPath.concat('.' + name));
|
||||
const fullAccessPath = [parentPath, name].filter((element) => element).join('.');
|
||||
const id = getIdFromFullAccessPath(fullAccessPath);
|
||||
|
||||
useEffect(() => {
|
||||
renderCount.current++;
|
||||
@@ -26,7 +28,7 @@ export const ListComponent = React.memo((props: ListComponentProps) => {
|
||||
return (
|
||||
<div className={'listComponent'} id={id}>
|
||||
{process.env.NODE_ENV === 'development' && (
|
||||
<p>Render count: {renderCount.current}</p>
|
||||
<div>Render count: {renderCount.current}</div>
|
||||
)}
|
||||
<DocStringComponent docString={docString} />
|
||||
{value.map((item, index) => {
|
||||
|
||||
@@ -1,17 +1,19 @@
|
||||
import React, { useState, useEffect, useRef } from 'react';
|
||||
import React, { useContext, useEffect, useRef, useState } from 'react';
|
||||
import { WebSettingsContext } from '../WebSettings';
|
||||
import { runMethod } from '../socket';
|
||||
import { Button, InputGroup, Form, Collapse } from 'react-bootstrap';
|
||||
import { DocStringComponent } from './DocStringComponent';
|
||||
import { getIdFromFullAccessPath } from '../utils/stringUtils';
|
||||
import { LevelName } from './NotificationsComponent';
|
||||
|
||||
interface MethodProps {
|
||||
type MethodProps = {
|
||||
name: string;
|
||||
parentPath: string;
|
||||
parameters: Record<string, string>;
|
||||
docString?: string;
|
||||
hideOutput?: boolean;
|
||||
addNotification: (message: string) => void;
|
||||
}
|
||||
addNotification: (message: string, levelname?: LevelName) => void;
|
||||
};
|
||||
|
||||
export const MethodComponent = React.memo((props: MethodProps) => {
|
||||
const { name, parentPath, docString, addNotification } = props;
|
||||
@@ -20,7 +22,14 @@ export const MethodComponent = React.memo((props: MethodProps) => {
|
||||
const [hideOutput, setHideOutput] = useState(false);
|
||||
// Add a new state variable to hold the list of function calls
|
||||
const [functionCalls, setFunctionCalls] = useState([]);
|
||||
const id = getIdFromFullAccessPath(parentPath.concat('.' + name));
|
||||
const fullAccessPath = [parentPath, name].filter((element) => element).join('.');
|
||||
const id = getIdFromFullAccessPath(fullAccessPath);
|
||||
const webSettings = useContext(WebSettingsContext);
|
||||
let displayName = name;
|
||||
|
||||
if (webSettings[fullAccessPath] && webSettings[fullAccessPath].displayName) {
|
||||
displayName = webSettings[fullAccessPath].displayName;
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
renderCount.current++;
|
||||
@@ -76,19 +85,17 @@ export const MethodComponent = React.memo((props: MethodProps) => {
|
||||
return (
|
||||
<div className="align-items-center methodComponent" id={id}>
|
||||
{process.env.NODE_ENV === 'development' && (
|
||||
<p>Render count: {renderCount.current}</p>
|
||||
<div>Render count: {renderCount.current}</div>
|
||||
)}
|
||||
<h5 onClick={() => setHideOutput(!hideOutput)} style={{ cursor: 'pointer' }}>
|
||||
Function: {name}
|
||||
<DocStringComponent docString={docString} />
|
||||
Function: {displayName}
|
||||
</h5>
|
||||
<Form onSubmit={execute}>
|
||||
{args}
|
||||
<div>
|
||||
<Button variant="primary" type="submit">
|
||||
Execute
|
||||
</Button>
|
||||
</div>
|
||||
<Button variant="primary" type="submit">
|
||||
Execute
|
||||
<DocStringComponent docString={docString} />
|
||||
</Button>
|
||||
</Form>
|
||||
|
||||
<Collapse in={!hideOutput}>
|
||||
|
||||
@@ -1,73 +1,71 @@
|
||||
import React from 'react';
|
||||
import { ToastContainer, Toast } from 'react-bootstrap';
|
||||
|
||||
export type LevelName = 'CRITICAL' | 'ERROR' | 'WARNING' | 'INFO' | 'DEBUG';
|
||||
export type Notification = {
|
||||
id: number;
|
||||
time: string;
|
||||
text: string;
|
||||
timeStamp: string;
|
||||
message: string;
|
||||
levelname: LevelName;
|
||||
};
|
||||
|
||||
type NotificationProps = {
|
||||
showNotification: boolean;
|
||||
notifications: Notification[];
|
||||
exceptions: Notification[];
|
||||
removeNotificationById: (id: number) => void;
|
||||
removeExceptionById: (id: number) => void;
|
||||
};
|
||||
|
||||
export const Notifications = React.memo((props: NotificationProps) => {
|
||||
const {
|
||||
showNotification,
|
||||
notifications,
|
||||
exceptions,
|
||||
removeExceptionById,
|
||||
removeNotificationById
|
||||
} = props;
|
||||
const { showNotification, notifications, removeNotificationById } = props;
|
||||
|
||||
return (
|
||||
<ToastContainer
|
||||
className="navbarOffset toastContainer"
|
||||
position="top-end"
|
||||
style={{ position: 'fixed' }}>
|
||||
{showNotification &&
|
||||
notifications.map((notification) => (
|
||||
<ToastContainer className="navbarOffset toastContainer" position="top-end">
|
||||
{notifications.map((notification) => {
|
||||
// Determine if the toast should be shown
|
||||
const shouldShow =
|
||||
notification.levelname === 'ERROR' ||
|
||||
notification.levelname === 'CRITICAL' ||
|
||||
(showNotification &&
|
||||
['WARNING', 'INFO', 'DEBUG'].includes(notification.levelname));
|
||||
|
||||
if (!shouldShow) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<Toast
|
||||
className="notificationToast"
|
||||
className={notification.levelname.toLowerCase() + 'Toast'}
|
||||
key={notification.id}
|
||||
onClose={() => removeNotificationById(notification.id)}
|
||||
onClick={() => {
|
||||
removeNotificationById(notification.id);
|
||||
}}
|
||||
onClick={() => removeNotificationById(notification.id)}
|
||||
onMouseLeave={() => {
|
||||
removeNotificationById(notification.id);
|
||||
if (notification.levelname !== 'ERROR') {
|
||||
removeNotificationById(notification.id);
|
||||
}
|
||||
}}
|
||||
show={true}
|
||||
autohide={true}
|
||||
delay={2000}>
|
||||
<Toast.Header closeButton={false} className="notificationToast text-right">
|
||||
<strong className="me-auto">Notification</strong>
|
||||
<small>{notification.time}</small>
|
||||
autohide={
|
||||
notification.levelname === 'WARNING' ||
|
||||
notification.levelname === 'INFO' ||
|
||||
notification.levelname === 'DEBUG'
|
||||
}
|
||||
delay={
|
||||
notification.levelname === 'WARNING' ||
|
||||
notification.levelname === 'INFO' ||
|
||||
notification.levelname === 'DEBUG'
|
||||
? 2000
|
||||
: undefined
|
||||
}>
|
||||
<Toast.Header
|
||||
closeButton={false}
|
||||
className={notification.levelname.toLowerCase() + 'Toast text-right'}>
|
||||
<strong className="me-auto">{notification.levelname}</strong>
|
||||
<small>{notification.timeStamp}</small>
|
||||
</Toast.Header>
|
||||
<Toast.Body>{notification.text}</Toast.Body>
|
||||
<Toast.Body>{notification.message}</Toast.Body>
|
||||
</Toast>
|
||||
))}
|
||||
{exceptions.map((exception) => (
|
||||
<Toast
|
||||
className="exceptionToast"
|
||||
key={exception.id}
|
||||
onClose={() => removeExceptionById(exception.id)}
|
||||
onClick={() => {
|
||||
removeExceptionById(exception.id);
|
||||
}}
|
||||
show={true}
|
||||
autohide={false}>
|
||||
<Toast.Header closeButton className="exceptionToast text-right">
|
||||
<strong className="me-auto">Exception</strong>
|
||||
<small>{exception.time}</small>
|
||||
</Toast.Header>
|
||||
<Toast.Body>{exception.text}</Toast.Body>
|
||||
</Toast>
|
||||
))}
|
||||
);
|
||||
})}
|
||||
</ToastContainer>
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1,13 +1,38 @@
|
||||
import React, { useEffect, useRef, useState } from 'react';
|
||||
import React, { useContext, useEffect, useRef, useState } from 'react';
|
||||
import { WebSettingsContext } from '../WebSettings';
|
||||
import { Form, InputGroup } from 'react-bootstrap';
|
||||
import { setAttribute } from '../socket';
|
||||
import { DocStringComponent } from './DocStringComponent';
|
||||
import '../App.css';
|
||||
import { getIdFromFullAccessPath } from '../utils/stringUtils';
|
||||
import { LevelName } from './NotificationsComponent';
|
||||
|
||||
// TODO: add button functionality
|
||||
|
||||
interface NumberComponentProps {
|
||||
export type QuantityObject = {
|
||||
type: 'Quantity';
|
||||
readonly: boolean;
|
||||
value: {
|
||||
magnitude: number;
|
||||
unit: string;
|
||||
};
|
||||
doc?: string;
|
||||
};
|
||||
export type IntObject = {
|
||||
type: 'int';
|
||||
readonly: boolean;
|
||||
value: number;
|
||||
doc?: string;
|
||||
};
|
||||
export type FloatObject = {
|
||||
type: 'float';
|
||||
readonly: boolean;
|
||||
value: number;
|
||||
doc?: string;
|
||||
};
|
||||
export type NumberObject = IntObject | FloatObject | QuantityObject;
|
||||
|
||||
type NumberComponentProps = {
|
||||
name: string;
|
||||
type: 'float' | 'int';
|
||||
parentPath?: string;
|
||||
@@ -17,14 +42,8 @@ interface NumberComponentProps {
|
||||
isInstantUpdate: boolean;
|
||||
unit?: string;
|
||||
showName?: boolean;
|
||||
customEmitUpdate?: (
|
||||
name: string,
|
||||
parent_path: string,
|
||||
value: number,
|
||||
callback?: (ack: unknown) => void
|
||||
) => void;
|
||||
addNotification: (message: string) => void;
|
||||
}
|
||||
addNotification: (message: string, levelname?: LevelName) => void;
|
||||
};
|
||||
|
||||
// TODO: highlight the digit that is being changed by setting both selectionStart and
|
||||
// selectionEnd
|
||||
@@ -122,18 +141,20 @@ export const NumberComponent = React.memo((props: NumberComponentProps) => {
|
||||
|
||||
// Whether to show the name infront of the component (false if used with a slider)
|
||||
const showName = props.showName !== undefined ? props.showName : true;
|
||||
// If emitUpdate is passed, use this instead of the emit_update from the socket
|
||||
// Also used when used with a slider
|
||||
const emitUpdate =
|
||||
props.customEmitUpdate !== undefined ? props.customEmitUpdate : setAttribute;
|
||||
|
||||
const renderCount = useRef(0);
|
||||
// Create a state for the cursor position
|
||||
const [cursorPosition, setCursorPosition] = useState(null);
|
||||
// Create a state for the input string
|
||||
const [inputString, setInputString] = useState(props.value.toString());
|
||||
const fullAccessPath = parentPath.concat('.' + name);
|
||||
const fullAccessPath = [parentPath, name].filter((element) => element).join('.');
|
||||
const id = getIdFromFullAccessPath(fullAccessPath);
|
||||
const webSettings = useContext(WebSettingsContext);
|
||||
let displayName = name;
|
||||
|
||||
if (webSettings[fullAccessPath] && webSettings[fullAccessPath].displayName) {
|
||||
displayName = webSettings[fullAccessPath].displayName;
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
renderCount.current++;
|
||||
@@ -262,7 +283,7 @@ export const NumberComponent = React.memo((props: NumberComponentProps) => {
|
||||
selectionEnd
|
||||
));
|
||||
} else if (key === 'Enter' && !isInstantUpdate) {
|
||||
emitUpdate(name, parentPath, Number(newValue));
|
||||
setAttribute(name, parentPath, Number(newValue));
|
||||
return;
|
||||
} else {
|
||||
console.debug(key);
|
||||
@@ -271,7 +292,7 @@ export const NumberComponent = React.memo((props: NumberComponentProps) => {
|
||||
|
||||
// Update the input value and maintain the cursor position
|
||||
if (isInstantUpdate) {
|
||||
emitUpdate(name, parentPath, Number(newValue));
|
||||
setAttribute(name, parentPath, Number(newValue));
|
||||
}
|
||||
|
||||
setInputString(newValue);
|
||||
@@ -283,19 +304,23 @@ export const NumberComponent = React.memo((props: NumberComponentProps) => {
|
||||
const handleBlur = () => {
|
||||
if (!isInstantUpdate) {
|
||||
// If not in "instant update" mode, emit an update when the input field loses focus
|
||||
emitUpdate(name, parentPath, Number(inputString));
|
||||
setAttribute(name, parentPath, Number(inputString));
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="numberComponent" id={id}>
|
||||
{process.env.NODE_ENV === 'development' && showName && (
|
||||
<p>Render count: {renderCount.current}</p>
|
||||
{process.env.NODE_ENV === 'development' && (
|
||||
<div>Render count: {renderCount.current}</div>
|
||||
)}
|
||||
<DocStringComponent docString={docString} />
|
||||
<div className="d-flex">
|
||||
<InputGroup>
|
||||
{showName && <InputGroup.Text>{name}</InputGroup.Text>}
|
||||
{showName && (
|
||||
<InputGroup.Text>
|
||||
{displayName}
|
||||
<DocStringComponent docString={docString} />
|
||||
</InputGroup.Text>
|
||||
)}
|
||||
<Form.Control
|
||||
type="text"
|
||||
value={inputString}
|
||||
|
||||
@@ -1,23 +1,25 @@
|
||||
import React, { useEffect, useRef, useState } from 'react';
|
||||
import React, { useContext, useEffect, useRef, useState } from 'react';
|
||||
import { WebSettingsContext } from '../WebSettings';
|
||||
import { InputGroup, Form, Row, Col, Collapse, ToggleButton } from 'react-bootstrap';
|
||||
import { setAttribute } from '../socket';
|
||||
import { DocStringComponent } from './DocStringComponent';
|
||||
import { Slider } from '@mui/material';
|
||||
import { NumberComponent } from './NumberComponent';
|
||||
import { NumberComponent, NumberObject } from './NumberComponent';
|
||||
import { getIdFromFullAccessPath } from '../utils/stringUtils';
|
||||
import { LevelName } from './NotificationsComponent';
|
||||
|
||||
interface SliderComponentProps {
|
||||
type SliderComponentProps = {
|
||||
name: string;
|
||||
min: number;
|
||||
max: number;
|
||||
min: NumberObject;
|
||||
max: NumberObject;
|
||||
parentPath?: string;
|
||||
value: number;
|
||||
value: NumberObject;
|
||||
readOnly: boolean;
|
||||
docString: string;
|
||||
stepSize: number;
|
||||
stepSize: NumberObject;
|
||||
isInstantUpdate: boolean;
|
||||
addNotification: (message: string) => void;
|
||||
}
|
||||
addNotification: (message: string, levelname?: LevelName) => void;
|
||||
};
|
||||
|
||||
export const SliderComponent = React.memo((props: SliderComponentProps) => {
|
||||
const renderCount = useRef(0);
|
||||
@@ -29,13 +31,18 @@ export const SliderComponent = React.memo((props: SliderComponentProps) => {
|
||||
min,
|
||||
max,
|
||||
stepSize,
|
||||
readOnly,
|
||||
docString,
|
||||
isInstantUpdate,
|
||||
addNotification
|
||||
} = props;
|
||||
const fullAccessPath = parentPath.concat('.' + name);
|
||||
const fullAccessPath = [parentPath, name].filter((element) => element).join('.');
|
||||
const id = getIdFromFullAccessPath(fullAccessPath);
|
||||
const webSettings = useContext(WebSettingsContext);
|
||||
let displayName = name;
|
||||
|
||||
if (webSettings[fullAccessPath] && webSettings[fullAccessPath].displayName) {
|
||||
displayName = webSettings[fullAccessPath].displayName;
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
renderCount.current++;
|
||||
@@ -57,77 +64,68 @@ export const SliderComponent = React.memo((props: SliderComponentProps) => {
|
||||
addNotification(`${parentPath}.${name}.stepSize changed to ${stepSize}.`);
|
||||
}, [props.stepSize]);
|
||||
|
||||
const emitSliderUpdate = (
|
||||
name: string,
|
||||
parentPath: string,
|
||||
value: number,
|
||||
callback?: (ack: unknown) => void,
|
||||
min: number = props.min,
|
||||
max: number = props.max,
|
||||
stepSize: number = props.stepSize
|
||||
) => {
|
||||
setAttribute(
|
||||
name,
|
||||
parentPath,
|
||||
{
|
||||
value: value,
|
||||
min: min,
|
||||
max: max,
|
||||
step_size: stepSize
|
||||
},
|
||||
callback
|
||||
);
|
||||
};
|
||||
const handleOnChange = (event, newNumber: number | number[]) => {
|
||||
// This will never be the case as we do not have a range slider. However, we should
|
||||
// make sure this is properly handled.
|
||||
if (Array.isArray(newNumber)) {
|
||||
newNumber = newNumber[0];
|
||||
}
|
||||
emitSliderUpdate(name, parentPath, newNumber);
|
||||
setAttribute(`${name}.value`, parentPath, newNumber);
|
||||
};
|
||||
|
||||
const handleValueChange = (newValue: number, valueType: string) => {
|
||||
switch (valueType) {
|
||||
case 'min':
|
||||
emitSliderUpdate(name, parentPath, value, undefined, newValue);
|
||||
break;
|
||||
case 'max':
|
||||
emitSliderUpdate(name, parentPath, value, undefined, min, newValue);
|
||||
break;
|
||||
case 'stepSize':
|
||||
emitSliderUpdate(name, parentPath, value, undefined, min, max, newValue);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
setAttribute(`${name}.${valueType}`, parentPath, newValue);
|
||||
};
|
||||
|
||||
const deconstructNumberDict = (
|
||||
numberDict: NumberObject
|
||||
): [number, boolean, string | null] => {
|
||||
let numberMagnitude: number;
|
||||
let numberUnit: string | null = null;
|
||||
const numberReadOnly = numberDict.readonly;
|
||||
|
||||
if (numberDict.type === 'int' || numberDict.type === 'float') {
|
||||
numberMagnitude = numberDict.value;
|
||||
} else if (numberDict.type === 'Quantity') {
|
||||
numberMagnitude = numberDict.value.magnitude;
|
||||
numberUnit = numberDict.value.unit;
|
||||
}
|
||||
|
||||
return [numberMagnitude, numberReadOnly, numberUnit];
|
||||
};
|
||||
|
||||
const [valueMagnitude, valueReadOnly, valueUnit] = deconstructNumberDict(value);
|
||||
const [minMagnitude, minReadOnly] = deconstructNumberDict(min);
|
||||
const [maxMagnitude, maxReadOnly] = deconstructNumberDict(max);
|
||||
const [stepSizeMagnitude, stepSizeReadOnly] = deconstructNumberDict(stepSize);
|
||||
|
||||
return (
|
||||
<div className="sliderComponent" id={id}>
|
||||
{process.env.NODE_ENV === 'development' && (
|
||||
<p>Render count: {renderCount.current}</p>
|
||||
<div>Render count: {renderCount.current}</div>
|
||||
)}
|
||||
|
||||
<DocStringComponent docString={docString} />
|
||||
<Row>
|
||||
<Col xs="auto" xl="auto">
|
||||
<InputGroup.Text>{name}</InputGroup.Text>
|
||||
<InputGroup.Text>
|
||||
{displayName}
|
||||
<DocStringComponent docString={docString} />
|
||||
</InputGroup.Text>
|
||||
</Col>
|
||||
<Col xs="5" xl>
|
||||
<Slider
|
||||
style={{ margin: '0px 0px 10px 0px' }}
|
||||
aria-label="Always visible"
|
||||
// valueLabelDisplay="on"
|
||||
disabled={readOnly}
|
||||
value={value}
|
||||
disabled={valueReadOnly}
|
||||
value={valueMagnitude}
|
||||
onChange={(event, newNumber) => handleOnChange(event, newNumber)}
|
||||
min={min}
|
||||
max={max}
|
||||
step={stepSize}
|
||||
min={minMagnitude}
|
||||
max={maxMagnitude}
|
||||
step={stepSizeMagnitude}
|
||||
marks={[
|
||||
{ value: min, label: `${min}` },
|
||||
{ value: max, label: `${max}` }
|
||||
{ value: minMagnitude, label: `${minMagnitude}` },
|
||||
{ value: maxMagnitude, label: `${maxMagnitude}` }
|
||||
]}
|
||||
/>
|
||||
</Col>
|
||||
@@ -135,13 +133,13 @@ export const SliderComponent = React.memo((props: SliderComponentProps) => {
|
||||
<NumberComponent
|
||||
isInstantUpdate={isInstantUpdate}
|
||||
parentPath={parentPath}
|
||||
name={name}
|
||||
name={`${name}.value`}
|
||||
docString=""
|
||||
readOnly={readOnly}
|
||||
readOnly={valueReadOnly}
|
||||
type="float"
|
||||
value={value}
|
||||
value={valueMagnitude}
|
||||
unit={valueUnit}
|
||||
showName={false}
|
||||
customEmitUpdate={emitSliderUpdate}
|
||||
addNotification={() => null}
|
||||
/>
|
||||
</Col>
|
||||
@@ -177,7 +175,8 @@ export const SliderComponent = React.memo((props: SliderComponentProps) => {
|
||||
<Form.Label>Min Value</Form.Label>
|
||||
<Form.Control
|
||||
type="number"
|
||||
value={min}
|
||||
value={minMagnitude}
|
||||
disabled={minReadOnly}
|
||||
onChange={(e) => handleValueChange(Number(e.target.value), 'min')}
|
||||
/>
|
||||
</Col>
|
||||
@@ -186,7 +185,8 @@ export const SliderComponent = React.memo((props: SliderComponentProps) => {
|
||||
<Form.Label>Max Value</Form.Label>
|
||||
<Form.Control
|
||||
type="number"
|
||||
value={max}
|
||||
value={maxMagnitude}
|
||||
disabled={maxReadOnly}
|
||||
onChange={(e) => handleValueChange(Number(e.target.value), 'max')}
|
||||
/>
|
||||
</Col>
|
||||
@@ -195,8 +195,9 @@ export const SliderComponent = React.memo((props: SliderComponentProps) => {
|
||||
<Form.Label>Step Size</Form.Label>
|
||||
<Form.Control
|
||||
type="number"
|
||||
value={stepSize}
|
||||
onChange={(e) => handleValueChange(Number(e.target.value), 'stepSize')}
|
||||
value={stepSizeMagnitude}
|
||||
disabled={stepSizeReadOnly}
|
||||
onChange={(e) => handleValueChange(Number(e.target.value), 'step_size')}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
|
||||
@@ -1,21 +1,23 @@
|
||||
import React, { useEffect, useRef, useState } from 'react';
|
||||
import React, { useContext, useEffect, useRef, useState } from 'react';
|
||||
import { Form, InputGroup } from 'react-bootstrap';
|
||||
import { setAttribute } from '../socket';
|
||||
import { DocStringComponent } from './DocStringComponent';
|
||||
import '../App.css';
|
||||
import { getIdFromFullAccessPath } from '../utils/stringUtils';
|
||||
import { LevelName } from './NotificationsComponent';
|
||||
import { WebSettingsContext } from '../WebSettings';
|
||||
|
||||
// TODO: add button functionality
|
||||
|
||||
interface StringComponentProps {
|
||||
type StringComponentProps = {
|
||||
name: string;
|
||||
parentPath?: string;
|
||||
value: string;
|
||||
readOnly: boolean;
|
||||
docString: string;
|
||||
isInstantUpdate: boolean;
|
||||
addNotification: (message: string) => void;
|
||||
}
|
||||
addNotification: (message: string, levelname?: LevelName) => void;
|
||||
};
|
||||
|
||||
export const StringComponent = React.memo((props: StringComponentProps) => {
|
||||
const { name, parentPath, readOnly, docString, isInstantUpdate, addNotification } =
|
||||
@@ -23,8 +25,14 @@ export const StringComponent = React.memo((props: StringComponentProps) => {
|
||||
|
||||
const renderCount = useRef(0);
|
||||
const [inputString, setInputString] = useState(props.value);
|
||||
const fullAccessPath = parentPath.concat('.' + name);
|
||||
const fullAccessPath = [parentPath, name].filter((element) => element).join('.');
|
||||
const id = getIdFromFullAccessPath(fullAccessPath);
|
||||
const webSettings = useContext(WebSettingsContext);
|
||||
let displayName = name;
|
||||
|
||||
if (webSettings[fullAccessPath] && webSettings[fullAccessPath].displayName) {
|
||||
displayName = webSettings[fullAccessPath].displayName;
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
renderCount.current++;
|
||||
@@ -60,11 +68,13 @@ export const StringComponent = React.memo((props: StringComponentProps) => {
|
||||
return (
|
||||
<div className={'stringComponent'} id={id}>
|
||||
{process.env.NODE_ENV === 'development' && (
|
||||
<p>Render count: {renderCount.current}</p>
|
||||
<div>Render count: {renderCount.current}</div>
|
||||
)}
|
||||
<DocStringComponent docString={docString} />
|
||||
<InputGroup>
|
||||
<InputGroup.Text>{name}</InputGroup.Text>
|
||||
<InputGroup.Text>
|
||||
{displayName}
|
||||
<DocStringComponent docString={docString} />
|
||||
</InputGroup.Text>
|
||||
<Form.Control
|
||||
type="text"
|
||||
value={inputString}
|
||||
|
||||
108
frontend/src/utils/stateUtils.ts
Normal file
108
frontend/src/utils/stateUtils.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
export interface SerializedValue {
|
||||
type: string;
|
||||
value: Record<string, unknown> | Array<Record<string, unknown>>;
|
||||
readonly: boolean;
|
||||
doc: string | null;
|
||||
async?: boolean;
|
||||
parameters?: unknown;
|
||||
}
|
||||
export type State = Record<string, SerializedValue> | null;
|
||||
|
||||
export function setNestedValueByPath(
|
||||
serializationDict: Record<string, SerializedValue>,
|
||||
path: string,
|
||||
serializedValue: SerializedValue
|
||||
): Record<string, SerializedValue> {
|
||||
const parentPathParts = path.split('.').slice(0, -1);
|
||||
const attrName = path.split('.').pop();
|
||||
|
||||
if (!attrName) {
|
||||
throw new Error('Invalid path');
|
||||
}
|
||||
|
||||
let currentSerializedValue: SerializedValue;
|
||||
const newSerializationDict: Record<string, SerializedValue> = JSON.parse(
|
||||
JSON.stringify(serializationDict)
|
||||
);
|
||||
|
||||
let currentDict = newSerializationDict;
|
||||
|
||||
try {
|
||||
for (const pathPart of parentPathParts) {
|
||||
currentSerializedValue = getNextLevelDictByKey(currentDict, pathPart, false);
|
||||
// @ts-expect-error The value will be of type SerializedValue as we are still
|
||||
// looping through the parent parts
|
||||
currentDict = currentSerializedValue['value'];
|
||||
}
|
||||
|
||||
currentSerializedValue = getNextLevelDictByKey(currentDict, attrName, true);
|
||||
|
||||
Object.assign(currentSerializedValue, serializedValue);
|
||||
return newSerializationDict;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return currentDict;
|
||||
}
|
||||
}
|
||||
|
||||
function getNextLevelDictByKey(
|
||||
serializationDict: Record<string, SerializedValue>,
|
||||
attrName: string,
|
||||
allowAppend: boolean = false
|
||||
): SerializedValue {
|
||||
const [key, index] = parseListAttrAndIndex(attrName);
|
||||
let currentDict: SerializedValue;
|
||||
|
||||
try {
|
||||
if (index !== null) {
|
||||
if (!serializationDict[key] || !Array.isArray(serializationDict[key]['value'])) {
|
||||
throw new Error(`Expected an array at '${key}', but found something else.`);
|
||||
}
|
||||
|
||||
if (index < serializationDict[key]['value'].length) {
|
||||
currentDict = serializationDict[key]['value'][index];
|
||||
} else if (allowAppend && index === serializationDict[key]['value'].length) {
|
||||
// Appending to list
|
||||
// @ts-expect-error When the index is not null, I expect an array
|
||||
serializationDict[key]['value'].push({});
|
||||
currentDict = serializationDict[key]['value'][index];
|
||||
} else {
|
||||
throw new Error(`Index out of range for '${key}[${index}]'.`);
|
||||
}
|
||||
} else {
|
||||
if (!serializationDict[key]) {
|
||||
throw new Error(`Key '${key}' not found.`);
|
||||
}
|
||||
currentDict = serializationDict[key];
|
||||
}
|
||||
} catch (error) {
|
||||
throw new Error(`Error occurred trying to access '${attrName}': ${error}`);
|
||||
}
|
||||
|
||||
if (typeof currentDict !== 'object' || currentDict === null) {
|
||||
throw new Error(
|
||||
`Expected a dictionary at '${attrName}', but found type '${typeof currentDict}' instead.`
|
||||
);
|
||||
}
|
||||
|
||||
return currentDict;
|
||||
}
|
||||
|
||||
function parseListAttrAndIndex(attrString: string): [string, number | null] {
|
||||
let index: number | null = null;
|
||||
let attrName = attrString;
|
||||
|
||||
if (attrString.includes('[') && attrString.endsWith(']')) {
|
||||
const parts = attrString.split('[');
|
||||
attrName = parts[0];
|
||||
const indexPart = parts[1].slice(0, -1); // Removes the closing ']'
|
||||
|
||||
if (!isNaN(parseInt(indexPart))) {
|
||||
index = parseInt(indexPart);
|
||||
} else {
|
||||
console.error(`Invalid index format in key: ${attrString}`);
|
||||
}
|
||||
}
|
||||
|
||||
return [attrName, index];
|
||||
}
|
||||
@@ -1,12 +1,16 @@
|
||||
export function getIdFromFullAccessPath(fullAccessPath: string) {
|
||||
// Replace '].' with a single dash
|
||||
let id = fullAccessPath.replace(/\]\./g, '-');
|
||||
if (fullAccessPath) {
|
||||
// Replace '].' with a single dash
|
||||
let id = fullAccessPath.replace(/\]\./g, '-');
|
||||
|
||||
// Replace any character that is not a word character or underscore with a dash
|
||||
id = id.replace(/[^\w_]+/g, '-');
|
||||
// Replace any character that is not a word character or underscore with a dash
|
||||
id = id.replace(/[^\w_]+/g, '-');
|
||||
|
||||
// Remove any trailing dashes
|
||||
id = id.replace(/-+$/, '');
|
||||
// Remove any trailing dashes
|
||||
id = id.replace(/-+$/, '');
|
||||
|
||||
return id;
|
||||
return id;
|
||||
} else {
|
||||
return 'main';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ nav:
|
||||
- Developer Guide: dev-guide/README.md
|
||||
- API Reference: dev-guide/api.md
|
||||
- Adding Components: dev-guide/Adding_Components.md
|
||||
- Observer Pattern Implementation: dev-guide/Observer_Pattern_Implementation.md # <-- New section
|
||||
- About:
|
||||
- Release Notes: about/release-notes.md
|
||||
- Contributing: about/contributing.md
|
||||
|
||||
1010
poetry.lock
generated
1010
poetry.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,2 +0,0 @@
|
||||
[virtualenvs]
|
||||
in-project = true
|
||||
@@ -1,6 +1,6 @@
|
||||
[tool.poetry]
|
||||
name = "pydase"
|
||||
version = "0.3.0"
|
||||
version = "0.5.2"
|
||||
description = "A flexible and robust Python library for creating, managing, and interacting with data services, with built-in support for web and RPC servers, and customizable features for diverse use cases."
|
||||
authors = ["Mose Mueller <mosmuell@ethz.ch>"]
|
||||
readme = "README.md"
|
||||
@@ -19,24 +19,22 @@ confz = "^2.0.0"
|
||||
pint = "^0.22"
|
||||
pillow = "^10.0.0"
|
||||
|
||||
[tool.poetry.group.dev]
|
||||
optional = true
|
||||
|
||||
[tool.poetry.group.dev.dependencies]
|
||||
types-toml = "^0.10.8.6"
|
||||
pytest = "^7.4.0"
|
||||
pytest-cov = "^4.1.0"
|
||||
mypy = "^1.4.1"
|
||||
black = "^23.1.0"
|
||||
isort = "^5.12.0"
|
||||
flake8 = "^5.0.4"
|
||||
flake8-use-fstring = "^1.4"
|
||||
flake8-functions = "^0.0.7"
|
||||
flake8-comprehensions = "^3.11.1"
|
||||
flake8-pep585 = "^0.1.7"
|
||||
flake8-pep604 = "^0.1.0"
|
||||
flake8-eradicate = "^1.4.0"
|
||||
matplotlib = "^3.7.2"
|
||||
pyright = "^1.1.323"
|
||||
pytest-mock = "^3.11.1"
|
||||
ruff = "^0.1.5"
|
||||
pytest-asyncio = "^0.23.2"
|
||||
|
||||
[tool.poetry.group.docs]
|
||||
optional = true
|
||||
|
||||
[tool.poetry.group.docs.dependencies]
|
||||
mkdocs = "^1.5.2"
|
||||
@@ -48,39 +46,59 @@ pymdown-extensions = "^10.1"
|
||||
requires = ["poetry-core"]
|
||||
build-backend = "poetry.core.masonry.api"
|
||||
|
||||
[tool.ruff]
|
||||
target-version = "py310" # Always generate Python 3.10-compatible code
|
||||
select = [
|
||||
"ASYNC", # flake8-async
|
||||
"C4", # flake8-comprehensions
|
||||
"C901", # mccabe complex-structure
|
||||
"E", # pycodestyle errors
|
||||
"ERA", # eradicate
|
||||
"F", # pyflakes
|
||||
"FLY", # flynt
|
||||
"G", # flake8-logging-format
|
||||
"I", # isort
|
||||
"ICN", # flake8-import-conventions
|
||||
"INP", # flake8-no-pep420
|
||||
"ISC", # flake8-implicit-str-concat
|
||||
"N", # pep8-naming
|
||||
"NPY", # NumPy-specific rules
|
||||
"PERF", # perflint
|
||||
"PIE", # flake8-pie
|
||||
"PL", # pylint
|
||||
"PYI", # flake8-pyi
|
||||
"Q", # flake8-quotes
|
||||
"RET", # flake8-return
|
||||
"RUF", # Ruff-specific rules
|
||||
"SIM", # flake8-simplify
|
||||
"TID", # flake8-tidy-imports
|
||||
"TCH", # flake8-type-checking
|
||||
"UP", # pyupgrade
|
||||
"YTT", # flake8-2020
|
||||
"W", # pycodestyle warnings
|
||||
]
|
||||
ignore = [
|
||||
"E203", # whitespace-before-punctuation
|
||||
"W292", # missing-newline-at-end-of-file
|
||||
"PERF203", # try-except-in-loop
|
||||
]
|
||||
extend-exclude = [
|
||||
"docs", "frontend"
|
||||
]
|
||||
|
||||
[tool.ruff.lint.mccabe]
|
||||
max-complexity = 7
|
||||
|
||||
|
||||
[tool.pyright]
|
||||
include = ["src/pydase"]
|
||||
exclude = ["**/node_modules", "**/__pycache__", "docs", "frontend", "tests"]
|
||||
venvPath = "."
|
||||
venv = ".venv"
|
||||
typeCheckingMode = "basic"
|
||||
reportUnknownMemberType = true
|
||||
reportUnknownParameterType = true
|
||||
|
||||
[tool.black]
|
||||
line-length = 88
|
||||
exclude = '''
|
||||
/(
|
||||
\.git
|
||||
| \.mypy_cache
|
||||
| \.tox
|
||||
| venv
|
||||
| \.venv
|
||||
| _build
|
||||
| buck-out
|
||||
| build
|
||||
| dist
|
||||
)/
|
||||
'''
|
||||
|
||||
[tool.isort]
|
||||
profile = "black"
|
||||
|
||||
[tool.mypy]
|
||||
mypy_path = "src/"
|
||||
show_error_codes = true
|
||||
disallow_untyped_defs = true
|
||||
disallow_untyped_calls = true
|
||||
disallow_incomplete_defs = true
|
||||
disallow_any_generics = true
|
||||
check_untyped_defs = true
|
||||
ignore_missing_imports = false
|
||||
|
||||
@@ -57,5 +57,3 @@ class ColouredEnum(Enum):
|
||||
my_service.status = MyStatus.FAILED
|
||||
```
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
@@ -2,10 +2,10 @@ import base64
|
||||
import io
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
from typing import TYPE_CHECKING
|
||||
from urllib.request import urlopen
|
||||
|
||||
import PIL.Image # type: ignore
|
||||
import PIL.Image # type: ignore[import-untyped]
|
||||
|
||||
from pydase.data_service.data_service import DataService
|
||||
|
||||
@@ -19,9 +19,9 @@ class Image(DataService):
|
||||
def __init__(
|
||||
self,
|
||||
) -> None:
|
||||
super().__init__()
|
||||
self._value: str = ""
|
||||
self._format: str = ""
|
||||
super().__init__()
|
||||
|
||||
@property
|
||||
def value(self) -> str:
|
||||
@@ -33,19 +33,19 @@ class Image(DataService):
|
||||
|
||||
def load_from_path(self, path: Path | str) -> None:
|
||||
with PIL.Image.open(path) as image:
|
||||
self._load_from_PIL(image)
|
||||
self._load_from_pil(image)
|
||||
|
||||
def load_from_matplotlib_figure(self, fig: "Figure", format_: str = "png") -> None:
|
||||
buffer = io.BytesIO()
|
||||
fig.savefig(buffer, format=format_) # type: ignore
|
||||
fig.savefig(buffer, format=format_)
|
||||
value_ = base64.b64encode(buffer.getvalue())
|
||||
self._load_from_base64(value_, format_)
|
||||
|
||||
def load_from_url(self, url: str) -> None:
|
||||
image = PIL.Image.open(urlopen(url))
|
||||
self._load_from_PIL(image)
|
||||
self._load_from_pil(image)
|
||||
|
||||
def load_from_base64(self, value_: bytes, format_: Optional[str] = None) -> None:
|
||||
def load_from_base64(self, value_: bytes, format_: str | None = None) -> None:
|
||||
if format_ is None:
|
||||
format_ = self._get_image_format_from_bytes(value_)
|
||||
if format_ is None:
|
||||
@@ -60,7 +60,7 @@ class Image(DataService):
|
||||
self._value = value
|
||||
self._format = format_
|
||||
|
||||
def _load_from_PIL(self, image: PIL.Image.Image) -> None:
|
||||
def _load_from_pil(self, image: PIL.Image.Image) -> None:
|
||||
if image.format is not None:
|
||||
format_ = image.format
|
||||
buffer = io.BytesIO()
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import logging
|
||||
from typing import Any, Literal
|
||||
from typing import Any
|
||||
|
||||
from pydase.data_service.data_service import DataService
|
||||
|
||||
@@ -13,23 +13,68 @@ class NumberSlider(DataService):
|
||||
|
||||
Parameters:
|
||||
-----------
|
||||
value (float | int, optional):
|
||||
value (float, optional):
|
||||
The initial value of the slider. Defaults to 0.
|
||||
min (float, optional):
|
||||
The minimum value of the slider. Defaults to 0.
|
||||
max (float, optional):
|
||||
The maximum value of the slider. Defaults to 100.
|
||||
step_size (float | int, optional):
|
||||
step_size (float, optional):
|
||||
The increment/decrement step size of the slider. Defaults to 1.0.
|
||||
type (Literal["int"] | Literal["float"], optional):
|
||||
The type of the slider value. Determines if the value is an integer or float.
|
||||
Defaults to "float".
|
||||
|
||||
Example:
|
||||
--------
|
||||
```python
|
||||
class MyService(DataService):
|
||||
voltage = NumberSlider(1, 0, 10, 0.1, "int")
|
||||
class MySlider(pydase.components.NumberSlider):
|
||||
def __init__(
|
||||
self,
|
||||
value: float = 0.0,
|
||||
min_: float = 0.0,
|
||||
max_: float = 100.0,
|
||||
step_size: float = 1.0,
|
||||
) -> None:
|
||||
super().__init__(value, min_, max_, step_size)
|
||||
|
||||
@property
|
||||
def min(self) -> float:
|
||||
return self._min
|
||||
|
||||
@min.setter
|
||||
def min(self, value: float) -> None:
|
||||
self._min = value
|
||||
|
||||
@property
|
||||
def max(self) -> float:
|
||||
return self._max
|
||||
|
||||
@max.setter
|
||||
def max(self, value: float) -> None:
|
||||
self._max = value
|
||||
|
||||
@property
|
||||
def step_size(self) -> float:
|
||||
return self._step_size
|
||||
|
||||
@step_size.setter
|
||||
def step_size(self, value: float) -> None:
|
||||
self._step_size = value
|
||||
|
||||
@property
|
||||
def value(self) -> float:
|
||||
return self._value
|
||||
|
||||
@value.setter
|
||||
def value(self, value: float) -> None:
|
||||
if value < self._min or value > self._max:
|
||||
raise ValueError(
|
||||
"Value is either below allowed min or above max value."
|
||||
)
|
||||
|
||||
self._value = value
|
||||
|
||||
class MyService(pydase.DataService):
|
||||
def __init__(self) -> None:
|
||||
self.voltage = MyService()
|
||||
|
||||
# Modifying or accessing the voltage value:
|
||||
my_service = MyService()
|
||||
@@ -40,28 +85,37 @@ class NumberSlider(DataService):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
value: float | int = 0,
|
||||
min: float = 0.0,
|
||||
max: float = 100.0,
|
||||
step_size: float | int = 1.0,
|
||||
type: Literal["int"] | Literal["float"] = "float",
|
||||
value: Any = 0.0,
|
||||
min_: float = 0.0,
|
||||
max_: float = 100.0,
|
||||
step_size: float = 1.0,
|
||||
) -> None:
|
||||
if type not in {"float", "int"}:
|
||||
logger.error(f"Unknown type '{type}'. Using 'float'.")
|
||||
type = "float"
|
||||
|
||||
self._type = type
|
||||
self.step_size = step_size
|
||||
self.value = value
|
||||
self.min = min
|
||||
self.max = max
|
||||
|
||||
super().__init__()
|
||||
self._step_size = step_size
|
||||
self._value = value
|
||||
self._min = min_
|
||||
self._max = max_
|
||||
|
||||
def __setattr__(self, name: str, value: Any) -> None:
|
||||
if name in ["value", "step_size"]:
|
||||
value = int(value) if self._type == "int" else float(value)
|
||||
elif not name.startswith("_"):
|
||||
value = float(value)
|
||||
@property
|
||||
def min(self) -> float:
|
||||
"""The min property."""
|
||||
return self._min
|
||||
|
||||
return super().__setattr__(name, value)
|
||||
@property
|
||||
def max(self) -> float:
|
||||
"""The min property."""
|
||||
return self._max
|
||||
|
||||
@property
|
||||
def step_size(self) -> float:
|
||||
"""The min property."""
|
||||
return self._step_size
|
||||
|
||||
@property
|
||||
def value(self) -> Any:
|
||||
"""The value property."""
|
||||
return self._value
|
||||
|
||||
@value.setter
|
||||
def value(self, value: Any) -> None:
|
||||
self._value = value
|
||||
|
||||
@@ -1,9 +1,24 @@
|
||||
from pathlib import Path
|
||||
from typing import Literal
|
||||
|
||||
from confz import BaseConfig, EnvSource
|
||||
|
||||
|
||||
class OperationMode(BaseConfig): # type: ignore
|
||||
environment: Literal["development"] | Literal["production"] = "development"
|
||||
class OperationMode(BaseConfig): # type: ignore[misc]
|
||||
environment: Literal["development", "production"] = "development"
|
||||
|
||||
CONFIG_SOURCES = EnvSource(allow=["ENVIRONMENT"])
|
||||
|
||||
|
||||
class ServiceConfig(BaseConfig): # type: ignore[misc]
|
||||
config_dir: Path = Path("config")
|
||||
web_port: int = 8001
|
||||
rpc_port: int = 18871
|
||||
|
||||
CONFIG_SOURCES = EnvSource(allow_all=True, prefix="SERVICE_")
|
||||
|
||||
|
||||
class WebServerConfig(BaseConfig): # type: ignore[misc]
|
||||
generate_web_settings: bool = False
|
||||
|
||||
CONFIG_SOURCES = EnvSource(allow=["GENERATE_WEB_SETTINGS"])
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from pydase.observer_pattern.observable.observable import Observable
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from pydase.data_service.callback_manager import CallbackManager
|
||||
from pydase.data_service.data_service import DataService
|
||||
from pydase.data_service.task_manager import TaskManager
|
||||
|
||||
|
||||
class AbstractDataService(ABC):
|
||||
class AbstractDataService(Observable):
|
||||
__root__: DataService
|
||||
_task_manager: TaskManager
|
||||
_callback_manager: CallbackManager
|
||||
_autostart_tasks: dict[str, tuple[Any]]
|
||||
|
||||
@@ -1,413 +0,0 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import inspect
|
||||
import logging
|
||||
from collections.abc import Callable
|
||||
from typing import TYPE_CHECKING, Any, cast
|
||||
|
||||
from pydase.data_service.abstract_data_service import AbstractDataService
|
||||
from pydase.utils.helpers import get_class_and_instance_attributes
|
||||
|
||||
from .data_service_list import DataServiceList
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .data_service import DataService
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class CallbackManager:
|
||||
_notification_callbacks: list[Callable[[str, str, Any], Any]] = []
|
||||
"""
|
||||
A list of callback functions that are executed when a change occurs in the
|
||||
DataService instance. These functions are intended to handle or respond to these
|
||||
changes in some way, such as emitting a socket.io message to the frontend.
|
||||
|
||||
Each function in this list should be a callable that accepts three parameters:
|
||||
|
||||
- parent_path (str): The path to the parent of the attribute that was changed.
|
||||
- name (str): The name of the attribute that was changed.
|
||||
- value (Any): The new value of the attribute.
|
||||
|
||||
A callback function can be added to this list using the add_notification_callback
|
||||
method. Whenever a change in the DataService instance occurs (or in its nested
|
||||
DataService or DataServiceList instances), the emit_notification method is invoked,
|
||||
which in turn calls all the callback functions in _notification_callbacks with the
|
||||
appropriate arguments.
|
||||
|
||||
This implementation follows the observer pattern, with the DataService instance as
|
||||
the "subject" and the callback functions as the "observers".
|
||||
"""
|
||||
_list_mapping: dict[int, DataServiceList] = {}
|
||||
"""
|
||||
A dictionary mapping the id of the original lists to the corresponding
|
||||
DataServiceList instances.
|
||||
This is used to ensure that all references to the same list within the DataService
|
||||
object point to the same DataServiceList, so that any modifications to that list can
|
||||
be tracked consistently. The keys of the dictionary are the ids of the original
|
||||
lists, and the values are the DataServiceList instances that wrap these lists.
|
||||
"""
|
||||
|
||||
def __init__(self, service: DataService) -> None:
|
||||
self.callbacks: set[Callable[[str, Any], None]] = set()
|
||||
self.service = service
|
||||
|
||||
def _register_list_change_callbacks( # noqa: C901
|
||||
self, obj: "AbstractDataService", parent_path: str
|
||||
) -> None:
|
||||
"""
|
||||
This method ensures that notifications are emitted whenever a public list
|
||||
attribute of a DataService instance changes. These notifications pertain solely
|
||||
to the list item changes, not to changes in attributes of objects within the
|
||||
list.
|
||||
|
||||
The method works by converting all list attributes (both at the class and
|
||||
instance levels) into DataServiceList objects. Each DataServiceList is then
|
||||
assigned a callback that is triggered whenever an item in the list is updated.
|
||||
The callback emits a notification, but only if the DataService instance was the
|
||||
root instance when the callback was registered.
|
||||
|
||||
This method operates recursively, processing the input object and all nested
|
||||
attributes that are instances of DataService. While navigating the structure,
|
||||
it constructs a path for each attribute that traces back to the root. This path
|
||||
is included in any emitted notifications to facilitate identification of the
|
||||
source of a change.
|
||||
|
||||
Parameters:
|
||||
-----------
|
||||
obj: DataService
|
||||
The target object to be processed. All list attributes (and those of its
|
||||
nested DataService attributes) will be converted into DataServiceList
|
||||
objects.
|
||||
parent_path: str
|
||||
The access path for the parent object. Used to construct the full access
|
||||
path for the notifications.
|
||||
"""
|
||||
|
||||
# Convert all list attributes (both class and instance) to DataServiceList
|
||||
attrs = get_class_and_instance_attributes(obj)
|
||||
|
||||
for attr_name, attr_value in attrs.items():
|
||||
if isinstance(attr_value, AbstractDataService):
|
||||
new_path = f"{parent_path}.{attr_name}"
|
||||
self._register_list_change_callbacks(attr_value, new_path)
|
||||
elif isinstance(attr_value, list):
|
||||
# Create callback for current attr_name
|
||||
# Default arguments solve the late binding problem by capturing the
|
||||
# value at the time the lambda is defined, not when it is called. This
|
||||
# prevents attr_name from being overwritten in the next loop iteration.
|
||||
callback = (
|
||||
lambda index, value, attr_name=attr_name: self.service._callback_manager.emit_notification(
|
||||
parent_path=parent_path,
|
||||
name=f"{attr_name}[{index}]",
|
||||
value=value,
|
||||
)
|
||||
if self.service == self.service.__root__
|
||||
# Skip private and protected lists
|
||||
and not cast(str, attr_name).startswith("_")
|
||||
else None
|
||||
)
|
||||
|
||||
# Check if attr_value is already a DataServiceList or in the mapping
|
||||
if isinstance(attr_value, DataServiceList):
|
||||
attr_value.add_callback(callback)
|
||||
continue
|
||||
if id(attr_value) in self._list_mapping:
|
||||
# If the list `attr_value` was already referenced somewhere else
|
||||
notifying_list = self._list_mapping[id(attr_value)]
|
||||
notifying_list.add_callback(callback)
|
||||
else:
|
||||
# convert the builtin list into a DataServiceList and add the
|
||||
# callback
|
||||
notifying_list = DataServiceList(attr_value, callback=[callback])
|
||||
self._list_mapping[id(attr_value)] = notifying_list
|
||||
|
||||
setattr(obj, attr_name, notifying_list)
|
||||
|
||||
# recursively add callbacks to list attributes of DataService instances
|
||||
for i, item in enumerate(attr_value):
|
||||
if isinstance(item, AbstractDataService):
|
||||
new_path = f"{parent_path}.{attr_name}[{i}]"
|
||||
self._register_list_change_callbacks(item, new_path)
|
||||
|
||||
def _register_DataService_instance_callbacks(
|
||||
self, obj: "AbstractDataService", parent_path: str
|
||||
) -> None:
|
||||
"""
|
||||
This function is a key part of the observer pattern implemented by the
|
||||
DataService class.
|
||||
Its purpose is to allow the system to automatically send out notifications
|
||||
whenever an attribute of a DataService instance is updated, which is especially
|
||||
useful when the DataService instance is part of a nested structure.
|
||||
|
||||
It works by recursively registering callbacks for a given DataService instance
|
||||
and all of its nested attributes. Each callback is responsible for emitting a
|
||||
notification when the attribute it is attached to is modified.
|
||||
|
||||
This function ensures that only the root DataService instance (the one directly
|
||||
exposed to the user or another system via rpyc) emits notifications.
|
||||
|
||||
Each notification contains a 'parent_path' that traces the attribute's location
|
||||
within the nested DataService structure, starting from the root. This makes it
|
||||
easier for observers to determine exactly where a change has occurred.
|
||||
|
||||
Parameters:
|
||||
-----------
|
||||
obj: DataService
|
||||
The target object on which callbacks are to be registered.
|
||||
parent_path: str
|
||||
The access path for the parent object. This is used to construct the full
|
||||
access path for the notifications.
|
||||
"""
|
||||
|
||||
# Create and register a callback for the object
|
||||
# only emit the notification when the call was registered by the root object
|
||||
callback: Callable[[str, Any], None] = (
|
||||
lambda name, value: obj._callback_manager.emit_notification(
|
||||
parent_path=parent_path, name=name, value=value
|
||||
)
|
||||
if self.service == obj.__root__
|
||||
and not name.startswith("_") # we are only interested in public attributes
|
||||
and not isinstance(
|
||||
getattr(type(obj), name, None), property
|
||||
) # exlude proerty notifications -> those are handled in separate callbacks
|
||||
else None
|
||||
)
|
||||
|
||||
obj._callback_manager.callbacks.add(callback)
|
||||
|
||||
# Recursively register callbacks for all nested attributes of the object
|
||||
attrs = get_class_and_instance_attributes(obj)
|
||||
|
||||
for nested_attr_name, nested_attr in attrs.items():
|
||||
if isinstance(nested_attr, DataServiceList):
|
||||
self._register_list_callbacks(
|
||||
nested_attr, parent_path, nested_attr_name
|
||||
)
|
||||
elif isinstance(nested_attr, AbstractDataService):
|
||||
self._register_service_callbacks(
|
||||
nested_attr, parent_path, nested_attr_name
|
||||
)
|
||||
|
||||
def _register_list_callbacks(
|
||||
self, nested_attr: list[Any], parent_path: str, attr_name: str
|
||||
) -> None:
|
||||
"""Handles registration of callbacks for list attributes"""
|
||||
for i, list_item in enumerate(nested_attr):
|
||||
if isinstance(list_item, AbstractDataService):
|
||||
self._register_service_callbacks(
|
||||
list_item, parent_path, f"{attr_name}[{i}]"
|
||||
)
|
||||
|
||||
def _register_service_callbacks(
|
||||
self, nested_attr: "AbstractDataService", parent_path: str, attr_name: str
|
||||
) -> None:
|
||||
"""Handles registration of callbacks for DataService attributes"""
|
||||
|
||||
# as the DataService is an attribute of self, change the root object
|
||||
# use the dictionary to not trigger callbacks on initialised objects
|
||||
nested_attr.__dict__["__root__"] = self.service.__root__
|
||||
|
||||
new_path = f"{parent_path}.{attr_name}"
|
||||
self._register_DataService_instance_callbacks(nested_attr, new_path)
|
||||
|
||||
def __register_recursive_parameter_callback(
|
||||
self,
|
||||
obj: "AbstractDataService | DataServiceList",
|
||||
callback: Callable[[str | int, Any], None],
|
||||
) -> None:
|
||||
"""
|
||||
Register callback to a DataService or DataServiceList instance and its nested
|
||||
instances.
|
||||
|
||||
For a DataService, this method traverses its attributes and recursively adds the
|
||||
callback for nested DataService or DataServiceList instances. For a
|
||||
DataServiceList,
|
||||
the callback is also triggered when an item gets reassigned.
|
||||
"""
|
||||
|
||||
if isinstance(obj, DataServiceList):
|
||||
# emits callback when item in list gets reassigned
|
||||
obj.add_callback(callback=callback)
|
||||
obj_list: DataServiceList | list[AbstractDataService] = obj
|
||||
else:
|
||||
obj_list = [obj]
|
||||
|
||||
# this enables notifications when a class instance was changed (-> item is
|
||||
# changed, not reassigned)
|
||||
for item in obj_list:
|
||||
if isinstance(item, AbstractDataService):
|
||||
item._callback_manager.callbacks.add(callback)
|
||||
for attr_name in set(dir(item)) - set(dir(object)) - {"__root__"}:
|
||||
attr_value = getattr(item, attr_name)
|
||||
if isinstance(attr_value, (AbstractDataService, DataServiceList)):
|
||||
self.__register_recursive_parameter_callback(
|
||||
attr_value, callback
|
||||
)
|
||||
|
||||
def _register_property_callbacks( # noqa: C901
|
||||
self,
|
||||
obj: "AbstractDataService",
|
||||
parent_path: str,
|
||||
) -> None:
|
||||
"""
|
||||
Register callbacks to notify when properties or their dependencies change.
|
||||
|
||||
This method cycles through all attributes (both class and instance level) of the
|
||||
input `obj`. For each attribute that is a property, it identifies dependencies
|
||||
used in the getter method and creates a callback for each one.
|
||||
|
||||
The method is recursive for attributes that are of type DataService or
|
||||
DataServiceList. It attaches the callback directly to DataServiceList items or
|
||||
propagates it through nested DataService instances.
|
||||
"""
|
||||
|
||||
attrs = get_class_and_instance_attributes(obj)
|
||||
|
||||
for attr_name, attr_value in attrs.items():
|
||||
if isinstance(attr_value, AbstractDataService):
|
||||
self._register_property_callbacks(
|
||||
attr_value, parent_path=f"{parent_path}.{attr_name}"
|
||||
)
|
||||
elif isinstance(attr_value, DataServiceList):
|
||||
for i, item in enumerate(attr_value):
|
||||
if isinstance(item, AbstractDataService):
|
||||
self._register_property_callbacks(
|
||||
item, parent_path=f"{parent_path}.{attr_name}[{i}]"
|
||||
)
|
||||
if isinstance(attr_value, property):
|
||||
dependencies = attr_value.fget.__code__.co_names # type: ignore
|
||||
source_code_string = inspect.getsource(attr_value.fget) # type: ignore
|
||||
|
||||
for dependency in dependencies:
|
||||
# check if the dependencies are attributes of obj
|
||||
# This doesn't have to be the case like, for example, here:
|
||||
# >>> @property
|
||||
# >>> def power(self) -> float:
|
||||
# >>> return self.class_attr.voltage * self.current
|
||||
#
|
||||
# The dependencies for this property are:
|
||||
# > ('class_attr', 'voltage', 'current')
|
||||
if f"self.{dependency}" not in source_code_string:
|
||||
continue
|
||||
|
||||
# use `obj` instead of `type(obj)` to get DataServiceList
|
||||
# instead of list
|
||||
dependency_value = getattr(obj, dependency)
|
||||
|
||||
if isinstance(
|
||||
dependency_value, (DataServiceList, AbstractDataService)
|
||||
):
|
||||
callback = (
|
||||
lambda name, value, dependent_attr=attr_name: obj._callback_manager.emit_notification(
|
||||
parent_path=parent_path,
|
||||
name=dependent_attr,
|
||||
value=getattr(obj, dependent_attr),
|
||||
)
|
||||
if self.service == obj.__root__
|
||||
else None
|
||||
)
|
||||
|
||||
self.__register_recursive_parameter_callback(
|
||||
dependency_value,
|
||||
callback=callback,
|
||||
)
|
||||
else:
|
||||
callback = (
|
||||
lambda name, _, dep_attr=attr_name, dep=dependency: obj._callback_manager.emit_notification( # type: ignore
|
||||
parent_path=parent_path,
|
||||
name=dep_attr,
|
||||
value=getattr(obj, dep_attr),
|
||||
)
|
||||
if name == dep and self.service == obj.__root__
|
||||
else None
|
||||
)
|
||||
# Add to callbacks
|
||||
obj._callback_manager.callbacks.add(callback)
|
||||
|
||||
def _register_start_stop_task_callbacks(
|
||||
self, obj: "AbstractDataService", parent_path: str
|
||||
) -> None:
|
||||
"""
|
||||
This function registers callbacks for start and stop methods of async functions.
|
||||
These callbacks are stored in the '_task_status_change_callbacks' attribute and
|
||||
are called when the status of a task changes.
|
||||
|
||||
Parameters:
|
||||
-----------
|
||||
obj: AbstractDataService
|
||||
The target object on which callbacks are to be registered.
|
||||
parent_path: str
|
||||
The access path for the parent object. This is used to construct the full
|
||||
access path for the notifications.
|
||||
"""
|
||||
|
||||
# Create and register a callback for the object
|
||||
# only emit the notification when the call was registered by the root object
|
||||
callback: Callable[[str, dict[str, Any] | None], None] = (
|
||||
lambda name, status: obj._callback_manager.emit_notification(
|
||||
parent_path=parent_path, name=name, value=status
|
||||
)
|
||||
if self.service == obj.__root__
|
||||
and not name.startswith("_") # we are only interested in public attributes
|
||||
else None
|
||||
)
|
||||
|
||||
obj._task_manager.task_status_change_callbacks.append(callback)
|
||||
|
||||
# Recursively register callbacks for all nested attributes of the object
|
||||
attrs: dict[str, Any] = get_class_and_instance_attributes(obj)
|
||||
|
||||
for nested_attr_name, nested_attr in attrs.items():
|
||||
if isinstance(nested_attr, DataServiceList):
|
||||
for i, item in enumerate(nested_attr):
|
||||
if isinstance(item, AbstractDataService):
|
||||
self._register_start_stop_task_callbacks(
|
||||
item, parent_path=f"{parent_path}.{nested_attr_name}[{i}]"
|
||||
)
|
||||
if isinstance(nested_attr, AbstractDataService):
|
||||
self._register_start_stop_task_callbacks(
|
||||
nested_attr, parent_path=f"{parent_path}.{nested_attr_name}"
|
||||
)
|
||||
|
||||
def register_callbacks(self) -> None:
|
||||
self._register_list_change_callbacks(
|
||||
self.service, f"{self.service.__class__.__name__}"
|
||||
)
|
||||
self._register_DataService_instance_callbacks(
|
||||
self.service, f"{self.service.__class__.__name__}"
|
||||
)
|
||||
self._register_property_callbacks(
|
||||
self.service, f"{self.service.__class__.__name__}"
|
||||
)
|
||||
self._register_start_stop_task_callbacks(
|
||||
self.service, f"{self.service.__class__.__name__}"
|
||||
)
|
||||
|
||||
def emit_notification(self, parent_path: str, name: str, value: Any) -> None:
|
||||
logger.debug(f"{parent_path}.{name} changed to {value}!")
|
||||
|
||||
for callback in self._notification_callbacks:
|
||||
try:
|
||||
callback(parent_path, name, value)
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
|
||||
def add_notification_callback(
|
||||
self, callback: Callable[[str, str, Any], None]
|
||||
) -> None:
|
||||
"""
|
||||
Adds a new notification callback function to the list of callbacks.
|
||||
|
||||
This function is intended to be used for registering a function that will be
|
||||
called whenever a the value of an attribute changes.
|
||||
|
||||
Args:
|
||||
callback (Callable[[str, str, Any], None]): The callback function to
|
||||
register.
|
||||
It should accept three parameters:
|
||||
- parent_path (str): The parent path of the parameter.
|
||||
- name (str): The name of the changed parameter.
|
||||
- value (Any): The value of the parameter.
|
||||
"""
|
||||
self._notification_callbacks.append(callback)
|
||||
@@ -1,15 +1,17 @@
|
||||
import inspect
|
||||
import logging
|
||||
import warnings
|
||||
from enum import Enum
|
||||
from pathlib import Path
|
||||
from typing import Any, Optional, get_type_hints
|
||||
from typing import TYPE_CHECKING, Any, get_type_hints
|
||||
|
||||
import rpyc # type: ignore
|
||||
import rpyc # type: ignore[import-untyped]
|
||||
|
||||
import pydase.units as u
|
||||
from pydase.data_service.abstract_data_service import AbstractDataService
|
||||
from pydase.data_service.callback_manager import CallbackManager
|
||||
from pydase.data_service.task_manager import TaskManager
|
||||
from pydase.observer_pattern.observable.observable import (
|
||||
Observable,
|
||||
)
|
||||
from pydase.utils.helpers import (
|
||||
convert_arguments_to_hinted_types,
|
||||
get_class_and_instance_attributes,
|
||||
@@ -23,9 +25,9 @@ from pydase.utils.serializer import (
|
||||
generate_serialized_data_paths,
|
||||
get_nested_dict_by_path,
|
||||
)
|
||||
from pydase.utils.warnings import (
|
||||
warn_if_instance_class_does_not_inherit_from_DataService,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from pathlib import Path
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -43,67 +45,93 @@ def process_callable_attribute(attr: Any, args: dict[str, Any]) -> Any:
|
||||
|
||||
class DataService(rpyc.Service, AbstractDataService):
|
||||
def __init__(self, **kwargs: Any) -> None:
|
||||
self._callback_manager: CallbackManager = CallbackManager(self)
|
||||
super().__init__()
|
||||
self._task_manager = TaskManager(self)
|
||||
|
||||
if not hasattr(self, "_autostart_tasks"):
|
||||
self._autostart_tasks = {}
|
||||
|
||||
self.__root__: "DataService" = self
|
||||
"""Keep track of the root object. This helps to filter the emission of
|
||||
notifications."""
|
||||
|
||||
filename = kwargs.pop("filename", None)
|
||||
if filename is not None:
|
||||
warnings.warn(
|
||||
"The 'filename' argument is deprecated and will be removed in a future version. "
|
||||
"Please pass the 'filename' argument to `pydase.Server`.",
|
||||
"The 'filename' argument is deprecated and will be removed in a future "
|
||||
"version. Please pass the 'filename' argument to `pydase.Server`.",
|
||||
DeprecationWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
self._filename: str | Path = filename
|
||||
|
||||
self._callback_manager.register_callbacks()
|
||||
self.__check_instance_classes()
|
||||
self._initialised = True
|
||||
|
||||
def __setattr__(self, __name: str, __value: Any) -> None:
|
||||
# converting attributes that are not properties
|
||||
if not isinstance(getattr(type(self), __name, None), property):
|
||||
current_value = getattr(self, __name, None)
|
||||
# parse ints into floats if current value is a float
|
||||
if isinstance(current_value, float) and isinstance(__value, int):
|
||||
__value = float(__value)
|
||||
# Check and warn for unexpected type changes in attributes
|
||||
self._warn_on_type_change(__name, __value)
|
||||
|
||||
if isinstance(current_value, u.Quantity):
|
||||
__value = u.convert_to_quantity(__value, str(current_value.u))
|
||||
# every class defined by the user should inherit from DataService if it is
|
||||
# assigned to a public attribute
|
||||
if not __name.startswith("_") and not inspect.isfunction(__value):
|
||||
self.__warn_if_not_observable(__value)
|
||||
|
||||
# Set the attribute
|
||||
super().__setattr__(__name, __value)
|
||||
|
||||
if self.__dict__.get("_initialised") and not __name == "_initialised":
|
||||
for callback in self._callback_manager.callbacks:
|
||||
callback(__name, __value)
|
||||
elif __name.startswith(f"_{self.__class__.__name__}__"):
|
||||
def _warn_on_type_change(self, attr_name: str, new_value: Any) -> None:
|
||||
if is_property_attribute(self, attr_name):
|
||||
return
|
||||
|
||||
current_value = getattr(self, attr_name, None)
|
||||
if self._is_unexpected_type_change(current_value, new_value):
|
||||
logger.warning(
|
||||
f"Warning: You should not set private but rather protected attributes! "
|
||||
f"Use {__name.replace(f'_{self.__class__.__name__}__', '_')} instead "
|
||||
f"of {__name.replace(f'_{self.__class__.__name__}__', '__')}."
|
||||
"Type of '%s' changed from '%s' to '%s'. This may have unwanted "
|
||||
"side effects! Consider setting it to '%s' directly.",
|
||||
attr_name,
|
||||
type(current_value).__name__,
|
||||
type(new_value).__name__,
|
||||
type(current_value).__name__,
|
||||
)
|
||||
|
||||
def _is_unexpected_type_change(self, current_value: Any, new_value: Any) -> bool:
|
||||
return (
|
||||
isinstance(current_value, float)
|
||||
and not isinstance(new_value, float)
|
||||
or (
|
||||
isinstance(current_value, u.Quantity)
|
||||
and not isinstance(new_value, u.Quantity)
|
||||
)
|
||||
)
|
||||
|
||||
def __warn_if_not_observable(self, __value: Any) -> None:
|
||||
value_class = __value if inspect.isclass(__value) else __value.__class__
|
||||
|
||||
if not issubclass(
|
||||
value_class,
|
||||
(int | float | bool | str | list | Enum | u.Quantity | Observable),
|
||||
):
|
||||
logger.warning(
|
||||
"Class '%s' does not inherit from DataService. This may lead to"
|
||||
" unexpected behaviour!",
|
||||
value_class.__name__,
|
||||
)
|
||||
|
||||
def __check_instance_classes(self) -> None:
|
||||
for attr_name, attr_value in get_class_and_instance_attributes(self).items():
|
||||
# every class defined by the user should inherit from DataService if it is
|
||||
# assigned to a public attribute
|
||||
if not attr_name.startswith("_"):
|
||||
warn_if_instance_class_does_not_inherit_from_DataService(attr_value)
|
||||
if (
|
||||
not attr_name.startswith("_")
|
||||
and not inspect.isfunction(attr_value)
|
||||
and not isinstance(attr_value, property)
|
||||
):
|
||||
self.__warn_if_not_observable(attr_value)
|
||||
|
||||
def __set_attribute_based_on_type( # noqa:CFQ002
|
||||
def __set_attribute_based_on_type( # noqa: PLR0913
|
||||
self,
|
||||
target_obj: Any,
|
||||
attr_name: str,
|
||||
attr: Any,
|
||||
value: Any,
|
||||
index: Optional[int],
|
||||
index: int | None,
|
||||
path_list: list[str],
|
||||
) -> None:
|
||||
if isinstance(attr, Enum):
|
||||
@@ -154,9 +182,11 @@ class DataService(rpyc.Service, AbstractDataService):
|
||||
)
|
||||
|
||||
if hasattr(self, "_state_manager"):
|
||||
getattr(self, "_state_manager").save_state()
|
||||
self._state_manager.save_state()
|
||||
|
||||
def load_DataService_from_JSON(self, json_dict: dict[str, Any]) -> None:
|
||||
def load_DataService_from_JSON( # noqa: N802
|
||||
self, json_dict: dict[str, Any]
|
||||
) -> None:
|
||||
warnings.warn(
|
||||
"'load_DataService_from_JSON' is deprecated and will be removed in a "
|
||||
"future version. "
|
||||
@@ -178,8 +208,9 @@ class DataService(rpyc.Service, AbstractDataService):
|
||||
class_attr_is_read_only = nested_class_dict["readonly"]
|
||||
if class_attr_is_read_only:
|
||||
logger.debug(
|
||||
f'Attribute "{path}" is read-only. Ignoring value from JSON '
|
||||
"file..."
|
||||
"Attribute '%s' is read-only. Ignoring value from JSON "
|
||||
"file...",
|
||||
path,
|
||||
)
|
||||
continue
|
||||
# Split the path into parts
|
||||
@@ -193,11 +224,14 @@ class DataService(rpyc.Service, AbstractDataService):
|
||||
self.update_DataService_attribute(parts[:-1], attr_name, value)
|
||||
else:
|
||||
logger.info(
|
||||
f'Attribute type of "{path}" changed from "{value_type}" to '
|
||||
f'"{class_value_type}". Ignoring value from JSON file...'
|
||||
"Attribute type of '%s' changed from '%s' to "
|
||||
"'%s'. Ignoring value from JSON file...",
|
||||
path,
|
||||
value_type,
|
||||
class_value_type,
|
||||
)
|
||||
|
||||
def serialize(self) -> dict[str, dict[str, Any]]: # noqa
|
||||
def serialize(self) -> dict[str, dict[str, Any]]:
|
||||
"""
|
||||
Serializes the instance into a dictionary, preserving the structure of the
|
||||
instance.
|
||||
@@ -216,7 +250,7 @@ class DataService(rpyc.Service, AbstractDataService):
|
||||
"""
|
||||
return Serializer.serialize_object(self)["value"]
|
||||
|
||||
def update_DataService_attribute(
|
||||
def update_DataService_attribute( # noqa: N802
|
||||
self,
|
||||
path_list: list[str],
|
||||
attr_name: str,
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
import logging
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from pydase.utils.serializer import set_nested_value_by_path
|
||||
from pydase.utils.serializer import (
|
||||
SerializationPathError,
|
||||
SerializationValueError,
|
||||
get_nested_dict_by_path,
|
||||
set_nested_value_by_path,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from pydase import DataService
|
||||
@@ -23,13 +28,12 @@ class DataServiceCache:
|
||||
"""Initializes the cache and sets up the callback."""
|
||||
logger.debug("Initializing cache.")
|
||||
self._cache = self.service.serialize()
|
||||
self.service._callback_manager.add_notification_callback(self.update_cache)
|
||||
|
||||
def update_cache(self, parent_path: str, name: str, value: Any) -> None:
|
||||
# Remove the part before the first "." in the parent_path
|
||||
parent_path = ".".join(parent_path.split(".")[1:])
|
||||
def update_cache(self, full_access_path: str, value: Any) -> None:
|
||||
set_nested_value_by_path(self._cache, full_access_path, value)
|
||||
|
||||
# Construct the full path
|
||||
full_path = f"{parent_path}.{name}" if parent_path else name
|
||||
|
||||
set_nested_value_by_path(self._cache, full_path, value)
|
||||
def get_value_dict_from_cache(self, full_access_path: str) -> dict[str, Any]:
|
||||
try:
|
||||
return get_nested_dict_by_path(self._cache, full_access_path)
|
||||
except (SerializationPathError, SerializationValueError, KeyError):
|
||||
return {}
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
from collections.abc import Callable
|
||||
from typing import Any
|
||||
|
||||
import pydase.units as u
|
||||
from pydase.utils.warnings import (
|
||||
warn_if_instance_class_does_not_inherit_from_DataService,
|
||||
)
|
||||
|
||||
|
||||
class DataServiceList(list):
|
||||
"""
|
||||
DataServiceList is a list with additional functionality to trigger callbacks
|
||||
whenever an item is set. This can be used to track changes in the list items.
|
||||
|
||||
The class takes the same arguments as the list superclass during initialization,
|
||||
with an additional optional 'callback' argument that is a list of functions.
|
||||
These callbacks are stored and executed whenever an item in the DataServiceList
|
||||
is set via the __setitem__ method. The callbacks receive the index of the changed
|
||||
item and its new value as arguments.
|
||||
|
||||
The original list that is passed during initialization is kept as a private
|
||||
attribute to prevent it from being garbage collected.
|
||||
|
||||
Additional callbacks can be added after initialization using the `add_callback`
|
||||
method.
|
||||
|
||||
Attributes:
|
||||
callbacks (list):
|
||||
List of callback functions to be executed on item set.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*args: list[Any],
|
||||
callback: list[Callable[[int, Any], None]] | None = None,
|
||||
**kwargs: Any,
|
||||
) -> None:
|
||||
self.callbacks: list[Callable[[int, Any], None]] = []
|
||||
if isinstance(callback, list):
|
||||
self.callbacks = callback
|
||||
|
||||
for item in args[0]:
|
||||
warn_if_instance_class_does_not_inherit_from_DataService(item)
|
||||
|
||||
# prevent gc to delete the passed list by keeping a reference
|
||||
self._original_list = args[0]
|
||||
|
||||
super().__init__(*args, **kwargs) # type: ignore
|
||||
|
||||
def __setitem__(self, key: int, value: Any) -> None: # type: ignore
|
||||
current_value = self.__getitem__(key)
|
||||
|
||||
# parse ints into floats if current value is a float
|
||||
if isinstance(current_value, float) and isinstance(value, int):
|
||||
value = float(value)
|
||||
|
||||
if isinstance(current_value, u.Quantity):
|
||||
value = u.convert_to_quantity(value, str(current_value.u))
|
||||
super().__setitem__(key, value) # type: ignore
|
||||
|
||||
for callback in self.callbacks:
|
||||
callback(key, value)
|
||||
|
||||
def add_callback(self, callback: Callable[[int, Any], None]) -> None:
|
||||
"""
|
||||
Add a new callback function to be executed on item set.
|
||||
|
||||
Args:
|
||||
callback (Callable[[int, Any], None]): Callback function that takes two
|
||||
arguments - index of the changed item and its new value.
|
||||
"""
|
||||
self.callbacks.append(callback)
|
||||
107
src/pydase/data_service/data_service_observer.py
Normal file
107
src/pydase/data_service/data_service_observer.py
Normal file
@@ -0,0 +1,107 @@
|
||||
import logging
|
||||
from collections.abc import Callable
|
||||
from copy import deepcopy
|
||||
from typing import Any
|
||||
|
||||
from pydase.data_service.state_manager import StateManager
|
||||
from pydase.observer_pattern.observable.observable_object import ObservableObject
|
||||
from pydase.observer_pattern.observer.property_observer import (
|
||||
PropertyObserver,
|
||||
)
|
||||
from pydase.utils.helpers import get_object_attr_from_path_list
|
||||
from pydase.utils.serializer import dump
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class DataServiceObserver(PropertyObserver):
|
||||
def __init__(self, state_manager: StateManager) -> None:
|
||||
self.state_manager = state_manager
|
||||
self._notification_callbacks: list[
|
||||
Callable[[str, Any, dict[str, Any]], None]
|
||||
] = []
|
||||
super().__init__(state_manager.service)
|
||||
|
||||
def on_change(self, full_access_path: str, value: Any) -> None:
|
||||
cached_value_dict = deepcopy(
|
||||
self.state_manager._data_service_cache.get_value_dict_from_cache(
|
||||
full_access_path
|
||||
)
|
||||
)
|
||||
|
||||
cached_value = cached_value_dict.get("value")
|
||||
if cached_value != dump(value)["value"] and all(
|
||||
part[0] != "_" for part in full_access_path.split(".")
|
||||
):
|
||||
logger.debug("'%s' changed to '%s'", full_access_path, value)
|
||||
|
||||
self._update_cache_value(full_access_path, value, cached_value_dict)
|
||||
|
||||
for callback in self._notification_callbacks:
|
||||
callback(full_access_path, value, cached_value_dict)
|
||||
|
||||
if isinstance(value, ObservableObject):
|
||||
self._update_property_deps_dict()
|
||||
|
||||
self._notify_dependent_property_changes(full_access_path)
|
||||
|
||||
def _update_cache_value(
|
||||
self, full_access_path: str, value: Any, cached_value_dict: dict[str, Any]
|
||||
) -> None:
|
||||
value_dict = dump(value)
|
||||
if cached_value_dict != {}:
|
||||
if (
|
||||
cached_value_dict["type"] != "method"
|
||||
and cached_value_dict["type"] != value_dict["type"]
|
||||
):
|
||||
logger.warning(
|
||||
"Type of '%s' changed from '%s' to '%s'. This could have unwanted "
|
||||
"side effects! Consider setting it to '%s' directly.",
|
||||
full_access_path,
|
||||
cached_value_dict["type"],
|
||||
value_dict["type"],
|
||||
cached_value_dict["type"],
|
||||
)
|
||||
self.state_manager._data_service_cache.update_cache(
|
||||
full_access_path,
|
||||
value,
|
||||
)
|
||||
|
||||
def _notify_dependent_property_changes(self, changed_attr_path: str) -> None:
|
||||
changed_props = self.property_deps_dict.get(changed_attr_path, [])
|
||||
for prop in changed_props:
|
||||
# only notify about changing attribute if it is not currently being
|
||||
# "changed" e.g. when calling the getter of a property within another
|
||||
# property
|
||||
if prop not in self.changing_attributes:
|
||||
self._notify_changed(
|
||||
prop,
|
||||
get_object_attr_from_path_list(self.observable, prop.split(".")),
|
||||
)
|
||||
|
||||
def add_notification_callback(
|
||||
self, callback: Callable[[str, Any, dict[str, Any]], None]
|
||||
) -> None:
|
||||
"""
|
||||
Registers a callback function to be invoked upon attribute changes in the
|
||||
observed object.
|
||||
|
||||
This method allows for the addition of custom callback functions that will be
|
||||
executed whenever there is a change in the value of an observed attribute. The
|
||||
callback function is called with detailed information about the change, enabling
|
||||
external logic to respond to specific state changes within the observable
|
||||
object.
|
||||
|
||||
Args:
|
||||
callback (Callable[[str, Any, dict[str, Any]]): The callback function to be
|
||||
registered. The function should have the following signature:
|
||||
- full_access_path (str): The full dot-notation access path of the
|
||||
changed attribute. This path indicates the location of the changed
|
||||
attribute within the observable object's structure.
|
||||
- value (Any): The new value of the changed attribute.
|
||||
- cached_value_dict (dict[str, Any]): A dictionary representing the
|
||||
cached state of the attribute prior to the change. This can be useful
|
||||
for understanding the nature of the change and for historical
|
||||
comparison.
|
||||
"""
|
||||
self._notification_callbacks.append(callback)
|
||||
@@ -3,18 +3,20 @@ import logging
|
||||
import os
|
||||
from collections.abc import Callable
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING, Any, Optional, cast
|
||||
from typing import TYPE_CHECKING, Any, cast
|
||||
|
||||
import pydase.units as u
|
||||
from pydase.data_service.data_service_cache import DataServiceCache
|
||||
from pydase.utils.helpers import (
|
||||
get_object_attr_from_path_list,
|
||||
is_property_attribute,
|
||||
parse_list_attr_and_index,
|
||||
)
|
||||
from pydase.utils.serializer import (
|
||||
dump,
|
||||
generate_serialized_data_paths,
|
||||
get_nested_dict_by_path,
|
||||
serialized_dict_is_nested_object,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -41,17 +43,17 @@ def load_state(func: Callable[..., Any]) -> Callable[..., Any]:
|
||||
... self._name = value
|
||||
"""
|
||||
|
||||
func._load_state = True
|
||||
func._load_state = True # type: ignore[attr-defined]
|
||||
return func
|
||||
|
||||
|
||||
def has_load_state_decorator(prop: property):
|
||||
def has_load_state_decorator(prop: property) -> bool:
|
||||
"""Determines if the property's setter method is decorated with the `@load_state`
|
||||
decorator.
|
||||
"""
|
||||
|
||||
try:
|
||||
return getattr(prop.fset, "_load_state")
|
||||
return prop.fset._load_state # type: ignore[union-attr]
|
||||
except AttributeError:
|
||||
return False
|
||||
|
||||
@@ -96,13 +98,15 @@ class StateManager:
|
||||
update.
|
||||
"""
|
||||
|
||||
def __init__(self, service: "DataService", filename: Optional[str | Path] = None):
|
||||
def __init__(
|
||||
self, service: "DataService", filename: str | Path | None = None
|
||||
) -> None:
|
||||
self.filename = getattr(service, "_filename", None)
|
||||
|
||||
if filename is not None:
|
||||
if self.filename is not None:
|
||||
logger.warning(
|
||||
f"Overwriting filename {self.filename!r} with {filename!r}."
|
||||
"Overwriting filename '%s' with '%s'.", self.filename, filename
|
||||
)
|
||||
self.filename = filename
|
||||
|
||||
@@ -124,7 +128,7 @@ class StateManager:
|
||||
with open(self.filename, "w") as f:
|
||||
json.dump(self.cache, f, indent=4)
|
||||
else:
|
||||
logger.error(
|
||||
logger.info(
|
||||
"State manager was not initialised with a filename. Skipping "
|
||||
"'save_state'..."
|
||||
)
|
||||
@@ -136,34 +140,36 @@ class StateManager:
|
||||
"""
|
||||
|
||||
# Traverse the serialized representation and set the attributes of the class
|
||||
json_dict = self._get_state_dict_from_JSON_file()
|
||||
json_dict = self._get_state_dict_from_json_file()
|
||||
if json_dict == {}:
|
||||
logger.debug("Could not load the service state.")
|
||||
return
|
||||
|
||||
for path in generate_serialized_data_paths(json_dict):
|
||||
nested_json_dict = get_nested_dict_by_path(json_dict, path)
|
||||
nested_class_dict = get_nested_dict_by_path(self.cache, path)
|
||||
nested_class_dict = self._data_service_cache.get_value_dict_from_cache(path)
|
||||
|
||||
value, value_type = nested_json_dict["value"], nested_json_dict["type"]
|
||||
class_attr_value_type = nested_class_dict.get("type", None)
|
||||
|
||||
if class_attr_value_type == value_type:
|
||||
self.set_service_attribute_value_by_path(path, value)
|
||||
if self.__is_loadable_state_attribute(path):
|
||||
self.set_service_attribute_value_by_path(path, value)
|
||||
else:
|
||||
logger.info(
|
||||
f"Attribute type of {path!r} changed from {value_type!r} to "
|
||||
f"{class_attr_value_type!r}. Ignoring value from JSON file..."
|
||||
"Attribute type of '%s' changed from '%s' to "
|
||||
"'%s'. Ignoring value from JSON file...",
|
||||
path,
|
||||
value_type,
|
||||
class_attr_value_type,
|
||||
)
|
||||
|
||||
def _get_state_dict_from_JSON_file(self) -> dict[str, Any]:
|
||||
if self.filename is not None:
|
||||
# Check if the file specified by the filename exists
|
||||
if os.path.exists(self.filename):
|
||||
with open(self.filename, "r") as f:
|
||||
# Load JSON data from file and update class attributes with these
|
||||
# values
|
||||
return cast(dict[str, Any], json.load(f))
|
||||
def _get_state_dict_from_json_file(self) -> dict[str, Any]:
|
||||
if self.filename is not None and os.path.exists(self.filename):
|
||||
with open(self.filename) as f:
|
||||
# Load JSON data from file and update class attributes with these
|
||||
# values
|
||||
return cast(dict[str, Any], json.load(f))
|
||||
return {}
|
||||
|
||||
def set_service_attribute_value_by_path(
|
||||
@@ -189,7 +195,7 @@ class StateManager:
|
||||
|
||||
# This will also filter out methods as they are 'read-only'
|
||||
if current_value_dict["readonly"]:
|
||||
logger.debug(f"Attribute {path!r} is read-only. Ignoring new value...")
|
||||
logger.debug("Attribute '%s' is read-only. Ignoring new value...", path)
|
||||
return
|
||||
|
||||
converted_value = self.__convert_value_if_needed(value, current_value_dict)
|
||||
@@ -198,7 +204,7 @@ class StateManager:
|
||||
if self.__attr_value_has_changed(converted_value, current_value_dict["value"]):
|
||||
self.__update_attribute_by_path(path, converted_value)
|
||||
else:
|
||||
logger.debug(f"Value of attribute {path!r} has not changed...")
|
||||
logger.debug("Value of attribute '%s' has not changed...", path)
|
||||
|
||||
def __attr_value_has_changed(self, value_object: Any, current_value: Any) -> bool:
|
||||
"""Check if the serialized value of `value_object` differs from `current_value`.
|
||||
@@ -214,6 +220,8 @@ class StateManager:
|
||||
) -> Any:
|
||||
if current_value_dict["type"] == "Quantity":
|
||||
return u.convert_to_quantity(value, current_value_dict["value"]["unit"])
|
||||
if current_value_dict["type"] == "float" and not isinstance(value, float):
|
||||
return float(value)
|
||||
return value
|
||||
|
||||
def __update_attribute_by_path(self, path: str, value: Any) -> None:
|
||||
@@ -231,21 +239,45 @@ class StateManager:
|
||||
# Traverse the object according to the path parts
|
||||
target_obj = get_object_attr_from_path_list(self.service, parent_path_list)
|
||||
|
||||
if self.__attr_value_should_change(target_obj, attr_name):
|
||||
if attr_cache_type in ("ColouredEnum", "Enum"):
|
||||
enum_attr = get_object_attr_from_path_list(target_obj, [attr_name])
|
||||
setattr(target_obj, attr_name, enum_attr.__class__[value])
|
||||
elif attr_cache_type == "list":
|
||||
list_obj = get_object_attr_from_path_list(target_obj, [attr_name])
|
||||
list_obj[index] = value
|
||||
else:
|
||||
setattr(target_obj, attr_name, value)
|
||||
if attr_cache_type in ("ColouredEnum", "Enum"):
|
||||
enum_attr = get_object_attr_from_path_list(target_obj, [attr_name])
|
||||
setattr(target_obj, attr_name, enum_attr.__class__[value])
|
||||
elif attr_cache_type == "list":
|
||||
list_obj = get_object_attr_from_path_list(target_obj, [attr_name])
|
||||
list_obj[index] = value
|
||||
else:
|
||||
setattr(target_obj, attr_name, value)
|
||||
|
||||
def __attr_value_should_change(self, parent_object: Any, attr_name: str) -> bool:
|
||||
# If the attribute is a property, change it using the setter without getting
|
||||
# the property value (would otherwise be bad for expensive getter methods)
|
||||
prop = getattr(type(parent_object), attr_name, None)
|
||||
def __is_loadable_state_attribute(self, full_access_path: str) -> bool:
|
||||
"""Checks if an attribute defined by a dot-separated path should be loaded from
|
||||
storage.
|
||||
|
||||
if isinstance(prop, property):
|
||||
return has_load_state_decorator(prop)
|
||||
return True
|
||||
For properties, it verifies the presence of the '@load_state' decorator. Regular
|
||||
attributes default to being loadable.
|
||||
"""
|
||||
|
||||
parent_object = get_object_attr_from_path_list(
|
||||
self.service, full_access_path.split(".")[:-1]
|
||||
)
|
||||
attr_name = full_access_path.split(".")[-1]
|
||||
|
||||
if is_property_attribute(parent_object, attr_name):
|
||||
prop = getattr(type(parent_object), attr_name)
|
||||
has_decorator = has_load_state_decorator(prop)
|
||||
if not has_decorator:
|
||||
logger.debug(
|
||||
"Property '%s' has no '@load_state' decorator. "
|
||||
"Ignoring value from JSON file...",
|
||||
attr_name,
|
||||
)
|
||||
return has_decorator
|
||||
|
||||
cached_serialization_dict = get_nested_dict_by_path(
|
||||
self.cache, full_access_path
|
||||
)
|
||||
|
||||
if cached_serialization_dict["value"] == "method":
|
||||
return False
|
||||
|
||||
# nested objects cannot be loaded
|
||||
return not serialized_dict_is_nested_object(cached_serialization_dict)
|
||||
|
||||
@@ -3,15 +3,14 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
import inspect
|
||||
import logging
|
||||
from collections.abc import Callable
|
||||
from functools import wraps
|
||||
from typing import TYPE_CHECKING, Any, TypedDict
|
||||
|
||||
from pydase.data_service.abstract_data_service import AbstractDataService
|
||||
from pydase.data_service.data_service_list import DataServiceList
|
||||
from pydase.utils.helpers import get_class_and_instance_attributes
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Callable
|
||||
|
||||
from .data_service import DataService
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -78,7 +77,6 @@ class TaskManager:
|
||||
|
||||
def __init__(self, service: DataService) -> None:
|
||||
self.service = service
|
||||
self._loop = asyncio.get_event_loop()
|
||||
|
||||
self.tasks: dict[str, TaskDict] = {}
|
||||
"""A dictionary to keep track of running tasks. The keys are the names of the
|
||||
@@ -86,15 +84,13 @@ class TaskManager:
|
||||
its kwargs.
|
||||
"""
|
||||
|
||||
self.task_status_change_callbacks: list[
|
||||
Callable[[str, dict[str, Any] | None], Any]
|
||||
] = []
|
||||
"""A list of callback functions to be invoked when the status of a task (start
|
||||
or stop) changes."""
|
||||
|
||||
self._set_start_and_stop_for_async_methods()
|
||||
|
||||
def _set_start_and_stop_for_async_methods(self) -> None: # noqa: C901
|
||||
@property
|
||||
def _loop(self) -> asyncio.AbstractEventLoop:
|
||||
return asyncio.get_running_loop()
|
||||
|
||||
def _set_start_and_stop_for_async_methods(self) -> None:
|
||||
# inspect the methods of the class
|
||||
for name, method in inspect.getmembers(
|
||||
self.service, predicate=inspect.iscoroutinefunction
|
||||
@@ -111,18 +107,18 @@ class TaskManager:
|
||||
start_method(*args)
|
||||
else:
|
||||
logger.warning(
|
||||
f"No start method found for service '{service_name}'"
|
||||
"No start method found for service '%s'", service_name
|
||||
)
|
||||
|
||||
def start_autostart_tasks(self) -> None:
|
||||
self._initiate_task_startup()
|
||||
attrs = get_class_and_instance_attributes(self.service)
|
||||
|
||||
for _, attr_value in attrs.items():
|
||||
for attr_value in attrs.values():
|
||||
if isinstance(attr_value, AbstractDataService):
|
||||
attr_value._task_manager.start_autostart_tasks()
|
||||
elif isinstance(attr_value, DataServiceList):
|
||||
for i, item in enumerate(attr_value):
|
||||
elif isinstance(attr_value, list):
|
||||
for item in attr_value:
|
||||
if isinstance(item, AbstractDataService):
|
||||
item._task_manager.start_autostart_tasks()
|
||||
|
||||
@@ -145,7 +141,7 @@ class TaskManager:
|
||||
|
||||
return stop_task
|
||||
|
||||
def _make_start_task( # noqa
|
||||
def _make_start_task(
|
||||
self, name: str, method: Callable[..., Any]
|
||||
) -> Callable[..., Any]:
|
||||
"""
|
||||
@@ -160,7 +156,6 @@ class TaskManager:
|
||||
method (callable): The coroutine to be turned into an asyncio task.
|
||||
"""
|
||||
|
||||
@wraps(method)
|
||||
def start_task(*args: Any, **kwargs: Any) -> None:
|
||||
def task_done_callback(task: asyncio.Task[None], name: str) -> None:
|
||||
"""Handles tasks that have finished.
|
||||
@@ -172,15 +167,16 @@ class TaskManager:
|
||||
self.tasks.pop(name, None)
|
||||
|
||||
# emit the notification that the task was stopped
|
||||
for callback in self.task_status_change_callbacks:
|
||||
callback(name, None)
|
||||
self.service._notify_changed(name, None)
|
||||
|
||||
exception = task.exception()
|
||||
if exception is not None:
|
||||
# Handle the exception, or you can re-raise it.
|
||||
logger.error(
|
||||
f"Task '{name}' encountered an exception: "
|
||||
f"{type(exception).__name__}: {exception}"
|
||||
"Task '%s' encountered an exception: %s: %s",
|
||||
name,
|
||||
type(exception).__name__,
|
||||
exception,
|
||||
)
|
||||
raise exception
|
||||
|
||||
@@ -188,7 +184,7 @@ class TaskManager:
|
||||
try:
|
||||
await method(*args, **kwargs)
|
||||
except asyncio.CancelledError:
|
||||
logger.info(f"Task {name} was cancelled")
|
||||
logger.info("Task '%s' was cancelled", name)
|
||||
|
||||
if not self.tasks.get(name):
|
||||
# Get the signature of the coroutine method to start
|
||||
@@ -207,7 +203,7 @@ class TaskManager:
|
||||
# with the 'kwargs' dictionary. If a parameter is specified in both
|
||||
# 'args_padded' and 'kwargs', the value from 'kwargs' is used.
|
||||
kwargs_updated = {
|
||||
**dict(zip(parameter_names, args_padded)),
|
||||
**dict(zip(parameter_names, args_padded, strict=True)),
|
||||
**kwargs,
|
||||
}
|
||||
|
||||
@@ -227,9 +223,8 @@ class TaskManager:
|
||||
}
|
||||
|
||||
# emit the notification that the task was started
|
||||
for callback in self.task_status_change_callbacks:
|
||||
callback(name, kwargs_updated)
|
||||
self.service._notify_changed(name, kwargs_updated)
|
||||
else:
|
||||
logger.error(f"Task `{name}` is already running!")
|
||||
logger.error("Task '%s' is already running!", name)
|
||||
|
||||
return start_task
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
{
|
||||
"files": {
|
||||
"main.css": "/static/css/main.c444b055.css",
|
||||
"main.js": "/static/js/main.08edc629.js",
|
||||
"main.css": "/static/css/main.2d8458eb.css",
|
||||
"main.js": "/static/js/main.dba067e7.js",
|
||||
"index.html": "/index.html",
|
||||
"main.c444b055.css.map": "/static/css/main.c444b055.css.map",
|
||||
"main.08edc629.js.map": "/static/js/main.08edc629.js.map"
|
||||
"main.2d8458eb.css.map": "/static/css/main.2d8458eb.css.map",
|
||||
"main.dba067e7.js.map": "/static/js/main.dba067e7.js.map"
|
||||
},
|
||||
"entrypoints": [
|
||||
"static/css/main.c444b055.css",
|
||||
"static/js/main.08edc629.js"
|
||||
"static/css/main.2d8458eb.css",
|
||||
"static/js/main.dba067e7.js"
|
||||
]
|
||||
}
|
||||
@@ -1 +1 @@
|
||||
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site displaying a pydase UI."/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>pydase App</title><script defer="defer" src="/static/js/main.08edc629.js"></script><link href="/static/css/main.c444b055.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>
|
||||
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site displaying a pydase UI."/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>pydase App</title><script defer="defer" src="/static/js/main.dba067e7.js"></script><link href="/static/css/main.2d8458eb.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>
|
||||
6
src/pydase/frontend/static/css/main.2d8458eb.css
Normal file
6
src/pydase/frontend/static/css/main.2d8458eb.css
Normal file
File diff suppressed because one or more lines are too long
1
src/pydase/frontend/static/css/main.2d8458eb.css.map
Normal file
1
src/pydase/frontend/static/css/main.2d8458eb.css.map
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
3
src/pydase/frontend/static/js/main.dba067e7.js
Normal file
3
src/pydase/frontend/static/js/main.dba067e7.js
Normal file
File diff suppressed because one or more lines are too long
@@ -4,8 +4,6 @@
|
||||
http://jedwatson.github.io/classnames
|
||||
*/
|
||||
|
||||
/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */
|
||||
|
||||
/**
|
||||
* @license React
|
||||
* react-dom.production.min.js
|
||||
1
src/pydase/frontend/static/js/main.dba067e7.js.map
Normal file
1
src/pydase/frontend/static/js/main.dba067e7.js.map
Normal file
File diff suppressed because one or more lines are too long
0
src/pydase/observer_pattern/__init__.py
Normal file
0
src/pydase/observer_pattern/__init__.py
Normal file
3
src/pydase/observer_pattern/observable/__init__.py
Normal file
3
src/pydase/observer_pattern/observable/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from pydase.observer_pattern.observable.observable import Observable
|
||||
|
||||
__all__ = ["Observable"]
|
||||
71
src/pydase/observer_pattern/observable/observable.py
Normal file
71
src/pydase/observer_pattern/observable/observable.py
Normal file
@@ -0,0 +1,71 @@
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from pydase.observer_pattern.observable.observable_object import ObservableObject
|
||||
from pydase.utils.helpers import is_property_attribute
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Observable(ObservableObject):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
class_attrs = {
|
||||
k: type(self).__dict__[k]
|
||||
for k in set(type(self).__dict__)
|
||||
- set(Observable.__dict__)
|
||||
- set(self.__dict__)
|
||||
}
|
||||
for name, value in class_attrs.items():
|
||||
if isinstance(value, property) or callable(value):
|
||||
continue
|
||||
self.__dict__[name] = self._initialise_new_objects(name, value)
|
||||
|
||||
def __setattr__(self, name: str, value: Any) -> None:
|
||||
if not hasattr(self, "_observers") and name != "_observers":
|
||||
logger.warning(
|
||||
"Ensure that super().__init__() is called at the start of the '%s' "
|
||||
"constructor! Failing to do so may lead to unexpected behavior.",
|
||||
type(self).__name__,
|
||||
)
|
||||
self._observers = {}
|
||||
|
||||
value = self._handle_observable_setattr(name, value)
|
||||
|
||||
super().__setattr__(name, value)
|
||||
|
||||
self._notify_changed(name, value)
|
||||
|
||||
def __getattribute__(self, name: str) -> Any:
|
||||
if is_property_attribute(self, name):
|
||||
self._notify_change_start(name)
|
||||
|
||||
value = super().__getattribute__(name)
|
||||
|
||||
if is_property_attribute(self, name):
|
||||
self._notify_changed(name, value)
|
||||
|
||||
return value
|
||||
|
||||
def _handle_observable_setattr(self, name: str, value: Any) -> Any:
|
||||
if name == "_observers":
|
||||
return value
|
||||
|
||||
self._remove_observer_if_observable(name)
|
||||
value = self._initialise_new_objects(name, value)
|
||||
self._notify_change_start(name)
|
||||
return value
|
||||
|
||||
def _remove_observer_if_observable(self, name: str) -> None:
|
||||
if not is_property_attribute(self, name):
|
||||
current_value = getattr(self, name, None)
|
||||
|
||||
if isinstance(current_value, ObservableObject):
|
||||
current_value._remove_observer(self, name)
|
||||
|
||||
def _construct_extended_attr_path(
|
||||
self, observer_attr_name: str, instance_attr_name: str
|
||||
) -> str:
|
||||
if observer_attr_name != "":
|
||||
return f"{observer_attr_name}.{instance_attr_name}"
|
||||
return instance_attr_name
|
||||
263
src/pydase/observer_pattern/observable/observable_object.py
Normal file
263
src/pydase/observer_pattern/observable/observable_object.py
Normal file
@@ -0,0 +1,263 @@
|
||||
import logging
|
||||
from abc import ABC, abstractmethod
|
||||
from collections.abc import Iterable
|
||||
from typing import TYPE_CHECKING, Any, ClassVar, SupportsIndex
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from pydase.observer_pattern.observer.observer import Observer
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ObservableObject(ABC):
|
||||
_list_mapping: ClassVar[dict[int, "_ObservableList"]] = {}
|
||||
_dict_mapping: ClassVar[dict[int, "_ObservableDict"]] = {}
|
||||
|
||||
def __init__(self) -> None:
|
||||
if not hasattr(self, "_observers"):
|
||||
self._observers: dict[str, list["ObservableObject | Observer"]] = {}
|
||||
|
||||
def add_observer(
|
||||
self, observer: "ObservableObject | Observer", attr_name: str = ""
|
||||
) -> None:
|
||||
if attr_name not in self._observers:
|
||||
self._observers[attr_name] = []
|
||||
if observer not in self._observers[attr_name]:
|
||||
self._observers[attr_name].append(observer)
|
||||
|
||||
def _remove_observer(self, observer: "ObservableObject", attribute: str) -> None:
|
||||
if attribute in self._observers:
|
||||
self._observers[attribute].remove(observer)
|
||||
|
||||
@abstractmethod
|
||||
def _remove_observer_if_observable(self, name: str) -> None:
|
||||
"""Removes the current object as an observer from an observable attribute.
|
||||
|
||||
This method is called before an attribute of the observable object is
|
||||
changed. If the current value of the attribute is an instance of
|
||||
`ObservableObject`, this method removes the current object from its list
|
||||
of observers. This is a crucial step to avoid unwanted notifications from
|
||||
the old value of the attribute.
|
||||
"""
|
||||
|
||||
def _notify_changed(self, changed_attribute: str, value: Any) -> None:
|
||||
"""Notifies all observers about changes to an attribute.
|
||||
|
||||
This method iterates through all observers registered for the object and
|
||||
invokes their notification method. It is called whenever an attribute of
|
||||
the observable object is changed.
|
||||
|
||||
Args:
|
||||
changed_attribute (str): The name of the changed attribute.
|
||||
value (Any): The value that the attribute was set to.
|
||||
"""
|
||||
for attr_name, observer_list in self._observers.items():
|
||||
for observer in observer_list:
|
||||
extendend_attr_path = self._construct_extended_attr_path(
|
||||
attr_name, changed_attribute
|
||||
)
|
||||
observer._notify_changed(extendend_attr_path, value)
|
||||
|
||||
def _notify_change_start(self, changing_attribute: str) -> None:
|
||||
"""Notify observers that an attribute or item change process has started.
|
||||
|
||||
This method is called at the start of the process of modifying an attribute in
|
||||
the observed `Observable` object. It registers the attribute as currently
|
||||
undergoing a change. This registration helps in managing and tracking changes as
|
||||
they occur, especially in scenarios where the order of changes or their state
|
||||
during the transition is significant.
|
||||
|
||||
Args:
|
||||
changing_attribute (str): The name of the attribute that is starting to
|
||||
change. This is typically the full access path of the attribute in the
|
||||
`Observable`.
|
||||
value (Any): The value that the attribute is being set to.
|
||||
"""
|
||||
|
||||
for attr_name, observer_list in self._observers.items():
|
||||
for observer in observer_list:
|
||||
extended_attr_path = self._construct_extended_attr_path(
|
||||
attr_name, changing_attribute
|
||||
)
|
||||
observer._notify_change_start(extended_attr_path)
|
||||
|
||||
def _initialise_new_objects(self, attr_name_or_key: Any, value: Any) -> Any:
|
||||
new_value = value
|
||||
if isinstance(value, list):
|
||||
if id(value) in self._list_mapping:
|
||||
# If the list `value` was already referenced somewhere else
|
||||
new_value = self._list_mapping[id(value)]
|
||||
else:
|
||||
# convert the builtin list into a ObservableList
|
||||
new_value = _ObservableList(original_list=value)
|
||||
self._list_mapping[id(value)] = new_value
|
||||
elif isinstance(value, dict):
|
||||
if id(value) in self._dict_mapping:
|
||||
# If the list `value` was already referenced somewhere else
|
||||
new_value = self._dict_mapping[id(value)]
|
||||
else:
|
||||
# convert the builtin list into a ObservableList
|
||||
new_value = _ObservableDict(original_dict=value)
|
||||
self._dict_mapping[id(value)] = new_value
|
||||
if isinstance(new_value, ObservableObject):
|
||||
new_value.add_observer(self, str(attr_name_or_key))
|
||||
return new_value
|
||||
|
||||
@abstractmethod
|
||||
def _construct_extended_attr_path(
|
||||
self, observer_attr_name: str, instance_attr_name: str
|
||||
) -> str:
|
||||
"""
|
||||
Constructs the extended attribute path for notification purposes, which is used
|
||||
in the observer pattern to specify the full path of an observed attribute.
|
||||
|
||||
This abstract method is implemented by the classes inheriting from
|
||||
`ObservableObject`.
|
||||
|
||||
Args:
|
||||
observer_attr_name (str): The name of the attribute in the observer that
|
||||
holds a reference to the instance. Equals `""` if observer itself is of type
|
||||
`Observer`.
|
||||
instance_attr_name (str): The name of the attribute within the instance that
|
||||
has changed.
|
||||
|
||||
Returns:
|
||||
str: The constructed extended attribute path.
|
||||
"""
|
||||
|
||||
|
||||
class _ObservableList(ObservableObject, list[Any]):
|
||||
def __init__(
|
||||
self,
|
||||
original_list: list[Any],
|
||||
) -> None:
|
||||
self._original_list = original_list
|
||||
ObservableObject.__init__(self)
|
||||
list.__init__(self, self._original_list)
|
||||
for i, item in enumerate(self._original_list):
|
||||
super().__setitem__(i, self._initialise_new_objects(f"[{i}]", item))
|
||||
|
||||
def __setitem__(self, key: int, value: Any) -> None: # type: ignore[override]
|
||||
if hasattr(self, "_observers"):
|
||||
self._remove_observer_if_observable(f"[{key}]")
|
||||
value = self._initialise_new_objects(f"[{key}]", value)
|
||||
self._notify_change_start(f"[{key}]")
|
||||
|
||||
super().__setitem__(key, value)
|
||||
|
||||
self._notify_changed(f"[{key}]", value)
|
||||
|
||||
def append(self, __object: Any) -> None:
|
||||
self._initialise_new_objects(f"[{len(self)}]", __object)
|
||||
super().append(__object)
|
||||
self._notify_changed("", self)
|
||||
|
||||
def clear(self) -> None:
|
||||
self._remove_self_from_observables()
|
||||
|
||||
super().clear()
|
||||
|
||||
self._notify_changed("", self)
|
||||
|
||||
def extend(self, __iterable: Iterable[Any]) -> None:
|
||||
self._remove_self_from_observables()
|
||||
|
||||
try:
|
||||
super().extend(__iterable)
|
||||
finally:
|
||||
for i, item in enumerate(self):
|
||||
super().__setitem__(i, self._initialise_new_objects(f"[{i}]", item))
|
||||
|
||||
self._notify_changed("", self)
|
||||
|
||||
def insert(self, __index: SupportsIndex, __object: Any) -> None:
|
||||
self._remove_self_from_observables()
|
||||
|
||||
try:
|
||||
super().insert(__index, __object)
|
||||
finally:
|
||||
for i, item in enumerate(self):
|
||||
super().__setitem__(i, self._initialise_new_objects(f"[{i}]", item))
|
||||
|
||||
self._notify_changed("", self)
|
||||
|
||||
def pop(self, __index: SupportsIndex = -1) -> Any:
|
||||
self._remove_self_from_observables()
|
||||
|
||||
try:
|
||||
popped_item = super().pop(__index)
|
||||
finally:
|
||||
for i, item in enumerate(self):
|
||||
super().__setitem__(i, self._initialise_new_objects(f"[{i}]", item))
|
||||
|
||||
self._notify_changed("", self)
|
||||
return popped_item
|
||||
|
||||
def remove(self, __value: Any) -> None:
|
||||
self._remove_self_from_observables()
|
||||
|
||||
try:
|
||||
super().remove(__value)
|
||||
finally:
|
||||
for i, item in enumerate(self):
|
||||
super().__setitem__(i, self._initialise_new_objects(f"[{i}]", item))
|
||||
|
||||
self._notify_changed("", self)
|
||||
|
||||
def _remove_self_from_observables(self) -> None:
|
||||
for i in range(len(self)):
|
||||
self._remove_observer_if_observable(f"[{i}]")
|
||||
|
||||
def _remove_observer_if_observable(self, name: str) -> None:
|
||||
key = int(name[1:-1])
|
||||
current_value = self.__getitem__(key)
|
||||
|
||||
if isinstance(current_value, ObservableObject):
|
||||
current_value._remove_observer(self, name)
|
||||
|
||||
def _construct_extended_attr_path(
|
||||
self, observer_attr_name: str, instance_attr_name: str
|
||||
) -> str:
|
||||
if observer_attr_name != "":
|
||||
return f"{observer_attr_name}{instance_attr_name}"
|
||||
return instance_attr_name
|
||||
|
||||
|
||||
class _ObservableDict(dict[str, Any], ObservableObject):
|
||||
def __init__(
|
||||
self,
|
||||
original_dict: dict[str, Any],
|
||||
) -> None:
|
||||
self._original_dict = original_dict
|
||||
ObservableObject.__init__(self)
|
||||
dict.__init__(self)
|
||||
for key, value in self._original_dict.items():
|
||||
super().__setitem__(key, self._initialise_new_objects(f"['{key}']", value))
|
||||
|
||||
def __setitem__(self, key: str, value: Any) -> None:
|
||||
if not isinstance(key, str):
|
||||
logger.warning("Converting non-string dictionary key %s to string.", key)
|
||||
key = str(key)
|
||||
|
||||
if hasattr(self, "_observers"):
|
||||
self._remove_observer_if_observable(f"['{key}']")
|
||||
value = self._initialise_new_objects(key, value)
|
||||
self._notify_change_start(f"['{key}']")
|
||||
|
||||
super().__setitem__(key, value)
|
||||
|
||||
self._notify_changed(f"['{key}']", value)
|
||||
|
||||
def _remove_observer_if_observable(self, name: str) -> None:
|
||||
key = name[2:-2]
|
||||
current_value = self.get(key, None)
|
||||
|
||||
if isinstance(current_value, ObservableObject):
|
||||
current_value._remove_observer(self, name)
|
||||
|
||||
def _construct_extended_attr_path(
|
||||
self, observer_attr_name: str, instance_attr_name: str
|
||||
) -> str:
|
||||
if observer_attr_name != "":
|
||||
return f"{observer_attr_name}{instance_attr_name}"
|
||||
return instance_attr_name
|
||||
7
src/pydase/observer_pattern/observer/__init__.py
Normal file
7
src/pydase/observer_pattern/observer/__init__.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from pydase.observer_pattern.observer.observer import Observer
|
||||
from pydase.observer_pattern.observer.property_observer import PropertyObserver
|
||||
|
||||
__all__ = [
|
||||
"Observer",
|
||||
"PropertyObserver",
|
||||
]
|
||||
31
src/pydase/observer_pattern/observer/observer.py
Normal file
31
src/pydase/observer_pattern/observer/observer.py
Normal file
@@ -0,0 +1,31 @@
|
||||
import logging
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Any
|
||||
|
||||
from pydase.observer_pattern.observable import Observable
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Observer(ABC):
|
||||
def __init__(self, observable: Observable) -> None:
|
||||
self.observable = observable
|
||||
self.observable.add_observer(self)
|
||||
self.changing_attributes: list[str] = []
|
||||
|
||||
def _notify_changed(self, changed_attribute: str, value: Any) -> None:
|
||||
if changed_attribute in self.changing_attributes:
|
||||
self.changing_attributes.remove(changed_attribute)
|
||||
|
||||
self.on_change(full_access_path=changed_attribute, value=value)
|
||||
|
||||
def _notify_change_start(self, changing_attribute: str) -> None:
|
||||
self.changing_attributes.append(changing_attribute)
|
||||
self.on_change_start(changing_attribute)
|
||||
|
||||
@abstractmethod
|
||||
def on_change(self, full_access_path: str, value: Any) -> None:
|
||||
...
|
||||
|
||||
def on_change_start(self, full_access_path: str) -> None:
|
||||
return
|
||||
95
src/pydase/observer_pattern/observer/property_observer.py
Normal file
95
src/pydase/observer_pattern/observer/property_observer.py
Normal file
@@ -0,0 +1,95 @@
|
||||
import inspect
|
||||
import logging
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
from pydase.observer_pattern.observable.observable import Observable
|
||||
from pydase.observer_pattern.observer.observer import Observer
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def reverse_dict(original_dict: dict[str, list[str]]) -> dict[str, list[str]]:
|
||||
reversed_dict: dict[str, list[str]] = {
|
||||
value: [] for values in original_dict.values() for value in values
|
||||
}
|
||||
for key, values in original_dict.items():
|
||||
for value in values:
|
||||
reversed_dict[value].append(key)
|
||||
return reversed_dict
|
||||
|
||||
|
||||
def get_property_dependencies(prop: property, prefix: str = "") -> list[str]:
|
||||
source_code_string = inspect.getsource(prop.fget) # type: ignore[arg-type]
|
||||
pattern = r"self\.([^\s\{\}]+)"
|
||||
matches = re.findall(pattern, source_code_string)
|
||||
return [prefix + match for match in matches if "(" not in match]
|
||||
|
||||
|
||||
class PropertyObserver(Observer):
|
||||
def __init__(self, observable: Observable) -> None:
|
||||
super().__init__(observable)
|
||||
self._update_property_deps_dict()
|
||||
|
||||
def _update_property_deps_dict(self) -> None:
|
||||
self.property_deps_dict = reverse_dict(
|
||||
self._get_properties_and_their_dependencies(self.observable)
|
||||
)
|
||||
|
||||
def _get_properties_and_their_dependencies(
|
||||
self, obj: Observable, prefix: str = ""
|
||||
) -> dict[str, list[str]]:
|
||||
deps: dict[str, Any] = {}
|
||||
|
||||
self._process_observable_properties(obj, deps, prefix)
|
||||
self._process_nested_observables_properties(obj, deps, prefix)
|
||||
|
||||
return deps
|
||||
|
||||
def _process_observable_properties(
|
||||
self, obj: Observable, deps: dict[str, Any], prefix: str
|
||||
) -> None:
|
||||
for k, value in vars(type(obj)).items():
|
||||
prefix = (
|
||||
f"{prefix}." if prefix != "" and not prefix.endswith(".") else prefix
|
||||
)
|
||||
key = f"{prefix}{k}"
|
||||
if isinstance(value, property):
|
||||
deps[key] = get_property_dependencies(value, prefix)
|
||||
|
||||
def _process_nested_observables_properties(
|
||||
self, obj: Observable, deps: dict[str, Any], prefix: str
|
||||
) -> None:
|
||||
for k, value in vars(obj).items():
|
||||
prefix = (
|
||||
f"{prefix}." if prefix != "" and not prefix.endswith(".") else prefix
|
||||
)
|
||||
parent_path = f"{prefix}{k}"
|
||||
if isinstance(value, Observable):
|
||||
new_prefix = f"{parent_path}."
|
||||
deps.update(
|
||||
self._get_properties_and_their_dependencies(value, new_prefix)
|
||||
)
|
||||
elif isinstance(value, list | dict):
|
||||
self._process_collection_item_properties(value, deps, parent_path)
|
||||
|
||||
def _process_collection_item_properties(
|
||||
self,
|
||||
collection: list[Any] | dict[str, Any],
|
||||
deps: dict[str, Any],
|
||||
parent_path: str,
|
||||
) -> None:
|
||||
if isinstance(collection, list):
|
||||
for i, item in enumerate(collection):
|
||||
if isinstance(item, Observable):
|
||||
new_prefix = f"{parent_path}[{i}]"
|
||||
deps.update(
|
||||
self._get_properties_and_their_dependencies(item, new_prefix)
|
||||
)
|
||||
elif isinstance(collection, dict):
|
||||
for key, val in collection.items():
|
||||
if isinstance(val, Observable):
|
||||
new_prefix = f"{parent_path}['{key}']"
|
||||
deps.update(
|
||||
self._get_properties_and_their_dependencies(val, new_prefix)
|
||||
)
|
||||
@@ -1,3 +1,7 @@
|
||||
from pydase.server.server import Server
|
||||
from pydase.server.web_server.web_server import WebServer
|
||||
|
||||
__all__ = ["Server"]
|
||||
__all__ = [
|
||||
"Server",
|
||||
"WebServer",
|
||||
]
|
||||
|
||||
@@ -4,21 +4,18 @@ import os
|
||||
import signal
|
||||
import threading
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from enum import Enum
|
||||
from pathlib import Path
|
||||
from types import FrameType
|
||||
from typing import Any, Optional, Protocol, TypedDict
|
||||
from typing import Any, Protocol, TypedDict
|
||||
|
||||
import uvicorn
|
||||
from rpyc import ForkingServer, ThreadedServer # type: ignore
|
||||
from rpyc import ThreadedServer # type: ignore[import-untyped]
|
||||
from uvicorn.server import HANDLED_SIGNALS
|
||||
|
||||
import pydase.units as u
|
||||
from pydase import DataService
|
||||
from pydase.config import ServiceConfig
|
||||
from pydase.data_service.data_service_observer import DataServiceObserver
|
||||
from pydase.data_service.state_manager import StateManager
|
||||
from pydase.version import __version__
|
||||
|
||||
from .web_server import WebAPI
|
||||
from pydase.server.web_server import WebServer
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -32,35 +29,27 @@ class AdditionalServerProtocol(Protocol):
|
||||
any server implementing it should have an __init__ method for initialization and a
|
||||
serve method for starting the server.
|
||||
|
||||
Parameters:
|
||||
-----------
|
||||
service: DataService
|
||||
The instance of DataService that the server will use. This could be the main
|
||||
application or a specific service that the server will provide.
|
||||
|
||||
port: int
|
||||
The port number at which the server will be accessible. This should be a valid
|
||||
port number, typically in the range 1024-65535.
|
||||
|
||||
host: str
|
||||
The hostname or IP address at which the server will be hosted. This could be a
|
||||
local address (like '127.0.0.1' for localhost) or a public IP address.
|
||||
|
||||
state_manager: StateManager
|
||||
The state manager managing the state cache and persistence of the exposed
|
||||
service.
|
||||
|
||||
**kwargs: Any
|
||||
Any additional parameters required for initializing the server. These parameters
|
||||
are specific to the server's implementation.
|
||||
Args:
|
||||
data_service_observer:
|
||||
Observer for the DataService, handling state updates and communication to
|
||||
connected clients through injected callbacks. Can be utilized to access the
|
||||
service and state manager, and to add custom state-update callbacks.
|
||||
host:
|
||||
Hostname or IP address where the server is accessible. Commonly '0.0.0.0' to
|
||||
bind to all network interfaces.
|
||||
port:
|
||||
Port number on which the server listens. Typically in the range 1024-65535
|
||||
(non-standard ports).
|
||||
**kwargs:
|
||||
Any additional parameters required for initializing the server. These
|
||||
parameters are specific to the server's implementation.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
service: DataService,
|
||||
port: int,
|
||||
data_service_observer: DataServiceObserver,
|
||||
host: str,
|
||||
state_manager: StateManager,
|
||||
port: int,
|
||||
**kwargs: Any,
|
||||
) -> None:
|
||||
...
|
||||
@@ -69,7 +58,6 @@ class AdditionalServerProtocol(Protocol):
|
||||
"""Starts the server. This method should be implemented as an asynchronous
|
||||
method, which means that it should be able to run concurrently with other tasks.
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
class AdditionalServer(TypedDict):
|
||||
@@ -91,121 +79,107 @@ class Server:
|
||||
"""
|
||||
The `Server` class provides a flexible server implementation for the `DataService`.
|
||||
|
||||
Parameters:
|
||||
-----------
|
||||
service: DataService
|
||||
The DataService instance that this server will manage.
|
||||
host: str
|
||||
The host address for the server. Default is '0.0.0.0', which means all available
|
||||
network interfaces.
|
||||
rpc_port: int
|
||||
The port number for the RPC server. Default is 18871.
|
||||
web_port: int
|
||||
The port number for the web server. Default is 8001.
|
||||
enable_rpc: bool
|
||||
Whether to enable the RPC server. Default is True.
|
||||
enable_web: bool
|
||||
Whether to enable the web server. Default is True.
|
||||
filename: str | Path | None
|
||||
Filename of the file managing the service state persistence. Defaults to None.
|
||||
use_forking_server: bool
|
||||
Whether to use ForkingServer for multiprocessing. Default is False.
|
||||
web_settings: dict[str, Any]
|
||||
Additional settings for the web server. Default is {} (an empty dictionary).
|
||||
additional_servers : list[AdditionalServer]
|
||||
A list of additional servers to run alongside the main server. Each entry in the
|
||||
list should be a dictionary with the following structure:
|
||||
Args:
|
||||
service: DataService
|
||||
The DataService instance that this server will manage.
|
||||
host: str
|
||||
The host address for the server. Default is '0.0.0.0', which means all
|
||||
available network interfaces.
|
||||
rpc_port: int
|
||||
The port number for the RPC server. Default is
|
||||
`pydase.config.ServiceConfig().rpc_port`.
|
||||
web_port: int
|
||||
The port number for the web server. Default is
|
||||
`pydase.config.ServiceConfig().web_port`.
|
||||
enable_rpc: bool
|
||||
Whether to enable the RPC server. Default is True.
|
||||
enable_web: bool
|
||||
Whether to enable the web server. Default is True.
|
||||
filename: str | Path | None
|
||||
Filename of the file managing the service state persistence. Defaults to None.
|
||||
use_forking_server: bool
|
||||
Whether to use ForkingServer for multiprocessing. Default is False.
|
||||
additional_servers : list[AdditionalServer]
|
||||
A list of additional servers to run alongside the main server. Each entry in
|
||||
the list should be a dictionary with the following structure:
|
||||
- server: A class that adheres to the AdditionalServerProtocol. This class
|
||||
should have an `__init__` method that accepts the DataService instance,
|
||||
port, host, and optional keyword arguments, and a `serve` method that is
|
||||
a coroutine responsible for starting the server.
|
||||
- port: The port on which the additional server will be running.
|
||||
- kwargs: A dictionary containing additional keyword arguments that will be
|
||||
passed to the server's `__init__` method.
|
||||
|
||||
- server: A class that adheres to the AdditionalServerProtocol. This class
|
||||
should have an `__init__` method that accepts the DataService instance,
|
||||
port, host, and optional keyword arguments, and a `serve` method that is a
|
||||
coroutine responsible for starting the server.
|
||||
- port: The port on which the additional server will be running.
|
||||
- kwargs: A dictionary containing additional keyword arguments that will be
|
||||
passed to the server's `__init__` method.
|
||||
|
||||
Here's an example of how you might define an additional server:
|
||||
Here's an example of how you might define an additional server:
|
||||
|
||||
|
||||
>>> class MyCustomServer:
|
||||
... def __init__(
|
||||
... self,
|
||||
... service: DataService,
|
||||
... port: int,
|
||||
... host: str,
|
||||
... state_manager: StateManager,
|
||||
... **kwargs: Any
|
||||
... ):
|
||||
... self.service = service
|
||||
... self.state_manager = state_manager
|
||||
... self.port = port
|
||||
... self.host = host
|
||||
... # handle any additional arguments...
|
||||
...
|
||||
... async def serve(self):
|
||||
... # code to start the server...
|
||||
>>> class MyCustomServer:
|
||||
... def __init__(
|
||||
... self,
|
||||
... data_service_observer: DataServiceObserver,
|
||||
... host: str,
|
||||
... port: int,
|
||||
... **kwargs: Any,
|
||||
... ) -> None:
|
||||
... self.observer = data_service_observer
|
||||
... self.state_manager = self.observer.state_manager
|
||||
... self.service = self.state_manager.service
|
||||
... self.port = port
|
||||
... self.host = host
|
||||
... # handle any additional arguments...
|
||||
...
|
||||
... async def serve(self):
|
||||
... # code to start the server...
|
||||
|
||||
And here's how you might add it to the `additional_servers` list when creating a
|
||||
`Server` instance:
|
||||
And here's how you might add it to the `additional_servers` list when creating
|
||||
a `Server` instance:
|
||||
|
||||
>>> server = Server(
|
||||
... service=my_data_service,
|
||||
... additional_servers=[
|
||||
... {
|
||||
... "server": MyCustomServer,
|
||||
... "port": 12345,
|
||||
... "kwargs": {"some_arg": "some_value"}
|
||||
... }
|
||||
... ],
|
||||
... )
|
||||
... server.run()
|
||||
>>> server = Server(
|
||||
... service=my_data_service,
|
||||
... additional_servers=[
|
||||
... {
|
||||
... "server": MyCustomServer,
|
||||
... "port": 12345,
|
||||
... "kwargs": {"some_arg": "some_value"}
|
||||
... }
|
||||
... ],
|
||||
... )
|
||||
... server.run()
|
||||
|
||||
**kwargs: Any
|
||||
Additional keyword arguments.
|
||||
**kwargs: Any
|
||||
Additional keyword arguments.
|
||||
"""
|
||||
|
||||
def __init__( # noqa: CFQ002
|
||||
def __init__( # noqa: PLR0913
|
||||
self,
|
||||
service: DataService,
|
||||
host: str = "0.0.0.0",
|
||||
rpc_port: int = 18871,
|
||||
web_port: int = 8001,
|
||||
rpc_port: int = ServiceConfig().rpc_port,
|
||||
web_port: int = ServiceConfig().web_port,
|
||||
enable_rpc: bool = True,
|
||||
enable_web: bool = True,
|
||||
filename: Optional[str | Path] = None,
|
||||
use_forking_server: bool = False,
|
||||
web_settings: dict[str, Any] = {},
|
||||
additional_servers: list[AdditionalServer] = [],
|
||||
filename: str | Path | None = None,
|
||||
additional_servers: list[AdditionalServer] | None = None,
|
||||
**kwargs: Any,
|
||||
) -> None:
|
||||
if additional_servers is None:
|
||||
additional_servers = []
|
||||
self._service = service
|
||||
self._host = host
|
||||
self._rpc_port = rpc_port
|
||||
self._web_port = web_port
|
||||
self._enable_rpc = enable_rpc
|
||||
self._enable_web = enable_web
|
||||
self._web_settings = web_settings
|
||||
self._kwargs = kwargs
|
||||
self._loop: asyncio.AbstractEventLoop
|
||||
self._rpc_server_type = ForkingServer if use_forking_server else ThreadedServer
|
||||
self._additional_servers = additional_servers
|
||||
self.should_exit = False
|
||||
self.servers: dict[str, asyncio.Future[Any]] = {}
|
||||
self.executor: ThreadPoolExecutor | None = None
|
||||
self._info: dict[str, Any] = {
|
||||
"name": self._service.get_service_name(),
|
||||
"version": __version__,
|
||||
"rpc_port": self._rpc_port,
|
||||
"web_port": self._web_port,
|
||||
"enable_rpc": self._enable_rpc,
|
||||
"enable_web": self._enable_web,
|
||||
"web_settings": self._web_settings,
|
||||
"additional_servers": [],
|
||||
**kwargs,
|
||||
}
|
||||
self._state_manager = StateManager(self._service, filename)
|
||||
if getattr(self._service, "_filename", None) is not None:
|
||||
self._service._state_manager = self._state_manager
|
||||
self._observer = DataServiceObserver(self._state_manager)
|
||||
self._state_manager.load_state()
|
||||
|
||||
def run(self) -> None:
|
||||
@@ -213,28 +187,13 @@ class Server:
|
||||
Initializes the asyncio event loop and starts the server.
|
||||
|
||||
This method should be called to start the server after it's been instantiated.
|
||||
|
||||
Raises
|
||||
------
|
||||
Exception
|
||||
If there's an error while running the server, the error will be propagated
|
||||
after the server is shut down.
|
||||
"""
|
||||
try:
|
||||
self._loop = asyncio.get_event_loop()
|
||||
except RuntimeError:
|
||||
self._loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(self._loop)
|
||||
try:
|
||||
self._loop.run_until_complete(self.serve())
|
||||
except Exception:
|
||||
self._loop.run_until_complete(self.shutdown())
|
||||
raise
|
||||
asyncio.run(self.serve())
|
||||
|
||||
async def serve(self) -> None:
|
||||
process_id = os.getpid()
|
||||
|
||||
logger.info(f"Started server process [{process_id}]")
|
||||
logger.info("Started server process [%s]", process_id)
|
||||
|
||||
await self.startup()
|
||||
if self.should_exit:
|
||||
@@ -242,9 +201,9 @@ class Server:
|
||||
await self.main_loop()
|
||||
await self.shutdown()
|
||||
|
||||
logger.info(f"Finished server process [{process_id}]")
|
||||
logger.info("Finished server process [%s]", process_id)
|
||||
|
||||
async def startup(self) -> None: # noqa: C901
|
||||
async def startup(self) -> None:
|
||||
self._loop = asyncio.get_running_loop()
|
||||
self._loop.set_exception_handler(self.custom_exception_handler)
|
||||
self.install_signal_handlers()
|
||||
@@ -252,7 +211,7 @@ class Server:
|
||||
|
||||
if self._enable_rpc:
|
||||
self.executor = ThreadPoolExecutor()
|
||||
self._rpc_server = self._rpc_server_type(
|
||||
self._rpc_server = ThreadedServer(
|
||||
self._service,
|
||||
port=self._rpc_port,
|
||||
protocol_config={
|
||||
@@ -266,75 +225,26 @@ class Server:
|
||||
self.servers["rpyc"] = future_or_task
|
||||
for server in self._additional_servers:
|
||||
addin_server = server["server"](
|
||||
self._service,
|
||||
port=server["port"],
|
||||
data_service_observer=self._observer,
|
||||
host=self._host,
|
||||
state_manager=self._state_manager,
|
||||
info=self._info,
|
||||
port=server["port"],
|
||||
**server["kwargs"],
|
||||
)
|
||||
|
||||
server_name = (
|
||||
addin_server.__module__ + "." + addin_server.__class__.__name__
|
||||
)
|
||||
self._info["additional_servers"].append(
|
||||
{
|
||||
"name": server_name,
|
||||
"port": server["port"],
|
||||
"host": self._host,
|
||||
**server["kwargs"],
|
||||
}
|
||||
)
|
||||
|
||||
future_or_task = self._loop.create_task(addin_server.serve())
|
||||
self.servers[server_name] = future_or_task
|
||||
if self._enable_web:
|
||||
self._wapi: WebAPI = WebAPI(
|
||||
service=self._service,
|
||||
info=self._info,
|
||||
state_manager=self._state_manager,
|
||||
self._web_server = WebServer(
|
||||
data_service_observer=self._observer,
|
||||
host=self._host,
|
||||
port=self._web_port,
|
||||
**self._kwargs,
|
||||
)
|
||||
web_server = uvicorn.Server(
|
||||
uvicorn.Config(
|
||||
self._wapi.fastapi_app, host=self._host, port=self._web_port
|
||||
)
|
||||
)
|
||||
|
||||
def sio_callback(parent_path: str, name: str, value: Any) -> None:
|
||||
# TODO: an error happens when an attribute is set to a list
|
||||
# > File "/usr/lib64/python3.11/json/encoder.py", line 180, in default
|
||||
# > raise TypeError(f'Object of type {o.__class__.__name__} '
|
||||
# > TypeError: Object of type list is not JSON serializable
|
||||
notify_value = value
|
||||
if isinstance(value, Enum):
|
||||
notify_value = value.name
|
||||
if isinstance(value, u.Quantity):
|
||||
notify_value = {"magnitude": value.m, "unit": str(value.u)}
|
||||
|
||||
async def notify() -> None:
|
||||
try:
|
||||
await self._wapi.sio.emit( # type: ignore
|
||||
"notify",
|
||||
{
|
||||
"data": {
|
||||
"parent_path": parent_path,
|
||||
"name": name,
|
||||
"value": notify_value,
|
||||
}
|
||||
},
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to send notification: {e}")
|
||||
|
||||
self._loop.create_task(notify())
|
||||
|
||||
self._service._callback_manager.add_notification_callback(sio_callback)
|
||||
|
||||
# overwrite uvicorn's signal handlers, otherwise it will bogart SIGINT and
|
||||
# SIGTERM, which makes it impossible to escape out of
|
||||
web_server.install_signal_handlers = lambda: None # type: ignore
|
||||
future_or_task = self._loop.create_task(web_server.serve())
|
||||
future_or_task = self._loop.create_task(self._web_server.serve())
|
||||
self.servers["web"] = future_or_task
|
||||
|
||||
async def main_loop(self) -> None:
|
||||
@@ -344,9 +254,8 @@ class Server:
|
||||
async def shutdown(self) -> None:
|
||||
logger.info("Shutting down")
|
||||
|
||||
logger.info(f"Saving data to {self._state_manager.filename}.")
|
||||
if self._state_manager is not None:
|
||||
self._state_manager.save_state()
|
||||
logger.info("Saving data to %s.", self._state_manager.filename)
|
||||
self._state_manager.save_state()
|
||||
|
||||
await self.__cancel_servers()
|
||||
await self.__cancel_tasks()
|
||||
@@ -361,9 +270,9 @@ class Server:
|
||||
try:
|
||||
await task
|
||||
except asyncio.CancelledError:
|
||||
logger.debug(f"Cancelled {server_name} server.")
|
||||
logger.debug("Cancelled '%s' server.", server_name)
|
||||
except Exception as e:
|
||||
logger.warning(f"Unexpected exception: {e}.")
|
||||
logger.warning("Unexpected exception: %s", e)
|
||||
|
||||
async def __cancel_tasks(self) -> None:
|
||||
for task in asyncio.all_tasks(self._loop):
|
||||
@@ -371,9 +280,9 @@ class Server:
|
||||
try:
|
||||
await task
|
||||
except asyncio.CancelledError:
|
||||
logger.debug(f"Cancelled task {task.get_coro()}.")
|
||||
logger.debug("Cancelled task '%s'.", task.get_coro())
|
||||
except Exception as e:
|
||||
logger.warning(f"Unexpected exception: {e}.")
|
||||
logger.exception("Unexpected exception: %s", e)
|
||||
|
||||
def install_signal_handlers(self) -> None:
|
||||
if threading.current_thread() is not threading.main_thread():
|
||||
@@ -383,13 +292,15 @@ class Server:
|
||||
for sig in HANDLED_SIGNALS:
|
||||
signal.signal(sig, self.handle_exit)
|
||||
|
||||
def handle_exit(self, sig: int = 0, frame: Optional[FrameType] = None) -> None:
|
||||
def handle_exit(self, sig: int = 0, frame: FrameType | None = None) -> None:
|
||||
if self.should_exit and sig == signal.SIGINT:
|
||||
logger.warning(f"Received signal {sig}, forcing exit...")
|
||||
logger.warning("Received signal '%s', forcing exit...", sig)
|
||||
os._exit(1)
|
||||
else:
|
||||
self.should_exit = True
|
||||
logger.warning(f"Received signal {sig}, exiting... (CTRL+C to force quit)")
|
||||
logger.warning(
|
||||
"Received signal '%s', exiting... (CTRL+C to force quit)", sig
|
||||
)
|
||||
|
||||
def custom_exception_handler(
|
||||
self, loop: asyncio.AbstractEventLoop, context: dict[str, Any]
|
||||
@@ -406,7 +317,7 @@ class Server:
|
||||
|
||||
async def emit_exception() -> None:
|
||||
try:
|
||||
await self._wapi.sio.emit( # type: ignore
|
||||
await self._web_server._sio.emit(
|
||||
"exception",
|
||||
{
|
||||
"data": {
|
||||
@@ -416,7 +327,7 @@ class Server:
|
||||
},
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to send notification: {e}")
|
||||
logger.exception("Failed to send notification: %s", e)
|
||||
|
||||
loop.create_task(emit_exception())
|
||||
else:
|
||||
|
||||
@@ -1,175 +0,0 @@
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Any, TypedDict
|
||||
|
||||
import socketio # type: ignore
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import FileResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
||||
from pydase import DataService
|
||||
from pydase.data_service.data_service import process_callable_attribute
|
||||
from pydase.data_service.state_manager import StateManager
|
||||
from pydase.utils.helpers import get_object_attr_from_path_list
|
||||
from pydase.version import __version__
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class UpdateDict(TypedDict):
|
||||
"""
|
||||
A TypedDict subclass representing a dictionary used for updating attributes in a
|
||||
DataService.
|
||||
|
||||
Attributes:
|
||||
----------
|
||||
name : str
|
||||
The name of the attribute to be updated in the DataService instance.
|
||||
If the attribute is part of a nested structure, this would be the name of the
|
||||
attribute in the last nested object. For example, for an attribute access path
|
||||
'attr1.list_attr[0].attr2', 'attr2' would be the name.
|
||||
|
||||
parent_path : str
|
||||
The access path for the parent object of the attribute to be updated. This is
|
||||
used to construct the full access path for the attribute. For example, for an
|
||||
attribute access path 'attr1.list_attr[0].attr2', 'attr1.list_attr[0]' would be
|
||||
the parent_path.
|
||||
|
||||
value : Any
|
||||
The new value to be assigned to the attribute. The type of this value should
|
||||
match the type of the attribute to be updated.
|
||||
"""
|
||||
|
||||
name: str
|
||||
parent_path: str
|
||||
value: Any
|
||||
|
||||
|
||||
class RunMethodDict(TypedDict):
|
||||
"""
|
||||
A TypedDict subclass representing a dictionary used for running methods from the
|
||||
exposed DataService.
|
||||
|
||||
Attributes:
|
||||
name (str): The name of the method to be run.
|
||||
parent_path (str): The access path for the parent object of the method to be
|
||||
run. This is used to construct the full access path for the method. For
|
||||
example, for an method with access path 'attr1.list_attr[0].method_name',
|
||||
'attr1.list_attr[0]' would be the parent_path.
|
||||
kwargs (dict[str, Any]): The arguments passed to the method.
|
||||
"""
|
||||
|
||||
name: str
|
||||
parent_path: str
|
||||
kwargs: dict[str, Any]
|
||||
|
||||
|
||||
class WebAPI:
|
||||
__sio_app: socketio.ASGIApp
|
||||
__fastapi_app: FastAPI
|
||||
|
||||
def __init__( # noqa: CFQ002
|
||||
self,
|
||||
service: DataService,
|
||||
state_manager: StateManager,
|
||||
frontend: str | Path | None = None,
|
||||
css: str | Path | None = None,
|
||||
enable_CORS: bool = True,
|
||||
info: dict[str, Any] = {},
|
||||
*args: Any,
|
||||
**kwargs: Any,
|
||||
):
|
||||
self.service = service
|
||||
self.state_manager = state_manager
|
||||
self.frontend = frontend
|
||||
self.css = css
|
||||
self.enable_CORS = enable_CORS
|
||||
self.info = info
|
||||
self.args = args
|
||||
self.kwargs = kwargs
|
||||
|
||||
self.setup_socketio()
|
||||
self.setup_fastapi_app()
|
||||
|
||||
def setup_socketio(self) -> None:
|
||||
# the socketio ASGI app, to notify clients when params update
|
||||
if self.enable_CORS:
|
||||
sio = socketio.AsyncServer(async_mode="asgi", cors_allowed_origins="*")
|
||||
else:
|
||||
sio = socketio.AsyncServer(async_mode="asgi")
|
||||
|
||||
@sio.event # type: ignore
|
||||
def set_attribute(sid: str, data: UpdateDict) -> Any:
|
||||
logger.debug(f"Received frontend update: {data}")
|
||||
path_list = [*data["parent_path"].split("."), data["name"]]
|
||||
path_list.remove("DataService") # always at the start, does not do anything
|
||||
path = ".".join(path_list)
|
||||
return self.state_manager.set_service_attribute_value_by_path(
|
||||
path=path, value=data["value"]
|
||||
)
|
||||
|
||||
@sio.event # type: ignore
|
||||
def run_method(sid: str, data: RunMethodDict) -> Any:
|
||||
logger.debug(f"Running method: {data}")
|
||||
path_list = [*data["parent_path"].split("."), data["name"]]
|
||||
path_list.remove("DataService") # always at the start, does not do anything
|
||||
method = get_object_attr_from_path_list(self.service, path_list)
|
||||
return process_callable_attribute(method, data["kwargs"])
|
||||
|
||||
self.__sio = sio
|
||||
self.__sio_app = socketio.ASGIApp(self.__sio)
|
||||
|
||||
def setup_fastapi_app(self) -> None: # noqa
|
||||
app = FastAPI()
|
||||
|
||||
if self.enable_CORS:
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_credentials=True,
|
||||
allow_origins=["*"],
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
app.mount("/ws", self.__sio_app)
|
||||
|
||||
@app.get("/version")
|
||||
def version() -> str:
|
||||
return __version__
|
||||
|
||||
@app.get("/name")
|
||||
def name() -> str:
|
||||
return self.service.get_service_name()
|
||||
|
||||
@app.get("/info")
|
||||
def info() -> dict[str, Any]:
|
||||
return self.info
|
||||
|
||||
@app.get("/service-properties")
|
||||
def service_properties() -> dict[str, Any]:
|
||||
return self.state_manager.cache
|
||||
|
||||
# exposing custom.css file provided by user
|
||||
if self.css is not None:
|
||||
|
||||
@app.get("/custom.css")
|
||||
async def styles() -> FileResponse:
|
||||
return FileResponse(str(self.css))
|
||||
|
||||
app.mount(
|
||||
"/",
|
||||
StaticFiles(
|
||||
directory=Path(__file__).parent.parent / "frontend",
|
||||
html=True,
|
||||
),
|
||||
)
|
||||
|
||||
self.__fastapi_app = app
|
||||
|
||||
@property
|
||||
def sio(self) -> socketio.AsyncServer:
|
||||
return self.__sio
|
||||
|
||||
@property
|
||||
def fastapi_app(self) -> FastAPI:
|
||||
return self.__fastapi_app
|
||||
3
src/pydase/server/web_server/__init__.py
Normal file
3
src/pydase/server/web_server/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from pydase.server.web_server.web_server import WebServer
|
||||
|
||||
__all__ = ["WebServer"]
|
||||
149
src/pydase/server/web_server/sio_setup.py
Normal file
149
src/pydase/server/web_server/sio_setup.py
Normal file
@@ -0,0 +1,149 @@
|
||||
import asyncio
|
||||
import logging
|
||||
from typing import Any, TypedDict
|
||||
|
||||
import socketio # type: ignore[import-untyped]
|
||||
|
||||
from pydase.data_service.data_service import process_callable_attribute
|
||||
from pydase.data_service.data_service_observer import DataServiceObserver
|
||||
from pydase.data_service.state_manager import StateManager
|
||||
from pydase.utils.helpers import get_object_attr_from_path_list
|
||||
from pydase.utils.logging import SocketIOHandler
|
||||
from pydase.utils.serializer import dump
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class UpdateDict(TypedDict):
|
||||
"""
|
||||
A TypedDict subclass representing a dictionary used for updating attributes in a
|
||||
DataService.
|
||||
|
||||
Attributes:
|
||||
----------
|
||||
name : str
|
||||
The name of the attribute to be updated in the DataService instance.
|
||||
If the attribute is part of a nested structure, this would be the name of the
|
||||
attribute in the last nested object. For example, for an attribute access path
|
||||
'attr1.list_attr[0].attr2', 'attr2' would be the name.
|
||||
|
||||
parent_path : str
|
||||
The access path for the parent object of the attribute to be updated. This is
|
||||
used to construct the full access path for the attribute. For example, for an
|
||||
attribute access path 'attr1.list_attr[0].attr2', 'attr1.list_attr[0]' would be
|
||||
the parent_path.
|
||||
|
||||
value : Any
|
||||
The new value to be assigned to the attribute. The type of this value should
|
||||
match the type of the attribute to be updated.
|
||||
"""
|
||||
|
||||
name: str
|
||||
parent_path: str
|
||||
value: Any
|
||||
|
||||
|
||||
class RunMethodDict(TypedDict):
|
||||
"""
|
||||
A TypedDict subclass representing a dictionary used for running methods from the
|
||||
exposed DataService.
|
||||
|
||||
Attributes:
|
||||
name (str): The name of the method to be run.
|
||||
parent_path (str): The access path for the parent object of the method to be
|
||||
run. This is used to construct the full access path for the method. For
|
||||
example, for an method with access path 'attr1.list_attr[0].method_name',
|
||||
'attr1.list_attr[0]' would be the parent_path.
|
||||
kwargs (dict[str, Any]): The arguments passed to the method.
|
||||
"""
|
||||
|
||||
name: str
|
||||
parent_path: str
|
||||
kwargs: dict[str, Any]
|
||||
|
||||
|
||||
def setup_sio_server(
|
||||
observer: DataServiceObserver,
|
||||
enable_cors: bool,
|
||||
loop: asyncio.AbstractEventLoop,
|
||||
) -> socketio.AsyncServer:
|
||||
"""
|
||||
Sets up and configures a Socket.IO asynchronous server.
|
||||
|
||||
Args:
|
||||
observer (DataServiceObserver):
|
||||
The observer managing state updates and communication.
|
||||
enable_cors (bool):
|
||||
Flag indicating whether CORS should be enabled for the server.
|
||||
loop (asyncio.AbstractEventLoop):
|
||||
The event loop in which the server will run.
|
||||
|
||||
Returns:
|
||||
socketio.AsyncServer: The configured Socket.IO asynchronous server.
|
||||
"""
|
||||
|
||||
state_manager = observer.state_manager
|
||||
|
||||
if enable_cors:
|
||||
sio = socketio.AsyncServer(async_mode="asgi", cors_allowed_origins="*")
|
||||
else:
|
||||
sio = socketio.AsyncServer(async_mode="asgi")
|
||||
|
||||
setup_sio_events(sio, state_manager)
|
||||
setup_logging_handler(sio)
|
||||
|
||||
# Add notification callback to observer
|
||||
def sio_callback(
|
||||
full_access_path: str, value: Any, cached_value_dict: dict[str, Any]
|
||||
) -> None:
|
||||
if cached_value_dict != {}:
|
||||
serialized_value = dump(value)
|
||||
if cached_value_dict["type"] != "method":
|
||||
cached_value_dict["type"] = serialized_value["type"]
|
||||
|
||||
cached_value_dict["value"] = serialized_value["value"]
|
||||
|
||||
async def notify() -> None:
|
||||
try:
|
||||
await sio.emit(
|
||||
"notify",
|
||||
{
|
||||
"data": {
|
||||
"full_access_path": full_access_path,
|
||||
"value": cached_value_dict,
|
||||
}
|
||||
},
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning("Failed to send notification: %s", e)
|
||||
|
||||
loop.create_task(notify())
|
||||
|
||||
observer.add_notification_callback(sio_callback)
|
||||
|
||||
return sio
|
||||
|
||||
|
||||
def setup_sio_events(sio: socketio.AsyncServer, state_manager: StateManager) -> None:
|
||||
@sio.event
|
||||
def set_attribute(sid: str, data: UpdateDict) -> Any:
|
||||
logger.debug("Received frontend update: %s", data)
|
||||
parent_path = data["parent_path"].split(".")
|
||||
path_list = [element for element in parent_path if element] + [data["name"]]
|
||||
path = ".".join(path_list)
|
||||
return state_manager.set_service_attribute_value_by_path(
|
||||
path=path, value=data["value"]
|
||||
)
|
||||
|
||||
@sio.event
|
||||
def run_method(sid: str, data: RunMethodDict) -> Any:
|
||||
logger.debug("Running method: %s", data)
|
||||
parent_path = data["parent_path"].split(".")
|
||||
path_list = [element for element in parent_path if element] + [data["name"]]
|
||||
method = get_object_attr_from_path_list(state_manager.service, path_list)
|
||||
return process_callable_attribute(method, data["kwargs"])
|
||||
|
||||
|
||||
def setup_logging_handler(sio: socketio.AsyncServer) -> None:
|
||||
logger = logging.getLogger()
|
||||
logger.addHandler(SocketIOHandler(sio))
|
||||
185
src/pydase/server/web_server/web_server.py
Normal file
185
src/pydase/server/web_server/web_server.py
Normal file
@@ -0,0 +1,185 @@
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import socketio # type: ignore[import-untyped]
|
||||
import uvicorn
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import FileResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
||||
from pydase.config import ServiceConfig, WebServerConfig
|
||||
from pydase.data_service.data_service_observer import DataServiceObserver
|
||||
from pydase.server.web_server.sio_setup import (
|
||||
setup_sio_server,
|
||||
)
|
||||
from pydase.utils.serializer import generate_serialized_data_paths
|
||||
from pydase.version import __version__
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class WebServer:
|
||||
"""
|
||||
Represents a web server that adheres to the AdditionalServerProtocol, designed to
|
||||
work with a DataService instance. This server facilitates client-server
|
||||
communication and state management through web protocols and socket connections.
|
||||
|
||||
The WebServer class initializes and manages a web server environment using FastAPI
|
||||
and Socket.IO, allowing for HTTP and WebSocket communications. It incorporates CORS
|
||||
(Cross-Origin Resource Sharing) support, custom CSS, and serves a frontend static
|
||||
files directory. It also initializes web server settings based on configuration
|
||||
files or generates default settings if necessary.
|
||||
|
||||
Configuration for the web server (like service configuration directory and whether
|
||||
to generate new web settings) is determined in the following order of precedence:
|
||||
1. Values provided directly to the constructor.
|
||||
2. Environment variable settings (via configuration classes like
|
||||
`pydase.config.ServiceConfig` and `pydase.config.WebServerConfig`).
|
||||
3. Default values defined in the configuration classes.
|
||||
|
||||
Args:
|
||||
data_service_observer (DataServiceObserver): Observer for the DataService,
|
||||
handling state updates and communication to connected clients.
|
||||
host (str): Hostname or IP address where the server is accessible. Commonly
|
||||
'0.0.0.0' to bind to all network interfaces.
|
||||
port (int): Port number on which the server listens. Typically in the range
|
||||
1024-65535 (non-standard ports).
|
||||
css (str | Path | None, optional): Path to a custom CSS file for styling the
|
||||
frontend. If None, no custom styles are applied. Defaults to None.
|
||||
enable_cors (bool, optional): Flag to enable or disable CORS policy. When True,
|
||||
CORS is enabled, allowing cross-origin requests. Defaults to True.
|
||||
config_dir (Path | None, optional): Path to the configuration
|
||||
directory where the web settings will be stored. Defaults to
|
||||
`pydase.config.ServiceConfig().config_dir`.
|
||||
generate_new_web_settings (bool | None, optional): Flag to enable or disable
|
||||
generation of new web settings if the configuration file is missing. Defaults
|
||||
to `pydase.config.WebServerConfig().generate_new_web_settings`.
|
||||
**kwargs (Any): Additional unused keyword arguments.
|
||||
"""
|
||||
|
||||
def __init__( # noqa: PLR0913
|
||||
self,
|
||||
data_service_observer: DataServiceObserver,
|
||||
host: str,
|
||||
port: int,
|
||||
css: str | Path | None = None,
|
||||
enable_cors: bool = True,
|
||||
config_dir: Path = ServiceConfig().config_dir,
|
||||
generate_web_settings: bool = WebServerConfig().generate_web_settings,
|
||||
**kwargs: Any,
|
||||
) -> None:
|
||||
self.observer = data_service_observer
|
||||
self.state_manager = self.observer.state_manager
|
||||
self.service = self.state_manager.service
|
||||
self.port = port
|
||||
self.host = host
|
||||
self.css = css
|
||||
self.enable_cors = enable_cors
|
||||
self._service_config_dir = config_dir
|
||||
self._generate_web_settings = generate_web_settings
|
||||
self._loop: asyncio.AbstractEventLoop
|
||||
self._initialise_configuration()
|
||||
|
||||
async def serve(self) -> None:
|
||||
self._loop = asyncio.get_running_loop()
|
||||
self._setup_socketio()
|
||||
self._setup_fastapi_app()
|
||||
self.web_server = uvicorn.Server(
|
||||
uvicorn.Config(self.__fastapi_app, host=self.host, port=self.port)
|
||||
)
|
||||
# overwrite uvicorn's signal handlers, otherwise it will bogart SIGINT and
|
||||
# SIGTERM, which makes it impossible to escape out of
|
||||
self.web_server.install_signal_handlers = lambda: None # type: ignore[method-assign]
|
||||
await self.web_server.serve()
|
||||
|
||||
def _initialise_configuration(self) -> None:
|
||||
logger.debug("Initialising web server configuration...")
|
||||
|
||||
file_path = self._service_config_dir / "web_settings.json"
|
||||
|
||||
if self._generate_web_settings:
|
||||
# File does not exist, create it with default content
|
||||
logger.debug("Generating web settings file...")
|
||||
file_path.parent.mkdir(
|
||||
parents=True, exist_ok=True
|
||||
) # Ensure directory exists
|
||||
file_path.write_text(json.dumps(self.web_settings, indent=4))
|
||||
|
||||
def _get_web_settings_from_file(self) -> dict[str, dict[str, Any]]:
|
||||
file_path = self._service_config_dir / "web_settings.json"
|
||||
web_settings = {}
|
||||
|
||||
# File exists, read its content
|
||||
if file_path.exists():
|
||||
logger.debug(
|
||||
"Reading configuration from file '%s' ...", file_path.absolute()
|
||||
)
|
||||
|
||||
web_settings = json.loads(file_path.read_text())
|
||||
|
||||
return web_settings
|
||||
|
||||
@property
|
||||
def web_settings(self) -> dict[str, dict[str, Any]]:
|
||||
current_web_settings = self._get_web_settings_from_file()
|
||||
for path in generate_serialized_data_paths(self.state_manager.cache):
|
||||
if path in current_web_settings:
|
||||
continue
|
||||
|
||||
current_web_settings[path] = {"displayName": path.split(".")[-1]}
|
||||
|
||||
return current_web_settings
|
||||
|
||||
def _setup_socketio(self) -> None:
|
||||
self._sio = setup_sio_server(self.observer, self.enable_cors, self._loop)
|
||||
self.__sio_app = socketio.ASGIApp(self._sio)
|
||||
|
||||
def _setup_fastapi_app(self) -> None: # noqa: C901
|
||||
app = FastAPI()
|
||||
|
||||
if self.enable_cors:
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_credentials=True,
|
||||
allow_origins=["*"],
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
app.mount("/ws", self.__sio_app)
|
||||
|
||||
@app.get("/version")
|
||||
def version() -> str:
|
||||
return __version__
|
||||
|
||||
@app.get("/name")
|
||||
def name() -> str:
|
||||
return type(self.service).__name__
|
||||
|
||||
@app.get("/service-properties")
|
||||
def service_properties() -> dict[str, Any]:
|
||||
return self.state_manager.cache
|
||||
|
||||
@app.get("/web-settings")
|
||||
def web_settings() -> dict[str, Any]:
|
||||
return self.web_settings
|
||||
|
||||
# exposing custom.css file provided by user
|
||||
if self.css is not None:
|
||||
|
||||
@app.get("/custom.css")
|
||||
async def styles() -> FileResponse:
|
||||
return FileResponse(str(self.css))
|
||||
|
||||
app.mount(
|
||||
"/",
|
||||
StaticFiles(
|
||||
directory=Path(__file__).parent.parent.parent / "frontend",
|
||||
html=True,
|
||||
),
|
||||
)
|
||||
|
||||
self.__fastapi_app = app
|
||||
@@ -15,7 +15,7 @@ class QuantityDict(TypedDict):
|
||||
|
||||
|
||||
def convert_to_quantity(
|
||||
value: QuantityDict | float | int | Quantity, unit: str = ""
|
||||
value: QuantityDict | float | Quantity, unit: str = ""
|
||||
) -> Quantity:
|
||||
"""
|
||||
Convert a given value into a pint.Quantity object with the specified unit.
|
||||
@@ -53,4 +53,4 @@ def convert_to_quantity(
|
||||
quantity = float(value["magnitude"]) * Unit(value["unit"])
|
||||
else:
|
||||
quantity = value
|
||||
return quantity # type: ignore
|
||||
return quantity
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import inspect
|
||||
import logging
|
||||
from itertools import chain
|
||||
from typing import Any, Optional
|
||||
from typing import Any
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_attribute_doc(attr: Any) -> Optional[str]:
|
||||
def get_attribute_doc(attr: Any) -> str | None:
|
||||
"""This function takes an input attribute attr and returns its documentation
|
||||
string if it's different from the documentation of its type, otherwise,
|
||||
it returns None.
|
||||
@@ -26,9 +26,7 @@ def get_class_and_instance_attributes(obj: object) -> dict[str, Any]:
|
||||
loops.
|
||||
"""
|
||||
|
||||
attrs = dict(chain(type(obj).__dict__.items(), obj.__dict__.items()))
|
||||
attrs.pop("__root__")
|
||||
return attrs
|
||||
return dict(chain(type(obj).__dict__.items(), obj.__dict__.items()))
|
||||
|
||||
|
||||
def get_object_attr_from_path_list(target_obj: Any, path: list[str]) -> Any:
|
||||
@@ -59,7 +57,7 @@ def get_object_attr_from_path_list(target_obj: Any, path: list[str]) -> Any:
|
||||
target_obj = getattr(target_obj, part)
|
||||
except AttributeError:
|
||||
# The attribute doesn't exist
|
||||
logger.debug(f"Attribute {part} does not exist in the object.")
|
||||
logger.debug("Attribute % does not exist in the object.", part)
|
||||
return None
|
||||
return target_obj
|
||||
|
||||
@@ -141,10 +139,10 @@ def update_value_if_changed(
|
||||
if getattr(target, attr_name_or_index) != new_value:
|
||||
setattr(target, attr_name_or_index, new_value)
|
||||
else:
|
||||
logger.error(f"Incompatible arguments: {target}, {attr_name_or_index}.")
|
||||
logger.error("Incompatible arguments: %s, %s.", target, attr_name_or_index)
|
||||
|
||||
|
||||
def parse_list_attr_and_index(attr_string: str) -> tuple[str, Optional[int]]:
|
||||
def parse_list_attr_and_index(attr_string: str) -> tuple[str, int | None]:
|
||||
"""
|
||||
Parses an attribute string and extracts a potential list attribute name and its
|
||||
index.
|
||||
@@ -175,23 +173,25 @@ def parse_list_attr_and_index(attr_string: str) -> tuple[str, Optional[int]]:
|
||||
if index_part.isdigit():
|
||||
index = int(index_part)
|
||||
else:
|
||||
logger.error(f"Invalid index format in key: {attr_name}")
|
||||
logger.error("Invalid index format in key: %s", attr_name)
|
||||
return attr_name, index
|
||||
|
||||
|
||||
def get_component_class_names() -> list[str]:
|
||||
def get_component_classes() -> list[type]:
|
||||
"""
|
||||
Returns the names of the component classes in a list.
|
||||
|
||||
It takes the names from the pydase/components/__init__.py file, so this file should
|
||||
always be up-to-date with the currently available components.
|
||||
|
||||
Returns:
|
||||
list[str]: List of component class names
|
||||
Returns references to the component classes in a list.
|
||||
"""
|
||||
import pydase.components
|
||||
|
||||
return pydase.components.__all__
|
||||
return [
|
||||
getattr(pydase.components, cls_name) for cls_name in pydase.components.__all__
|
||||
]
|
||||
|
||||
|
||||
def get_data_service_class_reference() -> Any:
|
||||
import pydase.data_service.data_service
|
||||
|
||||
return getattr(pydase.data_service.data_service, "DataService")
|
||||
|
||||
|
||||
def is_property_attribute(target_obj: Any, attr_name: str) -> bool:
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import sys
|
||||
from copy import copy
|
||||
from typing import Optional
|
||||
|
||||
import socketio # type: ignore[import-untyped]
|
||||
import uvicorn.logging
|
||||
from uvicorn.config import LOGGING_CONFIG
|
||||
|
||||
@@ -18,7 +19,7 @@ class DefaultFormatter(uvicorn.logging.ColourizedFormatter):
|
||||
for formatting the output, instead of the plain text message.
|
||||
"""
|
||||
|
||||
def formatMessage(self, record: logging.LogRecord) -> str:
|
||||
def formatMessage(self, record: logging.LogRecord) -> str: # noqa: N802
|
||||
recordcopy = copy(record)
|
||||
levelname = recordcopy.levelname
|
||||
seperator = " " * (8 - len(recordcopy.levelname))
|
||||
@@ -31,10 +32,39 @@ class DefaultFormatter(uvicorn.logging.ColourizedFormatter):
|
||||
return logging.Formatter.formatMessage(self, recordcopy)
|
||||
|
||||
def should_use_colors(self) -> bool:
|
||||
return sys.stderr.isatty() # pragma: no cover
|
||||
return sys.stderr.isatty()
|
||||
|
||||
|
||||
def setup_logging(level: Optional[str | int] = None) -> None:
|
||||
class SocketIOHandler(logging.Handler):
|
||||
"""
|
||||
Custom logging handler that emits ERROR and CRITICAL log records to a Socket.IO
|
||||
server, allowing for real-time logging in applications that use Socket.IO for
|
||||
communication.
|
||||
"""
|
||||
|
||||
def __init__(self, sio: socketio.AsyncServer) -> None:
|
||||
super().__init__(logging.ERROR)
|
||||
self._sio = sio
|
||||
|
||||
def format(self, record: logging.LogRecord) -> str:
|
||||
return f"{record.name}:{record.funcName}:{record.lineno} - {record.message}"
|
||||
|
||||
def emit(self, record: logging.LogRecord) -> None:
|
||||
log_entry = self.format(record)
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.create_task(
|
||||
self._sio.emit(
|
||||
"log",
|
||||
{
|
||||
"levelname": record.levelname,
|
||||
"message": log_entry,
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def setup_logging(level: str | int | None = None) -> None:
|
||||
"""
|
||||
Configures the logging settings for the application.
|
||||
|
||||
@@ -43,7 +73,7 @@ def setup_logging(level: Optional[str | int] = None) -> None:
|
||||
with an option to override the level. By default, in a development environment, the
|
||||
log level is set to DEBUG, whereas in other environments, it is set to INFO.
|
||||
|
||||
Parameters:
|
||||
Args:
|
||||
level (Optional[str | int]):
|
||||
A specific log level to set for the application. If None, the log level is
|
||||
determined based on the application's operation mode. Accepts standard log
|
||||
@@ -92,7 +122,10 @@ def setup_logging(level: Optional[str | int] = None) -> None:
|
||||
# add formatter to ch
|
||||
ch.setFormatter(
|
||||
DefaultFormatter(
|
||||
fmt="%(asctime)s.%(msecs)03d | %(levelprefix)s | %(name)s:%(funcName)s:%(lineno)d - %(message)s",
|
||||
fmt=(
|
||||
"%(asctime)s.%(msecs)03d | %(levelprefix)s | "
|
||||
"%(name)s:%(funcName)s:%(lineno)d - %(message)s"
|
||||
),
|
||||
datefmt="%Y-%m-%d %H:%M:%S",
|
||||
)
|
||||
)
|
||||
@@ -109,7 +142,8 @@ def setup_logging(level: Optional[str | int] = None) -> None:
|
||||
"fmt"
|
||||
] = "%(asctime)s.%(msecs)03d | %(levelprefix)s %(message)s"
|
||||
LOGGING_CONFIG["formatters"]["default"]["datefmt"] = "%Y-%m-%d %H:%M:%S"
|
||||
LOGGING_CONFIG["formatters"]["access"][
|
||||
"fmt"
|
||||
] = '%(asctime)s.%(msecs)03d | %(levelprefix)s %(client_addr)s - "%(request_line)s" %(status_code)s'
|
||||
LOGGING_CONFIG["formatters"]["access"]["fmt"] = (
|
||||
"%(asctime)s.%(msecs)03d | %(levelprefix)s %(client_addr)s "
|
||||
'- "%(request_line)s" %(status_code)s'
|
||||
)
|
||||
LOGGING_CONFIG["formatters"]["access"]["datefmt"] = "%Y-%m-%d %H:%M:%S"
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
import inspect
|
||||
import logging
|
||||
import sys
|
||||
from collections.abc import Callable
|
||||
from enum import Enum
|
||||
from typing import Any, Optional
|
||||
from typing import Any
|
||||
|
||||
import pydase.units as u
|
||||
from pydase.data_service.abstract_data_service import AbstractDataService
|
||||
from pydase.utils.helpers import (
|
||||
get_attribute_doc,
|
||||
get_component_class_names,
|
||||
get_component_classes,
|
||||
get_data_service_class_reference,
|
||||
parse_list_attr_and_index,
|
||||
)
|
||||
|
||||
@@ -28,7 +30,7 @@ class Serializer:
|
||||
def serialize_object(obj: Any) -> dict[str, Any]:
|
||||
result: dict[str, Any] = {}
|
||||
if isinstance(obj, AbstractDataService):
|
||||
result = Serializer._serialize_DataService(obj)
|
||||
result = Serializer._serialize_data_service(obj)
|
||||
|
||||
elif isinstance(obj, list):
|
||||
result = Serializer._serialize_list(obj)
|
||||
@@ -38,7 +40,7 @@ class Serializer:
|
||||
|
||||
# Special handling for u.Quantity
|
||||
elif isinstance(obj, u.Quantity):
|
||||
result = Serializer._serialize_Quantity(obj)
|
||||
result = Serializer._serialize_quantity(obj)
|
||||
|
||||
# Handling for Enums
|
||||
elif isinstance(obj, Enum):
|
||||
@@ -66,7 +68,9 @@ class Serializer:
|
||||
def _serialize_enum(obj: Enum) -> dict[str, Any]:
|
||||
value = obj.name
|
||||
readonly = False
|
||||
doc = get_attribute_doc(obj)
|
||||
doc = obj.__doc__
|
||||
if sys.version_info < (3, 11) and doc == "An enumeration.":
|
||||
doc = None
|
||||
if type(obj).__base__.__name__ == "ColouredEnum":
|
||||
obj_type = "ColouredEnum"
|
||||
else:
|
||||
@@ -83,7 +87,7 @@ class Serializer:
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _serialize_Quantity(obj: u.Quantity) -> dict[str, Any]:
|
||||
def _serialize_quantity(obj: u.Quantity) -> dict[str, Any]:
|
||||
obj_type = "Quantity"
|
||||
readonly = False
|
||||
doc = get_attribute_doc(obj)
|
||||
@@ -130,7 +134,7 @@ class Serializer:
|
||||
|
||||
# Store parameters and their anotations in a dictionary
|
||||
sig = inspect.signature(obj)
|
||||
parameters: dict[str, Optional[str]] = {}
|
||||
parameters: dict[str, str | None] = {}
|
||||
|
||||
for k, v in sig.parameters.items():
|
||||
annotation = v.annotation
|
||||
@@ -154,35 +158,35 @@ class Serializer:
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _serialize_DataService(obj: AbstractDataService) -> dict[str, Any]:
|
||||
def _serialize_data_service(obj: AbstractDataService) -> dict[str, Any]:
|
||||
readonly = False
|
||||
doc = get_attribute_doc(obj)
|
||||
obj_type = type(obj).__name__
|
||||
if type(obj).__name__ not in get_component_class_names():
|
||||
obj_type = "DataService"
|
||||
obj_type = "DataService"
|
||||
|
||||
# Get the dictionary of the base class
|
||||
base_set = set(type(obj).__base__.__dict__)
|
||||
# Get the dictionary of the derived class
|
||||
derived_set = set(type(obj).__dict__)
|
||||
# Get the difference between the two dictionaries
|
||||
derived_only_set = derived_set - base_set
|
||||
# Get component base class if any
|
||||
component_base_cls = next(
|
||||
(cls for cls in get_component_classes() if isinstance(obj, cls)), None
|
||||
)
|
||||
if component_base_cls:
|
||||
obj_type = component_base_cls.__name__
|
||||
|
||||
# Get the set of DataService class attributes
|
||||
data_service_attr_set = set(dir(get_data_service_class_reference()))
|
||||
# Get the set of the object attributes
|
||||
obj_attr_set = set(dir(obj))
|
||||
# Get the difference between the two sets
|
||||
derived_only_attr_set = obj_attr_set - data_service_attr_set
|
||||
|
||||
instance_dict = set(obj.__dict__)
|
||||
# Merge the class and instance dictionaries
|
||||
merged_set = derived_only_set | instance_dict
|
||||
value = {}
|
||||
|
||||
# Iterate over attributes, properties, class attributes, and methods
|
||||
for key in sorted(merged_set):
|
||||
for key in sorted(derived_only_attr_set):
|
||||
if key.startswith("_"):
|
||||
continue # Skip attributes that start with underscore
|
||||
|
||||
# Skip keys that start with "start_" or "stop_" and end with an async
|
||||
# method name
|
||||
if (key.startswith("start_") or key.startswith("stop_")) and key.split(
|
||||
"_", 1
|
||||
)[1] in {
|
||||
if key.startswith(("start_", "stop_")) and key.split("_", 1)[1] in {
|
||||
name
|
||||
for name, _ in inspect.getmembers(
|
||||
obj, predicate=inspect.iscoroutinefunction
|
||||
@@ -259,8 +263,9 @@ def set_nested_value_by_path(
|
||||
# setting the new value
|
||||
serialized_value = dump(value)
|
||||
if "readonly" in current_dict:
|
||||
if current_dict["type"] != "method":
|
||||
current_dict["type"] = serialized_value["type"]
|
||||
current_dict["value"] = serialized_value["value"]
|
||||
current_dict["type"] = serialized_value["type"]
|
||||
else:
|
||||
current_dict.update(serialized_value)
|
||||
|
||||
@@ -272,26 +277,18 @@ def get_nested_dict_by_path(
|
||||
parent_path_parts, attr_name = path.split(".")[:-1], path.split(".")[-1]
|
||||
current_dict: dict[str, Any] = serialization_dict
|
||||
|
||||
try:
|
||||
for path_part in parent_path_parts:
|
||||
current_dict = get_next_level_dict_by_key(
|
||||
current_dict, path_part, allow_append=False
|
||||
)
|
||||
current_dict = current_dict["value"]
|
||||
for path_part in parent_path_parts:
|
||||
current_dict = get_next_level_dict_by_key(
|
||||
current_dict, attr_name, allow_append=False
|
||||
current_dict, path_part, allow_append=False
|
||||
)
|
||||
|
||||
except (SerializationPathError, SerializationValueError, KeyError) as e:
|
||||
logger.error(e)
|
||||
return {}
|
||||
|
||||
return current_dict
|
||||
current_dict = current_dict["value"]
|
||||
return get_next_level_dict_by_key(current_dict, attr_name, allow_append=False)
|
||||
|
||||
|
||||
def get_next_level_dict_by_key(
|
||||
serialization_dict: dict[str, Any],
|
||||
attr_name: str,
|
||||
*,
|
||||
allow_append: bool = False,
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
@@ -347,41 +344,48 @@ def get_next_level_dict_by_key(
|
||||
|
||||
|
||||
def generate_serialized_data_paths(
|
||||
data: dict[str, Any], parent_path: str = ""
|
||||
data: dict[str, dict[str, Any]], parent_path: str = ""
|
||||
) -> list[str]:
|
||||
"""
|
||||
Generate a list of access paths for all attributes in a dictionary representing
|
||||
data serialized with `pydase.utils.serializer.Serializer`, excluding those that are
|
||||
methods.
|
||||
methods. This function handles nested structures, including lists, by generating
|
||||
paths for each element in the nested lists.
|
||||
|
||||
Args:
|
||||
data: The dictionary representing serialized data, typically produced by
|
||||
`pydase.utils.serializer.Serializer`.
|
||||
parent_path: The base path to prepend to the keys in the `data` dictionary to
|
||||
form the access paths. Defaults to an empty string.
|
||||
data (dict[str, Any]): The dictionary representing serialized data, typically
|
||||
produced by `pydase.utils.serializer.Serializer`.
|
||||
parent_path (str, optional): The base path to prepend to the keys in the `data`
|
||||
dictionary to form the access paths. Defaults to an empty string.
|
||||
|
||||
Returns:
|
||||
A list of strings where each string is a dot-notation access path to an
|
||||
attribute in the serialized data.
|
||||
list[str]: A list of strings where each string is a dot-notation access path
|
||||
to an attribute in the serialized data. For list elements, the path includes
|
||||
the index in square brackets.
|
||||
"""
|
||||
|
||||
paths = []
|
||||
paths: list[str] = []
|
||||
for key, value in data.items():
|
||||
if value["type"] == "method":
|
||||
# ignoring methods
|
||||
continue
|
||||
new_path = f"{parent_path}.{key}" if parent_path else key
|
||||
if isinstance(value["value"], dict) and value["type"] != "Quantity":
|
||||
paths.extend(generate_serialized_data_paths(value["value"], new_path)) # type: ignore
|
||||
elif isinstance(value["value"], list):
|
||||
for index, item in enumerate(value["value"]):
|
||||
indexed_key_path = f"{new_path}[{index}]"
|
||||
if isinstance(item["value"], dict):
|
||||
paths.extend( # type: ignore
|
||||
generate_serialized_data_paths(item["value"], indexed_key_path)
|
||||
)
|
||||
else:
|
||||
paths.append(indexed_key_path) # type: ignore
|
||||
else:
|
||||
paths.append(new_path) # type: ignore
|
||||
paths.append(new_path)
|
||||
if serialized_dict_is_nested_object(value):
|
||||
if isinstance(value["value"], list):
|
||||
for index, item in enumerate(value["value"]):
|
||||
indexed_key_path = f"{new_path}[{index}]"
|
||||
paths.append(indexed_key_path)
|
||||
if serialized_dict_is_nested_object(item):
|
||||
paths.extend(
|
||||
generate_serialized_data_paths(
|
||||
item["value"], indexed_key_path
|
||||
)
|
||||
)
|
||||
continue
|
||||
paths.extend(generate_serialized_data_paths(value["value"], new_path))
|
||||
return paths
|
||||
|
||||
|
||||
def serialized_dict_is_nested_object(serialized_dict: dict[str, Any]) -> bool:
|
||||
return (
|
||||
serialized_dict["type"] != "Quantity"
|
||||
and isinstance(serialized_dict["value"], dict)
|
||||
) or isinstance(serialized_dict["value"], list)
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
import logging
|
||||
|
||||
from pydase.utils.helpers import get_component_class_names
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def warn_if_instance_class_does_not_inherit_from_DataService(__value: object) -> None:
|
||||
base_class_name = __value.__class__.__base__.__name__
|
||||
module_name = __value.__class__.__module__
|
||||
|
||||
if (
|
||||
module_name
|
||||
not in [
|
||||
"builtins",
|
||||
"__builtin__",
|
||||
"asyncio.unix_events",
|
||||
"_abc",
|
||||
]
|
||||
and base_class_name
|
||||
not in ["DataService", "list", "Enum"] + get_component_class_names()
|
||||
and type(__value).__name__ not in ["CallbackManager", "TaskManager", "Quantity"]
|
||||
):
|
||||
logger.warning(
|
||||
f"Warning: Class {type(__value).__name__} does not inherit from DataService."
|
||||
)
|
||||
@@ -1,4 +1,4 @@
|
||||
from importlib.metadata import distribution
|
||||
|
||||
__version__ = distribution("pydase").version
|
||||
__major__, __minor__, __patch__ = [int(v) for v in __version__.split(".")]
|
||||
__major__, __minor__, __patch__ = (int(v) for v in __version__.split("."))
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
from pytest import LogCaptureFixture
|
||||
|
||||
from pydase.components.coloured_enum import ColouredEnum
|
||||
from pydase.data_service.data_service import DataService
|
||||
from pydase.data_service.data_service_observer import DataServiceObserver
|
||||
from pydase.data_service.state_manager import StateManager
|
||||
from pytest import LogCaptureFixture
|
||||
|
||||
|
||||
def test_ColouredEnum(caplog: LogCaptureFixture) -> None:
|
||||
@@ -21,14 +22,16 @@ def test_ColouredEnum(caplog: LogCaptureFixture) -> None:
|
||||
# do something ...
|
||||
self._status = value
|
||||
|
||||
service = ServiceClass()
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
service.status = MyStatus.FAILING
|
||||
service_instance.status = MyStatus.FAILING
|
||||
|
||||
assert "ServiceClass.status changed to MyStatus.FAILING" in caplog.text
|
||||
assert "'status' changed to 'MyStatus.FAILING'" in caplog.text
|
||||
|
||||
|
||||
def test_warning(caplog: LogCaptureFixture) -> None: # noqa
|
||||
def test_warning(caplog: LogCaptureFixture) -> None:
|
||||
class MyStatus(ColouredEnum):
|
||||
RUNNING = "#00FF00"
|
||||
FAILING = "#FF0000"
|
||||
@@ -36,6 +39,9 @@ def test_warning(caplog: LogCaptureFixture) -> None: # noqa
|
||||
class ServiceClass(DataService):
|
||||
status = MyStatus.RUNNING
|
||||
|
||||
ServiceClass()
|
||||
|
||||
assert (
|
||||
"Warning: Class MyStatus does not inherit from DataService." not in caplog.text
|
||||
"Class 'MyStatus' does not inherit from DataService. This may lead to "
|
||||
"unexpected behaviour!" not in caplog.text
|
||||
)
|
||||
|
||||
@@ -1,43 +1,83 @@
|
||||
from pytest import CaptureFixture, LogCaptureFixture
|
||||
import logging
|
||||
from collections.abc import Callable
|
||||
|
||||
from pydase.components.number_slider import NumberSlider
|
||||
from pydase.data_service.data_service import DataService
|
||||
from pydase.data_service.data_service_observer import DataServiceObserver
|
||||
from pydase.data_service.state_manager import StateManager
|
||||
from pytest import LogCaptureFixture
|
||||
|
||||
from tests.utils.test_serializer import pytest
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def test_NumberSlider(caplog: LogCaptureFixture) -> None:
|
||||
class ServiceClass(DataService):
|
||||
number_slider = NumberSlider(1, 0, 10, 1)
|
||||
int_number_slider = NumberSlider(1, 0, 10, 1, "int")
|
||||
def test_number_slider(caplog: LogCaptureFixture) -> None:
|
||||
class MySlider(NumberSlider):
|
||||
def __init__(
|
||||
self,
|
||||
value: float = 0,
|
||||
min_: float = 0,
|
||||
max_: float = 100,
|
||||
step_size: float = 1,
|
||||
callback: Callable[..., None] = lambda: None,
|
||||
) -> None:
|
||||
super().__init__(value, min_, max_, step_size)
|
||||
self._callback = callback
|
||||
|
||||
service = ServiceClass()
|
||||
@property
|
||||
def value(self) -> float:
|
||||
return self._value
|
||||
|
||||
assert service.number_slider.value == 1
|
||||
assert isinstance(service.number_slider.value, float)
|
||||
assert service.number_slider.min == 0
|
||||
assert isinstance(service.number_slider.min, float)
|
||||
assert service.number_slider.max == 10
|
||||
assert isinstance(service.number_slider.max, float)
|
||||
assert service.number_slider.step_size == 1
|
||||
assert isinstance(service.number_slider.step_size, float)
|
||||
@value.setter
|
||||
def value(self, value: float) -> None:
|
||||
self._callback(value)
|
||||
self._value = value
|
||||
|
||||
assert service.int_number_slider.value == 1
|
||||
assert isinstance(service.int_number_slider.value, int)
|
||||
assert service.int_number_slider.step_size == 1
|
||||
assert isinstance(service.int_number_slider.step_size, int)
|
||||
@property
|
||||
def max(self) -> float:
|
||||
return self._max
|
||||
|
||||
service.number_slider.value = 10.0
|
||||
service.int_number_slider.value = 10.1
|
||||
@max.setter
|
||||
def max(self, value: float) -> None:
|
||||
self._max = value
|
||||
|
||||
assert "ServiceClass.number_slider.value changed to 10.0" in caplog.text
|
||||
assert "ServiceClass.int_number_slider.value changed to 10" in caplog.text
|
||||
@property
|
||||
def step_size(self) -> float:
|
||||
return self._step_size
|
||||
|
||||
@step_size.setter
|
||||
def step_size(self, value: float) -> None:
|
||||
self._step_size = value
|
||||
|
||||
class MyService(DataService):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.my_slider = MySlider(callback=self.some_method)
|
||||
|
||||
def some_method(self, slider_value: float) -> None:
|
||||
logger.info("Slider changed to '%s'", slider_value)
|
||||
|
||||
service_instance = MyService()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
service_instance.my_slider.value = 10.0
|
||||
|
||||
assert "'my_slider.value' changed to '10.0'" in caplog.text
|
||||
assert "Slider changed to '10.0'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
service.number_slider.min = 1.1
|
||||
service_instance.my_slider.max = 12.0
|
||||
|
||||
assert "ServiceClass.number_slider.min changed to 1.1" in caplog.text
|
||||
assert "'my_slider.max' changed to '12.0'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
service_instance.my_slider.step_size = 0.1
|
||||
|
||||
def test_init_error(caplog: LogCaptureFixture) -> None: # noqa
|
||||
number_slider = NumberSlider(type="str") # type: ignore # noqa
|
||||
assert "'my_slider.step_size' changed to '0.1'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
assert "Unknown type 'str'. Using 'float'" in caplog.text
|
||||
# by overriding the getter only you can make the property read-only
|
||||
with pytest.raises(AttributeError):
|
||||
service_instance.my_slider.min = 1.1 # type: ignore[reportGeneralTypeIssues, misc]
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
import logging
|
||||
|
||||
from pytest import LogCaptureFixture
|
||||
|
||||
import pydase
|
||||
|
||||
logger = logging.getLogger()
|
||||
|
||||
|
||||
def test_DataService_task_callback(caplog: LogCaptureFixture) -> None:
|
||||
class MyService(pydase.DataService):
|
||||
async def my_task(self) -> None:
|
||||
logger.info("Triggered task.")
|
||||
|
||||
async def my_other_task(self) -> None:
|
||||
logger.info("Triggered other task.")
|
||||
|
||||
service = MyService()
|
||||
service.start_my_task() # type: ignore
|
||||
service.start_my_other_task() # type: ignore
|
||||
|
||||
assert "MyService.my_task changed to {}" in caplog.text
|
||||
assert "MyService.my_other_task changed to {}" in caplog.text
|
||||
|
||||
|
||||
def test_DataServiceList_task_callback(caplog: LogCaptureFixture) -> None:
|
||||
class MySubService(pydase.DataService):
|
||||
async def my_task(self) -> None:
|
||||
logger.info("Triggered task.")
|
||||
|
||||
async def my_other_task(self) -> None:
|
||||
logger.info("Triggered other task.")
|
||||
|
||||
class MyService(pydase.DataService):
|
||||
sub_services_list = [MySubService() for i in range(2)]
|
||||
|
||||
service = MyService()
|
||||
service.sub_services_list[0].start_my_task() # type: ignore
|
||||
service.sub_services_list[1].start_my_other_task() # type: ignore
|
||||
|
||||
assert "MyService.sub_services_list[0].my_task changed to {}" in caplog.text
|
||||
assert "MyService.sub_services_list[1].my_other_task changed to {}" in caplog.text
|
||||
@@ -0,0 +1,116 @@
|
||||
from enum import Enum
|
||||
|
||||
import pydase.units as u
|
||||
from pydase import DataService
|
||||
from pydase.data_service.data_service_observer import DataServiceObserver
|
||||
from pydase.data_service.state_manager import StateManager
|
||||
from pytest import LogCaptureFixture
|
||||
|
||||
|
||||
def test_unexpected_type_change_warning(caplog: LogCaptureFixture) -> None:
|
||||
class ServiceClass(DataService):
|
||||
attr_1 = 1.0
|
||||
current = 1.0 * u.units.A
|
||||
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
service_instance.attr_1 = 2
|
||||
|
||||
assert "'attr_1' changed to '2'" in caplog.text
|
||||
assert (
|
||||
"Type of 'attr_1' changed from 'float' to 'int'. This may have unwanted "
|
||||
"side effects! Consider setting it to 'float' directly." in caplog.text
|
||||
)
|
||||
|
||||
service_instance.current = 2
|
||||
assert "'current' changed to '2'" in caplog.text
|
||||
assert (
|
||||
"Type of 'current' changed from 'Quantity' to 'int'. This may have unwanted "
|
||||
"side effects! Consider setting it to 'Quantity' directly." in caplog.text
|
||||
)
|
||||
|
||||
|
||||
def test_basic_inheritance_warning(caplog: LogCaptureFixture) -> None:
|
||||
class SubService(DataService):
|
||||
...
|
||||
|
||||
class SomeEnum(Enum):
|
||||
HI = 0
|
||||
|
||||
class ServiceClass(DataService):
|
||||
sub_service = SubService()
|
||||
some_int = 1
|
||||
some_float = 1.0
|
||||
some_bool = True
|
||||
some_quantity = 1.0 * u.units.A
|
||||
some_list = [1, 2]
|
||||
some_string = "Hello"
|
||||
some_enum = SomeEnum.HI
|
||||
_name = "Service"
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self._name
|
||||
|
||||
def some_method(self) -> None:
|
||||
...
|
||||
|
||||
async def some_task(self) -> None:
|
||||
...
|
||||
|
||||
ServiceClass()
|
||||
|
||||
# neither of the attributes, methods or properties cause a warning log
|
||||
assert "WARNING" not in caplog.text
|
||||
|
||||
|
||||
def test_class_attr_inheritance_warning(caplog: LogCaptureFixture) -> None:
|
||||
class SubClass:
|
||||
name = "Hello"
|
||||
|
||||
class ServiceClass(DataService):
|
||||
attr_1 = SubClass()
|
||||
|
||||
ServiceClass()
|
||||
|
||||
assert (
|
||||
"Class 'SubClass' does not inherit from DataService. This may lead to "
|
||||
"unexpected behaviour!"
|
||||
) in caplog.text
|
||||
|
||||
|
||||
def test_instance_attr_inheritance_warning(caplog: LogCaptureFixture) -> None:
|
||||
class SubClass:
|
||||
name = "Hello"
|
||||
|
||||
class ServiceClass(DataService):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.attr_1 = SubClass()
|
||||
|
||||
ServiceClass()
|
||||
|
||||
assert (
|
||||
"Class 'SubClass' does not inherit from DataService. This may lead to "
|
||||
"unexpected behaviour!"
|
||||
) in caplog.text
|
||||
|
||||
|
||||
def test_protected_and_private_attribute_warning(caplog: LogCaptureFixture) -> None:
|
||||
class SubClass:
|
||||
name = "Hello"
|
||||
|
||||
class ServiceClass(DataService):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self._subclass = SubClass()
|
||||
self.__other_subclass = SubClass()
|
||||
|
||||
ServiceClass()
|
||||
|
||||
# Protected and private attributes are not checked
|
||||
assert (
|
||||
"Class 'SubClass' does not inherit from DataService. This may lead to "
|
||||
"unexpected behaviour!"
|
||||
) not in caplog.text
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import logging
|
||||
|
||||
import pydase
|
||||
from pydase.data_service.data_service_cache import DataServiceCache
|
||||
from pydase.utils.serializer import get_nested_dict_by_path
|
||||
import pytest
|
||||
from pydase.data_service.data_service_observer import DataServiceObserver
|
||||
from pydase.data_service.state_manager import StateManager
|
||||
|
||||
logger = logging.getLogger()
|
||||
|
||||
@@ -15,11 +16,56 @@ def test_nested_attributes_cache_callback() -> None:
|
||||
class_attr = SubClass()
|
||||
name = "World"
|
||||
|
||||
test_service = ServiceClass()
|
||||
cache = DataServiceCache(test_service)
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
test_service.name = "Peepz"
|
||||
assert get_nested_dict_by_path(cache.cache, "name")["value"] == "Peepz"
|
||||
service_instance.name = "Peepz"
|
||||
assert (
|
||||
state_manager._data_service_cache.get_value_dict_from_cache("name")["value"]
|
||||
== "Peepz"
|
||||
)
|
||||
|
||||
test_service.class_attr.name = "Ciao"
|
||||
assert get_nested_dict_by_path(cache.cache, "class_attr.name")["value"] == "Ciao"
|
||||
service_instance.class_attr.name = "Ciao"
|
||||
assert (
|
||||
state_manager._data_service_cache.get_value_dict_from_cache("class_attr.name")[
|
||||
"value"
|
||||
]
|
||||
== "Ciao"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_task_status_update() -> None:
|
||||
class ServiceClass(pydase.DataService):
|
||||
name = "World"
|
||||
|
||||
async def my_method(self) -> None:
|
||||
pass
|
||||
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
assert (
|
||||
state_manager._data_service_cache.get_value_dict_from_cache("my_method")["type"]
|
||||
== "method"
|
||||
)
|
||||
assert (
|
||||
state_manager._data_service_cache.get_value_dict_from_cache("my_method")[
|
||||
"value"
|
||||
]
|
||||
is None
|
||||
)
|
||||
|
||||
service_instance.start_my_method() # type: ignore
|
||||
assert (
|
||||
state_manager._data_service_cache.get_value_dict_from_cache("my_method")["type"]
|
||||
== "method"
|
||||
)
|
||||
assert (
|
||||
state_manager._data_service_cache.get_value_dict_from_cache("my_method")[
|
||||
"value"
|
||||
]
|
||||
== {}
|
||||
)
|
||||
|
||||
@@ -1,129 +0,0 @@
|
||||
from typing import Any
|
||||
|
||||
from pytest import LogCaptureFixture
|
||||
|
||||
import pydase.units as u
|
||||
from pydase import DataService
|
||||
|
||||
|
||||
def test_class_list_attribute(caplog: LogCaptureFixture) -> None:
|
||||
class ServiceClass(DataService):
|
||||
attr = [0, 1]
|
||||
|
||||
service_instance = ServiceClass()
|
||||
|
||||
service_instance.attr[0] = 1337
|
||||
assert "ServiceClass.attr[0] changed to 1337" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
|
||||
def test_instance_list_attribute(caplog: LogCaptureFixture) -> None:
|
||||
class SubClass(DataService):
|
||||
name = "SubClass"
|
||||
|
||||
class ServiceClass(DataService):
|
||||
def __init__(self) -> None:
|
||||
self.attr: list[Any] = [0, SubClass()]
|
||||
super().__init__()
|
||||
|
||||
service_instance = ServiceClass()
|
||||
|
||||
service_instance.attr[0] = "Hello"
|
||||
assert "ServiceClass.attr[0] changed to Hello" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
service_instance.attr[1] = SubClass()
|
||||
assert f"ServiceClass.attr[1] changed to {service_instance.attr[1]}" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
|
||||
def test_reused_instance_list_attribute(caplog: LogCaptureFixture) -> None:
|
||||
some_list = [0, 1, 2]
|
||||
|
||||
class ServiceClass(DataService):
|
||||
def __init__(self) -> None:
|
||||
self.attr = some_list
|
||||
self.attr_2 = some_list
|
||||
self.attr_3 = [0, 1, 2]
|
||||
super().__init__()
|
||||
|
||||
service_instance = ServiceClass()
|
||||
|
||||
service_instance.attr[0] = 20
|
||||
assert service_instance.attr == service_instance.attr_2
|
||||
assert service_instance.attr != service_instance.attr_3
|
||||
|
||||
assert "ServiceClass.attr[0] changed to 20" in caplog.text
|
||||
assert "ServiceClass.attr_2[0] changed to 20" in caplog.text
|
||||
|
||||
|
||||
def test_nested_reused_instance_list_attribute(caplog: LogCaptureFixture) -> None:
|
||||
some_list = [0, 1, 2]
|
||||
|
||||
class SubClass(DataService):
|
||||
attr_list = some_list
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.attr_list_2 = some_list
|
||||
super().__init__()
|
||||
|
||||
class ServiceClass(DataService):
|
||||
def __init__(self) -> None:
|
||||
self.attr = some_list
|
||||
self.subclass = SubClass()
|
||||
super().__init__()
|
||||
|
||||
service_instance = ServiceClass()
|
||||
|
||||
service_instance.attr[0] = 20
|
||||
|
||||
assert service_instance.attr == service_instance.subclass.attr_list
|
||||
|
||||
assert "ServiceClass.attr[0] changed to 20" in caplog.text
|
||||
assert "ServiceClass.subclass.attr_list[0] changed to 20" in caplog.text
|
||||
assert "ServiceClass.subclass.attr_list_2[0] changed to 20" in caplog.text
|
||||
|
||||
|
||||
def test_protected_list_attribute(caplog: LogCaptureFixture) -> None:
|
||||
"""Changing protected lists should not emit notifications for the lists themselves,
|
||||
but still for all properties depending on them.
|
||||
"""
|
||||
|
||||
class ServiceClass(DataService):
|
||||
_attr = [0, 1]
|
||||
|
||||
@property
|
||||
def list_dependend_property(self) -> int:
|
||||
return self._attr[0]
|
||||
|
||||
service_instance = ServiceClass()
|
||||
|
||||
service_instance._attr[0] = 1337
|
||||
assert "ServiceClass.list_dependend_property changed to 1337" in caplog.text
|
||||
|
||||
|
||||
def test_converting_int_to_float_entries(caplog: LogCaptureFixture) -> None:
|
||||
class ServiceClass(DataService):
|
||||
float_list = [0.0]
|
||||
|
||||
service_instance = ServiceClass()
|
||||
service_instance.float_list[0] = 1
|
||||
|
||||
assert isinstance(service_instance.float_list[0], float)
|
||||
assert "ServiceClass.float_list[0] changed to 1.0" in caplog.text
|
||||
|
||||
|
||||
def test_converting_number_to_quantity_entries(caplog: LogCaptureFixture) -> None:
|
||||
class ServiceClass(DataService):
|
||||
quantity_list: list[u.Quantity] = [1 * u.units.A]
|
||||
|
||||
service_instance = ServiceClass()
|
||||
service_instance.quantity_list[0] = 4 # type: ignore
|
||||
|
||||
assert isinstance(service_instance.quantity_list[0], u.Quantity)
|
||||
assert "ServiceClass.quantity_list[0] changed to 4.0 A" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
service_instance.quantity_list[0] = 3.1 * u.units.mA
|
||||
assert isinstance(service_instance.quantity_list[0], u.Quantity)
|
||||
assert "ServiceClass.quantity_list[0] changed to 3.1 mA" in caplog.text
|
||||
96
tests/data_service/test_data_service_observer.py
Normal file
96
tests/data_service/test_data_service_observer.py
Normal file
@@ -0,0 +1,96 @@
|
||||
import logging
|
||||
|
||||
import pydase
|
||||
import pytest
|
||||
from pydase.data_service.data_service_observer import DataServiceObserver
|
||||
from pydase.data_service.state_manager import StateManager
|
||||
|
||||
logger = logging.getLogger()
|
||||
|
||||
|
||||
def test_static_property_dependencies() -> None:
|
||||
class SubClass(pydase.DataService):
|
||||
_name = "SubClass"
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self._name
|
||||
|
||||
@name.setter
|
||||
def name(self, value: str) -> None:
|
||||
self._name = value
|
||||
|
||||
class ServiceClass(pydase.DataService):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.list_attr = [SubClass()]
|
||||
self._name = "ServiceClass"
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self._name
|
||||
|
||||
@name.setter
|
||||
def name(self, value: str) -> None:
|
||||
self._name = value
|
||||
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
observer = DataServiceObserver(state_manager)
|
||||
logger.debug(observer.property_deps_dict)
|
||||
assert observer.property_deps_dict == {
|
||||
"list_attr[0]._name": ["list_attr[0].name"],
|
||||
"_name": ["name"],
|
||||
}
|
||||
|
||||
|
||||
def test_dynamic_list_property_dependencies() -> None:
|
||||
class SubClass(pydase.DataService):
|
||||
_name = "SubClass"
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self._name
|
||||
|
||||
@name.setter
|
||||
def name(self, value: str) -> None:
|
||||
self._name = value
|
||||
|
||||
class ServiceClass(pydase.DataService):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.list_attr = [SubClass()]
|
||||
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
observer = DataServiceObserver(state_manager)
|
||||
|
||||
assert observer.property_deps_dict == {
|
||||
"list_attr[0]._name": ["list_attr[0].name"],
|
||||
}
|
||||
|
||||
service_instance.list_attr.append(SubClass())
|
||||
|
||||
assert observer.property_deps_dict == {
|
||||
"list_attr[0]._name": ["list_attr[0].name"],
|
||||
"list_attr[1]._name": ["list_attr[1].name"],
|
||||
}
|
||||
|
||||
|
||||
def test_protected_or_private_change_logs(caplog: pytest.LogCaptureFixture) -> None:
|
||||
class OtherService(pydase.DataService):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self._name = "Hi"
|
||||
|
||||
class MyService(pydase.DataService):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.subclass = OtherService()
|
||||
|
||||
service = MyService()
|
||||
state_manager = StateManager(service)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
service.subclass._name = "Hello"
|
||||
assert "'subclass._name' changed to 'Hello'" not in caplog.text
|
||||
@@ -2,30 +2,73 @@ import json
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from pytest import LogCaptureFixture
|
||||
|
||||
import pydase
|
||||
import pydase.components
|
||||
import pydase.units as u
|
||||
from pydase.components.coloured_enum import ColouredEnum
|
||||
import pytest
|
||||
from pydase.data_service.data_service_observer import DataServiceObserver
|
||||
from pydase.data_service.state_manager import (
|
||||
StateManager,
|
||||
has_load_state_decorator,
|
||||
load_state,
|
||||
)
|
||||
from pytest import LogCaptureFixture
|
||||
|
||||
|
||||
class SubService(pydase.DataService):
|
||||
name = "SubService"
|
||||
|
||||
|
||||
class State(ColouredEnum):
|
||||
class State(pydase.components.ColouredEnum):
|
||||
RUNNING = "#0000FF80"
|
||||
COMPLETED = "hsl(120, 100%, 50%)"
|
||||
FAILED = "hsla(0, 100%, 50%, 0.7)"
|
||||
|
||||
|
||||
class MySlider(pydase.components.NumberSlider):
|
||||
@property
|
||||
def min(self) -> float:
|
||||
return self._min
|
||||
|
||||
@min.setter
|
||||
@load_state
|
||||
def min(self, value: float) -> None:
|
||||
self._min = value
|
||||
|
||||
@property
|
||||
def max(self) -> float:
|
||||
return self._max
|
||||
|
||||
@max.setter
|
||||
@load_state
|
||||
def max(self, value: float) -> None:
|
||||
self._max = value
|
||||
|
||||
@property
|
||||
def step_size(self) -> float:
|
||||
return self._step_size
|
||||
|
||||
@step_size.setter
|
||||
@load_state
|
||||
def step_size(self, value: float) -> None:
|
||||
self._step_size = value
|
||||
|
||||
@property
|
||||
def value(self) -> float:
|
||||
return self._value
|
||||
|
||||
@value.setter
|
||||
@load_state
|
||||
def value(self, value: float) -> None:
|
||||
if value < self._min or value > self._max:
|
||||
raise ValueError("Value is either below allowed min or above max value.")
|
||||
|
||||
self._value = value
|
||||
|
||||
|
||||
class Service(pydase.DataService):
|
||||
def __init__(self, **kwargs: Any) -> None:
|
||||
super().__init__(**kwargs)
|
||||
self.subservice = SubService()
|
||||
self.some_unit: u.Quantity = 1.2 * u.units.A
|
||||
self.some_float = 1.0
|
||||
@@ -33,7 +76,7 @@ class Service(pydase.DataService):
|
||||
self._property_attr = 1337.0
|
||||
self._name = "Service"
|
||||
self.state = State.RUNNING
|
||||
super().__init__(**kwargs)
|
||||
self.my_slider = MySlider()
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
@@ -60,6 +103,37 @@ LOAD_STATE = {
|
||||
"readonly": False,
|
||||
"doc": None,
|
||||
},
|
||||
"my_slider": {
|
||||
"type": "NumberSlider",
|
||||
"value": {
|
||||
"max": {
|
||||
"type": "float",
|
||||
"value": 101.0,
|
||||
"readonly": False,
|
||||
"doc": "The min property.",
|
||||
},
|
||||
"min": {
|
||||
"type": "float",
|
||||
"value": 1.0,
|
||||
"readonly": False,
|
||||
"doc": "The min property.",
|
||||
},
|
||||
"step_size": {
|
||||
"type": "float",
|
||||
"value": 2.0,
|
||||
"readonly": False,
|
||||
"doc": "The min property.",
|
||||
},
|
||||
"value": {
|
||||
"type": "float",
|
||||
"value": 1.0,
|
||||
"readonly": False,
|
||||
"doc": "The value property.",
|
||||
},
|
||||
},
|
||||
"readonly": False,
|
||||
"doc": None,
|
||||
},
|
||||
"name": {
|
||||
"type": "str",
|
||||
"value": "Another name",
|
||||
@@ -117,7 +191,7 @@ LOAD_STATE = {
|
||||
}
|
||||
|
||||
|
||||
def test_save_state(tmp_path: Path):
|
||||
def test_save_state(tmp_path: Path) -> None:
|
||||
# Create a StateManager instance with a temporary file
|
||||
file = tmp_path / "test_state.json"
|
||||
manager = StateManager(service=Service(), filename=str(file))
|
||||
@@ -129,7 +203,7 @@ def test_save_state(tmp_path: Path):
|
||||
assert file.read_text() == json.dumps(CURRENT_STATE, indent=4)
|
||||
|
||||
|
||||
def test_load_state(tmp_path: Path, caplog: LogCaptureFixture):
|
||||
def test_load_state(tmp_path: Path, caplog: LogCaptureFixture) -> None:
|
||||
# Create a StateManager instance with a temporary file
|
||||
file = tmp_path / "test_state.json"
|
||||
|
||||
@@ -138,8 +212,9 @@ def test_load_state(tmp_path: Path, caplog: LogCaptureFixture):
|
||||
json.dump(LOAD_STATE, f, indent=4)
|
||||
|
||||
service = Service()
|
||||
manager = StateManager(service=service, filename=str(file))
|
||||
manager.load_state()
|
||||
state_manager = StateManager(service=service, filename=str(file))
|
||||
DataServiceObserver(state_manager)
|
||||
state_manager.load_state()
|
||||
|
||||
assert service.some_unit == u.Quantity(12, "A") # has changed
|
||||
assert service.list_attr[0] == 1.4 # has changed
|
||||
@@ -151,29 +226,42 @@ def test_load_state(tmp_path: Path, caplog: LogCaptureFixture):
|
||||
assert service.name == "Service" # has not changed as readonly
|
||||
assert service.some_float == 1.0 # has not changed due to different type
|
||||
assert service.subservice.name == "SubService" # didn't change
|
||||
assert service.my_slider.value == 1.0 # changed
|
||||
assert service.my_slider.min == 1.0 # changed
|
||||
assert service.my_slider.max == 101.0 # changed
|
||||
assert service.my_slider.step_size == 2.0 # changed
|
||||
|
||||
assert "Service.some_unit changed to 12.0 A!" in caplog.text
|
||||
assert "Attribute 'name' is read-only. Ignoring new value..." in caplog.text
|
||||
assert "'some_unit' changed to '12.0 A'" in caplog.text
|
||||
assert (
|
||||
"Property 'name' has no '@load_state' decorator. "
|
||||
"Ignoring value from JSON file..." in caplog.text
|
||||
)
|
||||
assert (
|
||||
"Attribute type of 'some_float' changed from 'int' to 'float'. "
|
||||
"Ignoring value from JSON file..."
|
||||
) in caplog.text
|
||||
assert (
|
||||
"Attribute type of 'removed_attr' changed from 'str' to None. "
|
||||
"Attribute type of 'removed_attr' changed from 'str' to 'None'. "
|
||||
"Ignoring value from JSON file..." in caplog.text
|
||||
)
|
||||
assert "Value of attribute 'subservice.name' has not changed..." in caplog.text
|
||||
assert "'my_slider.value' changed to '1.0'" in caplog.text
|
||||
assert "'my_slider.min' changed to '1.0'" in caplog.text
|
||||
assert "'my_slider.max' changed to '101.0'" in caplog.text
|
||||
assert "'my_slider.step_size' changed to '2.0'" in caplog.text
|
||||
|
||||
|
||||
def test_filename_warning(tmp_path: Path, caplog: LogCaptureFixture):
|
||||
def test_filename_warning(tmp_path: Path, caplog: LogCaptureFixture) -> None:
|
||||
file = tmp_path / "test_state.json"
|
||||
|
||||
service = Service(filename=str(file))
|
||||
StateManager(service=service, filename=str(file))
|
||||
with pytest.warns(DeprecationWarning):
|
||||
service = Service(filename=str(file))
|
||||
StateManager(service=service, filename=str(file))
|
||||
|
||||
assert f"Overwriting filename {str(file)!r} with {str(file)!r}." in caplog.text
|
||||
|
||||
|
||||
def test_filename_error(caplog: LogCaptureFixture):
|
||||
def test_filename_error(caplog: LogCaptureFixture) -> None:
|
||||
service = Service()
|
||||
manager = StateManager(service=service)
|
||||
|
||||
@@ -184,7 +272,7 @@ def test_filename_error(caplog: LogCaptureFixture):
|
||||
)
|
||||
|
||||
|
||||
def test_readonly_attribute(tmp_path: Path, caplog: LogCaptureFixture):
|
||||
def test_readonly_attribute(tmp_path: Path, caplog: LogCaptureFixture) -> None:
|
||||
# Create a StateManager instance with a temporary file
|
||||
file = tmp_path / "test_state.json"
|
||||
|
||||
@@ -195,10 +283,14 @@ def test_readonly_attribute(tmp_path: Path, caplog: LogCaptureFixture):
|
||||
service = Service()
|
||||
manager = StateManager(service=service, filename=str(file))
|
||||
manager.load_state()
|
||||
assert "Attribute 'name' is read-only. Ignoring new value..." in caplog.text
|
||||
assert service.name == "Service"
|
||||
assert (
|
||||
"Property 'name' has no '@load_state' decorator. "
|
||||
"Ignoring value from JSON file..." in caplog.text
|
||||
)
|
||||
|
||||
|
||||
def test_changed_type(tmp_path: Path, caplog: LogCaptureFixture):
|
||||
def test_changed_type(tmp_path: Path, caplog: LogCaptureFixture) -> None:
|
||||
# Create a StateManager instance with a temporary file
|
||||
file = tmp_path / "test_state.json"
|
||||
|
||||
@@ -215,7 +307,7 @@ def test_changed_type(tmp_path: Path, caplog: LogCaptureFixture):
|
||||
) in caplog.text
|
||||
|
||||
|
||||
def test_property_load_state(tmp_path: Path):
|
||||
def test_property_load_state(tmp_path: Path) -> None:
|
||||
# Create a StateManager instance with a temporary file
|
||||
file = tmp_path / "test_state.json"
|
||||
|
||||
|
||||
@@ -1,20 +1,24 @@
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
from pytest import LogCaptureFixture
|
||||
|
||||
import pydase
|
||||
import pytest
|
||||
from pydase.data_service.data_service_observer import DataServiceObserver
|
||||
from pydase.data_service.state_manager import StateManager
|
||||
from pytest import LogCaptureFixture
|
||||
|
||||
logger = logging.getLogger()
|
||||
|
||||
|
||||
def test_autostart_task_callback(caplog: LogCaptureFixture) -> None:
|
||||
@pytest.mark.asyncio
|
||||
async def test_autostart_task_callback(caplog: LogCaptureFixture) -> None:
|
||||
class MyService(pydase.DataService):
|
||||
def __init__(self) -> None:
|
||||
self._autostart_tasks = { # type: ignore
|
||||
"my_task": (),
|
||||
"my_other_task": (),
|
||||
}
|
||||
super().__init__()
|
||||
self._autostart_tasks = { # type: ignore
|
||||
"my_task": (), # type: ignore
|
||||
"my_other_task": (), # type: ignore
|
||||
}
|
||||
|
||||
async def my_task(self) -> None:
|
||||
logger.info("Triggered task.")
|
||||
@@ -22,23 +26,27 @@ def test_autostart_task_callback(caplog: LogCaptureFixture) -> None:
|
||||
async def my_other_task(self) -> None:
|
||||
logger.info("Triggered other task.")
|
||||
|
||||
service = MyService()
|
||||
service._task_manager.start_autostart_tasks()
|
||||
# Your test code here
|
||||
service_instance = MyService()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
service_instance._task_manager.start_autostart_tasks()
|
||||
|
||||
assert "MyService.my_task changed to {}" in caplog.text
|
||||
assert "MyService.my_other_task changed to {}" in caplog.text
|
||||
assert "'my_task' changed to '{}'" in caplog.text
|
||||
assert "'my_other_task' changed to '{}'" in caplog.text
|
||||
|
||||
|
||||
def test_DataService_subclass_autostart_task_callback(
|
||||
@pytest.mark.asyncio
|
||||
async def test_DataService_subclass_autostart_task_callback(
|
||||
caplog: LogCaptureFixture,
|
||||
) -> None:
|
||||
class MySubService(pydase.DataService):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self._autostart_tasks = { # type: ignore
|
||||
"my_task": (),
|
||||
"my_other_task": (),
|
||||
}
|
||||
super().__init__()
|
||||
|
||||
async def my_task(self) -> None:
|
||||
logger.info("Triggered task.")
|
||||
@@ -49,23 +57,26 @@ def test_DataService_subclass_autostart_task_callback(
|
||||
class MyService(pydase.DataService):
|
||||
sub_service = MySubService()
|
||||
|
||||
service = MyService()
|
||||
service._task_manager.start_autostart_tasks()
|
||||
service_instance = MyService()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
service_instance._task_manager.start_autostart_tasks()
|
||||
|
||||
assert "MyService.sub_service.my_task changed to {}" in caplog.text
|
||||
assert "MyService.sub_service.my_other_task changed to {}" in caplog.text
|
||||
assert "'sub_service.my_task' changed to '{}'" in caplog.text
|
||||
assert "'sub_service.my_other_task' changed to '{}'" in caplog.text
|
||||
|
||||
|
||||
def test_DataServiceList_subclass_autostart_task_callback(
|
||||
@pytest.mark.asyncio
|
||||
async def test_DataService_subclass_list_autostart_task_callback(
|
||||
caplog: LogCaptureFixture,
|
||||
) -> None:
|
||||
class MySubService(pydase.DataService):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self._autostart_tasks = { # type: ignore
|
||||
"my_task": (),
|
||||
"my_other_task": (),
|
||||
}
|
||||
super().__init__()
|
||||
|
||||
async def my_task(self) -> None:
|
||||
logger.info("Triggered task.")
|
||||
@@ -76,10 +87,39 @@ def test_DataServiceList_subclass_autostart_task_callback(
|
||||
class MyService(pydase.DataService):
|
||||
sub_services_list = [MySubService() for i in range(2)]
|
||||
|
||||
service = MyService()
|
||||
service._task_manager.start_autostart_tasks()
|
||||
service_instance = MyService()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
service_instance._task_manager.start_autostart_tasks()
|
||||
|
||||
assert "MyService.sub_services_list[0].my_task changed to {}" in caplog.text
|
||||
assert "MyService.sub_services_list[0].my_other_task changed to {}" in caplog.text
|
||||
assert "MyService.sub_services_list[1].my_task changed to {}" in caplog.text
|
||||
assert "MyService.sub_services_list[1].my_other_task changed to {}" in caplog.text
|
||||
assert "'sub_services_list[0].my_task' changed to '{}'" in caplog.text
|
||||
assert "'sub_services_list[0].my_other_task' changed to '{}'" in caplog.text
|
||||
assert "'sub_services_list[1].my_task' changed to '{}'" in caplog.text
|
||||
assert "'sub_services_list[1].my_other_task' changed to '{}'" in caplog.text
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_start_and_stop_task_methods(caplog: LogCaptureFixture) -> None:
|
||||
class MyService(pydase.DataService):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
|
||||
async def my_task(self, param: str) -> None:
|
||||
while True:
|
||||
logger.debug("Logging param: %s", param)
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
# Your test code here
|
||||
service_instance = MyService()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
service_instance.start_my_task("Hello")
|
||||
await asyncio.sleep(0.01)
|
||||
|
||||
assert "'my_task' changed to '{'param': 'Hello'}'" in caplog.text
|
||||
assert "Logging param: Hello" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
service_instance.stop_my_task()
|
||||
await asyncio.sleep(0.01)
|
||||
assert "Task 'my_task' was cancelled" in caplog.text
|
||||
|
||||
173
tests/observer_pattern/observable/test_observable.py
Normal file
173
tests/observer_pattern/observable/test_observable.py
Normal file
@@ -0,0 +1,173 @@
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from pydase.observer_pattern.observable import Observable
|
||||
from pydase.observer_pattern.observer import Observer
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class MyObserver(Observer):
|
||||
def on_change(self, full_access_path: str, value: Any) -> None:
|
||||
logger.info("'%s' changed to '%s'", full_access_path, value)
|
||||
|
||||
|
||||
def test_constructor_error_message(caplog: pytest.LogCaptureFixture) -> None:
|
||||
class MyObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
self.attr = 1
|
||||
super().__init__()
|
||||
|
||||
MyObservable()
|
||||
|
||||
assert (
|
||||
"Ensure that super().__init__() is called at the start of the 'MyObservable' "
|
||||
"constructor! Failing to do so may lead to unexpected behavior." in caplog.text
|
||||
)
|
||||
|
||||
|
||||
def test_simple_class_attribute(caplog: pytest.LogCaptureFixture) -> None:
|
||||
class MyObservable(Observable):
|
||||
int_attribute = 10
|
||||
|
||||
instance = MyObservable()
|
||||
observer = MyObserver(instance)
|
||||
instance.int_attribute = 12
|
||||
|
||||
assert "'int_attribute' changed to '12'" in caplog.text
|
||||
|
||||
|
||||
def test_simple_instance_attribute(caplog: pytest.LogCaptureFixture) -> None:
|
||||
class MyObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.int_attribute = 10
|
||||
|
||||
instance = MyObservable()
|
||||
observer = MyObserver(instance)
|
||||
instance.int_attribute = 12
|
||||
|
||||
assert "'int_attribute' changed to '12'" in caplog.text
|
||||
|
||||
|
||||
def test_nested_class_attribute(caplog: pytest.LogCaptureFixture) -> None:
|
||||
class MySubclass(Observable):
|
||||
name = "My Subclass"
|
||||
|
||||
class MyObservable(Observable):
|
||||
subclass = MySubclass()
|
||||
|
||||
instance = MyObservable()
|
||||
observer = MyObserver(instance)
|
||||
instance.subclass.name = "Other name"
|
||||
|
||||
assert "'subclass.name' changed to 'Other name'" in caplog.text
|
||||
|
||||
|
||||
def test_nested_instance_attribute(caplog: pytest.LogCaptureFixture) -> None:
|
||||
class MySubclass(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.name = "Subclass name"
|
||||
|
||||
class MyObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.subclass = MySubclass()
|
||||
|
||||
instance = MyObservable()
|
||||
observer = MyObserver(instance)
|
||||
instance.subclass.name = "Other name"
|
||||
|
||||
assert "'subclass.name' changed to 'Other name'" in caplog.text
|
||||
|
||||
|
||||
def test_removed_observer_on_class_attr(caplog: pytest.LogCaptureFixture) -> None:
|
||||
class NestedObservable(Observable):
|
||||
name = "Hello"
|
||||
|
||||
nested_instance = NestedObservable()
|
||||
|
||||
class MyObservable(Observable):
|
||||
nested_attr = nested_instance
|
||||
changed_attr = nested_instance
|
||||
|
||||
instance = MyObservable()
|
||||
observer = MyObserver(instance)
|
||||
instance.changed_attr = "Ciao"
|
||||
|
||||
assert "'changed_attr' changed to 'Ciao'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
instance.nested_attr.name = "Hi"
|
||||
|
||||
assert "'nested_attr.name' changed to 'Hi'" in caplog.text
|
||||
assert "'changed_attr.name' changed to 'Hi'" not in caplog.text
|
||||
|
||||
|
||||
def test_removed_observer_on_instance_attr(caplog: pytest.LogCaptureFixture) -> None:
|
||||
class NestedObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.name = "Hello"
|
||||
|
||||
nested_instance = NestedObservable()
|
||||
|
||||
class MyObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.nested_attr = nested_instance
|
||||
self.changed_attr = nested_instance
|
||||
|
||||
instance = MyObservable()
|
||||
observer = MyObserver(instance)
|
||||
instance.changed_attr = "Ciao"
|
||||
|
||||
assert "'changed_attr' changed to 'Ciao'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
instance.nested_attr.name = "Hi"
|
||||
|
||||
assert "'nested_attr.name' changed to 'Hi'" in caplog.text
|
||||
assert "'changed_attr.name' changed to 'Hi'" not in caplog.text
|
||||
|
||||
|
||||
def test_property_getter(caplog: pytest.LogCaptureFixture) -> None:
|
||||
class MyObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self._name = "Hello"
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""The name property."""
|
||||
return self._name
|
||||
|
||||
instance = MyObservable()
|
||||
observer = MyObserver(instance)
|
||||
_ = instance.name
|
||||
|
||||
assert "'name' changed to 'Hello'" in caplog.text
|
||||
|
||||
|
||||
def test_property_setter(caplog: pytest.LogCaptureFixture) -> None:
|
||||
class MyObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self._name = "Hello"
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self._name
|
||||
|
||||
@name.setter
|
||||
def name(self, value: str) -> None:
|
||||
self._name = value
|
||||
|
||||
instance = MyObservable()
|
||||
observer = MyObserver(instance)
|
||||
instance.name = "Ciao"
|
||||
|
||||
assert "'name' changed to 'Hello'" not in caplog.text
|
||||
assert "'name' changed to 'Ciao'" in caplog.text
|
||||
474
tests/observer_pattern/observable/test_observable_object.py
Normal file
474
tests/observer_pattern/observable/test_observable_object.py
Normal file
@@ -0,0 +1,474 @@
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from pydase.observer_pattern.observable import Observable
|
||||
from pydase.observer_pattern.observer import Observer
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class MyObserver(Observer):
|
||||
def on_change(self, full_access_path: str, value: Any) -> None:
|
||||
logger.info("'%s' changed to '%s'", full_access_path, value)
|
||||
|
||||
|
||||
def test_simple_instance_list_attribute(caplog: pytest.LogCaptureFixture) -> None:
|
||||
class MyObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.list_attr = [1, 2]
|
||||
|
||||
instance = MyObservable()
|
||||
MyObserver(instance)
|
||||
instance.list_attr[0] = 12
|
||||
|
||||
assert "'list_attr[0]' changed to '12'" in caplog.text
|
||||
|
||||
|
||||
def test_instance_object_list_attribute(caplog: pytest.LogCaptureFixture) -> None:
|
||||
class NestedObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.name = "Hello"
|
||||
|
||||
class MyObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.list_attr = [NestedObservable()]
|
||||
|
||||
instance = MyObservable()
|
||||
MyObserver(instance)
|
||||
instance.list_attr[0].name = "Ciao"
|
||||
|
||||
assert "'list_attr[0].name' changed to 'Ciao'" in caplog.text
|
||||
|
||||
|
||||
def test_simple_class_list_attribute(caplog: pytest.LogCaptureFixture) -> None:
|
||||
class MyObservable(Observable):
|
||||
list_attr = [1, 2]
|
||||
|
||||
instance = MyObservable()
|
||||
MyObserver(instance)
|
||||
instance.list_attr[0] = 12
|
||||
|
||||
assert "'list_attr[0]' changed to '12'" in caplog.text
|
||||
|
||||
|
||||
def test_class_object_list_attribute(caplog: pytest.LogCaptureFixture) -> None:
|
||||
class NestedObservable(Observable):
|
||||
name = "Hello"
|
||||
|
||||
class MyObservable(Observable):
|
||||
list_attr = [NestedObservable()]
|
||||
|
||||
instance = MyObservable()
|
||||
MyObserver(instance)
|
||||
instance.list_attr[0].name = "Ciao"
|
||||
|
||||
assert "'list_attr[0].name' changed to 'Ciao'" in caplog.text
|
||||
|
||||
|
||||
def test_simple_instance_dict_attribute(caplog: pytest.LogCaptureFixture) -> None:
|
||||
class MyObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.dict_attr = {"first": "Hello"}
|
||||
|
||||
instance = MyObservable()
|
||||
MyObserver(instance)
|
||||
instance.dict_attr["first"] = "Ciao"
|
||||
instance.dict_attr["second"] = "World"
|
||||
|
||||
assert "'dict_attr['first']' changed to 'Ciao'" in caplog.text
|
||||
assert "'dict_attr['second']' changed to 'World'" in caplog.text
|
||||
|
||||
|
||||
def test_simple_class_dict_attribute(caplog: pytest.LogCaptureFixture) -> None:
|
||||
class MyObservable(Observable):
|
||||
dict_attr = {"first": "Hello"}
|
||||
|
||||
instance = MyObservable()
|
||||
MyObserver(instance)
|
||||
instance.dict_attr["first"] = "Ciao"
|
||||
instance.dict_attr["second"] = "World"
|
||||
|
||||
assert "'dict_attr['first']' changed to 'Ciao'" in caplog.text
|
||||
assert "'dict_attr['second']' changed to 'World'" in caplog.text
|
||||
|
||||
|
||||
def test_instance_dict_attribute(caplog: pytest.LogCaptureFixture) -> None:
|
||||
class NestedObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.name = "Hello"
|
||||
|
||||
class MyObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.dict_attr = {"first": NestedObservable()}
|
||||
|
||||
instance = MyObservable()
|
||||
MyObserver(instance)
|
||||
instance.dict_attr["first"].name = "Ciao"
|
||||
|
||||
assert "'dict_attr['first'].name' changed to 'Ciao'" in caplog.text
|
||||
|
||||
|
||||
def test_class_dict_attribute(caplog: pytest.LogCaptureFixture) -> None:
|
||||
class NestedObservable(Observable):
|
||||
name = "Hello"
|
||||
|
||||
class MyObservable(Observable):
|
||||
dict_attr = {"first": NestedObservable()}
|
||||
|
||||
instance = MyObservable()
|
||||
MyObserver(instance)
|
||||
instance.dict_attr["first"].name = "Ciao"
|
||||
|
||||
assert "'dict_attr['first'].name' changed to 'Ciao'" in caplog.text
|
||||
|
||||
|
||||
def test_removed_observer_on_class_list_attr(caplog: pytest.LogCaptureFixture) -> None:
|
||||
class NestedObservable(Observable):
|
||||
name = "Hello"
|
||||
|
||||
nested_instance = NestedObservable()
|
||||
|
||||
class MyObservable(Observable):
|
||||
nested_attr = nested_instance
|
||||
changed_list_attr = [nested_instance]
|
||||
|
||||
instance = MyObservable()
|
||||
MyObserver(instance)
|
||||
instance.changed_list_attr[0] = "Ciao"
|
||||
|
||||
assert "'changed_list_attr[0]' changed to 'Ciao'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
instance.nested_attr.name = "Hi"
|
||||
|
||||
assert "'nested_attr.name' changed to 'Hi'" in caplog.text
|
||||
assert "'changed_list_attr[0].name' changed to 'Hi'" not in caplog.text
|
||||
|
||||
|
||||
def test_removed_observer_on_instance_dict_attr(
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
class NestedObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.name = "Hello"
|
||||
|
||||
nested_instance = NestedObservable()
|
||||
|
||||
class MyObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.nested_attr = nested_instance
|
||||
self.changed_dict_attr = {"nested": nested_instance}
|
||||
|
||||
instance = MyObservable()
|
||||
MyObserver(instance)
|
||||
instance.changed_dict_attr["nested"] = "Ciao"
|
||||
|
||||
assert "'changed_dict_attr['nested']' changed to 'Ciao'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
instance.nested_attr.name = "Hi"
|
||||
|
||||
assert "'nested_attr.name' changed to 'Hi'" in caplog.text
|
||||
assert "'changed_dict_attr['nested'].name' changed to 'Hi'" not in caplog.text
|
||||
|
||||
|
||||
def test_removed_observer_on_instance_list_attr(
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
class NestedObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.name = "Hello"
|
||||
|
||||
nested_instance = NestedObservable()
|
||||
|
||||
class MyObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.nested_attr = nested_instance
|
||||
self.changed_list_attr = [nested_instance]
|
||||
|
||||
instance = MyObservable()
|
||||
MyObserver(instance)
|
||||
instance.changed_list_attr[0] = "Ciao"
|
||||
|
||||
assert "'changed_list_attr[0]' changed to 'Ciao'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
instance.nested_attr.name = "Hi"
|
||||
|
||||
assert "'nested_attr.name' changed to 'Hi'" in caplog.text
|
||||
assert "'changed_list_attr[0].name' changed to 'Hi'" not in caplog.text
|
||||
|
||||
|
||||
def test_removed_observer_on_class_dict_attr(caplog: pytest.LogCaptureFixture) -> None:
|
||||
class NestedObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.name = "Hello"
|
||||
|
||||
nested_instance = NestedObservable()
|
||||
|
||||
class MyObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.nested_attr = nested_instance
|
||||
self.changed_dict_attr = {"nested": nested_instance}
|
||||
|
||||
instance = MyObservable()
|
||||
MyObserver(instance)
|
||||
instance.changed_dict_attr["nested"] = "Ciao"
|
||||
|
||||
assert "'changed_dict_attr['nested']' changed to 'Ciao'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
instance.nested_attr.name = "Hi"
|
||||
|
||||
assert "'nested_attr.name' changed to 'Hi'" in caplog.text
|
||||
assert "'changed_dict_attr['nested'].name' changed to 'Hi'" not in caplog.text
|
||||
|
||||
|
||||
def test_nested_dict_instances(caplog: pytest.LogCaptureFixture) -> None:
|
||||
dict_instance = {"first": "Hello", "second": "World"}
|
||||
|
||||
class MyObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.nested_dict_attr = {"nested": dict_instance}
|
||||
|
||||
instance = MyObservable()
|
||||
MyObserver(instance)
|
||||
instance.nested_dict_attr["nested"]["first"] = "Ciao"
|
||||
|
||||
assert "'nested_dict_attr['nested']['first']' changed to 'Ciao'" in caplog.text
|
||||
|
||||
|
||||
def test_dict_in_list_instance(caplog: pytest.LogCaptureFixture) -> None:
|
||||
dict_instance = {"first": "Hello", "second": "World"}
|
||||
|
||||
class MyObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.dict_in_list = [dict_instance]
|
||||
|
||||
instance = MyObservable()
|
||||
MyObserver(instance)
|
||||
instance.dict_in_list[0]["first"] = "Ciao"
|
||||
|
||||
assert "'dict_in_list[0]['first']' changed to 'Ciao'" in caplog.text
|
||||
|
||||
|
||||
def test_list_in_dict_instance(caplog: pytest.LogCaptureFixture) -> None:
|
||||
list_instance: list[Any] = [1, 2, 3]
|
||||
|
||||
class MyObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.list_in_dict = {"some_list": list_instance}
|
||||
|
||||
instance = MyObservable()
|
||||
MyObserver(instance)
|
||||
instance.list_in_dict["some_list"][0] = "Ciao"
|
||||
|
||||
assert "'list_in_dict['some_list'][0]' changed to 'Ciao'" in caplog.text
|
||||
|
||||
|
||||
def test_list_append(caplog: pytest.LogCaptureFixture) -> None:
|
||||
class OtherObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.greeting = "Other Observable"
|
||||
|
||||
class MyObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.my_list = []
|
||||
|
||||
observable_instance = MyObservable()
|
||||
MyObserver(observable_instance)
|
||||
|
||||
observable_instance.my_list.append(OtherObservable())
|
||||
assert f"'my_list' changed to '{observable_instance.my_list}'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
observable_instance.my_list.append(OtherObservable())
|
||||
assert f"'my_list' changed to '{observable_instance.my_list}'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
observable_instance.my_list[0].greeting = "Hi"
|
||||
observable_instance.my_list[1].greeting = "Hello"
|
||||
|
||||
assert observable_instance.my_list[0].greeting == "Hi"
|
||||
assert observable_instance.my_list[1].greeting == "Hello"
|
||||
assert "'my_list[0].greeting' changed to 'Hi'" in caplog.text
|
||||
assert "'my_list[1].greeting' changed to 'Hello'" in caplog.text
|
||||
|
||||
|
||||
def test_list_pop(caplog: pytest.LogCaptureFixture) -> None:
|
||||
class OtherObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.greeting = "Hello there!"
|
||||
|
||||
class MyObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.my_list = [OtherObservable() for _ in range(2)]
|
||||
|
||||
observable_instance = MyObservable()
|
||||
MyObserver(observable_instance)
|
||||
|
||||
popped_instance = observable_instance.my_list.pop(0)
|
||||
|
||||
assert len(observable_instance.my_list) == 1
|
||||
assert f"'my_list' changed to '{observable_instance.my_list}'" in caplog.text
|
||||
|
||||
# checks if observer is removed
|
||||
popped_instance.greeting = "Ciao"
|
||||
assert "'my_list[0].greeting' changed to 'Ciao'" not in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
# checks if observer keys have been updated (index 1 moved to 0)
|
||||
observable_instance.my_list[0].greeting = "Hi"
|
||||
assert "'my_list[0].greeting' changed to 'Hi'" in caplog.text
|
||||
|
||||
|
||||
def test_list_clear(caplog: pytest.LogCaptureFixture) -> None:
|
||||
class OtherObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.greeting = "Hello there!"
|
||||
|
||||
other_observable_instance = OtherObservable()
|
||||
|
||||
class MyObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.my_list = [other_observable_instance]
|
||||
|
||||
observable_instance = MyObservable()
|
||||
MyObserver(observable_instance)
|
||||
|
||||
other_observable_instance.greeting = "Hello"
|
||||
assert "'my_list[0].greeting' changed to 'Hello'" in caplog.text
|
||||
|
||||
observable_instance.my_list.clear()
|
||||
|
||||
assert len(observable_instance.my_list) == 0
|
||||
assert "'my_list' changed to '[]'" in caplog.text
|
||||
|
||||
# checks if observer has been removed
|
||||
other_observable_instance.greeting = "Hi"
|
||||
assert "'my_list[0].greeting' changed to 'Hi'" not in caplog.text
|
||||
|
||||
|
||||
def test_list_extend(caplog: pytest.LogCaptureFixture) -> None:
|
||||
class OtherObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.greeting = "Hello there!"
|
||||
|
||||
other_observable_instance = OtherObservable()
|
||||
|
||||
class MyObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.my_list = []
|
||||
|
||||
observable_instance = MyObservable()
|
||||
MyObserver(observable_instance)
|
||||
|
||||
other_observable_instance.greeting = "Hello"
|
||||
assert "'my_list[0].greeting' changed to 'Hello'" not in caplog.text
|
||||
|
||||
observable_instance.my_list.extend([other_observable_instance, OtherObservable()])
|
||||
|
||||
assert len(observable_instance.my_list) == 2
|
||||
assert f"'my_list' changed to '{observable_instance.my_list}'" in caplog.text
|
||||
|
||||
# checks if observer has been removed
|
||||
other_observable_instance.greeting = "Hi"
|
||||
assert "'my_list[0].greeting' changed to 'Hi'" in caplog.text
|
||||
observable_instance.my_list[1].greeting = "Ciao"
|
||||
assert "'my_list[1].greeting' changed to 'Ciao'" in caplog.text
|
||||
|
||||
|
||||
def test_list_insert(caplog: pytest.LogCaptureFixture) -> None:
|
||||
class OtherObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.greeting = "Hello there!"
|
||||
|
||||
other_observable_instance_1 = OtherObservable()
|
||||
other_observable_instance_2 = OtherObservable()
|
||||
|
||||
class MyObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.my_list = [other_observable_instance_1, OtherObservable()]
|
||||
|
||||
observable_instance = MyObservable()
|
||||
MyObserver(observable_instance)
|
||||
|
||||
other_observable_instance_1.greeting = "Hello"
|
||||
assert "'my_list[0].greeting' changed to 'Hello'" in caplog.text
|
||||
|
||||
observable_instance.my_list.insert(0, other_observable_instance_2)
|
||||
|
||||
assert len(observable_instance.my_list) == 3
|
||||
assert f"'my_list' changed to '{observable_instance.my_list}'" in caplog.text
|
||||
|
||||
# checks if observer keys have been updated
|
||||
other_observable_instance_2.greeting = "Hey"
|
||||
other_observable_instance_1.greeting = "Hi"
|
||||
observable_instance.my_list[2].greeting = "Ciao"
|
||||
|
||||
assert "'my_list[0].greeting' changed to 'Hey'" in caplog.text
|
||||
assert "'my_list[1].greeting' changed to 'Hi'" in caplog.text
|
||||
assert "'my_list[2].greeting' changed to 'Ciao'" in caplog.text
|
||||
|
||||
|
||||
def test_list_remove(caplog: pytest.LogCaptureFixture) -> None:
|
||||
class OtherObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.greeting = "Hello there!"
|
||||
|
||||
other_observable_instance_1 = OtherObservable()
|
||||
other_observable_instance_2 = OtherObservable()
|
||||
|
||||
class MyObservable(Observable):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.my_list = [other_observable_instance_1, other_observable_instance_2]
|
||||
|
||||
observable_instance = MyObservable()
|
||||
MyObserver(observable_instance)
|
||||
|
||||
other_observable_instance_1.greeting = "Hello"
|
||||
other_observable_instance_2.greeting = "Hi"
|
||||
caplog.clear()
|
||||
|
||||
observable_instance.my_list.remove(other_observable_instance_1)
|
||||
|
||||
assert len(observable_instance.my_list) == 1
|
||||
assert f"'my_list' changed to '{observable_instance.my_list}'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
# checks if observer has been removed
|
||||
other_observable_instance_1.greeting = "Hi"
|
||||
assert "'my_list[0].greeting' changed to 'Hi'" not in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
# checks if observer key was updated correctly (was index 1)
|
||||
other_observable_instance_2.greeting = "Ciao"
|
||||
assert "'my_list[0].greeting' changed to 'Ciao'" in caplog.text
|
||||
25
tests/observer_pattern/observer/test_observer.py
Normal file
25
tests/observer_pattern/observer/test_observer.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from pydase.observer_pattern.observable import Observable
|
||||
from pydase.observer_pattern.observer import Observer
|
||||
|
||||
|
||||
def test_abstract_method_error() -> None:
|
||||
class MyObserver(Observer):
|
||||
pass
|
||||
|
||||
class MyObservable(Observable):
|
||||
pass
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
MyObserver(MyObservable())
|
||||
|
||||
|
||||
def test_constructor_error() -> None:
|
||||
class MyObserver(Observer):
|
||||
def on_change(self, full_access_path: str, value: Any) -> None:
|
||||
pass
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
MyObserver()
|
||||
@@ -1,8 +1,15 @@
|
||||
import json
|
||||
import signal
|
||||
|
||||
from pytest_mock import MockerFixture
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import pydase
|
||||
import pydase.components
|
||||
import pydase.units as u
|
||||
from pydase.data_service.state_manager import load_state
|
||||
from pydase.server.server import Server
|
||||
from pytest import LogCaptureFixture
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
|
||||
def test_signal_handling(mocker: MockerFixture):
|
||||
@@ -33,3 +40,64 @@ def test_signal_handling(mocker: MockerFixture):
|
||||
# Simulate receiving a SIGINT signal for the second time
|
||||
server.handle_exit(signal.SIGINT, None)
|
||||
mock_exit.assert_called_once_with(1)
|
||||
|
||||
|
||||
class Service(pydase.DataService):
|
||||
def __init__(self, **kwargs: Any) -> None:
|
||||
super().__init__(**kwargs)
|
||||
self.some_unit: u.Quantity = 1.2 * u.units.A
|
||||
self.some_float = 1.0
|
||||
self._property_attr = 1337.0
|
||||
|
||||
@property
|
||||
def property_attr(self) -> float:
|
||||
return self._property_attr
|
||||
|
||||
@property_attr.setter
|
||||
@load_state
|
||||
def property_attr(self, value: float) -> None:
|
||||
self._property_attr = value
|
||||
|
||||
|
||||
CURRENT_STATE = Service().serialize()
|
||||
|
||||
LOAD_STATE = {
|
||||
"some_float": {
|
||||
"type": "float",
|
||||
"value": 10.0,
|
||||
"readonly": False,
|
||||
"doc": None,
|
||||
},
|
||||
"property_attr": {
|
||||
"type": "float",
|
||||
"value": 1337.1,
|
||||
"readonly": False,
|
||||
"doc": None,
|
||||
},
|
||||
"some_unit": {
|
||||
"type": "Quantity",
|
||||
"value": {"magnitude": 12.0, "unit": "A"},
|
||||
"readonly": False,
|
||||
"doc": None,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def test_load_state(tmp_path: Path, caplog: LogCaptureFixture) -> None:
|
||||
# Create a StateManager instance with a temporary file
|
||||
file = tmp_path / "test_state.json"
|
||||
|
||||
# Write a temporary JSON file to read back
|
||||
with open(file, "w") as f:
|
||||
json.dump(LOAD_STATE, f, indent=4)
|
||||
|
||||
service = Service()
|
||||
Server(service, filename=str(file))
|
||||
|
||||
assert service.some_unit == u.Quantity(12, "A")
|
||||
assert service.property_attr == 1337.1
|
||||
assert service.some_float == 10.0
|
||||
|
||||
assert "'some_unit' changed to '12.0 A'" in caplog.text
|
||||
assert "'some_float' changed to '10.0'" in caplog.text
|
||||
assert "'property_attr' changed to '1337.1'" in caplog.text
|
||||
|
||||
68
tests/server/web_server/test_sio_setup.py
Normal file
68
tests/server/web_server/test_sio_setup.py
Normal file
@@ -0,0 +1,68 @@
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
import pydase
|
||||
import pytest
|
||||
from pydase.data_service.data_service_observer import DataServiceObserver
|
||||
from pydase.data_service.state_manager import StateManager
|
||||
from pydase.server.web_server.sio_setup import (
|
||||
RunMethodDict,
|
||||
UpdateDict,
|
||||
setup_sio_server,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_set_attribute_event() -> None:
|
||||
class SubClass(pydase.DataService):
|
||||
name = "SubClass"
|
||||
|
||||
class ServiceClass(pydase.DataService):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.sub_class = SubClass()
|
||||
|
||||
def some_method(self) -> None:
|
||||
logger.info("Triggered 'test_method'.")
|
||||
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
observer = DataServiceObserver(state_manager)
|
||||
|
||||
server = setup_sio_server(observer, False, asyncio.get_running_loop())
|
||||
|
||||
test_sid = 1234
|
||||
test_data: UpdateDict = {
|
||||
"parent_path": "sub_class",
|
||||
"name": "name",
|
||||
"value": "new name",
|
||||
}
|
||||
|
||||
server.handlers["/"]["set_attribute"](test_sid, test_data)
|
||||
|
||||
assert service_instance.sub_class.name == "new name"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_method_event(caplog: pytest.LogCaptureFixture):
|
||||
class ServiceClass(pydase.DataService):
|
||||
def test_method(self) -> None:
|
||||
logger.info("Triggered 'test_method'.")
|
||||
|
||||
state_manager = StateManager(ServiceClass())
|
||||
observer = DataServiceObserver(state_manager)
|
||||
|
||||
server = setup_sio_server(observer, False, asyncio.get_running_loop())
|
||||
|
||||
test_sid = 1234
|
||||
test_data: RunMethodDict = {
|
||||
"parent_path": "",
|
||||
"name": "test_method",
|
||||
"kwargs": {},
|
||||
}
|
||||
|
||||
server.handlers["/"]["run_method"](test_sid, test_data)
|
||||
|
||||
assert "Triggered 'test_method'." in caplog.text
|
||||
51
tests/server/web_server/test_web_server.py
Normal file
51
tests/server/web_server/test_web_server.py
Normal file
@@ -0,0 +1,51 @@
|
||||
import json
|
||||
import logging
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
import pydase
|
||||
from pydase.data_service.data_service_observer import DataServiceObserver
|
||||
from pydase.data_service.state_manager import StateManager
|
||||
from pydase.server.web_server.web_server import WebServer
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def test_web_settings() -> None:
|
||||
class SubClass(pydase.DataService):
|
||||
name = "Hello"
|
||||
|
||||
class ServiceClass(pydase.DataService):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.attr_1 = SubClass()
|
||||
self.added = "added"
|
||||
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
observer = DataServiceObserver(state_manager)
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
web_settings = {
|
||||
"attr_1": {"displayName": "Attribute"},
|
||||
"attr_1.name": {"displayName": "Attribute name"},
|
||||
}
|
||||
web_settings_file = Path(tmp) / "web_settings.json"
|
||||
|
||||
with web_settings_file.open("w") as file:
|
||||
file.write(json.dumps(web_settings))
|
||||
|
||||
server = WebServer(
|
||||
observer,
|
||||
host="0.0.0.0",
|
||||
port=8001,
|
||||
generate_web_settings=True,
|
||||
config_dir=Path(tmp),
|
||||
)
|
||||
new_web_settings = server.web_settings
|
||||
|
||||
# existing entries are not overwritten, new entries are appended
|
||||
assert new_web_settings == {**web_settings, "added": {"displayName": "added"}}
|
||||
assert json.loads(web_settings_file.read_text()) == {
|
||||
**web_settings,
|
||||
"added": {"displayName": "added"},
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
from pytest import LogCaptureFixture
|
||||
|
||||
from pydase import DataService
|
||||
from pydase.data_service.data_service_observer import DataServiceObserver
|
||||
from pydase.data_service.state_manager import StateManager
|
||||
from pytest import LogCaptureFixture
|
||||
|
||||
|
||||
def test_class_attributes(caplog: LogCaptureFixture) -> None:
|
||||
@@ -11,9 +12,11 @@ def test_class_attributes(caplog: LogCaptureFixture) -> None:
|
||||
attr_1 = SubClass()
|
||||
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
service_instance.attr_1.name = "Hi"
|
||||
|
||||
assert "ServiceClass.attr_1.name changed to Hi" in caplog.text
|
||||
assert "'attr_1.name' changed to 'Hi'" in caplog.text
|
||||
|
||||
|
||||
def test_instance_attributes(caplog: LogCaptureFixture) -> None:
|
||||
@@ -22,13 +25,15 @@ def test_instance_attributes(caplog: LogCaptureFixture) -> None:
|
||||
|
||||
class ServiceClass(DataService):
|
||||
def __init__(self) -> None:
|
||||
self.attr_1 = SubClass()
|
||||
super().__init__()
|
||||
self.attr_1 = SubClass()
|
||||
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
service_instance.attr_1.name = "Hi"
|
||||
|
||||
assert "ServiceClass.attr_1.name changed to Hi" in caplog.text
|
||||
assert "'attr_1.name' changed to 'Hi'" in caplog.text
|
||||
|
||||
|
||||
def test_class_attribute(caplog: LogCaptureFixture) -> None:
|
||||
@@ -36,21 +41,25 @@ def test_class_attribute(caplog: LogCaptureFixture) -> None:
|
||||
attr = 0
|
||||
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
service_instance.attr = 1
|
||||
assert "ServiceClass.attr changed to 1" in caplog.text
|
||||
assert "'attr' changed to '1'" in caplog.text
|
||||
|
||||
|
||||
def test_instance_attribute(caplog: LogCaptureFixture) -> None:
|
||||
class ServiceClass(DataService):
|
||||
def __init__(self) -> None:
|
||||
self.attr = "Hello World"
|
||||
super().__init__()
|
||||
self.attr = "Hello World"
|
||||
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
service_instance.attr = "Hello"
|
||||
assert "ServiceClass.attr changed to Hello" in caplog.text
|
||||
assert "'attr' changed to 'Hello'" in caplog.text
|
||||
|
||||
|
||||
def test_reused_instance_attributes(caplog: LogCaptureFixture) -> None:
|
||||
@@ -61,16 +70,19 @@ def test_reused_instance_attributes(caplog: LogCaptureFixture) -> None:
|
||||
|
||||
class ServiceClass(DataService):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.attr_1 = subclass_instance
|
||||
self.attr_2 = subclass_instance
|
||||
super().__init__()
|
||||
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
service_instance.attr_1.name = "Hi"
|
||||
|
||||
assert service_instance.attr_1 == service_instance.attr_2
|
||||
assert "ServiceClass.attr_1.name changed to Hi" in caplog.text
|
||||
assert "ServiceClass.attr_2.name changed to Hi" in caplog.text
|
||||
assert "'attr_1.name' changed to 'Hi'" in caplog.text
|
||||
assert "'attr_2.name' changed to 'Hi'" in caplog.text
|
||||
|
||||
|
||||
def test_reused_attributes_mixed(caplog: LogCaptureFixture) -> None:
|
||||
@@ -83,15 +95,18 @@ def test_reused_attributes_mixed(caplog: LogCaptureFixture) -> None:
|
||||
attr_1 = subclass_instance
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.attr_2 = subclass_instance
|
||||
super().__init__()
|
||||
self.attr_2 = subclass_instance
|
||||
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
service_instance.attr_1.name = "Hi"
|
||||
|
||||
assert service_instance.attr_1 == service_instance.attr_2
|
||||
assert "ServiceClass.attr_1.name changed to Hi" in caplog.text
|
||||
assert "ServiceClass.attr_2.name changed to Hi" in caplog.text
|
||||
assert "'attr_1.name' changed to 'Hi'" in caplog.text
|
||||
assert "'attr_2.name' changed to 'Hi'" in caplog.text
|
||||
|
||||
|
||||
def test_nested_class_attributes(caplog: LogCaptureFixture) -> None:
|
||||
@@ -111,15 +126,18 @@ def test_nested_class_attributes(caplog: LogCaptureFixture) -> None:
|
||||
attr = SubClass()
|
||||
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
service_instance.attr.attr.attr.name = "Hi"
|
||||
service_instance.attr.attr.name = "Hou"
|
||||
service_instance.attr.name = "foo"
|
||||
service_instance.name = "bar"
|
||||
|
||||
assert "ServiceClass.attr.attr.attr.name changed to Hi" in caplog.text
|
||||
assert "ServiceClass.attr.attr.name changed to Hou" in caplog.text
|
||||
assert "ServiceClass.attr.name changed to foo" in caplog.text
|
||||
assert "ServiceClass.name changed to bar" in caplog.text
|
||||
assert "'attr.attr.attr.name' changed to 'Hi'" in caplog.text
|
||||
assert "'attr.attr.name' changed to 'Hou'" in caplog.text
|
||||
assert "'attr.name' changed to 'foo'" in caplog.text
|
||||
assert "'name' changed to 'bar'" in caplog.text
|
||||
|
||||
|
||||
def test_nested_instance_attributes(caplog: LogCaptureFixture) -> None:
|
||||
@@ -128,32 +146,35 @@ def test_nested_instance_attributes(caplog: LogCaptureFixture) -> None:
|
||||
|
||||
class SubSubClass(DataService):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.attr = SubSubSubClass()
|
||||
self.name = "Hello"
|
||||
super().__init__()
|
||||
|
||||
class SubClass(DataService):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.attr = SubSubClass()
|
||||
self.name = "Hello"
|
||||
super().__init__()
|
||||
|
||||
class ServiceClass(DataService):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.attr = SubClass()
|
||||
self.name = "Hello"
|
||||
super().__init__()
|
||||
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
service_instance.attr.attr.attr.name = "Hi"
|
||||
service_instance.attr.attr.name = "Hou"
|
||||
service_instance.attr.name = "foo"
|
||||
service_instance.name = "bar"
|
||||
|
||||
assert "ServiceClass.attr.attr.attr.name changed to Hi" in caplog.text
|
||||
assert "ServiceClass.attr.attr.name changed to Hou" in caplog.text
|
||||
assert "ServiceClass.attr.name changed to foo" in caplog.text
|
||||
assert "ServiceClass.name changed to bar" in caplog.text
|
||||
assert "'attr.attr.attr.name' changed to 'Hi'" in caplog.text
|
||||
assert "'attr.attr.name' changed to 'Hou'" in caplog.text
|
||||
assert "'attr.name' changed to 'foo'" in caplog.text
|
||||
assert "'name' changed to 'bar'" in caplog.text
|
||||
|
||||
|
||||
def test_advanced_nested_class_attributes(caplog: LogCaptureFixture) -> None:
|
||||
@@ -171,14 +192,19 @@ def test_advanced_nested_class_attributes(caplog: LogCaptureFixture) -> None:
|
||||
subattr = SubSubClass()
|
||||
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
service_instance.attr.attr.attr.name = "Hi"
|
||||
|
||||
assert "ServiceClass.attr.attr.attr.name changed to Hi" in caplog.text
|
||||
assert "ServiceClass.subattr.attr.name changed to Hi" in caplog.text
|
||||
assert "'attr.attr.attr.name' changed to 'Hi'" in caplog.text
|
||||
assert "'subattr.attr.name' changed to 'Hi'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
service_instance.subattr.attr.name = "Ho"
|
||||
|
||||
assert "ServiceClass.attr.attr.attr.name changed to Ho" in caplog.text
|
||||
assert "ServiceClass.subattr.attr.name changed to Ho" in caplog.text
|
||||
assert "'attr.attr.attr.name' changed to 'Ho'" in caplog.text
|
||||
assert "'subattr.attr.name' changed to 'Ho'" in caplog.text
|
||||
|
||||
|
||||
def test_advanced_nested_instance_attributes(caplog: LogCaptureFixture) -> None:
|
||||
@@ -187,32 +213,34 @@ def test_advanced_nested_instance_attributes(caplog: LogCaptureFixture) -> None:
|
||||
|
||||
class SubSubClass(DataService):
|
||||
def __init__(self) -> None:
|
||||
self.attr = SubSubSubClass()
|
||||
super().__init__()
|
||||
self.attr = SubSubSubClass()
|
||||
|
||||
subsubclass_instance = SubSubClass()
|
||||
|
||||
class SubClass(DataService):
|
||||
def __init__(self) -> None:
|
||||
self.attr = subsubclass_instance
|
||||
super().__init__()
|
||||
self.attr = subsubclass_instance
|
||||
|
||||
class ServiceClass(DataService):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.attr = SubClass()
|
||||
self.subattr = subsubclass_instance
|
||||
super().__init__()
|
||||
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
service_instance.attr.attr.attr.name = "Hi"
|
||||
assert "ServiceClass.attr.attr.attr.name changed to Hi" in caplog.text
|
||||
assert "ServiceClass.subattr.attr.name changed to Hi" in caplog.text
|
||||
assert "'attr.attr.attr.name' changed to 'Hi'" in caplog.text
|
||||
assert "'subattr.attr.name' changed to 'Hi'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
service_instance.subattr.attr.name = "Ho"
|
||||
assert "ServiceClass.attr.attr.attr.name changed to Ho" in caplog.text
|
||||
assert "ServiceClass.subattr.attr.name changed to Ho" in caplog.text
|
||||
assert "'attr.attr.attr.name' changed to 'Ho'" in caplog.text
|
||||
assert "'subattr.attr.name' changed to 'Ho'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
|
||||
@@ -224,17 +252,20 @@ def test_advanced_nested_attributes_mixed(caplog: LogCaptureFixture) -> None:
|
||||
class_attr = SubSubClass()
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.attr_1 = SubSubClass()
|
||||
super().__init__()
|
||||
self.attr_1 = SubSubClass()
|
||||
|
||||
class ServiceClass(DataService):
|
||||
class_attr = SubClass()
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.attr = SubClass()
|
||||
super().__init__()
|
||||
self.attr = SubClass()
|
||||
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
# Subclass.attr is the same for all instances
|
||||
assert service_instance.attr.class_attr == service_instance.class_attr.class_attr
|
||||
|
||||
@@ -245,23 +276,23 @@ def test_advanced_nested_attributes_mixed(caplog: LogCaptureFixture) -> None:
|
||||
assert service_instance.attr.attr_1 != service_instance.class_attr.class_attr
|
||||
|
||||
service_instance.class_attr.class_attr.name = "Ho"
|
||||
assert "ServiceClass.class_attr.class_attr.name changed to Ho" in caplog.text
|
||||
assert "ServiceClass.attr.class_attr.name changed to Ho" in caplog.text
|
||||
assert "'class_attr.class_attr.name' changed to 'Ho'" in caplog.text
|
||||
assert "'attr.class_attr.name' changed to 'Ho'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
service_instance.class_attr.attr_1.name = "Ho"
|
||||
assert "ServiceClass.class_attr.attr_1.name changed to Ho" in caplog.text
|
||||
assert "ServiceClass.attr.attr_1.name changed to Ho" not in caplog.text
|
||||
assert "'class_attr.attr_1.name' changed to 'Ho'" in caplog.text
|
||||
assert "'attr.attr_1.name' changed to 'Ho'" not in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
service_instance.attr.class_attr.name = "Ho"
|
||||
assert "ServiceClass.class_attr.class_attr.name changed to Ho" in caplog.text
|
||||
assert "ServiceClass.attr.class_attr.name changed to Ho" in caplog.text
|
||||
service_instance.attr.class_attr.name = "Hello"
|
||||
assert "'class_attr.class_attr.name' changed to 'Hello'" in caplog.text
|
||||
assert "'attr.class_attr.name' changed to 'Hello'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
service_instance.attr.attr_1.name = "Ho"
|
||||
assert "ServiceClass.attr.attr_1.name changed to Ho" in caplog.text
|
||||
assert "ServiceClass.class_attr.attr_1.name changed to Ho" not in caplog.text
|
||||
assert "'attr.attr_1.name' changed to 'Ho'" in caplog.text
|
||||
assert "'class_attr.attr_1.name' changed to 'Ho'" not in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
|
||||
@@ -277,32 +308,34 @@ def test_class_list_attributes(caplog: LogCaptureFixture) -> None:
|
||||
attr = subclass_instance
|
||||
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
assert service_instance.attr_list[0] != service_instance.attr_list[1]
|
||||
|
||||
service_instance.attr_list[0].name = "Ho"
|
||||
assert "ServiceClass.attr_list[0].name changed to Ho" in caplog.text
|
||||
assert "ServiceClass.attr_list[1].name changed to Ho" not in caplog.text
|
||||
assert "'attr_list[0].name' changed to 'Ho'" in caplog.text
|
||||
assert "'attr_list[1].name' changed to 'Ho'" not in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
service_instance.attr_list[1].name = "Ho"
|
||||
assert "ServiceClass.attr_list[0].name changed to Ho" not in caplog.text
|
||||
assert "ServiceClass.attr_list[1].name changed to Ho" in caplog.text
|
||||
assert "'attr_list[0].name' changed to 'Ho'" not in caplog.text
|
||||
assert "'attr_list[1].name' changed to 'Ho'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
assert service_instance.attr_list_2[0] == service_instance.attr
|
||||
assert service_instance.attr_list_2[0] == service_instance.attr_list_2[1]
|
||||
|
||||
service_instance.attr_list_2[0].name = "Ho"
|
||||
assert "ServiceClass.attr_list_2[0].name changed to Ho" in caplog.text
|
||||
assert "ServiceClass.attr_list_2[1].name changed to Ho" in caplog.text
|
||||
assert "ServiceClass.attr.name changed to Ho" in caplog.text
|
||||
service_instance.attr_list_2[0].name = "Ciao"
|
||||
assert "'attr_list_2[0].name' changed to 'Ciao'" in caplog.text
|
||||
assert "'attr_list_2[1].name' changed to 'Ciao'" in caplog.text
|
||||
assert "'attr.name' changed to 'Ciao'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
service_instance.attr_list_2[1].name = "Ho"
|
||||
assert "ServiceClass.attr_list_2[0].name changed to Ho" in caplog.text
|
||||
assert "ServiceClass.attr_list_2[1].name changed to Ho" in caplog.text
|
||||
assert "ServiceClass.attr.name changed to Ho" in caplog.text
|
||||
service_instance.attr_list_2[1].name = "Bye"
|
||||
assert "'attr_list_2[0].name' changed to 'Bye'" in caplog.text
|
||||
assert "'attr_list_2[1].name' changed to 'Bye'" in caplog.text
|
||||
assert "'attr.name' changed to 'Bye'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
|
||||
@@ -320,17 +353,19 @@ def test_nested_class_list_attributes(caplog: LogCaptureFixture) -> None:
|
||||
subattr = subsubclass_instance
|
||||
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
assert service_instance.attr[0].attr_list[0] == service_instance.subattr
|
||||
|
||||
service_instance.attr[0].attr_list[0].name = "Ho"
|
||||
assert "ServiceClass.attr[0].attr_list[0].name changed to Ho" in caplog.text
|
||||
assert "ServiceClass.subattr.name changed to Ho" in caplog.text
|
||||
assert "'attr[0].attr_list[0].name' changed to 'Ho'" in caplog.text
|
||||
assert "'subattr.name' changed to 'Ho'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
service_instance.subattr.name = "Ho"
|
||||
assert "ServiceClass.attr[0].attr_list[0].name changed to Ho" in caplog.text
|
||||
assert "ServiceClass.subattr.name changed to Ho" in caplog.text
|
||||
service_instance.subattr.name = "Hi"
|
||||
assert "'attr[0].attr_list[0].name' changed to 'Hi'" in caplog.text
|
||||
assert "'subattr.name' changed to 'Hi'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
|
||||
@@ -342,44 +377,46 @@ def test_instance_list_attributes(caplog: LogCaptureFixture) -> None:
|
||||
|
||||
class ServiceClass(DataService):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.attr_list = [SubClass() for _ in range(2)]
|
||||
self.attr_list_2 = [subclass_instance, subclass_instance]
|
||||
self.attr = subclass_instance
|
||||
super().__init__()
|
||||
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
assert service_instance.attr_list[0] != service_instance.attr_list[1]
|
||||
|
||||
service_instance.attr_list[0].name = "Ho"
|
||||
assert "ServiceClass.attr_list[0].name changed to Ho" in caplog.text
|
||||
assert "ServiceClass.attr_list[1].name changed to Ho" not in caplog.text
|
||||
assert "'attr_list[0].name' changed to 'Ho'" in caplog.text
|
||||
assert "'attr_list[1].name' changed to 'Ho'" not in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
service_instance.attr_list[1].name = "Ho"
|
||||
assert "ServiceClass.attr_list[0].name changed to Ho" not in caplog.text
|
||||
assert "ServiceClass.attr_list[1].name changed to Ho" in caplog.text
|
||||
service_instance.attr_list[1].name = "Hi"
|
||||
assert "'attr_list[0].name' changed to 'Hi'" not in caplog.text
|
||||
assert "'attr_list[1].name' changed to 'Hi'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
assert service_instance.attr_list_2[0] == service_instance.attr
|
||||
assert service_instance.attr_list_2[0] == service_instance.attr_list_2[1]
|
||||
|
||||
service_instance.attr_list_2[0].name = "Ho"
|
||||
assert "ServiceClass.attr.name changed to Ho" in caplog.text
|
||||
assert "ServiceClass.attr_list_2[0].name changed to Ho" in caplog.text
|
||||
assert "ServiceClass.attr_list_2[1].name changed to Ho" in caplog.text
|
||||
service_instance.attr_list_2[0].name = "Ciao"
|
||||
assert "'attr.name' changed to 'Ciao'" in caplog.text
|
||||
assert "'attr_list_2[0].name' changed to 'Ciao'" in caplog.text
|
||||
assert "'attr_list_2[1].name' changed to 'Ciao'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
service_instance.attr_list_2[1].name = "Ho"
|
||||
assert "ServiceClass.attr.name changed to Ho" in caplog.text
|
||||
assert "ServiceClass.attr_list_2[0].name changed to Ho" in caplog.text
|
||||
assert "ServiceClass.attr_list_2[1].name changed to Ho" in caplog.text
|
||||
service_instance.attr_list_2[1].name = "Bye"
|
||||
assert "'attr.name' changed to 'Bye'" in caplog.text
|
||||
assert "'attr_list_2[0].name' changed to 'Bye'" in caplog.text
|
||||
assert "'attr_list_2[1].name' changed to 'Bye'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
service_instance.attr.name = "Ho"
|
||||
assert "ServiceClass.attr.name changed to Ho" in caplog.text
|
||||
assert "ServiceClass.attr_list_2[0].name changed to Ho" in caplog.text
|
||||
assert "ServiceClass.attr_list_2[1].name changed to Ho" in caplog.text
|
||||
assert "'attr.name' changed to 'Ho'" in caplog.text
|
||||
assert "'attr_list_2[0].name' changed to 'Ho'" in caplog.text
|
||||
assert "'attr_list_2[1].name' changed to 'Ho'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
|
||||
@@ -391,26 +428,28 @@ def test_nested_instance_list_attributes(caplog: LogCaptureFixture) -> None:
|
||||
|
||||
class SubClass(DataService):
|
||||
def __init__(self) -> None:
|
||||
self.attr_list = [subsubclass_instance]
|
||||
super().__init__()
|
||||
self.attr_list = [subsubclass_instance]
|
||||
|
||||
class ServiceClass(DataService):
|
||||
class_attr = subsubclass_instance
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.attr = [SubClass()]
|
||||
super().__init__()
|
||||
self.attr = [SubClass()]
|
||||
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
assert service_instance.attr[0].attr_list[0] == service_instance.class_attr
|
||||
|
||||
service_instance.attr[0].attr_list[0].name = "Ho"
|
||||
assert "ServiceClass.attr[0].attr_list[0].name changed to Ho" in caplog.text
|
||||
assert "ServiceClass.class_attr.name changed to Ho" in caplog.text
|
||||
assert "'attr[0].attr_list[0].name' changed to 'Ho'" in caplog.text
|
||||
assert "'class_attr.name' changed to 'Ho'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
service_instance.class_attr.name = "Ho"
|
||||
assert "ServiceClass.attr[0].attr_list[0].name changed to Ho" in caplog.text
|
||||
assert "ServiceClass.class_attr.name changed to Ho" in caplog.text
|
||||
service_instance.class_attr.name = "Hi"
|
||||
assert "'attr[0].attr_list[0].name' changed to 'Hi'" in caplog.text
|
||||
assert "'class_attr.name' changed to 'Hi'" in caplog.text
|
||||
caplog.clear()
|
||||
@@ -1,6 +1,7 @@
|
||||
from pytest import LogCaptureFixture
|
||||
|
||||
from pydase import DataService
|
||||
from pydase.data_service.data_service_observer import DataServiceObserver
|
||||
from pydase.data_service.state_manager import StateManager
|
||||
from pytest import LogCaptureFixture
|
||||
|
||||
|
||||
def test_properties(caplog: LogCaptureFixture) -> None:
|
||||
@@ -28,17 +29,20 @@ def test_properties(caplog: LogCaptureFixture) -> None:
|
||||
def current(self, value: float) -> None:
|
||||
self._current = value
|
||||
|
||||
test_service = ServiceClass()
|
||||
test_service.voltage = 1
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
assert "ServiceClass.power changed to 1.0" in caplog.text
|
||||
assert "ServiceClass.voltage changed to 1.0" in caplog.text
|
||||
service_instance.voltage = 1.0
|
||||
|
||||
assert "'power' changed to '1.0'" in caplog.text
|
||||
assert "'voltage' changed to '1.0'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
test_service.current = 12.0
|
||||
service_instance.current = 12.0
|
||||
|
||||
assert "ServiceClass.power changed to 12.0" in caplog.text
|
||||
assert "ServiceClass.current changed to 12.0" in caplog.text
|
||||
assert "'power' changed to '12.0'" in caplog.text
|
||||
assert "'current' changed to '12.0'" in caplog.text
|
||||
|
||||
|
||||
def test_nested_properties(caplog: LogCaptureFixture) -> None:
|
||||
@@ -61,30 +65,32 @@ def test_nested_properties(caplog: LogCaptureFixture) -> None:
|
||||
def sub_name(self) -> str:
|
||||
return f"{self.class_attr.name} {self.name}"
|
||||
|
||||
test_service = ServiceClass()
|
||||
test_service.name = "Peepz"
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
assert "ServiceClass.name changed to Peepz" in caplog.text
|
||||
assert "ServiceClass.sub_name changed to Hello Peepz" in caplog.text
|
||||
assert "ServiceClass.subsub_name changed to Hello Peepz" in caplog.text
|
||||
service_instance.name = "Peepz"
|
||||
|
||||
assert "'name' changed to 'Peepz'" in caplog.text
|
||||
assert "'sub_name' changed to 'Hello Peepz'" in caplog.text
|
||||
assert "'subsub_name' changed to 'Hello Peepz'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
test_service.class_attr.name = "Hi"
|
||||
service_instance.class_attr.name = "Hi"
|
||||
assert service_instance.subsub_name == "Hello Peepz"
|
||||
|
||||
assert "ServiceClass.sub_name changed to Hi Peepz" in caplog.text
|
||||
assert (
|
||||
"ServiceClass.subsub_name changed to Hello Peepz" in caplog.text
|
||||
) # registers subclass changes
|
||||
assert "ServiceClass.class_attr.name changed to Hi" in caplog.text
|
||||
assert "'sub_name' changed to 'Hi Peepz'" in caplog.text
|
||||
assert "'subsub_name' " not in caplog.text # subsub_name does not depend on change
|
||||
assert "'class_attr.name' changed to 'Hi'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
test_service.class_attr.class_attr.name = "Ciao"
|
||||
service_instance.class_attr.class_attr.name = "Ciao"
|
||||
|
||||
assert (
|
||||
"ServiceClass.sub_name changed to Hi Peepz" in caplog.text
|
||||
) # registers subclass changes
|
||||
assert "ServiceClass.subsub_name changed to Ciao Peepz" in caplog.text
|
||||
assert "ServiceClass.class_attr.class_attr.name changed to Ciao" in caplog.text
|
||||
"'sub_name' changed to" not in caplog.text
|
||||
) # sub_name does not depend on change
|
||||
assert "'subsub_name' changed to 'Ciao Peepz'" in caplog.text
|
||||
assert "'class_attr.class_attr.name' changed to 'Ciao'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
|
||||
@@ -97,17 +103,20 @@ def test_simple_list_properties(caplog: LogCaptureFixture) -> None:
|
||||
def total_name(self) -> str:
|
||||
return f"{self.list[0]} {self.name}"
|
||||
|
||||
test_service = ServiceClass()
|
||||
test_service.name = "Peepz"
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
assert "ServiceClass.name changed to Peepz" in caplog.text
|
||||
assert "ServiceClass.total_name changed to Hello Peepz" in caplog.text
|
||||
service_instance.name = "Peepz"
|
||||
|
||||
assert "'name' changed to 'Peepz'" in caplog.text
|
||||
assert "'total_name' changed to 'Hello Peepz'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
test_service.list[0] = "Hi"
|
||||
service_instance.list[0] = "Hi"
|
||||
|
||||
assert "ServiceClass.total_name changed to Hi Peepz" in caplog.text
|
||||
assert "ServiceClass.list[0] changed to Hi" in caplog.text
|
||||
assert "'total_name' changed to 'Hi Peepz'" in caplog.text
|
||||
assert "'list[0]' changed to 'Hi'" in caplog.text
|
||||
|
||||
|
||||
def test_class_list_properties(caplog: LogCaptureFixture) -> None:
|
||||
@@ -122,23 +131,26 @@ def test_class_list_properties(caplog: LogCaptureFixture) -> None:
|
||||
def total_name(self) -> str:
|
||||
return f"{self.list[0].name} {self.name}"
|
||||
|
||||
test_service = ServiceClass()
|
||||
test_service.name = "Peepz"
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
assert "ServiceClass.name changed to Peepz" in caplog.text
|
||||
assert "ServiceClass.total_name changed to Hello Peepz" in caplog.text
|
||||
service_instance.name = "Peepz"
|
||||
|
||||
assert "'name' changed to 'Peepz'" in caplog.text
|
||||
assert "'total_name' changed to 'Hello Peepz'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
test_service.list[0].name = "Hi"
|
||||
service_instance.list[0].name = "Hi"
|
||||
|
||||
assert "ServiceClass.total_name changed to Hi Peepz" in caplog.text
|
||||
assert "ServiceClass.list[0].name changed to Hi" in caplog.text
|
||||
assert "'total_name' changed to 'Hi Peepz'" in caplog.text
|
||||
assert "'list[0].name' changed to 'Hi'" in caplog.text
|
||||
|
||||
|
||||
def test_subclass_properties(caplog: LogCaptureFixture) -> None:
|
||||
class SubClass(DataService):
|
||||
name = "Hello"
|
||||
_voltage = 10.0
|
||||
_voltage = 11.0
|
||||
_current = 1.0
|
||||
|
||||
@property
|
||||
@@ -168,14 +180,15 @@ def test_subclass_properties(caplog: LogCaptureFixture) -> None:
|
||||
def voltage(self) -> float:
|
||||
return self.class_attr.voltage
|
||||
|
||||
test_service = ServiceClass()
|
||||
test_service.class_attr.voltage = 10.0
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
# using a set here as "ServiceClass.voltage = 10.0" is emitted twice. Once for
|
||||
# changing voltage, and once for changing power.
|
||||
assert "ServiceClass.class_attr.voltage changed to 10.0" in caplog.text
|
||||
assert "ServiceClass.class_attr.power changed to 10.0" in caplog.text
|
||||
assert "ServiceClass.voltage changed to 10.0" in caplog.text
|
||||
service_instance.class_attr.voltage = 10.0
|
||||
|
||||
assert "'class_attr.voltage' changed to '10.0'" in caplog.text
|
||||
assert "'class_attr.power' changed to '10.0'" in caplog.text
|
||||
assert "'voltage' changed to '10.0'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
|
||||
@@ -212,17 +225,20 @@ def test_subclass_properties_2(caplog: LogCaptureFixture) -> None:
|
||||
def voltage(self) -> float:
|
||||
return self.class_attr[0].voltage
|
||||
|
||||
test_service = ServiceClass()
|
||||
test_service.class_attr[1].current = 10.0
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
# using a set here as "ServiceClass.voltage = 10.0" is emitted twice. Once for
|
||||
# changing current, and once for changing power. Note that the voltage property is
|
||||
# only dependent on class_attr[0] but still emits an update notification. This is
|
||||
# because every time any item in the list `test_service.class_attr` is changed,
|
||||
# a notification will be emitted.
|
||||
assert "ServiceClass.class_attr[1].current changed to 10.0" in caplog.text
|
||||
assert "ServiceClass.class_attr[1].power changed to 100.0" in caplog.text
|
||||
assert "ServiceClass.voltage changed to 10.0" in caplog.text
|
||||
service_instance.class_attr[0].current = 10.0
|
||||
|
||||
assert "'class_attr[0].current' changed to '10.0'" in caplog.text
|
||||
assert "'class_attr[0].power' changed to '100.0'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
service_instance.class_attr[0].voltage = 11.0
|
||||
assert "'class_attr[0].voltage' changed to '11.0'" in caplog.text
|
||||
assert "'class_attr[0].power' changed to '110.0'" in caplog.text
|
||||
assert "'voltage' changed to '11.0'" in caplog.text
|
||||
|
||||
|
||||
def test_subsubclass_properties(caplog: LogCaptureFixture) -> None:
|
||||
@@ -252,25 +268,23 @@ def test_subsubclass_properties(caplog: LogCaptureFixture) -> None:
|
||||
def power(self) -> float:
|
||||
return self.class_attr[0].power
|
||||
|
||||
test_service = ServiceClass()
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
test_service.class_attr[1].class_attr.voltage = 100.0
|
||||
assert (
|
||||
"ServiceClass.class_attr[0].class_attr.voltage changed to 100.0" in caplog.text
|
||||
)
|
||||
assert (
|
||||
"ServiceClass.class_attr[1].class_attr.voltage changed to 100.0" in caplog.text
|
||||
)
|
||||
assert "ServiceClass.class_attr[0].power changed to 50.0" in caplog.text
|
||||
assert "ServiceClass.class_attr[1].power changed to 50.0" in caplog.text
|
||||
assert "ServiceClass.power changed to 50.0" in caplog.text
|
||||
service_instance.class_attr[1].class_attr.voltage = 100.0
|
||||
assert "'class_attr[0].class_attr.voltage' changed to '100.0'" in caplog.text
|
||||
assert "'class_attr[1].class_attr.voltage' changed to '100.0'" in caplog.text
|
||||
assert "'class_attr[0].power' changed to '50.0'" in caplog.text
|
||||
assert "'class_attr[1].power' changed to '50.0'" in caplog.text
|
||||
assert "'power' changed to '50.0'" in caplog.text
|
||||
|
||||
|
||||
def test_subsubclass_instance_properties(caplog: LogCaptureFixture) -> None:
|
||||
class SubSubClass(DataService):
|
||||
def __init__(self) -> None:
|
||||
self._voltage = 10.0
|
||||
super().__init__()
|
||||
self._voltage = 10.0
|
||||
|
||||
@property
|
||||
def voltage(self) -> float:
|
||||
@@ -282,9 +296,9 @@ def test_subsubclass_instance_properties(caplog: LogCaptureFixture) -> None:
|
||||
|
||||
class SubClass(DataService):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.attr = [SubSubClass()]
|
||||
self.current = 0.5
|
||||
super().__init__()
|
||||
|
||||
@property
|
||||
def power(self) -> float:
|
||||
@@ -297,12 +311,11 @@ def test_subsubclass_instance_properties(caplog: LogCaptureFixture) -> None:
|
||||
def power(self) -> float:
|
||||
return self.class_attr[0].power
|
||||
|
||||
test_service = ServiceClass()
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
test_service.class_attr[1].attr[0].voltage = 100.0
|
||||
# again, changing an item in a list will trigger the callbacks. This is why a
|
||||
# notification for `ServiceClass.power` is emitted although it did not change its
|
||||
# value
|
||||
assert "ServiceClass.class_attr[1].attr[0].voltage changed to 100.0" in caplog.text
|
||||
assert "ServiceClass.class_attr[1].power changed to 50.0" in caplog.text
|
||||
assert "ServiceClass.power changed to 5.0" in caplog.text
|
||||
service_instance.class_attr[0].attr[0].voltage = 100.0
|
||||
assert "'class_attr[0].attr[0].voltage' changed to '100.0'" in caplog.text
|
||||
assert "'class_attr[0].power' changed to '50.0'" in caplog.text
|
||||
assert "'power' changed to '50.0'" in caplog.text
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
from typing import Any
|
||||
|
||||
from pytest import LogCaptureFixture
|
||||
|
||||
import pydase.units as u
|
||||
from pydase.data_service.data_service import DataService
|
||||
from pydase.data_service.data_service_observer import DataServiceObserver
|
||||
from pydase.data_service.state_manager import StateManager
|
||||
from pytest import LogCaptureFixture
|
||||
|
||||
|
||||
def test_DataService_setattr(caplog: LogCaptureFixture) -> None:
|
||||
@@ -19,26 +20,28 @@ def test_DataService_setattr(caplog: LogCaptureFixture) -> None:
|
||||
def current(self, value: Any) -> None:
|
||||
self._current = value
|
||||
|
||||
service = ServiceClass()
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
# You can just set floats to the Quantity objects. The DataService __setattr__ will
|
||||
# automatically convert this
|
||||
service.voltage = 10.0 # type: ignore
|
||||
service.current = 1.5
|
||||
service_instance.voltage = 10.0 * u.units.V
|
||||
service_instance.current = 1.5 * u.units.mA
|
||||
|
||||
assert service.voltage == 10.0 * u.units.V # type: ignore
|
||||
assert service.current == 1.5 * u.units.mA
|
||||
assert "'voltage' changed to '10.0 V'" in caplog.text
|
||||
assert "'current' changed to '1.5 mA'" in caplog.text
|
||||
|
||||
assert "ServiceClass.voltage changed to 10.0 V" in caplog.text
|
||||
assert "ServiceClass.current changed to 1.5 mA" in caplog.text
|
||||
assert service_instance.voltage == 10.0 * u.units.V
|
||||
assert service_instance.current == 1.5 * u.units.mA
|
||||
caplog.clear()
|
||||
|
||||
service.voltage = 12.0 * u.units.V # type: ignore
|
||||
service.current = 1.51 * u.units.A
|
||||
assert service.voltage == 12.0 * u.units.V # type: ignore
|
||||
assert service.current == 1.51 * u.units.A
|
||||
service_instance.voltage = 12.0 * u.units.V
|
||||
service_instance.current = 1.51 * u.units.A
|
||||
|
||||
assert "ServiceClass.voltage changed to 12.0 V" in caplog.text
|
||||
assert "ServiceClass.current changed to 1.51 A" in caplog.text
|
||||
assert "'voltage' changed to '12.0 V'" in caplog.text
|
||||
assert "'current' changed to '1.51 A'" in caplog.text
|
||||
|
||||
assert service_instance.voltage == 12.0 * u.units.V
|
||||
assert service_instance.current == 1.51 * u.units.A
|
||||
|
||||
|
||||
def test_convert_to_quantity() -> None:
|
||||
@@ -48,7 +51,7 @@ def test_convert_to_quantity() -> None:
|
||||
assert u.convert_to_quantity(1.0 * u.units.mV) == 1.0 * u.units.mV
|
||||
|
||||
|
||||
def test_update_DataService_attribute(caplog: LogCaptureFixture) -> None:
|
||||
def test_set_service_attribute_value_by_path(caplog: LogCaptureFixture) -> None:
|
||||
class ServiceClass(DataService):
|
||||
voltage = 1.0 * u.units.V
|
||||
_current: u.Quantity = 1.0 * u.units.mA
|
||||
@@ -61,23 +64,25 @@ def test_update_DataService_attribute(caplog: LogCaptureFixture) -> None:
|
||||
def current(self, value: Any) -> None:
|
||||
self._current = value
|
||||
|
||||
service = ServiceClass()
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
service.update_DataService_attribute(
|
||||
path_list=[], attr_name="voltage", value=1.0 * u.units.mV
|
||||
state_manager.set_service_attribute_value_by_path(
|
||||
path="voltage", value=1.0 * u.units.mV
|
||||
)
|
||||
assert "'voltage' changed to '1.0 mV'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
assert "ServiceClass.voltage changed to 1.0 mV" in caplog.text
|
||||
state_manager.set_service_attribute_value_by_path(path="voltage", value=2)
|
||||
|
||||
service.update_DataService_attribute(path_list=[], attr_name="voltage", value=2)
|
||||
assert "'voltage' changed to '2.0 mV'" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
assert "ServiceClass.voltage changed to 2.0 mV" in caplog.text
|
||||
|
||||
service.update_DataService_attribute(
|
||||
path_list=[], attr_name="voltage", value={"magnitude": 123, "unit": "kV"}
|
||||
state_manager.set_service_attribute_value_by_path(
|
||||
path="voltage", value={"magnitude": 123, "unit": "kV"}
|
||||
)
|
||||
|
||||
assert "ServiceClass.voltage changed to 123.0 kV" in caplog.text
|
||||
assert "'voltage' changed to '123.0 kV'" in caplog.text
|
||||
|
||||
|
||||
def test_autoconvert_offset_to_baseunit() -> None:
|
||||
@@ -104,9 +109,9 @@ def test_loading_from_json(caplog: LogCaptureFixture) -> None:
|
||||
}
|
||||
|
||||
class ServiceClass(DataService):
|
||||
def __init__(self):
|
||||
self._unit: u.Quantity = 1 * u.units.A
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self._unit: u.Quantity = 1 * u.units.A
|
||||
|
||||
@property
|
||||
def some_unit(self) -> u.Quantity:
|
||||
@@ -117,8 +122,10 @@ def test_loading_from_json(caplog: LogCaptureFixture) -> None:
|
||||
assert isinstance(value, u.Quantity)
|
||||
self._unit = value
|
||||
|
||||
service = ServiceClass()
|
||||
service_instance = ServiceClass()
|
||||
state_manager = StateManager(service_instance)
|
||||
DataServiceObserver(state_manager)
|
||||
|
||||
service.load_DataService_from_JSON(JSON_DICT)
|
||||
service_instance.load_DataService_from_JSON(JSON_DICT)
|
||||
|
||||
assert "ServiceClass.some_unit changed to 10.0 A" in caplog.text
|
||||
assert "'some_unit' changed to '10.0 A'" in caplog.text
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import toml
|
||||
|
||||
import pydase.version
|
||||
import toml
|
||||
|
||||
|
||||
def test_project_version() -> None:
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import pytest
|
||||
|
||||
from pydase.utils.helpers import is_property_attribute
|
||||
import pytest
|
||||
from pydase.utils.helpers import (
|
||||
is_property_attribute,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import logging
|
||||
|
||||
from pytest import LogCaptureFixture
|
||||
|
||||
from pydase.utils.logging import setup_logging
|
||||
from pytest import LogCaptureFixture
|
||||
|
||||
|
||||
def test_log_error(caplog: LogCaptureFixture):
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user