From d106109f6c737b58a0b35206a509f4a0a67ed2e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Fr=C3=B6jdh?= Date: Mon, 16 Jan 2023 13:33:11 +0100 Subject: [PATCH] fixed hostname not split (+) in python (#609) * fixed hostname not split (+) in python * also for rx_hostname --- python/slsdet/detector.py | 13 +++++-------- python/slsdet/utils.py | 19 ++++++++++++++++++- python/tests/test_utils.py | 31 ++++++++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/python/slsdet/detector.py b/python/slsdet/detector.py index 42346a8bc..efd08cc78 100755 --- a/python/slsdet/detector.py +++ b/python/slsdet/detector.py @@ -14,7 +14,7 @@ streamingInterface = slsDetectorDefs.streamingInterface defs = slsDetectorDefs from .utils import element_if_equal, all_equal, get_set_bits, list_to_bitmask -from .utils import Geometry, to_geo, element, reduce_time, is_iterable +from .utils import Geometry, to_geo, element, reduce_time, is_iterable, hostname_list from _slsdet import xy from . import utils as ut from .proxy import JsonProxy, SlowAdcProxy, ClkDivProxy, MaxPhaseProxy, ClkFreqProxy, PatLoopProxy, PatNLoopProxy, PatWaitProxy, PatWaitTimeProxy @@ -162,12 +162,8 @@ class Detector(CppDetectorApi): @hostname.setter def hostname(self, hostnames): - if isinstance(hostnames, str): - hostnames = [hostnames] - if isinstance(hostnames, list): - self.setHostname(hostnames) - else: - raise ValueError("hostname needs to be string or list of strings") + args = hostname_list(hostnames) + self.setHostname(args) @property @@ -784,7 +780,8 @@ class Detector(CppDetectorApi): @rx_hostname.setter def rx_hostname(self, hostname): - self.setRxHostname(hostname) + args = hostname_list(hostname) + self.setRxHostname(args) @property @element diff --git a/python/slsdet/utils.py b/python/slsdet/utils.py index afa9f59a2..60bbbb2a7 100755 --- a/python/slsdet/utils.py +++ b/python/slsdet/utils.py @@ -260,4 +260,21 @@ def merge_args(*args): return (ret,) else: - raise ValueError("Multiple dictionaries passes cannot merge args") \ No newline at end of file + raise ValueError("Multiple dictionaries passes cannot merge args") + + +def hostname_list(args): + """ + Generates a list from a hostname string + * Lists are passed through + * as are tuples (conversion in pybind11 to vector) + * if + is found it splits the string + """ + if isinstance(args, (list, tuple)): + return args + elif(isinstance(args, str)): + hosts = args.split('+') + hosts = [it for it in hosts if len(it)] + return hosts + else: + raise ValueError("hostname needs to be string or list of strings") diff --git a/python/tests/test_utils.py b/python/tests/test_utils.py index 1dd8dd152..92a32106f 100755 --- a/python/tests/test_utils.py +++ b/python/tests/test_utils.py @@ -341,4 +341,33 @@ def test_merge_args_tuple(): assert merge_args(*("a", "b"), 5) == ("a", "b", 5) def test_merge_args_dict_with_tuple(): - assert merge_args({0: (1,2)}, 3) == ({0: (1,2,3)},) \ No newline at end of file + assert merge_args({0: (1,2)}, 3) == ({0: (1,2,3)},) + + +def test_hostname_to_list(): + s = "localhost" + r = hostname_list(s) + assert r == [s] + +def test_hostname_to_list_passthrough(): + args = ["localhost"] + ret = hostname_list(args) + assert ret == args + + args = ("localhost",) + ret = hostname_list(args) + assert ret == args + +def test_splitting_hostname(): + args = 'apple+banana+pear+' + ret = hostname_list(args) + assert ret == ['apple', 'banana', 'pear'] + + #not sensitive to trailing + + args = 'apple+banana+pear' + ret = hostname_list(args) + assert ret == ['apple', 'banana', 'pear'] + +def test_hostame_throws_on_wrong_args(): + with pytest.raises(Exception) as e: + hostname_list(5)