diff options
author | Martin Sivak <msivak@redhat.com> | 2008-04-18 14:40:49 +0200 |
---|---|---|
committer | Martin Sivak <msivak@redhat.com> | 2008-04-18 14:40:49 +0200 |
commit | 3c7dbfa8e712ea250f3da0aee2bc6d9eab5b265c (patch) | |
tree | 43ea4175aea2c00ba2ec71c374dae06bf1918232 /pyfirstaidkit | |
parent | 2fd148209df86076c626bce908dcbaa5bdb8d4ae (diff) | |
download | firstaidkit-3c7dbfa8e712ea250f3da0aee2bc6d9eab5b265c.tar.gz firstaidkit-3c7dbfa8e712ea250f3da0aee2bc6d9eab5b265c.tar.xz firstaidkit-3c7dbfa8e712ea250f3da0aee2bc6d9eab5b265c.zip |
Introduce the reporting queue support for circular buffer and callback notifications during put operation
Diffstat (limited to 'pyfirstaidkit')
-rw-r--r-- | pyfirstaidkit/reporting.py | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/pyfirstaidkit/reporting.py b/pyfirstaidkit/reporting.py index 1554b74..eefc88c 100644 --- a/pyfirstaidkit/reporting.py +++ b/pyfirstaidkit/reporting.py @@ -69,14 +69,39 @@ class Reports(object): title - title of the message """ - def __init__(self, maxsize=-1): + def __init__(self, maxsize=-1, round = False): + """round - is this a round buffer? + maxsize - size of the buffer""" self._queue = Queue.Queue(maxsize = maxsize) + self._round = round self._mailboxes = [] + self._notify = [] + + def notify(self, cb, data): + """When putting anything new into the Queue, run notifications callbacks. Usefull for Gui and single-thread reporting. + The notification function has two parameters: data passed when registering callback, message recorded to the queue""" + return self._notify.append((cb, data)) def put(self, message, level, origin, action, importance = logging.INFO, reply = None, title = "", destination = None): - if destination is None: - destination = self._queue - return destination.put({"level": level, "origin": origin, "action": action, "importance": importance, "message": message, "reply": reply, "title": title}) + data = {"level": level, "origin": origin, "action": action, "importance": importance, "message": message, "reply": reply, "title": title} + + if destination is not None: + return destination.put(data) + + destination = self._queue + try: + ret=destination.put(data) + except Queue.Full, e: + if not self._round: + raise + destination.get() #Queue is full and it is a round buffer.. remove the oldest item and use the free space to put the new item + ret=destination.put(data) + + #call all the notify callbacks + for func, regdata in self._notify: + func(regdata, data) + + return ret def get(self, mailbox = None, *args, **kwargs): if mailbox is None: |