Skip to content

Commit 52116f9

Browse files
committed
Remote API: new get_object_shapes method
1 parent dc49c84 commit 52116f9

3 files changed

Lines changed: 71 additions & 0 deletions

File tree

cdlclient/baseproxy.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,28 @@ def get_object_uuids(self, panel: str | None = None) -> list[str]:
250250
ValueError: if panel not found
251251
"""
252252

253+
@abc.abstractmethod
254+
def get_object_shapes(
255+
self,
256+
index: int | None = None,
257+
group_index: int | None = None,
258+
panel: str | None = None,
259+
) -> list:
260+
"""Get plot item shapes associated to object (signal/image).
261+
262+
Args:
263+
index: Object index in current panel. Defaults to None.
264+
group_index: Group index. Defaults to None.
265+
panel: Panel name. Defaults to None.
266+
267+
If ``index`` is not specified, returns the currently selected object.
268+
If ``group_index`` is not specified, return an object from the current group.
269+
If ``panel`` is not specified, return an object from the current panel.
270+
271+
Returns:
272+
List of plot item shapes
273+
"""
274+
253275
@abc.abstractmethod
254276
def add_annotations_from_items(
255277
self, items: list, refresh_plot: bool = True, panel: str | None = None

cdlclient/remote.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import configparser as cp
2020
import importlib
21+
import json
2122
import os
2223
import os.path as osp
2324
import sys
@@ -167,6 +168,27 @@ def items_to_json(items: list) -> str | None:
167168
return None
168169

169170

171+
def json_to_items(json_str: str | None) -> list:
172+
"""Convert JSON string to plot items.
173+
174+
Args:
175+
json_str (str): JSON string or None
176+
177+
Returns:
178+
list: list of plot items
179+
"""
180+
from plotpy.io import load_items # pylint: disable=import-outside-toplevel
181+
182+
items = []
183+
if json_str:
184+
try:
185+
for item in load_items(JSONReader(json_str)):
186+
items.append(item)
187+
except json.decoder.JSONDecodeError:
188+
pass
189+
return items
190+
191+
170192
class SimpleRemoteProxy(SimpleBaseProxy):
171193
"""Object representing a proxy/client to DataLab XML-RPC server.
172194
This object is used to call DataLab functions from a Python script.
@@ -355,6 +377,29 @@ def calc(self, name: str, param: gds.DataSet | None = None) -> gds.DataSet:
355377
return self._cdl.calc(name)
356378
return self._cdl.calc(name, dataset_to_json(param))
357379

380+
def get_object_shapes(
381+
self,
382+
index: int | None = None,
383+
group_index: int | None = None,
384+
panel: str | None = None,
385+
) -> list:
386+
"""Get plot item shapes associated to object (signal/image).
387+
388+
Args:
389+
index: Object index in current panel. Defaults to None.
390+
group_index: Group index. Defaults to None.
391+
panel: Panel name. Defaults to None.
392+
393+
If ``index`` is not specified, returns the currently selected object.
394+
If ``group_index`` is not specified, return an object from the current group.
395+
If ``panel`` is not specified, return an object from the current panel.
396+
397+
Returns:
398+
List of plot item shapes
399+
"""
400+
items_json = self._cdl.get_object_shapes(index, group_index, panel)
401+
return json_to_items(items_json)
402+
358403
def add_annotations_from_items(
359404
self, items: list, refresh_plot: bool = True, panel: str | None = None
360405
) -> None:

cdlclient/tests/remoteclient_unit.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,12 @@ def multiple_commands(remote: SimpleRemoteProxy):
4949
z = np.random.rand(200, 200)
5050
remote.add_image("toto", z)
5151
rect = make.annotated_rectangle(100, 100, 200, 200, title="Test")
52+
area = rect.get_rect()
5253
remote.add_annotations_from_items([rect])
5354
uuid = remote.get_sel_object_uuids()[0]
55+
items = remote.get_object_shapes()
56+
assert len(items) == 1 and items[0].get_rect() == area
57+
execenv.print("OK")
5458
remote.add_label_with_title(f"Image uuid: {uuid}")
5559
remote.select_groups([0])
5660
remote.select_objects([uuid])

0 commit comments

Comments
 (0)