author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Thu, 14 May 2009 11:38:40 +0200 | |
branch | tls-sprint |
changeset 1802 | d628defebc17 |
parent 0 | b97547f5f1fa |
child 1977 | 606923dff11b |
permissions | -rw-r--r-- |
0 | 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) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
55 |
|
0 | 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 = [] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
76 |
output.set('layout', 'vertical, 14') |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
77 |
output.set('length', rset.rowcount) |
0 | 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 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
87 |
|
0 | 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() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
95 |
output.set('layout', 'vertical, 10') |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
96 |
output.set('length', 1) |
0 | 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 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
101 |
|
0 | 102 |
def new_sensor(args): |
103 |
return RQLSensor(*args) |