diff --git a/python/slsdet/detector.py b/python/slsdet/detector.py index bfbaa52fb..39dab3d70 100755 --- a/python/slsdet/detector.py +++ b/python/slsdet/detector.py @@ -95,6 +95,7 @@ class Detector(CppDetectorApi): @config.setter def config(self, fname): + fname = ut.make_string_path(fname) self.loadConfig(fname) @property @@ -119,6 +120,7 @@ class Detector(CppDetectorApi): @parameters.setter def parameters(self, fname): + fname = ut.make_string_path(fname) self.loadParameters(fname) @property @@ -359,6 +361,7 @@ class Detector(CppDetectorApi): @fpath.setter def fpath(self, path): + path = ut.make_string_path(path) self.setFilePath(path) @property @@ -589,6 +592,7 @@ class Detector(CppDetectorApi): @trimbits.setter def trimbits(self, fname): + fname = ut.make_string_path(fname) self.loadTrimbits(fname) @property @@ -1040,6 +1044,7 @@ class Detector(CppDetectorApi): @pattern.setter def pattern(self, fname): + fname = ut.make_string_path(fname) self.setPattern(fname) @property diff --git a/python/slsdet/utils.py b/python/slsdet/utils.py index 405ad6230..e9230f0bd 100755 --- a/python/slsdet/utils.py +++ b/python/slsdet/utils.py @@ -8,6 +8,8 @@ from collections import namedtuple import _slsdet #C++ lib import functools import datetime as dt +import pathlib +import os Geometry = namedtuple('Geometry', ['x', 'y']) @@ -77,6 +79,7 @@ def element(func): return element_if_equal(func(self, *args, **kwargs)) return wrapper + def eiger_register_to_time(register): """ Decode register value and return time in s. Values are stored in @@ -93,3 +96,15 @@ def make_timedelta(t): return t else: return dt.timedelta(seconds=t) + +def make_string_path(path): + """ + Accepts either a pathlib.Path or a string, expands ~ to user and convert + Path to str + """ + if isinstance(path, pathlib.Path): + return path.expanduser().as_posix() + elif isinstance(path, str): + return os.path.expanduser(path) + else: + raise ValueError("Cannot convert argument to posix path") \ No newline at end of file diff --git a/python/unit-tests/__pycache__/dac_test.cpython-37-PYTEST.pyc b/python/tests/__pycache__/dac_test.cpython-37-PYTEST.pyc similarity index 100% rename from python/unit-tests/__pycache__/dac_test.cpython-37-PYTEST.pyc rename to python/tests/__pycache__/dac_test.cpython-37-PYTEST.pyc diff --git a/python/unit-tests/__pycache__/detector_test.cpython-37-PYTEST.pyc b/python/tests/__pycache__/detector_test.cpython-37-PYTEST.pyc similarity index 100% rename from python/unit-tests/__pycache__/detector_test.cpython-37-PYTEST.pyc rename to python/tests/__pycache__/detector_test.cpython-37-PYTEST.pyc diff --git a/python/unit-tests/__pycache__/test_detector_property.cpython-37-PYTEST.pyc b/python/tests/__pycache__/test_detector_property.cpython-37-PYTEST.pyc similarity index 100% rename from python/unit-tests/__pycache__/test_detector_property.cpython-37-PYTEST.pyc rename to python/tests/__pycache__/test_detector_property.cpython-37-PYTEST.pyc diff --git a/python/unit-tests/__pycache__/test_utils.cpython-37-PYTEST.pyc b/python/tests/__pycache__/test_utils.cpython-37-PYTEST.pyc similarity index 100% rename from python/unit-tests/__pycache__/test_utils.cpython-37-PYTEST.pyc rename to python/tests/__pycache__/test_utils.cpython-37-PYTEST.pyc diff --git a/python/unit-tests/dac_test.py b/python/tests/dac_test.py similarity index 100% rename from python/unit-tests/dac_test.py rename to python/tests/dac_test.py diff --git a/python/unit-tests/detector_eiger.py b/python/tests/detector_eiger.py similarity index 100% rename from python/unit-tests/detector_eiger.py rename to python/tests/detector_eiger.py diff --git a/python/unit-tests/detector_test.py b/python/tests/detector_test.py similarity index 100% rename from python/unit-tests/detector_test.py rename to python/tests/detector_test.py diff --git a/python/unit-tests/test_detector_property.py b/python/tests/test_detector_property.py similarity index 100% rename from python/unit-tests/test_detector_property.py rename to python/tests/test_detector_property.py diff --git a/python/unit-tests/test_utils.py b/python/tests/test_utils.py similarity index 81% rename from python/unit-tests/test_utils.py rename to python/tests/test_utils.py index 5850aa4a4..862e7b463 100755 --- a/python/unit-tests/test_utils.py +++ b/python/tests/test_utils.py @@ -7,6 +7,7 @@ Testing functions from utils.py import pytest from slsdet.utils import * import datetime as dt +import pathlib def test_iterable(): assert is_iterable(5) == False @@ -93,4 +94,23 @@ def test_make_timedelta_from_timedelta(): t = dt.timedelta(minutes=1) r = make_timedelta(t) assert 60 == r.total_seconds() - assert r == dt.timedelta(minutes=1) \ No newline at end of file + assert r == dt.timedelta(minutes=1) + + +def test_make_string_path_from_Path(): + pathstr = "/some/temp/path" + p = pathlib.Path(pathstr) + r = make_string_path(p) + assert isinstance(r, str) + assert r == p.as_posix() + assert r == pathstr + +def test_make_string_path_expand_user(): + pathstr = "~/tmp/virtual.config" + home = pathlib.Path.home() + expanded_str = pathstr.replace('~', home.as_posix()) + p = pathlib.Path(pathstr) + rp = make_string_path(p) + rs = make_string_path(pathstr) + assert rp == expanded_str + assert rs == expanded_str \ No newline at end of file