00001 ############################################################################## 00002 # 00003 # Copyright (c) 2009 Albert Cervera i Areny <albert@nan-tic.com> 00004 # 00005 # WARNING: This program as such is intended to be used by professional 00006 # programmers who take the whole responsability of assessing all potential 00007 # consequences resulting from its eventual inadequacies and bugs 00008 # End users who are looking for a ready-to-use solution with commercial 00009 # garantees and support are strongly adviced to contract a Free Software 00010 # Service Company 00011 # 00012 # This program is Free Software; you can redistribute it and/or 00013 # modify it under the terms of the GNU General Public License 00014 # as published by the Free Software Foundation; either version 2 00015 # of the License, or (at your option) any later version. 00016 # 00017 # This program is distributed in the hope that it will be useful, 00018 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 # GNU General Public License for more details. 00021 # 00022 # You should have received a copy of the GNU General Public License 00023 # along with this program; if not, write to the Free Software 00024 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00025 # 00026 ############################################################################## 00027 00028 from PyQt4.QtCore import * 00029 from time import sleep 00030 00031 ## @brief The Subscriber class provides a mechanisme for subscribing to server events. 00032 # 00033 # In order to use this class effectively you'll need the koo module installed on the server. 00034 # This module adds a new /subscription service. You can use this class in conjunction with 00035 # your own server modules which can publish events. By default, the koo module already publishes 00036 # events on any update/create/delete operation on a model. 00037 # 00038 # If the 'koo' module is not installed the Subscription service won't emit any signals, but won't 00039 # return any errors either. 00040 # 00041 # Example of usage: 00042 # 00043 # self.subscriber = Rpc.Subscriber(Rpc.session, self) 00044 # self.subscriber.subscribe( 'updated_model:res.request', self.updateRequestsStatus ) 00045 # 00046 # This example will emit a signal (call self.updateRequestsStatus) each time a changed occurs 00047 # on any record in 'res.request' model. 00048 00049 class Subscriber(QThread): 00050 ## @brief Creates a new Subscriber object from the given session and with 'parent' as QObject parent. 00051 def __init__(self, session, parent=None): 00052 QThread.__init__(self, parent) 00053 self.session = session.copy() 00054 self.slot = None 00055 00056 ## @brief Subscribes to the given 'expression' event on the server. And calls 'slot' each 00057 # time the given event is published. 00058 def subscribe(self, expression, slot = None): 00059 self.expression = expression 00060 self.slot = slot 00061 if self.slot: 00062 self.connect( self, SIGNAL('published()'), self.slot ) 00063 self.start() 00064 00065 ## @brief Unsubscribes from the previously subscribed event. 00066 # 00067 # If subscribe() wasn't previously called, nothing happens. 00068 def unsubscribe(self): 00069 if self.slot: 00070 self.disconnect( self, SIGNAL('published()'), self.slot ) 00071 self.terminate() 00072 00073 def run(self): 00074 while True: 00075 try: 00076 self.result = self.session.call( '/subscription', 'wait', self.expression ) 00077 self.emit( SIGNAL('published()') ) 00078 except Exception, err: 00079 sleep( 60 ) 00080