-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathui.py
160 lines (120 loc) · 4.79 KB
/
ui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#-*- coding: utf-8 -*-
"""
Dojotools - tools for your coding dojo session
Copyright (C) 2009 Flávio Amieiro <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 dated June, 1991.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
If you find any bugs or have any suggestions email: [email protected]
"""
import os
import sys
import gtk
import gobject
try:
import pynotify
except ImportError:
pynotify = None
sys.stderr.write('\n\n*** Could not import pynotify. '
'Make sure it is installed so you can see the notifications ***\n\n\n')
IMAGE_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'images/')
PASS_ICON = os.path.join(IMAGE_DIR, 'green_belt.png')
FAIL_ICON = os.path.join(IMAGE_DIR, 'red_belt.png')
__all__ = ['UserInterface']
class UserInterface(object):
def __init__(self, timer):
self.timer = timer
self.current_status = 0
self.status_icon = gtk.StatusIcon()
self.status_icon.set_from_file(PASS_ICON)
self._create_menu()
self.status_icon.set_visible(True)
self.start_timer()
gobject.timeout_add(1000, self.update_timer)
def _create_menu(self):
self.menu = gtk.Menu()
self.pause_item = gtk.ImageMenuItem(gtk.STOCK_MEDIA_PAUSE)
self.pause_item.connect('activate', self.pause_timer)
self.play_item = gtk.ImageMenuItem(gtk.STOCK_MEDIA_PLAY)
self.play_item.connect('activate', self.start_timer)
self.quit_item = gtk.ImageMenuItem(gtk.STOCK_QUIT)
self.quit_item.connect('activate', gtk.main_quit, gtk)
self.separator = gtk.MenuItem()
self.menu.append(self.pause_item)
self.menu.append(self.play_item)
self.menu.append(self.separator)
self.menu.append(self.quit_item)
self.status_icon.connect('popup-menu', self._show_menu, self.menu)
def _show_menu(self, widget, button, time, data):
data.show_all()
data.popup(None, None, None, button, time)
def _set_icon(self):
self.status_icon.set_from_file(
PASS_ICON if self.current_status == 0 else FAIL_ICON
)
def pause_timer(self, widget=None):
self.status_icon.set_from_stock(gtk.STOCK_MEDIA_PAUSE)
self.timer.pause()
def start_timer(self, widget=None):
self._set_icon()
self.timer.start()
def warn_time_is_up(self):
"""Shows a dialog warning the pilot that his time is up"""
dialog = gtk.Dialog('Dojotools', buttons=(gtk.STOCK_OK, 0))
dialog.set_default_size(180, 120)
dialog.set_keep_above(True)
dialog.vbox.pack_start(gtk.Label('Your time is up!'))
dialog.show_all()
dialog.run()
dialog.destroy()
def html_escape(self, text):
"""Produce entities within text."""
html_escape_table = {
"&": "&",
'"': """,
"'": "'",
">": ">",
"<": "<",
}
return "".join(html_escape_table.get(c,c) for c in text)
def show_command_results(self, status, output):
"""
Shows the output to the users.
For now, it will write the output to stdout,
change the icon depending on the status (green
if the tests are passing, red otherwise) and,
if pynotify is installed, show a notification.
If you use this in Ubuntu, I'm sorry for you.
Canonical just decided to break libnotify in so
many ways that it is *impossible* to use it sanely.
"""
sys.stdout.write(output)
self.current_status = status
if self.timer.running:
self._set_icon()
if pynotify is not None:
pynotify.init('dojotools')
message = pynotify.Notification('Dojotools', self.html_escape(output))
message.set_urgency(
pynotify.URGENCY_NORMAL if status == 0
else pynotify.URGENCY_CRITICAL
)
message.show()
def update_timer(self):
if self.timer.time_left:
time_str = '%02d:%02d' % (
(self.timer.time_left / 60),
(self.timer.time_left % 60)
)
self.status_icon.set_tooltip(time_str)
else:
self.pause_timer()
self.warn_time_is_up()
self.start_timer()
return True