18
18
from memory import TYPE_SIZES
19
19
from memory import get_size
20
20
from memory .helpers import Type
21
+ from memory .hooks import HookType
21
22
from memory .manager import manager
22
23
# Paths
23
24
from paths import SP_DATA_PATH
@@ -73,6 +74,9 @@ def __init__(self, temp_entity):
73
74
# Add the current table to the properties...
74
75
self ._add_properties (prop .data_table )
75
76
77
+ # Get a dictionary to store our hooks...
78
+ self ._hooks = {HookType .PRE : set (), HookType .POST : set ()}
79
+
76
80
# Initialize the base class...
77
81
super ()._copy_base (temp_entity , self .size )
78
82
@@ -159,6 +163,80 @@ def _get_type_size(type_name):
159
163
# Raise an exception...
160
164
raise ValueError ('"{}" is not a supported type.' .format (type_name ))
161
165
166
+ def add_hook (self , hook_type , callback ):
167
+ """Register a hook for this temp entity.
168
+
169
+ :param HookType hook_type:
170
+ The type of the hook to register.
171
+ :param function callback:
172
+ The callback function to register.
173
+ """
174
+ # Get the set associated with the given hook type...
175
+ hooks = self .hooks .get (hook_type , None )
176
+
177
+ # Was the given hook type invalid?
178
+ if hooks is None :
179
+ raise TypeError ('The given hook type is invalid.' )
180
+
181
+ # Is the given callback not callable?
182
+ if not callable (callback ):
183
+ raise TypeError ('The given callback is not callable.' )
184
+
185
+ # Register the hook...
186
+ self .hooks [hook_type ].add (callback )
187
+
188
+ def remove_hook (self , hook_type , callback ):
189
+ """Unregister a hook for this temp entity.
190
+
191
+ :param HookType hook_type:
192
+ The type of the hook to unregister.
193
+ :param function callback:
194
+ The callback function to unregister.
195
+ """
196
+ # Get the set associated with the given hook type...
197
+ hooks = self .hooks .get (hook_type , None )
198
+
199
+ # Was the given hook type invalid?
200
+ if hooks is None :
201
+ raise TypeError ('The given hook type is invalid.' )
202
+
203
+ # Unregister the hook...
204
+ self .hooks [hook_type ].discard (callback )
205
+
206
+ def handle_hook (self , hook_type , temp_entity , recipient_filter ):
207
+ """Call the registered callbacks.
208
+
209
+ :param HookType hook_type:
210
+ The type of the hook to handle.
211
+ :param TempEntity temp_entity:
212
+ The TempEntity instance.
213
+ :param RecipientFilter recipient_filter:
214
+ The RecipientFilter instance.
215
+
216
+ :rtype: bool
217
+ """
218
+ # Flag variable to determine whether or not any callback wants to
219
+ # block the original call...
220
+ block = False
221
+
222
+ # Loop through all registered hooks for this temp entity...
223
+ for callback in self .hooks [hook_type ]:
224
+
225
+ # Call the callback and store the value it returned...
226
+ ret = callback (temp_entity , recipient_filter )
227
+
228
+ # Is the returned value not None and evaluate to False?
229
+ if ret is not None and not ret :
230
+
231
+ # This callback wants to block the original call...
232
+ block = True
233
+
234
+ # Does any callback wanted to block the original call?
235
+ if block :
236
+
237
+ # Yes, so block it...
238
+ return False
239
+
162
240
@property
163
241
def aliases (self ):
164
242
"""Return the aliases of the temp entity.
@@ -167,6 +245,14 @@ def aliases(self):
167
245
"""
168
246
return self ._aliases
169
247
248
+ @property
249
+ def hooks (self ):
250
+ """Return the registered hooks for this temp entity.
251
+
252
+ :rtype: dict
253
+ """
254
+ return self ._hooks
255
+
170
256
@property
171
257
def properties (self ):
172
258
"""Return the properties data of the temp entity.
0 commit comments