""":organization: Logilab:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses"""importwebbrowserreload(webbrowser)fromsensor.SensorimportSensorfromutilsimportdatatypes,i18nfromcubicweb.dbapiimportconnect_=strclassRQLSensor(Sensor):def__init__(self,*args):global_;_=i18n.Translator("rql-desklet")Sensor.__init__(self)# define configurationself._set_config_type("appid",datatypes.TYPE_STRING,"")self._set_config_type("user",datatypes.TYPE_STRING,"")self._set_config_type("passwd",datatypes.TYPE_SECRET_STRING,"")self._set_config_type("rql",datatypes.TYPE_STRING,"")self._set_config_type("url",datatypes.TYPE_STRING,"")self._set_config_type("delay",datatypes.TYPE_STRING,"600")# default timerself._add_timer(20,self.__update)defget_configurator(self):configurator=self._new_configurator()configurator.set_name(_("RQL"))configurator.add_title(_("CubicWeb source settings"))configurator.add_entry(_("ID",),"appid",_("The application id of this source"))configurator.add_entry(_("User",),"user",_("The user to connect to this source"))configurator.add_entry(_("Password",),"passwd",_("The user's password to connect to this source"))configurator.add_entry(_("URL",),"url",_("The url of the web interface for this source"))configurator.add_entry(_("RQL",),"rql",_("The rql query"))configurator.add_entry(_("Update interval",),"delay",_("Delay in seconds between updates"))returnconfiguratordefcall_action(self,action,path,args=[]):index=path[-1]output=self._new_output()# import sys# print >>sys.stderr, action, path, argsifaction=="enter-line":# change backgroundoutput.set('resultbg[%s]'%index,'yellow')elifaction=="leave-line":# change backgroundoutput.set('resultbg[%s]'%index,'black')elifaction=="click-line":# open urloutput.set('resultbg[%s]'%index,'black')webbrowser.open(self._urls[index])self._send_output(output)def__get_connection(self):try:returnself._v_cnxexceptAttributeError:appid,user,passwd=self._get_config("appid"),self._get_config("user"),self._get_config("passwd")cnx=connect(database=appid,login=user,password=passwd)self._v_cnx=cnxreturncnxdef__run_query(self,output):base=self._get_config('url')rql=self._get_config('rql')cnx=self.__get_connection()cursor=cnx.cursor()try:rset=cursor.execute(rql)except:delself._v_cnxraiseself._urls=[]output.set('layout','vertical, 14')output.set('length',rset.rowcount)i=0forlineinrset:output.set('result[%s]'%i,', '.join([str(v)forvinline[1:]]))output.set('resultbg[%s]'%i,'black')try:self._urls.append(base%'Any X WHERE X eid %s'%line[0])except:self._urls.append('')i+=1def__update(self):output=self._new_output()try:self.__run_query(output)exceptException,ex:importtracebacktraceback.print_exc()output.set('layout','vertical, 10')output.set('length',1)output.set('result[0]',str(ex))self._send_output(output)self._add_timer(int(self._get_config('delay'))*1000,self.__update)defnew_sensor(args):returnRQLSensor(*args)