########################################################################### # # Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. # # THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN # WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. # # @file base.py # # @date 20-July-2020 # @author P. Lucia # # @brief Contains base classes for Dialin API sub systems and observers # # ############################################################################ from abc import ABC, abstractmethod from datetime import datetime class AbstractObserver(ABC): """ Publicly accessible parent class for all observers. The update method will receive data when data is made available """ @abstractmethod def update(self): """ Attach an observer """ pass # abstract base class requires all abstract methods are overridden by children classes class _AbstractSubSystem(ABC): @abstractmethod def __init__(self): """ Initialization function for the sub system """ self._observers = [] self._datetime_fmt = "%m.%d.%Y_%I.%M.%S.%f" pass def attach(self, observer: AbstractObserver): """ Attach an observer so it is updated upon published events """ self._observers.append(observer) def detach(self, observer: AbstractObserver): """ Detach an observer """ self._observers.remove(observer) def _publish(keys): """ Decorator that accepts a list of variable names to publish To be used in any _AbstractSubSystem @param keys: The variable names to publish @return: A function that will take a function and return another function """ def _decorator(func): """ @param func: The handler function @return: The function to wrap around _publish """ def _wrapper(self, *args, **kwargs): func(self, *args, **kwargs) result = {} if not self._observers: return None result["datetime"] = datetime.now() for key in keys: result[key] = getattr(self, key) for observer in self._observers: observer.update(result) return _wrapper return _decorator class _AbstractSystem(ABC): """ Parent class for all system classes """ @abstractmethod def __init__(self): """ Initialization function for the system """ pass