~kiddo/specto/pygi

152 by Francesco Marella
Fix the UTF-8 file headers
1
# -*- coding: utf-8 -*-
25 by chalserogers
(Finally) Merge the module-organisation branch into trunk.
2
3
# Specto , Unobtrusive event notifier
4
#
5
#       watch.py
6
#
98.2.1 by Jean-François Fortin Tam
Clean up the copyright headers to ease maintenance and relevance
7
# See the AUTHORS file for copyright ownership information
25 by chalserogers
(Finally) Merge the module-organisation branch into trunk.
8
9
# This program is free software; you can redistribute it and/or
10
# modify it under the terms of the GNU General Public
11
# License as published by the Free Software Foundation; either
124 by Jean-François Fortin Tam
Fix the GPL version number, 2 instead of 2.1 (which doesn't exist)
12
# version 2 of the License, or (at your option) any later version.
25 by chalserogers
(Finally) Merge the module-organisation branch into trunk.
13
#
14
# This program is distributed in the hope that it will be useful,
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
# General Public License for more details.
18
#
19
# You should have received a copy of the GNU General Public
20
# License along with this program; if not, write to the
21
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22
# Boston, MA 02111-1307, USA.
98.1.24 by Wout Clymans
- Issue 162: Move towards pep8 (PART III)
23
import os
24
import sys
25
import time
25 by chalserogers
(Finally) Merge the module-organisation branch into trunk.
26
import gobject
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
27
import thread
164 by Jean-François Fortin Tam
First pass at porting from PyGTK to PyGI
28
from gi.repository import Gtk
98.2.32 by Jean-François Fortin Tam
Fix issue #255, and create an "escape" method to sanitize HTML entities
29
from cgi import escape
25 by chalserogers
(Finally) Merge the module-organisation branch into trunk.
30
120.1.1 by Wout Clymans
- Added pidgin watch
31
try:
32
    import dbus
33
    DBUS = True
34
except:
35
    DBUS = False
36
25 by chalserogers
(Finally) Merge the module-organisation branch into trunk.
37
#specto imports
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
38
import spectlib.config
39
from spectlib.tools.iniparser import ini_namespace
25 by chalserogers
(Finally) Merge the module-organisation branch into trunk.
40
from ConfigParser import ConfigParser
41
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
42
from datetime import datetime
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
43
155.1.12 by Francesco Marella
spectlib/watch: early set keyring
44
keyring = True
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
45
try:
46
    from spectlib.tools.keyringmanager import Keyring
47
except:
48
    keyring = False
69 by nekohayo
* Happy birthday to myself ;)
49
98.1.24 by Wout Clymans
- Issue 162: Move towards pep8 (PART III)
50
25 by chalserogers
(Finally) Merge the module-organisation branch into trunk.
51
def gettext_noop(s):
98.1.24 by Wout Clymans
- Issue 162: Move towards pep8 (PART III)
52
    return s
53
25 by chalserogers
(Finally) Merge the module-organisation branch into trunk.
54
55
class Watch:
98.1.24 by Wout Clymans
- Issue 162: Move towards pep8 (PART III)
56
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
57
    def __init__(self, specto, id, values, watch_values):
58
        self.specto = specto
59
        watch_values.extend([('name', spectlib.config.String(True)),
60
                            ('refresh', spectlib.config.Integer(True)),
61
                            ('type', spectlib.config.String(True)),
85.2.9 by Jean-François Fortin Tam
cleanup and major changes in terminology for the 0.3 series, as discussed on the mailing list
62
                            ('changed', spectlib.config.Boolean(False)),
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
63
                            ('command', spectlib.config.String(False)),
64
                            ('active', spectlib.config.Boolean(False)),
85.2.9 by Jean-François Fortin Tam
cleanup and major changes in terminology for the 0.3 series, as discussed on the mailing list
65
                            ('last_changed', spectlib.config.String(False)),
98.1.24 by Wout Clymans
- Issue 162: Move towards pep8 (PART III)
66
                            ('open_command', spectlib.config.String(False))])
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
67
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
68
        self.id = id
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
69
        self.use_network = False
70
        self.error = False
85.2.9 by Jean-François Fortin Tam
cleanup and major changes in terminology for the 0.3 series, as discussed on the mailing list
71
        self.actually_changed = False
120.1.1 by Wout Clymans
- Added pidgin watch
72
        self.dbus = False
73
        self.session_bus = None
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
74
        self.timer_id = -1
75
        self.deleted = False
98.3.29 by Wout Clymans
- Started Issue 221: error notifications should be meaningful (TODO: use new functions in other watches)
76
        self.error_message = ""
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
77
78
        self.watch_values = watch_values
79
        self.set_values(values)
98.1.24 by Wout Clymans
- Issue 162: Move towards pep8 (PART III)
80
85.1.15 by Wout Clymans
- Fixed importing watches (also from specto 0.2)
81
        self.watch_io = Watch_io(self.specto, self.specto.FILE)
98.1.24 by Wout Clymans
- Issue 162: Move towards pep8 (PART III)
82
25 by chalserogers
(Finally) Merge the module-organisation branch into trunk.
83
        global _
98.1.24 by Wout Clymans
- Issue 162: Move towards pep8 (PART III)
84
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
85
    def start(self):
86
        """ Start the watch. """
98.1.24 by Wout Clymans
- Issue 162: Move towards pep8 (PART III)
87
        try:
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
88
            self.active = True
89
            self.watch_io.write_option(self.name, 'active', self.active)
85.2.9 by Jean-François Fortin Tam
cleanup and major changes in terminology for the 0.3 series, as discussed on the mailing list
90
            self.start_checking()
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
91
        except:
92
            self.error = True
98.3.29 by Wout Clymans
- Started Issue 221: error notifications should be meaningful (TODO: use new functions in other watches)
93
            self.set_error(_("There was an error starting the watch"))
98.1.24 by Wout Clymans
- Issue 162: Move towards pep8 (PART III)
94
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
95
    def stop(self):
25 by chalserogers
(Finally) Merge the module-organisation branch into trunk.
96
        """ Stop the watch. """
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
97
        try:
98
            self.active = False
99
            self.watch_io.write_option(self.name, 'active', self.active)
120.1.1 by Wout Clymans
- Added pidgin watch
100
            if self.dbus and self.session_bus:
101
                for signal, call_back in self.signals.items():
102
                    self.session_bus.remove_signal_receiver(
103
                        call_back,
104
                        signal,
105
                        self.dbus_interface,
106
                        self.dbus_name,
107
                        self.dbus_path)
108
            else:
164 by Jean-François Fortin Tam
First pass at porting from PyGTK to PyGI
109
                GObject.source_remove(self.timer_id)
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
110
        except:
111
            self.error = True
98.3.29 by Wout Clymans
- Started Issue 221: error notifications should be meaningful (TODO: use new functions in other watches)
112
            self.set_error(_("There was an error stopping the watch"))
25 by chalserogers
(Finally) Merge the module-organisation branch into trunk.
113
98.3.12 by Wout Clymans
- Fixed Issue 193: clean up watch statuses/actions names and ensure they match the terminology
114
    def mark_as_read(self):
115
        """ mark the watch as read """
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
116
        try:
85.2.9 by Jean-François Fortin Tam
cleanup and major changes in terminology for the 0.3 series, as discussed on the mailing list
117
            self.changed = False
118
            self.watch_io.write_option(self.name, 'changed', self.changed)
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
119
            if not self.error:
98.3.62 by Wout Clymans
Added indicator support to specto (requires python-indicate)
120
                self.specto.mark_watch_status("read", self.id)
98.3.12 by Wout Clymans
- Fixed Issue 193: clean up watch statuses/actions names and ensure they match the terminology
121
                self.specto.mark_watch_status("idle", self.id)
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
122
        except:
98.3.29 by Wout Clymans
- Started Issue 221: error notifications should be meaningful (TODO: use new functions in other watches)
123
            self.set_error(_("There was an error marking the watch as read"))
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
124
125
    def restart(self):
126
        """ restart the watch """
127
        if self.active == True:
128
            self.stop()
98.1.24 by Wout Clymans
- Issue 162: Move towards pep8 (PART III)
129
        self.start()
130
85.2.9 by Jean-François Fortin Tam
cleanup and major changes in terminology for the 0.3 series, as discussed on the mailing list
131
    def start_checking(self):
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
132
        try:
98.2.8 by Jean-François Fortin Tam
Clarify some strings, standardize the "Unexpected error:" strings
133
            self.specto.logger.log(_("Watch started checking."),\
98.1.24 by Wout Clymans
- Issue 162: Move towards pep8 (PART III)
134
                                                 "debug", self.name)
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
135
            self.error = False
85.2.9 by Jean-François Fortin Tam
cleanup and major changes in terminology for the 0.3 series, as discussed on the mailing list
136
            self.actually_changed = False
120.1.1 by Wout Clymans
- Added pidgin watch
137
            
138
            if self.dbus and DBUS:
139
                self.session_bus = dbus.SessionBus()
140
                self.obj = self.session_bus.get_object(self.dbus_name, self.dbus_path)
141
                self.iface = dbus.Interface(self.obj, self.dbus_interface)
142
                
143
                for signal, call_back in self.signals.items():
144
                    self.session_bus.add_signal_receiver(
145
                        call_back,
146
                        signal,
147
                        self.dbus_interface,
148
                        self.dbus_name,
149
                        self.dbus_path)
150
            else:
151
                if self.use_network:
152
                    if not self.check_connection():
153
                        return                
154
                self.specto.mark_watch_status("checking", self.id) 
155
                               
156
                self.lock = thread.allocate_lock()
157
                self.lock.acquire()
158
                thread.start_new_thread(self.check, ())
159
                
160
                while self.lock.locked():
164 by Jean-François Fortin Tam
First pass at porting from PyGTK to PyGI
161
                    while Gtk.events_pending():
162
                        Gtk.main_iteration()
120.1.1 by Wout Clymans
- Added pidgin watch
163
                    time.sleep(0.05)
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
164
        except:
98.3.29 by Wout Clymans
- Started Issue 221: error notifications should be meaningful (TODO: use new functions in other watches)
165
            self.set_error(_("There was an error checking the watch"))
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
166
85.2.9 by Jean-François Fortin Tam
cleanup and major changes in terminology for the 0.3 series, as discussed on the mailing list
167
    def watch_changed(self):
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
168
        try:
98.1.24 by Wout Clymans
- Issue 162: Move towards pep8 (PART III)
169
            self.specto.logger.log(_("Watch has changed."), "info", self.name)
85.2.9 by Jean-François Fortin Tam
cleanup and major changes in terminology for the 0.3 series, as discussed on the mailing list
170
            self.actually_changed = False
98.1.24 by Wout Clymans
- Issue 162: Move towards pep8 (PART III)
171
            self.changed = True
85.2.9 by Jean-François Fortin Tam
cleanup and major changes in terminology for the 0.3 series, as discussed on the mailing list
172
            self.last_changed = datetime.today().strftime("%A %d %b %Y %H:%M")
173
            self.watch_io.write_option(self.name, 'changed', self.changed)
98.1.24 by Wout Clymans
- Issue 162: Move towards pep8 (PART III)
174
            self.watch_io.write_option(self.name, 'last_changed', \
175
                                                       self.last_changed)
85.2.9 by Jean-François Fortin Tam
cleanup and major changes in terminology for the 0.3 series, as discussed on the mailing list
176
            self.specto.mark_watch_status("changed", self.id)
98.3.59 by Wout Clymans
- Fixed issue from issue 'passing arguments to custom command'
177
            command = self.replace_variables(self.command)
178
            if command != "": #run watch specific "changed" command
179
                os.system(command + " &")
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
180
        except:
98.3.29 by Wout Clymans
- Started Issue 221: error notifications should be meaningful (TODO: use new functions in other watches)
181
            self.set_error(_("There was an error marking the watch as changed"))
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
182
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
183
    def timer_update(self):
184
        """ update the timer """
185
        try:
85.2.9 by Jean-François Fortin Tam
cleanup and major changes in terminology for the 0.3 series, as discussed on the mailing list
186
            if self.actually_changed == True:
187
                self.watch_changed()
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
188
            elif self.error == True:
189
                self.specto.mark_watch_status("error", self.id)
190
            elif self.active == False:
191
                self.stop()
192
            else:
193
                self.specto.mark_watch_status("idle", self.id)
194
            try:
195
                self.lock.release()
164 by Jean-François Fortin Tam
First pass at porting from PyGTK to PyGI
196
                self.timer_id = GObject.timeout_add(self.refresh, self.start_checking)
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
197
            except:
164 by Jean-François Fortin Tam
First pass at porting from PyGTK to PyGI
198
                self.timer_id = GObject.timeout_add(self.refresh, self.check)
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
199
        except:
98.3.29 by Wout Clymans
- Started Issue 221: error notifications should be meaningful (TODO: use new functions in other watches)
200
            self.set_error(_("There was an error checking the watch"))
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
201
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
202
    def check_connection(self):
100.1.2 by Jean-François Fortin Tam
Some initial cleanup
203
        if not self.specto.connection_manager.connected():
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
204
            self.specto.logger.log(_("No network connection detected"), "warning", self.name)
85.2.9 by Jean-François Fortin Tam
cleanup and major changes in terminology for the 0.3 series, as discussed on the mailing list
205
            self.specto.connection_manager.add_callback(self.start_checking)
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
206
            self.specto.mark_watch_status("no-network", self.id)
207
            return False
100.1.3 by Jean-François Fortin Tam
Some more cleanup
208
        else:
98.3.12 by Wout Clymans
- Fixed Issue 193: clean up watch statuses/actions names and ensure they match the terminology
209
            self.specto.mark_watch_status("idle", self.id)
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
210
            return True
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
211
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
212
    def get_values(self):
213
        return self.values
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
214
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
215
    def set_values(self, values, validate=False):
216
        error_fields = ""
217
        for key, type in self.watch_values:
218
            try:
219
                value = values[key]
220
            except KeyError:
221
                if type.mandatory == False:
222
                    values[key] = type.getStandardValue()
223
                    value = values[key]
224
                    if not validate:
225
                        setattr(self, key, value)
226
                else:
227
                    error_fields += ", " + key
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
228
            else:
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
229
                value = type.checkRestrictions(values[key])
230
                if value[0] == True:
231
                    if type.mandatory == True and value[1] == type.getStandardValue():
232
                        error_fields += ", " + key
233
                    else:
234
                        values[key] = value[1]
235
                        if not validate:
236
                            setattr(self, key, value[1])
237
                else:
238
                    error_fields += ", " + key
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
239
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
240
        if values['open_command'] == "":
241
            try:
242
                self.open_command = self.standard_open_command
243
            except:
244
                self.open_command = ""
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
245
85.2.9 by Jean-François Fortin Tam
cleanup and major changes in terminology for the 0.3 series, as discussed on the mailing list
246
        if self.last_changed == "" or self.last_changed == _("No changes yet") or self.last_changed == "No changes yet": #otherwise, it will be saved untranslated in the watch list
247
            self.last_changed = _("No changes yet")
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
248
100.1.9 by Jean-François Fortin Tam
Cleanup with codecheck, remove useless imports, bring error count from 979 to 790
249
        if len(error_fields) != 0:
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
250
            error_fields = error_fields.lstrip(",")
100.1.6 by Jean-François Fortin Tam
Full PEP-8 compliance, except the line lengths (we don't care!)
251
            raise AttributeError(error_fields)
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
252
        else:
253
            if not validate:
254
                self.values = values
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
255
100.1.2 by Jean-François Fortin Tam
Some initial cleanup
256
    def get_balloon_text(self):
85.2.7 by Jean-François Fortin Tam
make watch popup menu items translatable
257
        return "No message specified yet!" #no need to translate this, if users get to see this, it's no good
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
258
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
259
    def get_extra_information(self):
98.2.32 by Jean-François Fortin Tam
Fix issue #255, and create an "escape" method to sanitize HTML entities
260
        """Provide information to be shown in the Extra information tab in the notifier window.
261
        Remember to use the escape method to sanitize special characters before adding formatting."""
85.2.4 by Jean-François Fortin Tam
mark as many strings as possible as translatable
262
        return _("No extra information available.")
98.3.58 by Wout Clymans
- Fixed Issue 269: allow parameters to be passed to custom commands
263
    
98.3.62 by Wout Clymans
Added indicator support to specto (requires python-indicate)
264
    def open_watch(self, *args):
98.3.59 by Wout Clymans
- Fixed issue from issue 'passing arguments to custom command'
265
        open_command = self.replace_variables(self.open_command) 
98.3.58 by Wout Clymans
- Fixed Issue 269: allow parameters to be passed to custom commands
266
        if open_command != "":
267
            os.system(open_command + " &")
98.3.62 by Wout Clymans
Added indicator support to specto (requires python-indicate)
268
            self.mark_as_read()
98.3.58 by Wout Clymans
- Fixed Issue 269: allow parameters to be passed to custom commands
269
            return True
270
        else:
271
            return False
272
        
98.3.59 by Wout Clymans
- Fixed issue from issue 'passing arguments to custom command'
273
    def replace_variables(self, command):
274
        _command = command
275
        available_variables = {"%extra_information": "'" + self.get_extra_information().replace("'", "") + "'",
276
                               "%information": "'" + self.get_balloon_text().replace("'", "") + "'",
277
                               "%name": "'" +  self.name.replace("'", "\'") + "'",
278
                               "%last_changed": "'" + self.last_changed.replace("'", "") + "'"}
98.3.58 by Wout Clymans
- Fixed Issue 269: allow parameters to be passed to custom commands
279
        for variable in available_variables:
98.3.59 by Wout Clymans
- Fixed issue from issue 'passing arguments to custom command'
280
            _command = _command.replace(variable, available_variables[variable])
98.3.58 by Wout Clymans
- Fixed Issue 269: allow parameters to be passed to custom commands
281
            
98.3.59 by Wout Clymans
- Fixed issue from issue 'passing arguments to custom command'
282
        return _command
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
283
98.2.32 by Jean-François Fortin Tam
Fix issue #255, and create an "escape" method to sanitize HTML entities
284
    def escape(self, text):
285
        """Sanitize the input to remove special characters, PyGTK doesn't like them.
286
        Use this in the get_extra_information method of watches."""
287
        text = escape(text)  # Remove the biggest part using the CGI library's escape function
288
        text = str(text).replace('<', "&lt;").replace('>', "&gt;") # Escape the < and > characters
289
        return text
290
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
291
    def remove_cache_files(self):
292
        return ""
100.1.8 by Jean-François Fortin Tam
Bring us back to PEP-8 compliance. Notice that comment lines are counted as blank
293
98.3.29 by Wout Clymans
- Started Issue 221: error notifications should be meaningful (TODO: use new functions in other watches)
294
    def set_error(self, message=""):
295
        self.error = True
296
        if message != "":
158 by Pablo Bustamante
Subversion (SVN) watch
297
            self.error_message = str(message).replace('\n',' ')
298
            self.specto.logger.log(('%s') % self.error_message, "error", self.name)
98.3.29 by Wout Clymans
- Started Issue 221: error notifications should be meaningful (TODO: use new functions in other watches)
299
        else:
300
            self.error_message = _("Unexpected error:") + " " + str(sys.exc_info()[1])
100.1.8 by Jean-François Fortin Tam
Bring us back to PEP-8 compliance. Notice that comment lines are counted as blank
301
            self.specto.logger.log(self.error_message, "error", self.name)
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
302
100.1.6 by Jean-François Fortin Tam
Full PEP-8 compliance, except the line lengths (we don't care!)
303
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
304
class Watch_collection:
100.1.6 by Jean-François Fortin Tam
Full PEP-8 compliance, except the line lengths (we don't care!)
305
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
306
    def __init__(self, specto):
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
307
        self.watch_db = []
85.3.2 by Wout Clymans
- Implemented new add watch menu
308
        self.plugin_menu = {}
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
309
        self.id = 0
310
        self.plugin_dict = {}
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
311
        self.disabl_plugin_dict = {}
312
        self.specto = specto
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
313
        self.load_plugins()
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
314
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
315
    def load_plugins(self):
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
316
        dir = self.specto.SRC_PATH + "/plugins/"
98.2.30 by Jean-François Fortin Tam
Fix compatibility with Python 2.6
317
        sys.path.append(dir)
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
318
        for f in os.listdir(dir):
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
319
            if f[-3:] == ".py" and f != "__init__.py":
320
                if not os.path.exists('data'):
321
                    dir = "spectlib.plugins."
322
                else:
323
                    dir = "spectlib/plugins/"
98.2.30 by Jean-François Fortin Tam
Fix compatibility with Python 2.6
324
                _file = f[:-3]
98.1.18 by Wout Clymans
-Specto will not crash when trying to create a broken plugin
325
                try:
326
                    mod = __import__(_file, globals(), locals(), [''])
327
                    obj = sys.modules[_file]
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
328
98.1.18 by Wout Clymans
-Specto will not crash when trying to create a broken plugin
329
                    self.plugin_dict[obj.type] = mod
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
330
98.1.18 by Wout Clymans
-Specto will not crash when trying to create a broken plugin
331
                    #create the plugin dict for add menu
332
                    menu1 = obj.category
333
                    menu2 = [obj.type_desc, obj.icon, obj.type]
100.1.2 by Jean-François Fortin Tam
Some initial cleanup
334
                    if not menu1 in self.plugin_menu:
100.1.6 by Jean-François Fortin Tam
Full PEP-8 compliance, except the line lengths (we don't care!)
335
                        self.plugin_menu.update({menu1: []})
98.1.18 by Wout Clymans
-Specto will not crash when trying to create a broken plugin
336
                    self.plugin_menu[menu1].append(menu2)
337
                except:
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
338
                    self.specto.logger.log(_('There was an error opening the file %s') % _file, "critical", "specto")
339
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
340
    def create(self, values):
100.1.2 by Jean-François Fortin Tam
Some initial cleanup
341
        """ read the content from the dictionary and create the watch """
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
342
        _id = []
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
343
        for i in values:
344
            type = values[i]['type']
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
345
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
346
            #get the right object and create the watch object
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
347
            mod = ""
348
            try:
349
                mod = self.plugin_dict[type]
350
            except:
85.1.15 by Wout Clymans
- Fixed importing watches (also from specto 0.2)
351
                self.specto.logger.log(_('Please enable plugin "%s" if you want to use the watch "%s".') % (type, values[i]["name"]), "critical", "specto")
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
352
353
            if mod:
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
354
                obj = getattr(mod, type)
355
                try:
356
                    watch_ = obj(self.specto, self.id, values[i])
357
                except AttributeError, error_fields:
358
                    if len(values) > 1:
359
                        pass
360
                    else:
100.1.6 by Jean-François Fortin Tam
Full PEP-8 compliance, except the line lengths (we don't care!)
361
                        raise AttributeError(error_fields)
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
362
                else:
363
                    self.watch_db.append(watch_)
364
                    _id.append(self.id)
100.1.9 by Jean-François Fortin Tam
Cleanup with codecheck, remove useless imports, bring error count from 979 to 790
365
                    self.id += 1
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
366
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
367
        return _id
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
368
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
369
    def remove(self, id):
370
        """ remove the watch from the collection """
371
        self.watch_db[id].stop()
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
372
        self.watch_db[id].changed = False
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
373
        self.watch_db[id].deleted = True
374
        try:
375
            self.watch_db[id].remove_cache_files()
376
        except:
377
            pass
100.1.2 by Jean-François Fortin Tam
Some initial cleanup
378
        self.specto.logger.remove_watch_log(self.watch_db[id].name)
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
379
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
380
    def get(self, id):
381
        """ get a watch object """
382
        return self.watch_db[id]
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
383
98.3.12 by Wout Clymans
- Fixed Issue 193: clean up watch statuses/actions names and ensure they match the terminology
384
    def mark_all_watches_as_read(self):
85.2.9 by Jean-François Fortin Tam
cleanup and major changes in terminology for the 0.3 series, as discussed on the mailing list
385
        """ mark all watches as not changed """
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
386
        for watch in self.watch_db:
98.3.12 by Wout Clymans
- Fixed Issue 193: clean up watch statuses/actions names and ensure they match the terminology
387
            watch.mark_as_read()
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
388
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
389
    def start_all_watches(self):
390
        """ start all watches in the collection """
391
        for watch in self.watch_db:
392
            watch.start()
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
393
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
394
    def stop_all_watches(self):
395
        """ stop all watches in the collection """
396
        for watch in self.watch_db:
397
            watch.stop()
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
398
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
399
    def restart_all_watches(self):
400
        """ restart all watches in the collection """
401
        for watch in self.watch_db:
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
402
            if watch.active == True:
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
403
                watch.restart()
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
404
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
405
    def length(self):
406
        """ return the length from the collection """
407
        return len(self.watch_db)
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
408
85.2.9 by Jean-François Fortin Tam
cleanup and major changes in terminology for the 0.3 series, as discussed on the mailing list
409
    def count_changed_watches(self):
410
        """ Count the number of changed watches for the tooltip. """
411
        count_changed = {}
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
412
        for watch in self.watch_db:
413
            try:
85.2.9 by Jean-François Fortin Tam
cleanup and major changes in terminology for the 0.3 series, as discussed on the mailing list
414
                count_changed[watch.type_desc]
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
415
            except KeyError:
85.2.9 by Jean-François Fortin Tam
cleanup and major changes in terminology for the 0.3 series, as discussed on the mailing list
416
                count_changed[watch.type_desc] = 0
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
417
85.2.9 by Jean-François Fortin Tam
cleanup and major changes in terminology for the 0.3 series, as discussed on the mailing list
418
            if watch.changed == True:
419
                changes = count_changed[watch.type_desc]
420
                count_changed[watch.type_desc] = changes + 1
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
421
85.2.9 by Jean-François Fortin Tam
cleanup and major changes in terminology for the 0.3 series, as discussed on the mailing list
422
        return count_changed
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
423
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
424
    def find_watch(self, name):
425
        """
426
        Returns the key of a watch or None if it doesn't exists.
427
        """
428
        k = -1
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
429
        for watch in self.watch_db:
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
430
            if watch.name == name:
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
431
                k = watch.id
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
432
                break
433
        return k
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
434
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
435
    def set_interval(self, refresh, refresh_unit):
436
        """
85.2.9 by Jean-François Fortin Tam
cleanup and major changes in terminology for the 0.3 series, as discussed on the mailing list
437
        Set the interval between the checks.
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
438
        refresh = number
439
        refresh_unit = days, hours, minutes,... in values of 0, 1, 2, 3.
440
        """
441
        new_refresh = 0
100.1.2 by Jean-François Fortin Tam
Some initial cleanup
442
        if refresh_unit == 0:  # Seconds
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
443
            new_refresh = refresh * 1000
100.1.2 by Jean-François Fortin Tam
Some initial cleanup
444
        elif refresh_unit == 1:  # Minutes
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
445
            new_refresh = refresh * 60 * 1000
100.1.2 by Jean-François Fortin Tam
Some initial cleanup
446
        elif refresh_unit == 2:  # Hours
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
447
            new_refresh = (refresh * 60) * 60 * 1000
100.1.2 by Jean-François Fortin Tam
Some initial cleanup
448
        elif refresh_unit == 3:  # Days
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
449
            new_refresh = ((refresh * 60) * 60) * 24 *1000
450
        return new_refresh
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
451
85.1.27 by Wout Clymans
- Improved plaintext/keyring support for passwords
452
    def convert_passwords(self, use_keyring):
453
        self.specto.use_keyring = use_keyring
454
        for watch in self.watch_db:
85.1.28 by Wout Clymans
-Fixes Issue 180: keyring vs plaintext conversions should be smart and automatic
455
            if 'password' in watch.values:
456
                if use_keyring == False:
457
                    self.specto.watch_io.remove_keyring(watch.name)
85.1.27 by Wout Clymans
- Improved plaintext/keyring support for passwords
458
                self.specto.watch_io.write_option(watch.name, 'password', watch.password)
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
459
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
460
    def get_interval(self, value):
85.2.9 by Jean-François Fortin Tam
cleanup and major changes in terminology for the 0.3 series, as discussed on the mailing list
461
        """ Get the interval between 2 checks. """
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
462
        if ((value / 60) / 60) / 24 / 1000 > 0:
463
            refresh_value = ((value / 60) / 60) / 24 / 1000
464
            type = 3
465
        elif (value / 60) / 60 / 1000 > 0:
466
            refresh_value = (value / 60) / 60 / 1000
467
            type = 2
468
        elif value / 60 / 1000 > 0:
469
            refresh_value = value / 60 / 1000
470
            type = 1
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
471
        else:
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
472
            refresh_value = value / 1000
473
            type = 0
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
474
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
475
        return refresh_value, type
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
476
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
477
    def __getitem__(self, i):
478
        return self.watch_db[i]
25 by chalserogers
(Finally) Merge the module-organisation branch into trunk.
479
100.1.6 by Jean-François Fortin Tam
Full PEP-8 compliance, except the line lengths (we don't care!)
480
25 by chalserogers
(Finally) Merge the module-organisation branch into trunk.
481
class Watch_io:
482
    """
483
    A class for managing watches.
484
    """
100.1.6 by Jean-François Fortin Tam
Full PEP-8 compliance, except the line lengths (we don't care!)
485
85.1.15 by Wout Clymans
- Fixed importing watches (also from specto 0.2)
486
    def __init__(self, specto, file_name):
100.1.2 by Jean-François Fortin Tam
Some initial cleanup
487
        # Read the watch from file using the iniparser module
85.1.15 by Wout Clymans
- Fixed importing watches (also from specto 0.2)
488
        self.specto = specto
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
489
        self.file_name = file_name
85.1.15 by Wout Clymans
- Fixed importing watches (also from specto 0.2)
490
        self.valid = True
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
491
25 by chalserogers
(Finally) Merge the module-organisation branch into trunk.
492
        if not os.path.exists(self.file_name):
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
493
            try:
494
                f = open(self.file_name, "w")
495
            except:
85.1.15 by Wout Clymans
- Fixed importing watches (also from specto 0.2)
496
                self.valid = False
497
                self.specto.logger.log(_("There was an error writing to the file %s") % self.file_name, "critical", "specto")
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
498
            finally:
499
                f.close()
100.1.2 by Jean-François Fortin Tam
Some initial cleanup
500
        os.chmod(self.file_name, 0600)  # This is important for security purposes, we make the file read-write to the owner only, otherwise everyone can read passwords.
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
501
        try:
502
            self.cfg = ini_namespace(file(self.file_name))
503
        except:
85.1.15 by Wout Clymans
- Fixed importing watches (also from specto 0.2)
504
            self.valid = False
505
            self.specto.logger.log(_("There was an error initializing config file %s") % self.file_name, "critical", "specto")
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
506
98.1.17 by Wout Clymans
- Faster startup and less calls!
507
    def read_all_watches(self, startup=False):
25 by chalserogers
(Finally) Merge the module-organisation branch into trunk.
508
        """
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
509
        Read the watch options from the config file
25 by chalserogers
(Finally) Merge the module-organisation branch into trunk.
510
        and return a dictionary containing the info needed to start the watches.
511
        """
512
        watch_value_db = {}
513
98.1.17 by Wout Clymans
- Faster startup and less calls!
514
        if startup == False:
515
            try:
516
                self.cfg = ini_namespace(file(self.file_name))
517
            except:
518
                self.specto.logger.log(_("There was an error initializing config file %s") % self.file_name, "critical", "specto")
519
                return False
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
520
25 by chalserogers
(Finally) Merge the module-organisation branch into trunk.
521
        names = self.cfg._sections.keys()
522
        i = 0
523
        for name_ in names:
98.1.17 by Wout Clymans
- Faster startup and less calls!
524
            watch_value_db[i] = self.read_watch(name_, startup)
25 by chalserogers
(Finally) Merge the module-organisation branch into trunk.
525
            i += 1
526
        return watch_value_db
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
527
100.1.6 by Jean-François Fortin Tam
Full PEP-8 compliance, except the line lengths (we don't care!)
528
    def read_watch(self, name, startup=False):
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
529
        """
530
        Read the watch options from one watch.
531
        """
532
        watch_options = {}
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
533
534
        if startup == False:
98.1.17 by Wout Clymans
- Faster startup and less calls!
535
            try:
536
                self.cfg = ini_namespace(file(self.file_name))
537
            except:
538
                self.specto.logger.log(_("There was an error initializing config file %s") % self.file_name, "critical", "specto")
539
                return False
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
540
541
        name = self.hide_brackets(name)
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
542
        options = self.cfg._sections[name]._options.keys()
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
543
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
544
        for option_ in options:
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
545
            if option_ == "password" and not self.check_old_version(self.cfg[name]['type']): #don't use decoding for old watches.list
98.1.17 by Wout Clymans
- Faster startup and less calls!
546
                option = self.read_option(name, option_, startup)
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
547
                option = self.decode_password(name, option)
548
            else:
98.1.17 by Wout Clymans
- Faster startup and less calls!
549
                option = self.read_option(name, option_, startup)
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
550
100.1.6 by Jean-François Fortin Tam
Full PEP-8 compliance, except the line lengths (we don't care!)
551
            watch_options_ = {option_: option}
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
552
            watch_options.update(watch_options_)
553
        name = self.show_brackets(name)
100.1.6 by Jean-François Fortin Tam
Full PEP-8 compliance, except the line lengths (we don't care!)
554
        watch_options.update({'name': name})
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
555
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
556
        return watch_options
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
557
98.1.17 by Wout Clymans
- Faster startup and less calls!
558
    def read_option(self, name, option, startup=False):
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
559
        """ Read one option from a watch """
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
560
        if startup == False:
98.1.17 by Wout Clymans
- Faster startup and less calls!
561
            try:
562
                self.cfg = ini_namespace(file(self.file_name))
563
            except:
564
                self.specto.logger.log(_("There was an error initializing config file %s") % self.file_name, "critical", "specto")
565
                return False
25 by chalserogers
(Finally) Merge the module-organisation branch into trunk.
566
        try:
98.1.17 by Wout Clymans
- Faster startup and less calls!
567
            return self.cfg[name][option]
25 by chalserogers
(Finally) Merge the module-organisation branch into trunk.
568
        except:
569
            return 0
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
570
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
571
    def write_watch(self, values):
25 by chalserogers
(Finally) Merge the module-organisation branch into trunk.
572
        """
573
        Write or change the watch options in a configuration file.
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
574
        Values has to be a dictionary with the name from the options and the value. example: { 'name':'value', 'interval':'value' }
575
        If the name is not found, a new watch will be added, else the excisting watch will be changed.
25 by chalserogers
(Finally) Merge the module-organisation branch into trunk.
576
        """
48 by woutclymans
fixed a bug
577
        try:
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
578
            cfg = ini_namespace(file(self.file_name))
579
        except:
85.1.15 by Wout Clymans
- Fixed importing watches (also from specto 0.2)
580
            self.specto.logger.log(_("There was an error initializing config file %s") % self.file_name, "critical", "specto")
581
            return False
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
582
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
583
        if cfg:
584
            name = self.hide_brackets(values['name'])
100.1.2 by Jean-François Fortin Tam
Some initial cleanup
585
            if not name in cfg._sections:
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
586
                cfg.new_namespace(name) #add a new watch
587
                try:
588
                    f = open(self.file_name, "w")
589
                    f.write(str(cfg).strip()) #write the new configuration file
590
                except IOError:
85.1.15 by Wout Clymans
- Fixed importing watches (also from specto 0.2)
591
                    self.specto.logger.log(_("There was an error writing to the file %s") % self.file_name, "critical", "specto")
592
                    return False
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
593
                finally:
594
                    f.close()
595
596
            del values['name']
100.1.3 by Jean-François Fortin Tam
Some more cleanup
597
            for option, value in values.iteritems():
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
598
                self.write_option(name, option, value)
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
599
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
600
    def write_option(self, name, option, value):
601
        try:
602
            cfg = ini_namespace(file(self.file_name))
603
        except:
85.1.15 by Wout Clymans
- Fixed importing watches (also from specto 0.2)
604
            self.specto.logger.log(_("There was an error initializing config file %s") % self.file_name, "critical", "specto")
605
            return False
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
606
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
607
        if cfg:
608
            name = self.hide_brackets(name)
100.1.2 by Jean-François Fortin Tam
Some initial cleanup
609
            if not name in cfg._sections:
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
610
                return 0
611
            else:
612
                if option == "password": # and self.check_old_version(self.cfg[name]['type']): #don't use encoding for old watches.list
613
                    value = self.encode_password(name, value)
614
                cfg[name][option] = value
615
                try:
616
                    f = open(self.file_name, "w")
617
                    f.write(str(cfg).strip()) #write the new configuration file
618
                except IOError:
85.1.15 by Wout Clymans
- Fixed importing watches (also from specto 0.2)
619
                    self.specto.logger.log(_("There was an error writing to the file %s") % self.file_name, "critical", "specto")
620
                    return False
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
621
                finally:
622
                    f.close()
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
623
85.1.28 by Wout Clymans
-Fixes Issue 180: keyring vs plaintext conversions should be smart and automatic
624
    def remove_keyring(self, name):
625
        try:
626
            k = Keyring(name, "Specto " + name, "network")
627
            password = self.read_option(name, "password")
628
            k.remove_keyring(password)
629
        except:
630
            pass
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
631
25 by chalserogers
(Finally) Merge the module-organisation branch into trunk.
632
    def remove_watch(self, name):
633
        """ Remove a watch from the configuration file. """
85.1.28 by Wout Clymans
-Fixes Issue 180: keyring vs plaintext conversions should be smart and automatic
634
        self.remove_keyring(name)
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
635
48 by woutclymans
fixed a bug
636
        try:
637
            cfgpr = ConfigParser()
638
            cfgpr.read(self.file_name)
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
639
            cfgpr.remove_section(self.hide_brackets(name))
48 by woutclymans
fixed a bug
640
            f = open(self.file_name, "w")
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
641
            cfgpr.write(open(self.file_name, "w"))
48 by woutclymans
fixed a bug
642
        except IOError:
85.1.15 by Wout Clymans
- Fixed importing watches (also from specto 0.2)
643
            self.specto.logger.log(_("There was an error writing to the file %s") % self.file_name, "critical", "specto")
644
            return False
79 by nekohayo
accepted Wout's patch for adding a Clear menu in the tray's popup menu
645
        finally:
646
            f.close()
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
647
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
648
    def is_unique_watch(self, name):
649
        """
650
        Returns True if the watch is found in the file.
651
        """
652
        try:
653
            self.cfg = ini_namespace(file(self.file_name))
100.1.2 by Jean-François Fortin Tam
Some initial cleanup
654
            if not (self.hide_brackets(name)) in self.cfg._sections:
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
655
                return False
656
            else:
657
                return True
658
        except IOError:
85.1.15 by Wout Clymans
- Fixed importing watches (also from specto 0.2)
659
            self.specto.logger.log(_("There was an error initializing config file %s") % self.file_name, "critical", "specto")
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
660
            return False #this has to be an error
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
661
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
662
    def replace_name(self, name, new_name):
25 by chalserogers
(Finally) Merge the module-organisation branch into trunk.
663
        """ Replace a watch name (rename). """
664
        #read the file
48 by woutclymans
fixed a bug
665
        try:
666
            f = open(self.file_name, "r")
667
            text = f.read()
79 by nekohayo
accepted Wout's patch for adding a Clear menu in the tray's popup menu
668
        except IOError:
85.1.15 by Wout Clymans
- Fixed importing watches (also from specto 0.2)
669
            self.specto.logger.log(_("There was an error writing to the file %s") % self.file_name, "critical", "specto")
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
670
        except:
48 by woutclymans
fixed a bug
671
            f.close
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
672
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
673
        name = self.hide_brackets(name)
674
        new_name = self.hide_brackets(new_name)
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
675
        text = text.replace("[" + name + "]", "[" + new_name + "]")
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
676
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
677
        if text:
678
            #replace and write file
679
            try:
680
                f = open(self.file_name, "w")
681
                f.write(text)
682
            except IOError:
85.1.15 by Wout Clymans
- Fixed importing watches (also from specto 0.2)
683
                self.specto.logger.log(_("There was an error writing to the file %s") % self.file_name, "critical", "specto")
684
                return False
84 by Jean-François Fortin Tam
make the history between specto-svn and specto-woutc stitch together
685
            finally:
686
                f.close()
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
687
100.1.6 by Jean-François Fortin Tam
Full PEP-8 compliance, except the line lengths (we don't care!)
688
    def hide_brackets(self, name):
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
689
        name = name.replace("[", "&brStart;")
690
        name = name.replace("]", "&brEnd;")
691
        return name
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
692
100.1.6 by Jean-François Fortin Tam
Full PEP-8 compliance, except the line lengths (we don't care!)
693
    def show_brackets(self, name):
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
694
        name = name.replace("&brStart;", "[")
695
        name = name.replace("&brEnd;", "]")
696
        return name
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
697
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
698
    def encode_password(self, name, password):
85.1.27 by Wout Clymans
- Improved plaintext/keyring support for passwords
699
        if self.specto.use_keyring == True and keyring == True:
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
700
            k = Keyring(name, "Specto " + name, "network")
85.1.28 by Wout Clymans
-Fixes Issue 180: keyring vs plaintext conversions should be smart and automatic
701
            id = k.set_credentials((name, password))
702
            password = "keyring:" + str(id)
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
703
        return password
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
704
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
705
    def decode_password(self, name, password):
85.1.27 by Wout Clymans
- Improved plaintext/keyring support for passwords
706
        if self.specto.use_keyring == True and keyring == True:
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
707
            try:
708
                k = Keyring(name, "Specto " + name, "network")
709
                password = k.get_credentials()[1]
710
            except:
711
                password = password
712
        return password
100.1.4 by Jean-François Fortin Tam
Use "reindent.py -r -v" to fix trailing spaces automatically
713
85 by Wout Clymans
WARNING: this commit contains all the significant changes that happened in a specto-woutc branch over the past year. Large change log follows. Some commit log lines were intentionally left out.
714
    def check_old_version(self, type):
715
        old = True
716
        try:
717
            int(type) #type is int: old version
718
            old = True
719
        except ValueError:
720
            old = False #type is not int: new version
721
        return old