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

29 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-02-06 02:13 +0000

1#!/usr/bin/env python 

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

3import logging 

4from typing import Callable 

5 

6from daiquiri.core.hardware.abstract import ( 

7 MappedHardwareObject, 

8 HardwareProperty, 

9) 

10from daiquiri.core.utils import get_nested_attr 

11 

12 

13logger = logging.getLogger(__name__) 

14 

15 

16class DaiquiriObject(MappedHardwareObject): 

17 """The daiquiri hardware object 

18 

19 This is simple object that just maps properties onto the hardware 

20 objects properties. 

21 

22 It allows arbitrary config options to be passed and interpreted by the 

23 targeted hardware object. 

24 """ 

25 

26 _protocol = "daiquiri" 

27 _online = True 

28 

29 def __init__(self, *args, **kwargs): 

30 self._config = kwargs 

31 super().__init__(*args, **kwargs) 

32 

33 def __repr__(self) -> str: 

34 return f"<Daiquiri: {self.name()} ({self.__class__.__name__}/{self._object.__class__.__name__})>" 

35 

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

37 return setattr(self, prop.name, value) 

38 

39 def _do_get(self, prop: HardwareProperty): 

40 getter = prop.getter 

41 if getter is not None: 

42 return getter(self) 

43 try: 

44 return get_nested_attr(self, prop.name) 

45 except RuntimeError: 

46 logger.info( 

47 f"Could not get property {prop.name} from {self.name()}", exc_info=True 

48 ) 

49 return None 

50 

51 def _do_call(self, function: Callable, value, **kwargs): 

52 fn = getattr(self, self._callable_map[function]) 

53 if value is None: 

54 return fn(**kwargs) 

55 else: 

56 return fn(value, **kwargs)