|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import sys |
| 4 | +from abc import ABC, abstractmethod |
| 5 | +from types import TracebackType |
| 6 | +from typing import Optional, Type, TYPE_CHECKING |
| 7 | + |
| 8 | +from ni.pythonpanel.v1.python_panel_service_pb2_grpc import PythonPanelServiceStub |
| 9 | + |
| 10 | +if TYPE_CHECKING: |
| 11 | + if sys.version_info >= (3, 11): |
| 12 | + from typing import Self |
| 13 | + else: |
| 14 | + from typing_extensions import Self |
| 15 | + |
| 16 | + |
| 17 | +class Panel(ABC): |
| 18 | + """This class allows you to connect to a panel and specify values for its controls.""" |
| 19 | + |
| 20 | + _stub: PythonPanelServiceStub | None |
| 21 | + _panel_id: str |
| 22 | + _panel_uri: str |
| 23 | + |
| 24 | + __slots__ = ["_stub", "_panel_id", "_panel_uri", "__weakref__"] |
| 25 | + |
| 26 | + def __init__(self, panel_id: str, panel_uri: str) -> None: |
| 27 | + """Initialize the panel.""" |
| 28 | + self._panel_id = panel_id |
| 29 | + self._panel_uri = panel_uri |
| 30 | + |
| 31 | + @property |
| 32 | + def panel_id(self) -> str: |
| 33 | + """Read-only accessor for the panel ID.""" |
| 34 | + return self._panel_id |
| 35 | + |
| 36 | + @property |
| 37 | + def panel_uri(self) -> str: |
| 38 | + """Read-only accessor for the panel URI.""" |
| 39 | + return self._panel_uri |
| 40 | + |
| 41 | + def __enter__(self) -> Self: |
| 42 | + """Enter the runtime context related to this object.""" |
| 43 | + self.connect() |
| 44 | + return self |
| 45 | + |
| 46 | + def __exit__( |
| 47 | + self, |
| 48 | + exctype: Optional[Type[BaseException]], |
| 49 | + excinst: Optional[BaseException], |
| 50 | + exctb: Optional[TracebackType], |
| 51 | + ) -> Optional[bool]: |
| 52 | + """Exit the runtime context related to this object.""" |
| 53 | + self.disconnect() |
| 54 | + return None |
| 55 | + |
| 56 | + def connect(self) -> None: |
| 57 | + """Connect to the panel and open it.""" |
| 58 | + # TODO: AB#3095680 - Use gRPC pool management, create the _stub, and call _stub.Connect |
| 59 | + self._resolve_service_location() |
| 60 | + |
| 61 | + def disconnect(self) -> None: |
| 62 | + """Disconnect from the panel (does not close the panel).""" |
| 63 | + # TODO: AB#3095680 - Use gRPC pool management, call _stub.Disconnect |
| 64 | + pass |
| 65 | + |
| 66 | + def get_value(self, value_id: str) -> object: |
| 67 | + """Get the value for a control on the panel. |
| 68 | +
|
| 69 | + Args: |
| 70 | + value_id: The id of the value |
| 71 | +
|
| 72 | + Returns: |
| 73 | + The value |
| 74 | + """ |
| 75 | + # TODO: AB#3095681 - get the Any from _stub.GetValue and convert it to the correct type |
| 76 | + return "placeholder value" |
| 77 | + |
| 78 | + def set_value(self, value_id: str, value: object) -> None: |
| 79 | + """Set the value for a control on the panel. |
| 80 | +
|
| 81 | + Args: |
| 82 | + value_id: The id of the value |
| 83 | + value: The value |
| 84 | + """ |
| 85 | + # TODO: AB#3095681 - Convert the value to an Any and pass it to _stub.SetValue |
| 86 | + pass |
| 87 | + |
| 88 | + @abstractmethod |
| 89 | + def _resolve_service_location(self) -> str: |
| 90 | + """Resolve the service location for the panel.""" |
| 91 | + raise NotImplementedError |
0 commit comments