~lifeless/storm/bug-620615

« back to all changes in this revision

Viewing changes to storm/event.py

Merging fixes from James.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
#
21
21
import weakref
22
22
 
23
 
from storm import psycer
 
23
from storm import has_cextensions
24
24
 
25
25
 
26
26
__all__ = ["EventSystem"]
31
31
    def __init__(self, owner):
32
32
        self._owner_ref = weakref.ref(owner)
33
33
        self._hooks = {}
34
 
        self._saved_hooks = {}
35
 
 
36
 
    def save(self):
37
 
        hooks = {}
38
 
        for name, callbacks in self._hooks.items():
39
 
            hooks[name] = callbacks.copy()
40
 
        self._saved_hooks = hooks
41
 
 
42
 
    def restore(self):
43
 
        hooks = self._hooks
44
 
        hooks.clear()
45
 
        for name, callbacks in self._saved_hooks.items():
46
 
            hooks[name] = callbacks.copy()
47
34
 
48
35
    def hook(self, name, callback, *data):
49
36
        callbacks = self._hooks.get(name)
61
48
        owner = self._owner_ref()
62
49
        if owner is not None:
63
50
            callbacks = self._hooks.get(name)
64
 
            if callbacks is not None:
 
51
            if callbacks:
65
52
                for callback, data in tuple(callbacks):
66
53
                    if callback(owner, *(args+data)) is False:
67
54
                        callbacks.discard((callback, data))
68
55
 
69
56
 
70
 
psycer.bind(EventSystem, 0)
 
57
if has_cextensions:
 
58
    from storm.cextensions import EventSystem