/
notificator.py
1109 lines (869 loc) · 38.6 KB
/
notificator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
""" notificator.py contains class Notificator, which is the
the base implementation for a notification manager using
priority lists.
:author: Jose Carlos Recuero
:version: 0.1
:since: 08/13/2014
"""
__docformat__ = 'restructuredtext en'
###############################################################################
## _ _
## (_)_ __ ___ _ __ ___ _ __| |_ ___
## | | '_ ` _ \| '_ \ / _ \| '__| __/ __|
## | | | | | | | |_) | (_) | | | |_\__ \
## |_|_| |_| |_| .__/ \___/|_| \__|___/
## |_|
###############################################################################
#
# import std python modules
#
import functools
import mock
#
# import user python modules
#
import plist
import info
import error
import loggerator
###############################################################################
##
## ___ ___ _ __ ___| |_ __ _ _ __ | |_ ___
## / __/ _ \| '_ \/ __| __/ _` | '_ \| __/ __|
## | (_| (_) | | | \__ \ || (_| | | | | |_\__ \
## \___\___/|_| |_|___/\__\__,_|_| |_|\__|___/
##
###############################################################################
#
ID = 'id'
"""
:type: str
String that represents the key for the Id for the value returned when a
trigger method is registered to the Notificator.
"""
TRIGGER = 'trigger'
"""
:type: str
String that represents the key for the trigger method for the value
returned when a trigger method is registerd to the NotifyManager.
String that represents the key for the trigger method for the dictionary
where information about all triggers
methods are stored.
"""
METHOD = 'method'
"""
:type: str
String that represents the key for the notification method for the
dictionary where informaton about all notification methods are stored.
Notification are callback registered to trigger methods.
"""
ARGS = 'args'
"""
:type: str
String that represents the key for the list of arguments passed to the
trigger method for the dictionary where information about all triggers
methods are stored.
String that represents the key for the list of arguments passed to the
notification method for the dictionary where informaton about all
notification methods are stored.
Notification are callback registered to trigger methods.
"""
KWARGS = 'kwargs'
"""
:type: str
String that represents the key for the dictionary of arguments passed to
the trigger method for the dictionary where information about all triggers
methods are stored.
String that represents the key for the dictionary of arguments passed to
the notification method for the dictionary where informaton about all
notification methods are stored.
Notification are callback registered to trigger methods.
"""
PLIST = 'plist'
"""
:type: str
String that represents the key where priority list instance for
notifications registered to trigger methods are stored.
"""
BEFORE = 'before'
"""
:type: str
String that represents the key where where notifications registered to be
called before the trigger method runs are stored.
"""
AFTER = 'after'
"""
:type: str
String that represents the key where where notifications registered to be
called after the trigger method runs are stored.
"""
###############################################################################
## _ _ _
## ___ _ _| |__ _ __ ___ _ _| |_(_)_ __ ___ ___
## / __| | | | '_ \| '__/ _ \| | | | __| | '_ \ / _ \/ __|
## \__ \ |_| | |_) | | | (_) | |_| | |_| | | | | __/\__ \
## |___/\__,_|_.__/|_| \___/ \__,_|\__|_|_| |_|\___||___/
##
###############################################################################
#
###############################################################################
## _ _ __ _ _ _ _
## ___| | __ _ ___ ___ __| | ___ / _(_)_ __ (_) |_(_) ___ _ __ ___
## / __| |/ _` / __/ __| / _` |/ _ \ |_| | '_ \| | __| |/ _ \| '_ \/ __|
## | (__| | (_| \__ \__ \ | (_| | __/ _| | | | | | |_| | (_) | | | \__ \
## \___|_|\__,_|___/___/ \__,_|\___|_| |_|_| |_|_|\__|_|\___/|_| |_|___/
##
###############################################################################
#
#
# =============================================================================
#
class InvalidIdException(error.BaseException):
"""Invalid Id Exception.
Exception raised when an invalid id is passed to the notification manager.
A valid id should be in the range of ids with trigger methods registered
to the notification manager.
>>> ex = InvalidIdException()
>>> ex.BASE_MESSAGE
'Invalid ID provided'
>>> raise InvalidIdException('custom invalid id')
Traceback (most recent call last):
...
InvalidIdException: Invalid ID provided : custom invalid id
"""
BASE_MESSAGE = "Invalid ID provided"
#
# =============================================================================
#
class InvalidMethodCallException(error.BaseException):
"""Invalid Method Call Exception.
Exception raised when an invalid method call is passed to the notification
manager. A valid method call should have a '__call__' attribute in order to
be able to call it.
>>> ex = InvalidMethodCallException()
>>> ex.BASE_MESSAGE
'Invalid method call provided'
>>> raise InvalidMethodCallException('custom invalid method')
Traceback (most recent call last):
...
InvalidMethodCallException: Invalid method call provided : custom invalid method
"""
BASE_MESSAGE = "Invalid method call provided"
#
# =============================================================================
#
class RegistrationException(error.BaseException):
"""Registration Exception.
Exception raised when a registration for a trigger method or a notification
method fail.
>>> ex = RegistrationException()
>>> ex.BASE_MESSAGE
'Registration failed'
>>> raise RegistrationException('custom registration failed')
Traceback (most recent call last):
...
RegistrationException: Registration failed : custom registration failed
"""
BASE_MESSAGE = "Registration failed"
#
# =============================================================================
#
class DeregistrationException(error.BaseException):
"""Deregistration Exception.
Exception raised when a deregistration for a trigger method or a
notification method fail.
>>> ex = DeregistrationException()
>>> ex.BASE_MESSAGE
'Deregistration failed'
>>> raise DeregistrationException('custom deregistration failed')
Traceback (most recent call last):
...
DeregistrationException: Deregistration failed : custom deregistration failed
"""
BASE_MESSAGE = "Deregistration failed"
#
# =============================================================================
#
class TriggerException(error.BaseException):
"""Trigger Exception.
Exception raised when a trigger method failed in its execution.
>>> ex = TriggerException()
>>> ex.BASE_MESSAGE
'Trigger failed'
>>> raise TriggerException('custom Trigger failed')
Traceback (most recent call last):
...
TriggerException: Trigger failed : custom Trigger failed
"""
BASE_MESSAGE = "Trigger failed"
#
# =============================================================================
#
class Notificator(object):
"""Notificator provides notifications for clients subscribed.
Notificator implements a notification manager which provides priority
notifications to be called at the front or at the back of a registered
event.
The way in which it works is this:
- Notificator provides a registration mechanism, which clients will
use to register trigger methods. That registration will return a new
method that replaces the trigger method and a handler/id which will
be used to register notification to that trigger.
- Client can use that handler to register notifications to be called
before the trigger method is executed, or after the trigger method
has been executed.
- Those notification can be use different priorities.
- When the trigger method is called, the handler/id will be used in
order to properly retrieve all notification to be called at any
stage (before or after).
::
Data Structures:
<priorityValues> as PValuesList instance.
_________________
| | |
| P1 .. Pn | Px |
|___________|____|
<triggerInfo> as a dictionary
___________________
| | |
| Key: ID | Value |
|_________|_______|
<triggerInfo[ID]> as a dictionary
____________________________________________________________
| | |
| Key: 'method' | trigger method |
|_______________|__________________________________________|
| | |
| Key: 'args' | args to pass to trigger |
|_______________|__________________________________________|
| | |
| Key: 'kwargs' | kwargs to pass to trigger |
|_______________|__________________________________________|
| | |
| Key: 'plist' | Dictionary with PList for notifications |
|_______________|__________________________________________|
<trigger.Value['plist']> as a dictionary
_______________________________________________________________________
| | |
| Key: 'before' | PList with notification to be called before trigger |
|_______________|_____________________________________________________|
| | |
| Key: 'after' | PList with notification to be called after trigger |
|_______________|_____________________________________________________|
"""
# =========================================================================
def __init__(self, priorities, defaultPriority):
"""Notificator constructor.
It create a Notificator instance. Initialize Id to zero.
>>> nt = Notificator((1, 2, 3), 2)
>>> nt.logger # doctest: +ELLIPSIS
<loggerator.Loggerator object at 0x...>
>>> nt.priorityValues # doctest: +ELLIPSIS
<plist.PValuesList object at 0x...>
>>> nt.id
0
>>> nt.triggerInfo
{}
:type priorities: list
:param priorities:
It is the list of priorities to be used in the priority list.
:type defaultPriority: object
:param defaultPriority:
It is the default priority that will be used for any other method
when no priority value is provided.
:raise plist.PriorityListException:
No list of priorities is provided.
:raise plist.DefaultPriorityException:
Default priority does not belong to the list of priorities provided.
"""
self.logger = loggerator.getLoggerator('notificator',
color=(loggerator.FG_YELLOW))
"""
:type: loggerator.Loggerator
Variable for local logger. Disable debug logs by default.
"""
self.priorityValues = plist.PValuesList(priorities, defaultPriority)
"""
:type: PValuesList
PValuesList with the list with all possible priorites and the
default one.
"""
self.id = 0
"""
:type: int
Identification for every function registered to the notify manager.
"""
self.triggerInfo = {}
"""
:type: dict
Dictionary where triggers functions will be stored. A trigger
function is the trigger provided by the user wrapped up in order
to call all notification registered to that trigger.
**triggerInfo** dictionary
+----------+-----------+----------------------+------------+
| Key | KeyType | Value | Value Type |
+==========+===========+======================+============+
| ID | <int> | Trigger Info | <dict> |
+----------+-----------+----------------------+------------+
**triggerInfo[ID]** dictionary
+----------+---------+----------------------+------------+
| Key | KeyType | Value | Value Type |
+==========+=========+======================+============+
+----------+---------+----------------------+------------+
| 'method' | <str> | Trigger method | <func> |
+----------+-----------+--------------------+------------+
| 'args' | <str> | Trigger(args) | <list> |
+----------+-----------+--------------------+------------+
| 'kwargs' | <str> | Trigger(kwargs) | <dict> |
+----------+-----------+--------------------+------------+
| 'plist' | <str> | Notifications | <dict> |
+----------+-----------+--------------------+------------+
**triggerInfo[ID]['plist']** dictionary
+----------+-----------+--------------------+------------+
| Key | KeyType | Value | Value Type |
+==========+=========+======================+============+
| 'before' | <str> | Before notifications | <PList> |
+----------+-----------+--------------------+------------+
| 'after' | <str> | After notifications | <PList> |
+----------+---------+----------------------+------------+
"""
# =========================================================================
def _increaseId(self):
"""Increase the notify manager instance id and return
the new value.
>>> nt = Notificator((1, 2, 3), 2)
>>> nt._increaseId()
1
>>> nt.id
1
>>> nt._increaseId()
2
>>> nt.id
2
:rtype: int
:return:
New value for the instance id.
"""
self.id += 1
return self.id
# =========================================================================
def _isValidId(self, id):
"""Check if the given id is a valid one.
It checks if the given id value is a valid one, and it will be a valid
one if it is a valid/active key. It has to be lower or equal that the
actual key value.
>>> nt = Notificator((1, 2, 3), 2)
>>> nt.id = 2
>>> nt.triggerInfo = {1: True, 2: True, }
>>> nt._isValidId(1)
True
>>> nt._isValidId(2)
True
>>> nt._isValidId(3)
False
:type id: int
:param id:
Id to check if it is valid.
:rtype: boolean
:return:
True if the given id is valid. False if the given id is not valid.
"""
return id <= self.id and id in self.triggerInfo.keys()
# =========================================================================
def _validateId(self, id):
"""Validate the given id.
If the given id is not validated, it raises an exception.
>>> nt = Notificator((1, 2, 3), 2)
>>> nt.id = 2
>>> nt.triggerInfo = {1: True, 2: True, }
>>> nt._validateId(1)
>>> nt._validateId(2)
>>> nt._validateId(3)
Traceback (most recent call last):
...
InvalidIdException: Invalid ID provided : id: 3
:type id: int
:param id:
Id to check if it is valid.
:raise InvalidIdException:
Id give is not a valid one.
"""
if not self._isValidId(id):
raise InvalidIdException('id: %s' % id)
# =========================================================================
def _isMethodType(self, method):
"""Check if the given method is a valid one.
It checks if the given method object is valid. It will valid if
it is not None and it is a method call.
>>> nt = Notificator((1, 2, 3), 2)
>>> def say():
... pass
>>> nt._isMethodType(say)
True
>>> value = 0
>>> nt._isMethodType(value)
False
>>> value = 1
>>> nt._isMethodType(value)
False
>>> lista = []
>>> nt._isMethodType(lista)
False
:type method: function
:param method:
Notification object to check if it is valid.
:rtype: boolean
:return:
True if the given method is valid. False if the given
method is not valid.
"""
if method and hasattr(method, '__call__'):
return True
return False
# =========================================================================
def _validateMethodCall(self, method):
"""Validate the given method call.
If the given method calls is not a valid one, it raises an exception.
>>> nt = Notificator((1, 2, 3), 2)
>>> def say():
... pass
>>> nt._validateMethodCall(say)
>>> value = 0
>>> nt._validateMethodCall(value)
Traceback (most recent call last):
...
InvalidMethodCallException: Invalid method call provided : method call: 0
:type method: function
:param method:
Notification object to check if it is valid.
:raise InvalidMethodCallException:
Method call is not a valid one.
"""
if not self._isMethodType(method):
raise InvalidMethodCallException('method call: %s' % method)
# This is required for testing with Mock functions, because they are
# required to have a __name__ attribute in order to work with
# @functools.wraps decorator or any toher feature that requires the
# proper __name__ function attribute to be set.
if isinstance(method, mock.Mock):
method.__name__ = method._mock_name
# =========================================================================
def _validatePriorityValue(self, priority):
"""Validate the given priority value.
If the given priority value is not a valid one, it raises an exception.
>>> nt = Notificator((1, 2, 3), 2)
>>> nt._validatePriorityValue(1)
>>> nt._validatePriorityValue(0)
Traceback (most recent call last):
...
InvalidPriorityException: Priority is not in the priority list : priority: 0 not in (1, 2, 3)
:type priority: object
:param priority:
Notification object to check if it is valid.
:raise plist.InvalidPriorityException:
Priority value is not a valid one.
"""
if not self.priorityValues.isValid(priority):
raise plist.InvalidPriorityException('priority: %s not in %s' %
(priority,
self.priorityValues.getList()))
# =========================================================================
def _createPList(self, name=None):
"""Create a new PListFunction instance.
It creates a new PListFunction instance to be used inside a trigger
information dictionary with the instance priorities and defaultPriority
values.
>>> nt = Notificator((1, 2, 3), 2)
>>> pl = nt._createPList('custom plist')
>>> pl # doctest: +ELLIPSIS
<plist.PListFunction object at 0x...>
>>> pl.priorityValues.getList()
(1, 2, 3)
>>> pl.priorityValues.getDefault()
2
>>> pl.name
'custom plist'
:rtype: plist.PListFunction
:return:
A PListFunction instance using the list of priorities and the
default value for the notify manager instance.
"""
return plist.PListFunction(self.priorityValues.getList(),
self.priorityValues.getDefault(),
name=name)
# =========================================================================
def _getRegistrationSideFromFlag(self, isBeforeFlag):
"""Return string used for searching in priority list dictionary.
It translate the boolean flag to the string with the correct key
in the priority list dictionary.
>>> nt = Notificator((1, 2, 3), 2)
>>> nt._getRegistrationSideFromFlag(True)
'before'
>>> nt._getRegistrationSideFromFlag(False)
'after'
:type isBeforeFlag: boolean
:param isBeforeFlag:
If True, then return string used for inserting 'before.
If False, then return string used for inserting after.
.
:rtype: str
:return:
If True returns 'before' string. If False return 'after' string.
"""
return BEFORE if isBeforeFlag else AFTER
# =========================================================================
def registerTrigger(self, triggerFunction, *args, **kwargs):
"""Register a new trigger function.
It registers a new custom trigger function, where notification can be
registered later on, and they will be called when the trigger function
is called.
>>> nt = Notificator((1, 2, 3), 2)
>>> def trigger(): pass
>>> nt.registerTrigger(trigger) # doctest: +ELLIPSIS
{'trigger': <function trigger at 0x...>, 'id': 1}
>>> nt.triggerInfo[1] # doctest: +ELLIPSIS
{'trigger': <function trigger at 0x...>, 'plist': {'after': <plist.PListFunction object at 0x...>, 'before': <plist.PListFunction object at 0x...>}, 'args': (), 'kwargs': {}}
:type triggerFunction: function
:param triggerFunction:
Custom trigger function to be registered.
:type args: list
:param args:
Default list parameters when the trigger function is called.
:type kwargs: dict
:param kwargs:
Default dictionary parameters when the trigger function is called.
:rtype: dict
:return:
It return a dictionary with key 'id' with the id for the
registered trigger and key 'trigger' with the wrapped up method
that should be called now.
:raise RegistrationException:
Registration exception if the trigger method was not registered
properly.
"""
try:
self._validateMethodCall(triggerFunction)
id = self._increaseId()
## TODO - MOVED - This has been moved to _validateMethodCall
## function.
## # This is required for testing with Mock functions, because
## # they are required to have a __name__ attribute in order to
## # work with @functools.wraps decorator.
## if isinstance(triggerFunction, mock.Mock):
## triggerFunction.__name__ = triggerFunction._mock_name
# =================================================================
@functools.wraps(triggerFunction)
def _wrapTrigger(*args, **kwargs):
"""Wrapped up the trigger function.
It wraps up the custom trigger function, so notification can
be called when the trigger function is executed.
:type args: list
:param args: Default list parameters when the trigger function
is called.
:type kwargs: dict
:param kwargs: Default dictionary parameters when the trigger
function is called.
:rtype: function
:return: Return trigger function return value.
"""
triggerInfo = self.triggerInfo[id]
argsToUse = args if args else triggerInfo[ARGS]
kwargsToUse = kwargs if kwargs else triggerInfo[KWARGS]
if __debug__:
self.logger.debug('triggerFunction %s, argsToUse %s, kwargsToUse %s' %
(triggerInfo[TRIGGER], argsToUse, kwargsToUse))
triggerInfo[PLIST][BEFORE].callForAll(*argsToUse, **kwargsToUse)
retvalue = triggerFunction(*argsToUse, **kwargsToUse)
triggerInfo[PLIST][AFTER].callForAll(*argsToUse, **kwargsToUse)
return retvalue
self.triggerInfo[id] = {TRIGGER: _wrapTrigger, ARGS: args, KWARGS: kwargs,
PLIST: {BEFORE: self._createPList('BEFORE'), AFTER: self._createPList('AFTER')}}
if __debug__:
self.logger.debug('%s: register trigger %s with args %s and kwargs %s, id=%d' %
(info.FUNC(), triggerFunction, args, kwargs, id))
return {ID: id, TRIGGER: _wrapTrigger}
except InvalidMethodCallException:
self.logger.error('%s: Invalid Trigger Function: %s' %
(info.FUNC(), triggerFunction))
raise RegistrationException()
# =========================================================================
def deregisterTrigger(self, id):
"""Deregister a given trigger at the given id.
It proceeds to deregister the given trigger at the given id.
:type id: int
:param id:
Id where the trigger is located.
>>> nt = Notificator((1, 2, 3), 2)
>>> def trigger(): pass
>>> nt.registerTrigger(trigger) # doctest: +ELLIPSIS
{'trigger': <function trigger at 0x...>, 'id': 1}
>>> nt.triggerInfo[1] # doctest: +ELLIPSIS
{'trigger': <function trigger at 0x...>, 'plist': {'after': <plist.PListFunction object at 0x...>, 'before': <plist.PListFunction object at 0x...>}, 'args': (), 'kwargs': {}}
>>> nt.deregisterTrigger(1)
>>> nt.triggerInfo[1]
Traceback (most recent call last):
...
KeyError: 1
>>> nt.deregisterTrigger(1)
Traceback (most recent call last):
...
DeregistrationException: Deregistration failed : Invalid id value: 1
:raise DeregistrationException:
If the trigger was not deregistered for any reason.
"""
try:
if __debug__:
self.logger.debug('%s: deregister trigger with id=%d' %
(info.FUNC(), id))
self._validateId(id)
del self.triggerInfo[id]
except InvalidIdException:
raise DeregistrationException('Invalid id value: %s' % id)
# =========================================================================
def registerNotification(self,
id,
priority,
isBeforeFlag,
inFrontFlag,
notification,
*args, **kwargs):
"""Register notification to a trigger identified by the given id value.
It registers the given notification with the given priority at the
given position for a trigger which is identified by the given id value.
>>> nt = Notificator((1, 2, 3), 2)
>>> def trigger(): pass
>>> nt.registerTrigger(trigger) # doctest: +ELLIPSIS
{'trigger': <function trigger at 0x...>, 'id': 1}
>>> def notif(): pass
>>> nt.registerNotification(1, 1, True, True, notif)
True
>>> nt.registerNotification(2, 1, True, True, notif)
Traceback (most recent call last):
...
RegistrationException: Registration failed : Invalid id value: 2
>>> nt.registerNotification(1, 1, True, True, 'NOTIF')
Traceback (most recent call last):
...
RegistrationException: Registration failed : Invalid notification method: NOTIF
>>> nt.registerNotification(1, 0, True, True, notif)
Traceback (most recent call last):
...
RegistrationException: Registration failed : Invalid priority: 0 not in (1, 2, 3)
:type id: int
:param id:
Identifies the trigger where the notification will be registered.
:type priority: object
:param priority:
Notification priority.
:type isBeforeFlag: boolean
:param isBeforeFlag:
If True, then Notification will be called before the trigger runs.
If False, then Notification will be called after the trigger runs.
:type inFrontFlag: boolean
:param inFrontFlag:
If True, then notification is registered at the front of the
priority list.
If False, then notification is registered at the back of the
priority list.
:type notification: function
:param notification:
Notification to be registered. It should be a method call.
:type args: list
:param args:
List of parameters to pass when the notification is called.
:type kwargs: dict
:param kwargs:
Dictionary of parameters to pass when the notification is called.
:rtype: boolean
:return:
True if the notification was registered.
:raise RegistrationException:
Notification was not registered properly.
"""
try:
if __debug__:
self.logger.debug('%s: register %s notification %s with args %s and kwargs %s, id=%d' %
(info.FUNC(), BEFORE if isBeforeFlag else AFTER, notification, args, kwargs, id))
self._validateId(id)
self._validateMethodCall(notification)
self._validatePriorityValue(priority)
registrationSide = self._getRegistrationSideFromFlag(isBeforeFlag)
triggerInfoPList = self.triggerInfo[id][PLIST]
addNotificationToListMethod =\
triggerInfoPList[registrationSide].addAtFront if inFrontFlag\
else triggerInfoPList[registrationSide].addAtBack
addNotificationToListMethod({METHOD: notification,
ARGS: args,
KWARGS: kwargs}, priority)
return True
except InvalidIdException:
raise RegistrationException('Invalid id value: %s' % id)
except InvalidMethodCallException:
raise RegistrationException('Invalid notification method: %s' %
notification)
except plist.InvalidPriorityException:
raise RegistrationException('Invalid priority: %s not in %s' %
(priority,
self.priorityValues.getList()))
# =========================================================================
def deregisterNotification(self,
id,
priority,
isBeforeFlag,
notification):
"""Deregister notification to a trigger identified by given id value.
It deregisters the given notification with the given priority at the
given position for a trigger which is identified by the given id value.
>>> nt = Notificator((1, 2, 3), 2)
>>> def trigger(): pass
>>> nt.registerTrigger(trigger) # doctest: +ELLIPSIS
{'trigger': <function trigger at 0x...>, 'id': 1}
>>> def notif(): pass
>>> nt.registerNotification(1, 1, True, True, notif)
True
>>> nt.deregisterNotification(1, 1, True, notif) # doctest: +ELLIPSIS
<function notif at 0x...>
>>> nt.deregisterNotification(2, 1, True, notif)
Traceback (most recent call last):
...
DeregistrationException: Deregistration failed : Invalid id value: 2
>>> nt.deregisterNotification(1, 1, True, 'NOTIF')
Traceback (most recent call last):
...
DeregistrationException: Deregistration failed : Invalid notification method: NOTIF
>>> nt.deregisterNotification(1, 0, True, notif)
Traceback (most recent call last):
...
DeregistrationException: Deregistration failed : Invalid priority: 0 not in (1, 2, 3)
:type id: int
:param id:
Identifies the trigger where the notification will be registered.
:type priority: object
:param priority:
Notification priority.
:type isBeforeFlag: boolean
:param isBeforeFlag:
If True, then Notification will be called before the trigger runs.
If False, then Notification will be called after the trigger runs.
:type notification: function
:param notification:
Notification to be registered. It should be a method call.
:rtype: boolean
:return:
True if the notification was deregistered.
:raise DeregistrationException:
Notification was not deregistered properly.
"""
try:
if __debug__:
self.logger.debug('%s: deregister %s notification %s, id=%d' %
(info.FUNC(),
BEFORE if isBeforeFlag else AFTER,
notification,
id))
self._validateId(id)
self._validateMethodCall(notification)
self._validatePriorityValue(priority)
registrationSide = self._getRegistrationSideFromFlag(isBeforeFlag)
triggerInfoPList = self.triggerInfo[id][PLIST]
return triggerInfoPList[registrationSide].remove({METHOD: notification},
priority)
except InvalidIdException:
raise DeregistrationException('Invalid id value: %s' % id)
except InvalidMethodCallException, plist.InvalidEntryException:
raise DeregistrationException('Invalid notification method: %s' %
notification)
except plist.InvalidPriorityException:
raise DeregistrationException('Invalid priority: %s not in %s' %
(priority,
self.priorityValues.getList()))