diff --git a/pytac/device.py b/pytac/device.py index 8181d3bc..631988e7 100644 --- a/pytac/device.py +++ b/pytac/device.py @@ -6,12 +6,7 @@ magnets and a skew quadrupole. """ import pytac - - -class DeviceException(Exception): - """Exception associated with Device misconfiguration or invalid requests. - """ - pass +from pytac.exceptions import HandleException class Device(object): @@ -70,10 +65,10 @@ def set_value(self, value): value (float): The value to set on the PV. Raises: - DeviceException: if no setpoint PV exists. + HandleException: if no setpoint PV exists. """ if self.sp_pv is None: - raise DeviceException( + raise HandleException( "Device {0} has no setpoint PV.".format(self.name) ) self._cs.put(self.sp_pv, value) @@ -89,7 +84,7 @@ def get_value(self, handle): float: The value of the PV. Raises: - DeviceException: if the requested PV doesn't exist. + HandleException: if the requested PV doesn't exist. """ print('getting {}'.format('handle')) if handle == pytac.RB and self.rb_pv: @@ -97,7 +92,7 @@ def get_value(self, handle): elif handle == pytac.SP and self.sp_pv: return self._cs.get(self.sp_pv) - raise DeviceException( + raise HandleException( "Device {0} has no {1} PV.".format(self.name, handle) ) @@ -111,14 +106,14 @@ def get_pv_name(self, handle): str: A readback or setpoint PV. Raises: - DeviceException: if the PV doesn't exist. + HandleException: if the PV doesn't exist. """ if handle == pytac.RB and self.rb_pv: return self.rb_pv elif handle == pytac.SP and self.sp_pv: return self.sp_pv - raise DeviceException( + raise HandleException( "Device {0} has no {1} PV.".format(self.name, handle) ) diff --git a/pytac/element.py b/pytac/element.py index 47401473..8b4342d9 100644 --- a/pytac/element.py +++ b/pytac/element.py @@ -1,6 +1,6 @@ """Module containing the element class.""" import pytac -from pytac.device import DeviceException +from pytac.exceptions import FieldException, HandleException, DeviceException class Element(object): @@ -161,6 +161,7 @@ def get_value(self, field, handle=pytac.RB, units=pytac.ENG, Raises: DeviceException: if there is no device on the given field. + FieldException: if the element does not have the specified field. """ try: model = self._models[model] @@ -170,6 +171,8 @@ def get_value(self, field, handle=pytac.RB, units=pytac.ENG, except KeyError: raise DeviceException('No model type {} on element {}'.format(model, self)) + except FieldException: + raise FieldException('No field {} on element {}'.format(field, self)) def set_value(self, field, value, handle=pytac.SP, units=pytac.ENG, model=pytac.LIVE): @@ -187,16 +190,22 @@ def set_value(self, field, value, handle=pytac.SP, units=pytac.ENG, Raises: DeviceException: if arguments are incorrect. + FieldException: if the element does not have the specified field. """ if handle != pytac.SP: - raise DeviceException('Must write using {}'.format(pytac.SP)) + raise HandleException('Must write using {}'.format(pytac.SP)) try: model = self._models[model] - value = self._uc[field].convert(value, origin=units, target=model.units) - model.set_value(field, value) except KeyError: raise DeviceException('No model type {} on element {}'.format(model, self)) + try: + value = self._uc[field].convert(value, origin=units, target=model.units) + model.set_value(field, value) + except KeyError: + raise FieldException('No field {} on element {}'.format(model, self)) + except FieldException: + raise FieldException('No field {} on element {}'.format(field, self)) def get_pv_name(self, field, handle): """Get a PV name on a device. @@ -210,9 +219,12 @@ def get_pv_name(self, field, handle): Raises: DeviceException: if there is no device for this field. + FieldException: if the element does not have the specified field. """ try: return self._models[pytac.LIVE].get_pv_name(field, handle) except KeyError: raise DeviceException('{} has no device for field {}'.format(self, field)) + except FieldException: + raise FieldException('No field {} on element {}'.format(field, self)) diff --git a/pytac/exceptions.py b/pytac/exceptions.py new file mode 100644 index 00000000..60f67e8b --- /dev/null +++ b/pytac/exceptions.py @@ -0,0 +1,29 @@ +"""Module containing all the exceptions used in pytac.""" + + +class FieldException(Exception): + """Exception associated with invalid field requests. + """ + pass + + +class HandleException(Exception): + """Exception associated with requests with invalid handles. + """ + pass + + +class DeviceException(Exception): + """Exception associated with Device misconfiguration or invalid requests. + """ + pass + + +class UnitsException(Exception): + """Conversion not understood + """ + pass + + +class LatticeException(Exception): + pass diff --git a/pytac/lattice.py b/pytac/lattice.py index 021e524b..f0585369 100644 --- a/pytac/lattice.py +++ b/pytac/lattice.py @@ -2,10 +2,7 @@ machine. """ import numpy - - -class LatticeException(Exception): - pass +from pytac.exceptions import LatticeException class Lattice(object): diff --git a/pytac/model.py b/pytac/model.py index 5b9f720f..475f433e 100644 --- a/pytac/model.py +++ b/pytac/model.py @@ -1,5 +1,6 @@ """Module containing pytac model classes.""" import pytac +from pytac.exceptions import FieldException class Model(object): @@ -116,8 +117,14 @@ def get_value(self, field, handle): Returns: float: The value of the PV. + + Raises: + FieldException: if the device does not have the specified field. """ - return self._devices[field].get_value(handle) + try: + return self._devices[field].get_value(handle) + except KeyError: + raise FieldException('No field {} on device {}'.format(field, self)) def set_value(self, field, value): """Set the value of a readback or setpoint PV for a field from the @@ -126,5 +133,11 @@ def set_value(self, field, value): Args: field (str): field for the requested value. value (float): The value to set on the PV. + + Raises: + FieldException: if the device does not have the specified field. """ - self._devices[field].set_value(value) + try: + self._devices[field].set_value(value) + except KeyError: + raise FieldException('No field {} on device {}'.format(field, self)) diff --git a/pytac/units.py b/pytac/units.py index 6ce60ac5..a70e6f4c 100644 --- a/pytac/units.py +++ b/pytac/units.py @@ -1,15 +1,10 @@ """Classes for use in unit conversion.""" import pytac import numpy +from pytac.exceptions import UnitsException from scipy.interpolate import PchipInterpolator -class UnitsException(Exception): - """Conversion not understood - """ - pass - - def unit_function(value): """Default value for the pre and post functions used in unit conversion. diff --git a/test/test_device.py b/test/test_device.py index a21335c7..8bfb3370 100644 --- a/test/test_device.py +++ b/test/test_device.py @@ -22,13 +22,13 @@ def test_set_device_value(): def test_device_invalid_sp_raise_exception(): device2 = create_device(PREFIX, RB_PV, None) - with pytest.raises(pytac.device.DeviceException): + with pytest.raises(pytac.exceptions.HandleException): device2.set_value(40) def test_get_device_value(): device = create_device() - with pytest.raises(pytac.device.DeviceException): + with pytest.raises(pytac.exceptions.HandleException): device.get_value('non_existent') diff --git a/test/test_element.py b/test/test_element.py index 93b09dad..897d9eb8 100644 --- a/test/test_element.py +++ b/test/test_element.py @@ -106,16 +106,22 @@ def test_set_value_phys(test_element): (DUMMY_VALUE_2 / 2)) -def test_set_value_incorrect_field(test_element): - with pytest.raises(pytac.device.DeviceException): - test_element.set_value('non_existent', 40.0) - - -def test_get_pv_exceptions(test_element): - with pytest.raises(pytac.device.DeviceException): - test_element.get_value('setpoint', 'unknown_field') - with pytest.raises(pytac.device.DeviceException): - test_element.get_value('unknown_handle', 'y') +def test_set_exceptions(test_element): + with pytest.raises(pytac.exceptions.FieldException): + test_element.set_value('unknown_field', 40.0, 'setpoint') + with pytest.raises(pytac.exceptions.HandleException): + test_element.set_value('y', 40.0, 'unknown_handle') + with pytest.raises(pytac.exceptions.DeviceException): + test_element.set_value('y', 40.0, 'setpoint', model='unknown_model') + + +def test_get_exceptions(test_element): + with pytest.raises(pytac.exceptions.FieldException): + test_element.get_value('unknown_field', 'setpoint') + with pytest.raises(pytac.exceptions.HandleException): + test_element.get_value('y', 'unknown_handle') + with pytest.raises(pytac.exceptions.DeviceException): + test_element.get_value('y', 'setpoint', model='unknown_model') def test_identity_conversion(): diff --git a/test/test_lattice.py b/test/test_lattice.py index 576fa9b1..950c97af 100644 --- a/test/test_lattice.py +++ b/test/test_lattice.py @@ -129,7 +129,7 @@ def test_set_values(simple_element_and_lattice): def test_set_values_raise_exception_if_number_of_values_does_not_match(simple_element_and_lattice): element, lattice = simple_element_and_lattice - with pytest.raises(pytac.lattice.LatticeException): + with pytest.raises(pytac.exceptions.LatticeException): lattice.set_values('family', 'x', [1, 2]) @@ -149,7 +149,7 @@ def test_s_position(simple_element_and_lattice): def test_get_s_throws_exception_if_element_not_in_lattice(): lat = pytac.lattice.Lattice(LATTICE, mock.MagicMock(), 1) element = pytac.element.Element(1, 1.0, 'Quad') - with pytest.raises(pytac.lattice.LatticeException): + with pytest.raises(pytac.exceptions.LatticeException): lat.get_s(element) diff --git a/test/test_machine.py b/test/test_machine.py index 6cb40a7d..d37ecc3e 100644 --- a/test/test_machine.py +++ b/test/test_machine.py @@ -67,7 +67,7 @@ def test_load_bpms(ring_mode, n_bpms): for bpm in bpms: assert set(bpm.get_fields()) == bpm_fields assert re.match('SR.*BPM.*X', bpm.get_pv_name('x', pytac.RB)) - with pytest.raises(pytac.device.DeviceException): + with pytest.raises(pytac.exceptions.HandleException): bpm.get_pv_name('x', pytac.SP) assert len(bpms) == n_bpms assert bpms[0].cell == 1 @@ -184,7 +184,7 @@ def test_quad_unitconv(): def test_quad_unitconv_raise_exception(): uc = pytac.units.PchipUnitConv([50.0, 100.0, 180.0], [-4.95, -9.85, -17.56]) - with pytest.raises(pytac.units.UnitsException): + with pytest.raises(pytac.exceptions.UnitsException): uc.phys_to_eng(-0.7) diff --git a/test/test_units.py b/test/test_units.py index 35782ffd..9bad8a84 100644 --- a/test/test_units.py +++ b/test/test_units.py @@ -1,6 +1,7 @@ import pytest import pytac -from pytac.units import UnitConv, PolyUnitConv, PchipUnitConv, UnitsException +from pytac.units import UnitConv, PolyUnitConv, PchipUnitConv +from pytac.exceptions import UnitsException import numpy