Coverage for /opt/conda/envs/apienv/lib/python3.10/site-packages/daiquiri/core/hardware/hwr/object.py: 0%

34 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-14 02:13 +0000

1#!/usr/bin/env python 

2# -*- coding: utf-8 -*- 

3import typing 

4from daiquiri.core.hardware.abstract import MappedHardwareObject, HardwareProperty 

5 

6import logging 

7 

8logger = logging.getLogger(__name__) 

9 

10 

11class HWRProperty(HardwareProperty): 

12 def __init__(self, name: str, setter: str = None, getter: str = None): 

13 super(HWRProperty, self).__init__(name=name) 

14 self._setter = setter 

15 self._getter = getter 

16 

17 @property 

18 def setter(self) -> typing.Optional[str]: 

19 return self._setter 

20 

21 @property 

22 def getter(self) -> typing.Optional[str]: 

23 return self._getter 

24 

25 

26class HWRObject(MappedHardwareObject): 

27 _protocol = "hwr" 

28 

29 def __init__(self, **kwargs): 

30 super().__init__(**kwargs) 

31 

32 self._object = kwargs.get("obj") 

33 for p in self.property_getter_map.values(): 

34 logger.debug("connecting to {p} {obj}".format(p=p, obj=self._object)) 

35 # connect to hwr events here 

36 

37 # function to call when a HWR event happens 

38 def _event(self, value, *args, **kwargs): 

39 logger.debug("HWRObject._event {v} {kw}".format(v=value, kw=kwargs)) 

40 # for p, v in self._property_map.items(): 

41 # pass 

42 # some magic 

43 # self._update(p, v, value) 

44 

45 def _do_set(self, prop: HardwareProperty, value): 

46 """Set a property on the child object using the setter map 

47 

48 Args: 

49 prop (str): The property to set. 

50 value: Its value. 

51 """ 

52 assert isinstance(prop, HWRProperty) # nosec 

53 if prop in self.property_setter_map: 

54 return setattr(self._object, prop.setter)(value) 

55 else: 

56 raise KeyError(f"Couldnt find a setter for property {prop}") 

57 

58 def _do_get(self, prop: HWRProperty): 

59 """Get a property on the child object using the getter map 

60 

61 Args: 

62 prop (str): The property to set. 

63 

64 Returns: 

65 The property value 

66 """ 

67 assert isinstance(prop, HWRProperty) # nosec 

68 if prop in self.property_getter_map: 

69 return getattr(self._object, prop.getter)() 

70 else: 

71 raise KeyError(f"Couldnt find a getter for property {prop}")