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