|
1 import webbrowser |
|
2 reload(webbrowser) |
|
3 |
|
4 from sensor.Sensor import Sensor |
|
5 from utils import datatypes, i18n |
|
6 |
|
7 from cubicweb.dbapi import connect |
|
8 |
|
9 _ = str |
|
10 |
|
11 class RQLSensor(Sensor): |
|
12 |
|
13 def __init__(self, *args): |
|
14 global _; _ = i18n.Translator("rql-desklet") |
|
15 Sensor.__init__(self) |
|
16 # define configuration |
|
17 self._set_config_type("appid", datatypes.TYPE_STRING, "") |
|
18 self._set_config_type("user", datatypes.TYPE_STRING, "") |
|
19 self._set_config_type("passwd", datatypes.TYPE_SECRET_STRING, "") |
|
20 self._set_config_type("rql", datatypes.TYPE_STRING, "") |
|
21 self._set_config_type("url", datatypes.TYPE_STRING, "") |
|
22 self._set_config_type("delay", datatypes.TYPE_STRING, "600") |
|
23 # default timer |
|
24 self._add_timer(20, self.__update) |
|
25 |
|
26 def get_configurator(self): |
|
27 configurator = self._new_configurator() |
|
28 configurator.set_name(_("RQL")) |
|
29 configurator.add_title(_("CubicWeb source settings")) |
|
30 configurator.add_entry(_("ID",), "appid", _("The application id of this source")) |
|
31 configurator.add_entry(_("User",), "user", _("The user to connect to this source")) |
|
32 configurator.add_entry(_("Password",), "passwd", _("The user's password to connect to this source")) |
|
33 configurator.add_entry(_("URL",), "url", _("The url of the web interface for this source")) |
|
34 configurator.add_entry(_("RQL",), "rql", _("The rql query")) |
|
35 configurator.add_entry(_("Update interval",), "delay", _("Delay in seconds between updates")) |
|
36 return configurator |
|
37 |
|
38 |
|
39 def call_action(self, action, path, args=[]): |
|
40 index = path[-1] |
|
41 output = self._new_output() |
|
42 # import sys |
|
43 # print >>sys.stderr, action, path, args |
|
44 if action=="enter-line": |
|
45 # change background |
|
46 output.set('resultbg[%s]' % index, 'yellow') |
|
47 elif action=="leave-line": |
|
48 # change background |
|
49 output.set('resultbg[%s]' % index, 'black') |
|
50 elif action=="click-line": |
|
51 # open url |
|
52 output.set('resultbg[%s]' % index, 'black') |
|
53 webbrowser.open(self._urls[index]) |
|
54 self._send_output(output) |
|
55 |
|
56 def __get_connection(self): |
|
57 try: |
|
58 return self._v_cnx |
|
59 except AttributeError: |
|
60 appid, user, passwd = self._get_config("appid"), self._get_config("user"), self._get_config("passwd") |
|
61 cnx = connect(database=appid, user=user, password=passwd) |
|
62 self._v_cnx = cnx |
|
63 return cnx |
|
64 |
|
65 def __run_query(self, output): |
|
66 base = self._get_config('url') |
|
67 rql = self._get_config('rql') |
|
68 cnx = self.__get_connection() |
|
69 cursor = cnx.cursor() |
|
70 try: |
|
71 rset = cursor.execute(rql) |
|
72 except: |
|
73 del self._v_cnx |
|
74 raise |
|
75 self._urls = [] |
|
76 output.set('layout', 'vertical, 14') |
|
77 output.set('length', rset.rowcount) |
|
78 i = 0 |
|
79 for line in rset: |
|
80 output.set('result[%s]' % i, ', '.join([str(v) for v in line[1:]])) |
|
81 output.set('resultbg[%s]' % i, 'black') |
|
82 try: |
|
83 self._urls.append(base % 'Any X WHERE X eid %s' % line[0]) |
|
84 except: |
|
85 self._urls.append('') |
|
86 i += 1 |
|
87 |
|
88 def __update(self): |
|
89 output = self._new_output() |
|
90 try: |
|
91 self.__run_query(output) |
|
92 except Exception, ex: |
|
93 import traceback |
|
94 traceback.print_exc() |
|
95 output.set('layout', 'vertical, 10') |
|
96 output.set('length', 1) |
|
97 output.set('result[0]', str(ex)) |
|
98 self._send_output(output) |
|
99 self._add_timer(int(self._get_config('delay'))*1000, self.__update) |
|
100 |
|
101 |
|
102 def new_sensor(args): |
|
103 return RQLSensor(*args) |