| Total Complexity | 10 |
| Total Lines | 41 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 0 |
| 1 | DEFAULT_BAUDRATE = 38400 |
||
| 14 | class UART(object): |
||
| 15 | def __init__(self): |
||
| 16 | self.bus_name = uart_base.__name__ |
||
| 17 | self.bus = None |
||
| 18 | self.map = {} |
||
| 19 | |||
| 20 | def connection(self, port, baudrate=DEFAULT_BAUDRATE): |
||
| 21 | try: |
||
| 22 | self.bus = uart_base(port, baudrate) |
||
| 23 | self._mapping() |
||
| 24 | except Exception as err: |
||
| 25 | # logging exception |
||
| 26 | logger.error(err) |
||
| 27 | return None |
||
| 28 | |||
| 29 | return self |
||
| 30 | |||
| 31 | def __getattr__(self, item): |
||
| 32 | def args_wrapper(*args, **kwargs): |
||
| 33 | try: |
||
| 34 | response = getattr(self.bus, item)(*args, **kwargs) |
||
| 35 | except AttributeError: |
||
| 36 | response = self._invoke_mapping(item, *args, **kwargs) |
||
| 37 | return response |
||
| 38 | |||
| 39 | return args_wrapper |
||
| 40 | |||
| 41 | def _invoke_mapping(self, method, *args, **kwargs): |
||
| 42 | try: |
||
| 43 | item = self.map[self.bus_name][method] |
||
| 44 | return getattr(self.bus, item)(*args, **kwargs) if item else None |
||
| 45 | except KeyError: |
||
| 46 | raise Exception( |
||
| 47 | "Unregistered method or attribute {}".format(method)) |
||
| 48 | |||
| 49 | def _mapping(self): |
||
| 50 | self.map = { |
||
| 51 | "UART": { |
||
| 52 | "close": "deinit", |
||
| 53 | "flushInput": "", |
||
| 54 | "flushOutput": "" |
||
| 55 | }, |
||
| 57 |