misc/cwdesklets/rqlsensor/__init__.py
changeset 11057 0b59724cb3f2
parent 11052 058bb3dc685f
child 11058 23eb30449fe5
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
     1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 import webbrowser
       
    19 reload(webbrowser)
       
    20 
       
    21 from sensor.Sensor import Sensor
       
    22 from utils import datatypes, i18n
       
    23 
       
    24 from cubicweb.dbapi import connect
       
    25 
       
    26 _ = str
       
    27 
       
    28 class RQLSensor(Sensor):
       
    29 
       
    30     def __init__(self, *args):
       
    31         global _; _ = i18n.Translator("rql-desklet")
       
    32         Sensor.__init__(self)
       
    33         # define configuration
       
    34         self._set_config_type("appid", datatypes.TYPE_STRING, "")
       
    35         self._set_config_type("user", datatypes.TYPE_STRING, "")
       
    36         self._set_config_type("passwd", datatypes.TYPE_SECRET_STRING, "")
       
    37         self._set_config_type("rql", datatypes.TYPE_STRING, "")
       
    38         self._set_config_type("url", datatypes.TYPE_STRING, "")
       
    39         self._set_config_type("delay", datatypes.TYPE_STRING, "600")
       
    40         # default timer
       
    41         self._add_timer(20, self.__update)
       
    42 
       
    43     def get_configurator(self):
       
    44         configurator = self._new_configurator()
       
    45         configurator.set_name(_("RQL"))
       
    46         configurator.add_title(_("CubicWeb source settings"))
       
    47         configurator.add_entry(_("ID",), "appid", _("The application id of this source"))
       
    48         configurator.add_entry(_("User",), "user", _("The user to connect to this source"))
       
    49         configurator.add_entry(_("Password",), "passwd", _("The user's password to connect to this source"))
       
    50         configurator.add_entry(_("URL",), "url", _("The url of the web interface for this source"))
       
    51         configurator.add_entry(_("RQL",), "rql", _("The rql query"))
       
    52         configurator.add_entry(_("Update interval",), "delay", _("Delay in seconds between updates"))
       
    53         return configurator
       
    54 
       
    55 
       
    56     def call_action(self, action, path, args=[]):
       
    57         index = path[-1]
       
    58         output = self._new_output()
       
    59         if action=="enter-line":
       
    60             # change background
       
    61             output.set('resultbg[%s]' % index, 'yellow')
       
    62         elif action=="leave-line":
       
    63             # change background
       
    64             output.set('resultbg[%s]' % index, 'black')
       
    65         elif action=="click-line":
       
    66             # open url
       
    67             output.set('resultbg[%s]' % index, 'black')
       
    68             webbrowser.open(self._urls[index])
       
    69         self._send_output(output)
       
    70 
       
    71     def __get_connection(self):
       
    72         try:
       
    73             return self._v_cnx
       
    74         except AttributeError:
       
    75             appid, user, passwd = self._get_config("appid"), self._get_config("user"), self._get_config("passwd")
       
    76             cnx = connect(database=appid, login=user, password=passwd)
       
    77             self._v_cnx = cnx
       
    78             return cnx
       
    79 
       
    80     def __run_query(self, output):
       
    81         base = self._get_config('url')
       
    82         rql = self._get_config('rql')
       
    83         cnx = self.__get_connection()
       
    84         cursor = cnx.cursor()
       
    85         try:
       
    86             rset = cursor.execute(rql)
       
    87         except Exception:
       
    88             del self._v_cnx
       
    89             raise
       
    90         self._urls = []
       
    91         output.set('layout', 'vertical, 14')
       
    92         output.set('length', rset.rowcount)
       
    93         i = 0
       
    94         for line in rset:
       
    95             output.set('result[%s]' % i, ', '.join([str(v) for v in line[1:]]))
       
    96             output.set('resultbg[%s]' % i, 'black')
       
    97             try:
       
    98                 self._urls.append(base % 'Any X WHERE X eid %s' % line[0])
       
    99             except Exception:
       
   100                 self._urls.append('')
       
   101             i += 1
       
   102 
       
   103     def __update(self):
       
   104         output = self._new_output()
       
   105         try:
       
   106             self.__run_query(output)
       
   107         except Exception as ex:
       
   108             import traceback
       
   109             traceback.print_exc()
       
   110             output.set('layout', 'vertical, 10')
       
   111             output.set('length', 1)
       
   112             output.set('result[0]', str(ex))
       
   113         self._send_output(output)
       
   114         self._add_timer(int(self._get_config('delay'))*1000, self.__update)
       
   115 
       
   116 
       
   117 def new_sensor(args):
       
   118     return RQLSensor(*args)