author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 13 Jan 2010 15:57:57 +0100 | |
changeset 4226 | 67dd296f864d |
parent 2388 | fddb0fd11321 |
child 4212 | ab6573088b4a |
permissions | -rw-r--r-- |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
1 |
""" |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
2 |
|
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
3 |
:organization: Logilab |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
7 |
""" |
0 | 8 |
import webbrowser |
9 |
reload(webbrowser) |
|
10 |
||
11 |
from sensor.Sensor import Sensor |
|
12 |
from utils import datatypes, i18n |
|
13 |
||
14 |
from cubicweb.dbapi import connect |
|
15 |
||
16 |
_ = str |
|
17 |
||
18 |
class RQLSensor(Sensor): |
|
19 |
||
20 |
def __init__(self, *args): |
|
21 |
global _; _ = i18n.Translator("rql-desklet") |
|
22 |
Sensor.__init__(self) |
|
23 |
# define configuration |
|
24 |
self._set_config_type("appid", datatypes.TYPE_STRING, "") |
|
25 |
self._set_config_type("user", datatypes.TYPE_STRING, "") |
|
26 |
self._set_config_type("passwd", datatypes.TYPE_SECRET_STRING, "") |
|
27 |
self._set_config_type("rql", datatypes.TYPE_STRING, "") |
|
28 |
self._set_config_type("url", datatypes.TYPE_STRING, "") |
|
29 |
self._set_config_type("delay", datatypes.TYPE_STRING, "600") |
|
30 |
# default timer |
|
31 |
self._add_timer(20, self.__update) |
|
32 |
||
33 |
def get_configurator(self): |
|
34 |
configurator = self._new_configurator() |
|
35 |
configurator.set_name(_("RQL")) |
|
36 |
configurator.add_title(_("CubicWeb source settings")) |
|
37 |
configurator.add_entry(_("ID",), "appid", _("The application id of this source")) |
|
38 |
configurator.add_entry(_("User",), "user", _("The user to connect to this source")) |
|
39 |
configurator.add_entry(_("Password",), "passwd", _("The user's password to connect to this source")) |
|
40 |
configurator.add_entry(_("URL",), "url", _("The url of the web interface for this source")) |
|
41 |
configurator.add_entry(_("RQL",), "rql", _("The rql query")) |
|
42 |
configurator.add_entry(_("Update interval",), "delay", _("Delay in seconds between updates")) |
|
43 |
return configurator |
|
44 |
||
45 |
||
46 |
def call_action(self, action, path, args=[]): |
|
47 |
index = path[-1] |
|
48 |
output = self._new_output() |
|
49 |
# import sys |
|
50 |
# print >>sys.stderr, action, path, args |
|
51 |
if action=="enter-line": |
|
52 |
# change background |
|
53 |
output.set('resultbg[%s]' % index, 'yellow') |
|
54 |
elif action=="leave-line": |
|
55 |
# change background |
|
56 |
output.set('resultbg[%s]' % index, 'black') |
|
57 |
elif action=="click-line": |
|
58 |
# open url |
|
59 |
output.set('resultbg[%s]' % index, 'black') |
|
60 |
webbrowser.open(self._urls[index]) |
|
61 |
self._send_output(output) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
62 |
|
0 | 63 |
def __get_connection(self): |
64 |
try: |
|
65 |
return self._v_cnx |
|
66 |
except AttributeError: |
|
67 |
appid, user, passwd = self._get_config("appid"), self._get_config("user"), self._get_config("passwd") |
|
2388
fddb0fd11321
[api] update dbapi.connect() calls to match new prototype (user parameter is now named login)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
68 |
cnx = connect(database=appid, login=user, password=passwd) |
0 | 69 |
self._v_cnx = cnx |
70 |
return cnx |
|
71 |
||
72 |
def __run_query(self, output): |
|
73 |
base = self._get_config('url') |
|
74 |
rql = self._get_config('rql') |
|
75 |
cnx = self.__get_connection() |
|
76 |
cursor = cnx.cursor() |
|
77 |
try: |
|
78 |
rset = cursor.execute(rql) |
|
79 |
except: |
|
80 |
del self._v_cnx |
|
81 |
raise |
|
82 |
self._urls = [] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
83 |
output.set('layout', 'vertical, 14') |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
84 |
output.set('length', rset.rowcount) |
0 | 85 |
i = 0 |
86 |
for line in rset: |
|
87 |
output.set('result[%s]' % i, ', '.join([str(v) for v in line[1:]])) |
|
88 |
output.set('resultbg[%s]' % i, 'black') |
|
89 |
try: |
|
90 |
self._urls.append(base % 'Any X WHERE X eid %s' % line[0]) |
|
91 |
except: |
|
92 |
self._urls.append('') |
|
93 |
i += 1 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
94 |
|
0 | 95 |
def __update(self): |
96 |
output = self._new_output() |
|
97 |
try: |
|
98 |
self.__run_query(output) |
|
99 |
except Exception, ex: |
|
100 |
import traceback |
|
101 |
traceback.print_exc() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
102 |
output.set('layout', 'vertical, 10') |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
103 |
output.set('length', 1) |
0 | 104 |
output.set('result[0]', str(ex)) |
105 |
self._send_output(output) |
|
106 |
self._add_timer(int(self._get_config('delay'))*1000, self.__update) |
|
107 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
108 |
|
0 | 109 |
def new_sensor(args): |
110 |
return RQLSensor(*args) |