author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Wed, 28 May 2014 17:33:16 +0200 | |
changeset 9786 | 57dbd4450373 |
parent 8695 | 358d8bed9626 |
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/>. |
0 | 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) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
70 |
|
0 | 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") |
|
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
|
76 |
cnx = connect(database=appid, login=user, password=passwd) |
0 | 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) |
|
7815
2a164a9cf81c
[exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
87 |
except Exception: |
0 | 88 |
del self._v_cnx |
89 |
raise |
|
90 |
self._urls = [] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
91 |
output.set('layout', 'vertical, 14') |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
92 |
output.set('length', rset.rowcount) |
0 | 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]) |
|
7815
2a164a9cf81c
[exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
99 |
except Exception: |
0 | 100 |
self._urls.append('') |
101 |
i += 1 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
102 |
|
0 | 103 |
def __update(self): |
104 |
output = self._new_output() |
|
105 |
try: |
|
106 |
self.__run_query(output) |
|
8695
358d8bed9626
[toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
107 |
except Exception as ex: |
0 | 108 |
import traceback |
109 |
traceback.print_exc() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
110 |
output.set('layout', 'vertical, 10') |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
111 |
output.set('length', 1) |
0 | 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 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
116 |
|
0 | 117 |
def new_sensor(args): |
118 |
return RQLSensor(*args) |