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.

81 lines
1.9 KiB

  1. # event.py (improved)
  2. #
  3. # Based on pyevent originally found at http://www.emptypage.jp/notes/pyevent.en.html
  4. #
  5. # License: https://creativecommons.org/licenses/by/2.1/jp/deed.en
  6. #
  7. # Changes:
  8. # * Added type check in fire()
  9. # * Removed earg from fire() and added support for args/kwargs.
  10. class Event(object):
  11. def __init__(self, doc=None):
  12. self.__doc__ = doc
  13. def __get__(self, obj, objtype=None):
  14. if obj is None:
  15. return self
  16. return EventHandler(self, obj)
  17. def __set__(self, obj, value):
  18. pass
  19. class EventHandler(object):
  20. def __init__(self, event, obj):
  21. self.event = event
  22. self.obj = obj
  23. def _getfunctionlist(self):
  24. """(internal use) """
  25. try:
  26. eventhandler = self.obj.__eventhandler__
  27. except AttributeError:
  28. eventhandler = self.obj.__eventhandler__ = {}
  29. return eventhandler.setdefault(self.event, [])
  30. def add(self, func):
  31. """Add new event handler function.
  32. Event handler function must be defined like func(sender, earg).
  33. You can add handler also by using '+=' operator.
  34. """
  35. self._getfunctionlist().append(func)
  36. return self
  37. def remove(self, func):
  38. """Remove existing event handler function.
  39. You can remove handler also by using '-=' operator.
  40. """
  41. self._getfunctionlist().remove(func)
  42. return self
  43. def fire(self, *args, **kwargs):
  44. """Fire event and call all handler functions
  45. You can call EventHandler object itself like e(*args, **kwargs) instead of
  46. e.fire(*args, **kwargs).
  47. """
  48. for func in self._getfunctionlist():
  49. if type(func) == EventHandler:
  50. func.fire(*args, **kwargs)
  51. else:
  52. func(self.obj, *args, **kwargs)
  53. __iadd__ = add
  54. __isub__ = remove
  55. __call__ = fire