Coverage for C:\Users\hjanssen\HOME\pyCharmProjects\ethz_hvl\hvl_ccb\hvl_ccb\comm\base.py : 77%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Copyright (c) 2019-2020 ETH Zurich, SIS ID and HVL D-ITET
2#
3"""
4Module with base classes for communication protocols.
5"""
7from abc import ABC, abstractmethod
8from threading import RLock
9from typing import Type
11from ..configuration import ConfigurationMixin, EmptyConfig
14class CommunicationProtocol(ConfigurationMixin, ABC):
15 """
16 Communication protocol abstract base class.
18 Specifies the methods to implement for communication protocol, as well as
19 implements some default settings and checks.
20 """
22 def __init__(self, config) -> None:
23 """
24 Constructor for CommunicationProtocol. Takes a configuration dict or
25 configdataclass as the single parameter.
27 :param config: Configdataclass or dictionary to be used with the default
28 config dataclass.
29 """
31 super().__init__(config)
33 #: Access lock to use with context manager when
34 #: accessing the communication protocol (thread safety)
35 self.access_lock = RLock()
37 @abstractmethod
38 def open(self):
39 """
40 Open communication protocol
41 """
42 pass # pragma: no cover
44 @abstractmethod
45 def close(self):
46 """
47 Close the communication protocol
48 """
49 pass # pragma: no cover
51 def __enter__(self):
52 self.open()
53 return self
55 def __exit__(self, exc_type, exc_val, exc_tb):
56 self.close()
59class NullCommunicationProtocol(CommunicationProtocol):
60 """
61 Communication protocol that does nothing.
62 """
64 def open(self) -> None:
65 """
66 Void open function.
67 """
68 pass
70 def close(self) -> None:
71 """
72 Void close function.
73 """
74 pass
76 @staticmethod
77 def config_cls() -> Type[EmptyConfig]:
78 """
79 Empty configuration
81 :return: EmptyConfig
82 """
83 return EmptyConfig