##// END OF EJS Templates
Make glob expansion default in %run magic command
Make glob expansion default in %run magic command

File last commit:

r5390:c82649ea
r7947:afb25d01
Show More
notification.py
142 lines | 4.5 KiB | text/x-python | PythonLexer
Barry Wark
I.k.c.notification blueprint, test, implementation.
r1410 # encoding: utf-8
Brian Granger
Semi-working refactored ipcluster....
r2302 """
The IPython Core Notification Center.
Barry Wark
I.k.c.notification blueprint, test, implementation.
r1410
Barry Wark
changed notification.py instance variables to underscore_case
r1443 See docs/source/development/notification_blueprint.txt for an overview of the
Barry Wark
I.k.c.notification blueprint, test, implementation.
r1410 notification module.
Brian Granger
Semi-working refactored ipcluster....
r2302
Authors:
* Barry Wark
* Brian Granger
Barry Wark
I.k.c.notification blueprint, test, implementation.
r1410 """
#-----------------------------------------------------------------------------
Matthias BUSSONNIER
update copyright to 2011/20xx-2011...
r5390 # Copyright (C) 2008-2011 The IPython Development Team
Brian Granger
Semi-working refactored ipcluster....
r2302 #
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Code
Barry Wark
I.k.c.notification blueprint, test, implementation.
r1410 #-----------------------------------------------------------------------------
Brian Granger
Semi-working refactored ipcluster....
r2302
class NotificationError(Exception):
pass
Barry Wark
I.k.c.notification blueprint, test, implementation.
r1410
class NotificationCenter(object):
Brian Granger
Semi-working refactored ipcluster....
r2302 """Synchronous notification center.
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Small fixes to get a cleaner doc build, and junk removal....
r2109 Examples
--------
Brian Granger
Semi-working refactored ipcluster....
r2302 Here is a simple example of how to use this::
MinRK
remove IPython.kernel scripts and put migration notice in docs....
r3520 import IPython.util.notification as notification
Brian Granger
Semi-working refactored ipcluster....
r2302 def callback(ntype, theSender, args={}):
print ntype,theSender,args
notification.sharedCenter.add_observer(callback, 'NOTIFICATION_TYPE', None)
notification.sharedCenter.post_notification('NOTIFICATION_TYPE', object()) # doctest:+ELLIPSIS
NOTIFICATION_TYPE ...
Barry Wark
doctest example for NotificationCenter
r1412 """
Barry Wark
I.k.c.notification blueprint, test, implementation.
r1410 def __init__(self):
super(NotificationCenter, self).__init__()
Barry Wark
fixed None for type/sender
r1411 self._init_observers()
Brian Granger
Semi-working refactored ipcluster....
r2302
Barry Wark
fixed None for type/sender
r1411 def _init_observers(self):
"""Initialize observer storage"""
Brian Granger
Semi-working refactored ipcluster....
r2302
Barry Wark
changed notification.py instance variables to underscore_case
r1443 self.registered_types = set() #set of types that are observed
self.registered_senders = set() #set of senders that are observed
Barry Wark
I.k.c.notification blueprint, test, implementation.
r1410 self.observers = {} #map (type,sender) => callback (callable)
Fernando Perez
Small fixes to get a cleaner doc build, and junk removal....
r2109
Brian Granger
Semi-working refactored ipcluster....
r2302 def post_notification(self, ntype, sender, *args, **kwargs):
"""Post notification to all registered observers.
The registered callback will be called as::
callback(ntype, sender, *args, **kwargs)
Parameters
----------
ntype : hashable
The notification type.
sender : hashable
The object sending the notification.
*args : tuple
The positional arguments to be passed to the callback.
**kwargs : dict
The keyword argument to be passed to the callback.
Fernando Perez
Small fixes to get a cleaner doc build, and junk removal....
r2109
Brian Granger
Semi-working refactored ipcluster....
r2302 Notes
-----
Barry Wark
I.k.c.notification blueprint, test, implementation.
r1410 * If no registered observers, performance is O(1).
* Notificaiton order is undefined.
* Notifications are posted synchronously.
"""
Brian Granger
Semi-working refactored ipcluster....
r2302
if(ntype==None or sender==None):
raise NotificationError(
"Notification type and sender are required.")
Barry Wark
I.k.c.notification blueprint, test, implementation.
r1410 # If there are no registered observers for the type/sender pair
Bernardo B. Marques
remove all trailling spaces
r4872 if((ntype not in self.registered_types and
Barry Wark
changed notification.py instance variables to underscore_case
r1443 None not in self.registered_types) or
Bernardo B. Marques
remove all trailling spaces
r4872 (sender not in self.registered_senders and
Barry Wark
changed notification.py instance variables to underscore_case
r1443 None not in self.registered_senders)):
Barry Wark
I.k.c.notification blueprint, test, implementation.
r1410 return
Brian Granger
Semi-working refactored ipcluster....
r2302
for o in self._observers_for_notification(ntype, sender):
o(ntype, sender, *args, **kwargs)
def _observers_for_notification(self, ntype, sender):
Barry Wark
fixed None for type/sender
r1411 """Find all registered observers that should recieve notification"""
Brian Granger
Semi-working refactored ipcluster....
r2302
Barry Wark
fixed None for type/sender
r1411 keys = (
Brian Granger
Semi-working refactored ipcluster....
r2302 (ntype,sender),
(ntype, None),
(None, sender),
(None,None)
)
Barry Wark
fixed None for type/sender
r1411 obs = set()
for k in keys:
obs.update(self.observers.get(k, set()))
Brian Granger
Semi-working refactored ipcluster....
r2302
Barry Wark
fixed None for type/sender
r1411 return obs
Brian Granger
Semi-working refactored ipcluster....
r2302
def add_observer(self, callback, ntype, sender):
Barry Wark
I.k.c.notification blueprint, test, implementation.
r1410 """Add an observer callback to this notification center.
Bernardo B. Marques
remove all trailling spaces
r4872
Barry Wark
I.k.c.notification blueprint, test, implementation.
r1410 The given callback will be called upon posting of notifications of
Brian Granger
Semi-working refactored ipcluster....
r2302 the given type/sender and will receive any additional arguments passed
Barry Wark
I.k.c.notification blueprint, test, implementation.
r1410 to post_notification.
Bernardo B. Marques
remove all trailling spaces
r4872
Barry Wark
I.k.c.notification blueprint, test, implementation.
r1410 Parameters
----------
Brian Granger
Semi-working refactored ipcluster....
r2302 callback : callable
The callable that will be called by :meth:`post_notification`
as ``callback(ntype, sender, *args, **kwargs)
ntype : hashable
Barry Wark
I.k.c.notification blueprint, test, implementation.
r1410 The notification type. If None, all notifications from sender
will be posted.
sender : hashable
Brian Granger
Semi-working refactored ipcluster....
r2302 The notification sender. If None, all notifications of ntype
Barry Wark
I.k.c.notification blueprint, test, implementation.
r1410 will be posted.
"""
Barry Wark
fixed None for type/sender
r1411 assert(callback != None)
Brian Granger
Semi-working refactored ipcluster....
r2302 self.registered_types.add(ntype)
Barry Wark
changed notification.py instance variables to underscore_case
r1443 self.registered_senders.add(sender)
Brian Granger
Semi-working refactored ipcluster....
r2302 self.observers.setdefault((ntype,sender), set()).add(callback)
Bernardo B. Marques
remove all trailling spaces
r4872
Barry Wark
fixed None for type/sender
r1411 def remove_all_observers(self):
"""Removes all observers from this notification center"""
Bernardo B. Marques
remove all trailling spaces
r4872
Barry Wark
fixed None for type/sender
r1411 self._init_observers()
Bernardo B. Marques
remove all trailling spaces
r4872
Barry Wark
I.k.c.notification blueprint, test, implementation.
r1410
Brian Granger
Semi-working refactored ipcluster....
r2302 shared_center = NotificationCenter()