PageRenderTime 58ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_Main/Languages/IronPython/Public/Tutorial/pyevent.py

#
Python | 85 lines | 49 code | 14 blank | 22 comment | 13 complexity | 9598071f52f36870c460c5a113863711 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. #####################################################################################
  2. #
  3. # Copyright (c) Microsoft Corporation. All rights reserved.
  4. #
  5. # This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. # copy of the license can be found in the License.html file at the root of this distribution. If
  7. # you cannot locate the Apache License, Version 2.0, please send an email to
  8. # ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. # by the terms of the Apache License, Version 2.0.
  10. #
  11. # You must not remove this notice, or any other, from this software.
  12. #
  13. #
  14. #####################################################################################
  15. class event(object):
  16. """Provides CLR event-like functionality for Python. This is a public
  17. event helper that allows adding and removing handlers."""
  18. __slots__ = ['handlers']
  19. def __init__(self):
  20. self.handlers = []
  21. def __iadd__(self, other):
  22. if isinstance(other, event):
  23. self.handlers.extend(other.handlers)
  24. elif isinstance(other, event_caller):
  25. self.handlers.extend(other.event.handlers)
  26. else:
  27. if not callable(other):
  28. raise TypeError, "cannot assign to event unless value is callable"
  29. self.handlers.append(other)
  30. return self
  31. def __isub__(self, other):
  32. if isinstance(other, event):
  33. newEv = []
  34. for x in self.handlers:
  35. if not other.handlers.contains(x):
  36. newEv.append(x)
  37. self.handlers = newEv
  38. elif isinstance(other, event_caller):
  39. newEv = []
  40. for x in self.handlers:
  41. if not other.event.handlers.contains(x):
  42. newEv.append(x)
  43. self.handlers = newEv
  44. else:
  45. if other in self.handlers:
  46. self.handlers.remove(other)
  47. return self
  48. def make_caller(self):
  49. return event_caller(self)
  50. class event_caller(object):
  51. """Provides CLR event-like functionality for Python. This is the
  52. protected event caller that allows the owner to raise the event"""
  53. __slots__ = ['event']
  54. def __init__(self, event):
  55. self.event = event
  56. def __call__(self, *args):
  57. for ev in self.event.handlers:
  58. ev(*args)
  59. def __set__(self, val):
  60. raise ValueError, "cannot assign to an event, can only add or remove handlers"
  61. def __delete__(self, val):
  62. raise ValueError, "cannot delete an event, can only add or remove handlers"
  63. def __get__(self, instance, owner):
  64. return self
  65. def make_event():
  66. """Creates an event object tuple. The first value in the tuple can be
  67. exposed to allow external code to hook and unhook from the event. The
  68. second value can be used to raise the event and can be stored in a
  69. private variable."""
  70. res = event()
  71. return (res, res.make_caller())