Gnome SHELL Generic Monitor

Gnome SHELL Generic Monitor Commit Details

Date:2020-05-04 10:09:59 (6 months 25 days ago)
Author:Grégory Soutadé
Branch:master
Commit:506660f1da376e9ab0bd40819fe267f0906f4085
Parents: c7462662f4312e16c5f08ff9ee1d7965d902829d
Message:Update examples: * Move examples in examples directory * Create separate DBUSItem class * Add timer example

Changes:
Rexample.py → examples/mail.py
Aexamples/dbusitem.py (full)
Aexamples/timer.py (full)

File differences

examples/dbusitem.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
class DBUSItem:
def __init__(self, name, text='', style='', icon='', iconStyle='', onClick=''):
self.name = name
self.text = text
self.style= style
self.icon = icon
self.iconStyle = iconStyle
self.onClick = onClick
self._checkValues()
def _checkValues(self):
if self.onClick and not self.onClick in ('signal', 'delete'):
raise ValueError('Invalid onClick value')
def toMap(self):
myMap = {"name":self.name}
for p in ('text', 'style', 'icon'):
if self.__dict__[p]:
myMap[p] = self.__dict__[p]
if self.iconStyle:
myMap['icon-style'] = self.iconStyle
if self.onClick:
myMap['on-click'] = self.onClick
return [myMap]
examples/mail.py
3333
3434
3535
36
3637
3738
3839
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
5640
5741
5842
......
8569
8670
8771
88
72
8973
9074
9175
......
9781
9882
9983
100
84
10185
10286
10387
10488
105
89
10690
107
91
10892
10993
11094
......
209193
210194
211195
212
213
196
197
214198
215199
216200
from threading import Thread
from signal import signal, SIGINT
import sys
from dbusitem import DBUSItem
MAIL_ADDRESS='XXX@gmail.com'
class DBUSItem:
def __init__(self, name, text='', style='', icon='', iconStyle=''):
self.name = name
self.text = text
self.style= style
self.icon = icon
self.iconStyle = iconStyle
def toMap(self):
myMap = {"name":self.name}
for p in ('text', 'style', 'icon'):
if self.__dict__[p]:
myMap[p] = self.__dict__[p]
if self.iconStyle:
myMap['icon-style'] = self.iconStyle
return [myMap]
def getMail(user, password):
res = DBUSItem('mail')
print(str(e))
pass
class StockThread(Thread):
class EventThread(Thread):
SLEEP_TIME = 30
def stop(self):
while not self._stopLoop:
getEvents(MAIL_ADDRESS, mail_password, systray_proxy)
# Be more reactive on signal capture
for i in range(0, StockThread.SLEEP_TIME):
for i in range(0, EventThread.SLEEP_TIME):
if self._stopLoop: break
time.sleep(1)
def signalHandler(signal_received, frame):
stockThread.stop()
eventThread.stop()
mainLoop.quit()
stockThread.join()
eventThread.join()
encoder = json.JSONEncoder()
res = {'groups':['Mail', 'Pidgin']}
try:
bus.add_signal_receiver(pidginMessageWrote, 'WroteChatMsg', 'im.pidgin.purple.PurpleInterface')
bus.add_signal_receiver(pidginConversationUpdated, 'ConversationUpdated', 'im.pidgin.purple.PurpleInterface')
stockThread = StockThread()
stockThread.start()
eventThread = EventThread()
eventThread.start()
signal(SIGINT, signalHandler)
examples/timer.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
#!/usr/bin/python3
#
# 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, either version 3 of the License, or
# (at your option) any later version.
#
# 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 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 <https://www.gnu.org/licenses/>.
#
'''
Display two timers with interactive behaviour :
* One timer is white and background become red after one hour
* Second timer is blue
* Click : start/stop timer
* Double click : reset timer
* Right click : switch timer
'''
import json
import time
import dbus
from dbus.mainloop.glib import DBusGMainLoop
import gi
gi.require_version('Gst', '1.0')
from gi.repository import GObject, GLib, Gst
from threading import Thread
from signal import signal, SIGINT
import sys
from dbusitem import DBUSItem
class TimerThread(Thread):
def stop(self):
self._stopLoop = True
def _displayTimerValue(self):
encoder = json.JSONEncoder()
res = {'group':'Timer', 'items':[]}
item = DBUSItem('timer', onClick='signal')
curValue = self.timers[self.curTimer]
item.text = '%02d:%02d' % (int(curValue/60)%60, curValue%60)
if curValue >= (60*60):
item.text = '%02d:%s' % (int(curValue/(60*60)), item.text)
if self.curTimer == 0:
style = 'color:white'
if curValue > (60*60):
style += ';background-color:red'
else:
style = 'color:#215D9C'
item.style = style
res['items'] += item.toMap()
try:
systray_proxy.notify(encoder.encode(res), dbus_interface='com.soutade.GenericMonitor')
except:
pass
def run(self):
self.timers = [0, 0]
self.curTimer = 0
self.timerPaused = False
self._stopLoop = False
while not self._stopLoop:
time.sleep(1)
if not self.timerPaused:
self.timers[self.curTimer] += 1
self._displayTimerValue()
def _forMe(self, sender):
return sender == 'timer@Timer'
def onClick(self, sender):
if not self._forMe(sender): return
self.timerPaused = not self.timerPaused
self._displayTimerValue()
def onRightClick(self, sender):
if not self._forMe(sender): return
if self.curTimer == 0:
self.curTimer = 1
else:
self.curTimer = 0
self.timers[self.curTimer] = 0
self.timerPaused = False
self._displayTimerValue()
def onDblClick(self, sender):
if not self._forMe(sender): return
self.timers[self.curTimer] = 0
self.timerPaused = True
self._displayTimerValue()
def onDblRightClick(self, sender):
pass
def onClick(sender):
timerThread.onClick(sender)
def onRightClick(sender):
timerThread.onRightClick(sender)
def onDblClick(sender):
timerThread.onDblClick(sender)
def onDblRightClick(sender):
timerThread.onDblRightClick(sender)
def signalHandler(signal_received, frame):
timerThread.stop()
mainLoop.quit()
timerThread.join()
encoder = json.JSONEncoder()
res = {'groups':['Timer']}
try:
systray_proxy.deleteGroups(encoder.encode(res), dbus_interface='com.soutade.GenericMonitor')
except Exception as e:
pass
sys.exit(0)
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
systray_proxy = bus.get_object('org.gnome.Shell', '/com/soutade/GenericMonitor')
bus.add_signal_receiver(onClick, 'onClick', 'com.soutade.GenericMonitor')
bus.add_signal_receiver(onDblClick, 'onDblClick', 'com.soutade.GenericMonitor')
bus.add_signal_receiver(onRightClick, 'onRightClick', 'com.soutade.GenericMonitor')
bus.add_signal_receiver(onDblRightClick, 'onDblRightClick', 'com.soutade.GenericMonitor')
timerThread = TimerThread()
timerThread.start()
signal(SIGINT, signalHandler)
mainLoop = GLib.MainLoop()
mainLoop.run()

Archive Download the corresponding diff file

Branches

Tags