A clone of: https://github.com/nutechsoftware/alarmdecoder This is requires as they dropped support for older firmware releases w/o building in backward compatibility code, and they had previously hardcoded pyserial to a python2 only version.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.6 KiB

  1. # event.py (improved)
  2. class Event(object):
  3. def __init__(self, doc=None):
  4. self.__doc__ = doc
  5. def __get__(self, obj, objtype=None):
  6. if obj is None:
  7. return self
  8. return EventHandler(self, obj)
  9. def __set__(self, obj, value):
  10. pass
  11. class EventHandler(object):
  12. def __init__(self, event, obj):
  13. self.event = event
  14. self.obj = obj
  15. def _getfunctionlist(self):
  16. """(internal use) """
  17. try:
  18. eventhandler = self.obj.__eventhandler__
  19. except AttributeError:
  20. eventhandler = self.obj.__eventhandler__ = {}
  21. return eventhandler.setdefault(self.event, [])
  22. def add(self, func):
  23. """Add new event handler function.
  24. Event handler function must be defined like func(sender, earg).
  25. You can add handler also by using '+=' operator.
  26. """
  27. self._getfunctionlist().append(func)
  28. return self
  29. def remove(self, func):
  30. """Remove existing event handler function.
  31. You can remove handler also by using '-=' operator.
  32. """
  33. self._getfunctionlist().remove(func)
  34. return self
  35. def fire(self, earg=None):
  36. """Fire event and call all handler functions
  37. You can call EventHandler object itself like e(earg) instead of
  38. e.fire(earg).
  39. """
  40. for func in self._getfunctionlist():
  41. if type(func) == EventHandler:
  42. func.fire(earg)
  43. else:
  44. func(self.obj, earg)
  45. __iadd__ = add
  46. __isub__ = remove
  47. __call__ = fire