author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 12 Oct 2011 19:06:22 +0200 | |
branch | stable |
changeset 7941 | 018b5deca73e |
parent 7815 | 2a164a9cf81c |
child 7896 | 4c954e1e73ef |
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 |
# import sys |
|
60 |
# print >>sys.stderr, action, path, args |
|
61 |
if action=="enter-line": |
|
62 |
# change background |
|
63 |
output.set('resultbg[%s]' % index, 'yellow') |
|
64 |
elif action=="leave-line": |
|
65 |
# change background |
|
66 |
output.set('resultbg[%s]' % index, 'black') |
|
67 |
elif action=="click-line": |
|
68 |
# open url |
|
69 |
output.set('resultbg[%s]' % index, 'black') |
|
70 |
webbrowser.open(self._urls[index]) |
|
71 |
self._send_output(output) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
72 |
|
0 | 73 |
def __get_connection(self): |
74 |
try: |
|
75 |
return self._v_cnx |
|
76 |
except AttributeError: |
|
77 |
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
|
78 |
cnx = connect(database=appid, login=user, password=passwd) |
0 | 79 |
self._v_cnx = cnx |
80 |
return cnx |
|
81 |
||
82 |
def __run_query(self, output): |
|
83 |
base = self._get_config('url') |
|
84 |
rql = self._get_config('rql') |
|
85 |
cnx = self.__get_connection() |
|
86 |
cursor = cnx.cursor() |
|
87 |
try: |
|
88 |
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
|
89 |
except Exception: |
0 | 90 |
del self._v_cnx |
91 |
raise |
|
92 |
self._urls = [] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
93 |
output.set('layout', 'vertical, 14') |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
94 |
output.set('length', rset.rowcount) |
0 | 95 |
i = 0 |
96 |
for line in rset: |
|
97 |
output.set('result[%s]' % i, ', '.join([str(v) for v in line[1:]])) |
|
98 |
output.set('resultbg[%s]' % i, 'black') |
|
99 |
try: |
|
100 |
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
|
101 |
except Exception: |
0 | 102 |
self._urls.append('') |
103 |
i += 1 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
104 |
|
0 | 105 |
def __update(self): |
106 |
output = self._new_output() |
|
107 |
try: |
|
108 |
self.__run_query(output) |
|
109 |
except Exception, ex: |
|
110 |
import traceback |
|
111 |
traceback.print_exc() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
112 |
output.set('layout', 'vertical, 10') |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
113 |
output.set('length', 1) |
0 | 114 |
output.set('result[0]', str(ex)) |
115 |
self._send_output(output) |
|
116 |
self._add_timer(int(self._get_config('delay'))*1000, self.__update) |
|
117 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
118 |
|
0 | 119 |
def new_sensor(args): |
120 |
return RQLSensor(*args) |