Skip to content
This repository was archived by the owner on Nov 23, 2025. It is now read-only.

Commit f02b1d8

Browse files
committed
* Moving get_compatible_pins function to be defined at the UOSDevice level.
This is justified by the same reason as moving the ``get_pin``. * ``UOSDevice.device`` set protected to avoid confusion and misuse. This is primarily for internal use and where it is required for clients getter methods such as ``get_pin`` should be provided.
1 parent f7453f7 commit f02b1d8

5 files changed

Lines changed: 61 additions & 46 deletions

File tree

CHANGES.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@ Version 0.6.0
33

44
:Date: TBC
55

6-
* Moving get_pin function to be defined at the UOSDevice level.
6+
* Moving ``get_pin`` function to be defined at the UOSDevice level.
77
This is a client-facing function and low level devices aren't client-facing.
88
Also set ``pin_index`` argument to be ``pin`` so this is more consistent
99
with the other API functions.
1010
* ``Device._pins`` un-protected as this was purely to promote use of get_pin.
1111
* ``Device.get_pins`` removed as this is not required if pins is not protected.
12+
* Moving ``get_compatible_pins`` function to be defined at the UOSDevice level.
13+
This is justified by the same reason as moving the ``get_pin``.
14+
* ``UOSDevice.device`` set protected to avoid confusion and misuse.
15+
This is primarily for internal use and where it is required for clients
16+
getter methods such as ``get_pin`` should be provided.
1217

1318
Version 0.5.0
1419
-------------

tests/test_api.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ def test_context_manager(uos_identities: dict):
101101

102102

103103
@pytest.mark.parametrize("function", UOSFunctions.enumerate_functions())
104-
def test_device_function(uos_device, function: UOSFunction):
104+
def test_device_function(uos_device: UOSDevice, function: UOSFunction):
105105
"""Checks the UOS functions respond correctly."""
106106
for volatility in Persistence:
107-
if volatility not in uos_device.device.functions_enabled[function.name]:
107+
if volatility not in uos_device.get_functions_enabled()[function.name]:
108108
continue # Ignore unsupported volatilities for device
109-
pins_indices = uos_device.device.get_compatible_pins(function)
109+
pins_indices = uos_device.get_compatible_pins(function)
110110
for pin_index in pins_indices:
111111
api_function = getattr(uos_device, function.name)
112112
call_arguments = {}

tests/test_devices.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ def test_get_compatible_pins(uos_device: UOSDevice):
1414
UOSFunctions.get_gpio_input,
1515
UOSFunctions.get_adc_input,
1616
):
17-
if function.name in uos_device.device.functions_enabled:
18-
pins_indices = uos_device.device.get_compatible_pins(function)
17+
if function.name in uos_device.get_functions_enabled():
18+
pins_indices = uos_device.get_compatible_pins(function)
1919
assert len(pins_indices) > 0
2020
for pin_index in pins_indices:
2121
pin = uos_device.get_pin(pin_index)
@@ -25,11 +25,11 @@ def test_get_compatible_pins(uos_device: UOSDevice):
2525
elif function == UOSFunctions.set_gpio_output:
2626
assert pin.gpio_out
2727
# Check lookup of function without pins returns empty list.
28-
pins = uos_device.device.get_compatible_pins(UOSFunctions.hard_reset)
28+
pins = uos_device.get_compatible_pins(UOSFunctions.hard_reset)
2929
assert isinstance(pins, set)
3030
assert len(pins) == 0
3131
# Check bad function throws unsupported error
3232
with pytest.raises(UOSUnsupportedError):
33-
uos_device.device.get_compatible_pins(
33+
uos_device.get_compatible_pins(
3434
UOSFunction(name="bad_function", address_lut={}, ack=False)
3535
)

uoshardware/abstractions.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -335,27 +335,6 @@ class Device:
335335
pins: dict[int, Pin] = field(default_factory=dict)
336336
aux_params: dict = field(default_factory=dict)
337337

338-
def get_compatible_pins(self, function: UOSFunction) -> set:
339-
"""Get pins suitable for use with a particular UOS Function.
340-
341-
:param function: the string name of the UOS Schema function.
342-
:return: Set of pin indices which support the function.
343-
"""
344-
if (
345-
not isinstance(function, UOSFunction)
346-
or function not in UOSFunctions.enumerate_functions()
347-
):
348-
raise UOSUnsupportedError(f"UOS function {function.name} doesn't exist.")
349-
if function.pin_requirements is None: # pins are not relevant to this function
350-
return set()
351-
return {
352-
pin_index
353-
for pin_index, pin in self.pins.items()
354-
if all(
355-
getattr(pin, requirement) for requirement in function.pin_requirements
356-
)
357-
}
358-
359338
def update_adc_samples(self, result: ComResult):
360339
"""Update the pin samples with the response of a get_adc_input."""
361340
if not result.status or len(result.exception) != 0:

uoshardware/api.py

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ def get_device_definition(identity: str) -> Device | None:
5757
class UOSDevice: # dead: disable
5858
"""Class for high level object-orientated control of UOS devices.
5959
60-
:ivar device: Device definitions as parsed from a compatible ini.
60+
:ivar __device: Device definitions as parsed from a compatible ini.
6161
:ivar identity: The type of device, this is must have a valid device in the config.
6262
:ivar address: Compliant connection string for identifying the device and interface.
6363
:ivar __device_interface: Lower level communication protocol layer.
6464
:ivar __kwargs: Connection specific / optional parameters.
6565
"""
6666

67-
device: Device
67+
__device: Device
6868
identity = ""
6969
address = ""
7070
__device_interface: UOSInterface
@@ -101,13 +101,16 @@ def __init__(
101101
raise UOSUnsupportedError(
102102
f"'{self.identity}' does not have a valid look up table"
103103
)
104-
self.device = device
105-
if interface == Interface.SERIAL and Interface.SERIAL in self.device.interfaces:
104+
self.__device = device
105+
if (
106+
interface == Interface.SERIAL
107+
and Interface.SERIAL in self.__device.interfaces
108+
):
106109
self.__device_interface = Serial(
107110
address,
108-
baudrate=self.device.aux_params["default_baudrate"],
111+
baudrate=self.__device.aux_params["default_baudrate"],
109112
)
110-
elif interface == Interface.STUB and Interface.STUB in self.device.interfaces:
113+
elif interface == Interface.STUB and Interface.STUB in self.__device.interfaces:
111114
self.__device_interface = Stub(
112115
connection=address,
113116
errored=(kwargs["errored"] if "errored" in kwargs else False),
@@ -173,7 +176,7 @@ def get_gpio_input(
173176
),
174177
)
175178
if result.status:
176-
self.device.update_gpio_samples(result)
179+
self.__device.update_gpio_samples(result)
177180
return result
178181

179182
def get_adc_input(
@@ -192,7 +195,7 @@ def get_adc_input(
192195
),
193196
)
194197
if result.status: # update the samples in the device.
195-
self.device.update_adc_samples(result)
198+
self.__device.update_adc_samples(result)
196199
return result
197200

198201
def get_system_info(self) -> ComResult:
@@ -254,17 +257,16 @@ def __execute_instruction(
254257
:raises: UOSUnsupportedError if function is not possible on the loaded device.
255258
"""
256259
if (
257-
function.name not in self.device.functions_enabled
260+
function.name not in self.__device.functions_enabled
258261
or (
259262
instruction_data.check_pin is not None
260-
and instruction_data.check_pin
261-
not in self.device.get_compatible_pins(function)
263+
and instruction_data.check_pin not in self.get_compatible_pins(function)
262264
)
263265
or instruction_data.volatility
264-
not in self.device.functions_enabled[function.name]
266+
not in self.__device.functions_enabled[function.name]
265267
):
266268
logger.debug(
267-
"Known functions %s", str(self.device.functions_enabled.keys())
269+
"Known functions %s", str(self.__device.functions_enabled.keys())
268270
)
269271
raise UOSUnsupportedError(
270272
f"{function.name}({instruction_data.volatility.name}) "
@@ -332,11 +334,40 @@ def get_pin(self, pin: int) -> Pin: # dead: disable
332334
:param pin: The index of the pin to return.
333335
:return: Pin object for provided index.
334336
"""
335-
if pin not in self.device.pins:
337+
if pin not in self.__device.pins:
336338
raise UOSRuntimeError(
337-
f"Pin index {pin} doesn't exist for device {self.device.name}"
339+
f"Pin index {pin} doesn't exist for device {self.__device.name}"
340+
)
341+
return self.__device.pins[pin]
342+
343+
def get_compatible_pins(self, function: UOSFunction) -> set:
344+
"""Get pins suitable for use with a particular UOS Function.
345+
346+
:param function: the string name of the UOS Schema function.
347+
:return: Set of pin indices which support the function.
348+
"""
349+
if (
350+
not isinstance(function, UOSFunction)
351+
or function not in UOSFunctions.enumerate_functions()
352+
):
353+
raise UOSUnsupportedError(f"UOS function {function.name} doesn't exist.")
354+
if function.pin_requirements is None: # pins are not relevant to this function
355+
return set()
356+
return {
357+
pin_index
358+
for pin_index, pin in self.__device.pins.items()
359+
if all(
360+
getattr(pin, requirement) for requirement in function.pin_requirements
338361
)
339-
return self.device.pins[pin]
362+
}
363+
364+
# False positive as this is a client-facing function.
365+
def get_functions_enabled(self) -> dict: # dead: disable
366+
"""Return functions enabled for the device.
367+
368+
:return: Dictionary of function names to list of Persistence levels.
369+
"""
370+
return self.__device.functions_enabled
340371

341372
def __repr__(self):
342373
"""Representation of the UOS device.
@@ -345,6 +376,6 @@ def __repr__(self):
345376
"""
346377
return (
347378
f"<UOSDevice(address='{self.address}', identity='{self.identity}', "
348-
f"device={self.device}, __device_interface='{self.__device_interface}', "
379+
f"device={self.__device}, __device_interface='{self.__device_interface}', "
349380
f"__kwargs={self.__kwargs})>"
350381
)

0 commit comments

Comments
 (0)