|
| 1 | +# Copyright 2017, Optimizely |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +import sys |
| 14 | + |
| 15 | +from functools import reduce |
| 16 | +from .helpers import enums |
| 17 | + |
| 18 | + |
| 19 | +class NotificationCenter(object): |
| 20 | + """ Class encapsulating Broadcast Notifications. The enums.NotifcationTypes includes predefined notifications.""" |
| 21 | + |
| 22 | + def __init__(self, logger): |
| 23 | + self.notification_id = 1 |
| 24 | + self.notifications = {} |
| 25 | + for (attr, value) in enums.NotificationTypes.__dict__.items(): |
| 26 | + self.notifications[value] = [] |
| 27 | + self.logger = logger |
| 28 | + |
| 29 | + def add_notification_listener(self, notification_type, notification_callback): |
| 30 | + """ Add a notification callback to the notification center. |
| 31 | +
|
| 32 | + Args: |
| 33 | + notification_type: A string representing the notification type from .helpers.enums.NotificationTypes |
| 34 | + notification_callback: closure of function to call when event is triggered. |
| 35 | +
|
| 36 | + Returns: |
| 37 | + Integer notification id used to remove the notification or -1 if the notification has already been added. |
| 38 | + """ |
| 39 | + |
| 40 | + if notification_type not in self.notifications: |
| 41 | + self.notifications[notification_type] = [(self.notification_id, notification_callback)] |
| 42 | + else: |
| 43 | + if reduce(lambda a, b: a + 1, |
| 44 | + filter(lambda tup: tup[1] == notification_callback, self.notifications[notification_type]), |
| 45 | + 0) > 0: |
| 46 | + return -1 |
| 47 | + self.notifications[notification_type].append((self.notification_id, notification_callback)) |
| 48 | + |
| 49 | + ret_val = self.notification_id |
| 50 | + |
| 51 | + self.notification_id += 1 |
| 52 | + |
| 53 | + return ret_val |
| 54 | + |
| 55 | + def remove_notification_listener(self, notification_id): |
| 56 | + """ Remove a previously added notification callback. |
| 57 | +
|
| 58 | + Args: |
| 59 | + notification_id: The numeric id passed back from add_notification_listener |
| 60 | +
|
| 61 | + Returns: |
| 62 | + The function returns boolean true if found and removed, false otherwise. |
| 63 | + """ |
| 64 | + |
| 65 | + for v in self.notifications.values(): |
| 66 | + toRemove = list(filter(lambda tup: tup[0] == notification_id, v)) |
| 67 | + if len(toRemove) > 0: |
| 68 | + v.remove(toRemove[0]) |
| 69 | + return True |
| 70 | + |
| 71 | + return False |
| 72 | + |
| 73 | + def clear_all_notifications(self): |
| 74 | + """ Remove all notifications """ |
| 75 | + for key in self.notifications.keys(): |
| 76 | + self.notifications[key] = [] |
| 77 | + |
| 78 | + def clear_notifications(self, notification_type): |
| 79 | + """ Remove notifications for a certain notification type |
| 80 | +
|
| 81 | + Args: |
| 82 | + notification_type: key to the list of notifications .helpers.enums.NotificationTypes |
| 83 | + """ |
| 84 | + |
| 85 | + self.notifications[notification_type] = [] |
| 86 | + |
| 87 | + def send_notifications(self, notification_type, *args): |
| 88 | + """ Fires off the notification for the specific event. Uses var args to pass in a |
| 89 | + arbitrary list of parameter according to which notification type was fired. |
| 90 | +
|
| 91 | + Args: |
| 92 | + notification_type: Type of notification to fire (String from .helpers.enums.NotificationTypes) |
| 93 | + args: variable list of arguments to the callback. |
| 94 | + """ |
| 95 | + |
| 96 | + if notification_type in self.notifications: |
| 97 | + for notification_id, callback in self.notifications[notification_type]: |
| 98 | + try: |
| 99 | + callback(*args) |
| 100 | + except: |
| 101 | + error = sys.exc_info()[1] |
| 102 | + self.logger.log(enums.LogLevels.ERROR, 'Problem calling notify callback. Error: %s' % str(error)) |
0 commit comments