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.

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