From a5600c28cb936d8def81a61ba526517680fd45bd Mon Sep 17 00:00:00 2001 From: Nicolas Rabault Date: Fri, 26 Jan 2024 10:37:49 +0100 Subject: [PATCH] Add a `map_custom_service` function allowing to manage custom pyluos service type --- pyluos/__init__.py | 2 +- pyluos/device.py | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/pyluos/__init__.py b/pyluos/__init__.py index 420ab71..7bb78cc 100644 --- a/pyluos/__init__.py +++ b/pyluos/__init__.py @@ -1,6 +1,6 @@ import logging -from .device import Device +from .device import Device, map_custom_service from .services import * nh = logging.NullHandler() diff --git a/pyluos/device.py b/pyluos/device.py index c40cc79..9a9a88d 100644 --- a/pyluos/device.py +++ b/pyluos/device.py @@ -22,6 +22,8 @@ def run_from_unittest(): return 'unittest' in sys.services +def map_custom_service(service_type, service_class): + name2mod[service_type] = service_class class contList(list): def __repr__(self): @@ -180,9 +182,15 @@ def _setup(self): break # create the node self._nodes.append(AnyNode(id=node["node_id"], parent=parent_elem, connection=node["con"])) - filtered_services = contList([mod for mod in node["services"] if 'type' in mod and mod['type'] in name2mod.keys()]) + # list unrecognized services and print a warning + unrecognized_services = [mod for mod in node["services"] + if 'type' in mod and mod['type'] not in name2mod.keys()] + if (len(unrecognized_services) > 0): + self.logger.warning("Unrecognized services have been detected on node %d" % node["node_id"]) + for mod in unrecognized_services: + self.logger.warning(" - service %s of type %s" % (mod['alias'], mod['type'])) # Create a list of services in the node self._nodes[i].services = [ name2mod[mod['type']](id=mod['id'], @@ -212,6 +220,7 @@ def services(self): def nodes(self): return nodeList(self._nodes) + # Poll state from hardware. def _poll_once(self): self._state = self._io.read()