-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbdin.py
executable file
·257 lines (187 loc) · 7.76 KB
/
bdin.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Copyright (C) 2013 Rodrigo Pinheiro Marques de Araujo
#
# 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 2 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, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
LICENSE = """ Copyright (C) 2013 Rodrigo Pinheiro Marques de Araujo
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 2 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, write to the Free Software Foundation, Inc., 51
Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA."""
import dbus
import pygtk
pygtk.require('2.0')
import gtk
import gobject
import appindicator
from dbus.mainloop.glib import DBusGMainLoop
class UmountError(BaseException):
pass
class MountError(BaseException):
pass
class DetachError(BaseException):
pass
class Device(object):
def __init__(self, udisk_obj):
self.obj = udisk_obj
self.props = dbus.Interface(self.obj, dbus.PROPERTIES_IFACE)
def __repr__(self):
return "Device(file={},label={})".format(self.device_file,
self.name)
@property
def is_partition(self):
return bool(self.props.Get("org.freedesktop.UDisks.Device",
'DeviceIsPartition'))
@property
def name(self):
return self.props.Get("org.freedesktop.UDisks.Device",
'idLabel')
@property
def is_internal(self):
return bool(self.props.Get("org.freedesktop.UDisks.Device",
'DeviceIsSystemInternal'))
@property
def is_mounted(self):
return bool(self.props.Get("org.freedesktop.UDisks.Device",
'DeviceIsMounted'))
@property
def device_file(self):
return self.props.Get("org.freedesktop.UDisks.Device",
'DeviceFile')
def mount(self):
try:
return unicode(self.obj.FilesystemMount('',''))
except dbus.DBusException, e:
raise MountError(e.message)
def unmount(self):
try:
return self.obj.FilesystemUnmount('')
except dbus.DBusException, e:
raise UmountError(e.message)
def detach(self):
slave = self.props.Get("org.freedesktop.UDisks.Device",
'PartitionSlave')
bus = dbus.SystemBus()
drive_device_obj = bus.get_object("org.freedesktop.UDisks", slave)
drive_device_dbus = dbus.Interface(drive_device_obj, "org.freedesktop.UDisks.Device")
try:
drive_device_dbus.DriveDetach('')
except dbus.DBusException, e:
raise DetachError(e)
class UdiskManager(object):
def __init__(self, callback):
self.bus = dbus.SystemBus()
self.proxy = self.bus.get_object("org.freedesktop.UDisks",
"/org/freedesktop/UDisks")
self.iface = dbus.Interface(self.proxy, "org.freedesktop.UDisks")
def mycallback( *args ):
callback()
self.iface.connect_to_signal('DeviceAdded', mycallback)
self.iface.connect_to_signal('DeviceRemoved', mycallback)
self.iface.connect_to_signal('DeviceChanged', mycallback)
def list_devices(self):
result = []
for dev in self.iface.EnumerateDevices():
device_obj = self.bus.get_object("org.freedesktop.UDisks", dev)
device_dbus = dbus.Interface(device_obj, "org.freedesktop.UDisks.Device")
device = Device(device_dbus)
if (not device.is_internal) and device.is_partition:
result.append(device)
else:
continue
return result
def display_exception(method):
try:
method()
except (MountError, UmountError, DetachError), e:
dialog = gtk.MessageDialog(None, 0, gtk.MESSAGE_ERROR,
gtk.BUTTONS_CLOSE, e.message)
dialog.set_title("Bdin")
response = dialog.run()
dialog.destroy()
class App(object):
def __init__(self):
self.ind = appindicator.Indicator("bdin",
"indicator-messages",
appindicator.CATEGORY_APPLICATION_STATUS)
self.ind.set_status(appindicator.STATUS_ACTIVE)
self.ind.set_icon("block-device")
self.manager = UdiskManager(self.menu_setup)
self.menu_setup()
def menu_setup(self):
self.menu = gtk.Menu()
for dev in self.manager.list_devices():
name = "{} on {}".format(dev.name, dev.device_file)
item = gtk.MenuItem(name)
item.show()
d_e = display_exception
if not dev.is_mounted:
submenu = gtk.Menu()
mount_item = gtk.MenuItem("Mount")
mount_item.show()
mount_item.connect("activate", lambda i,d: d_e(d.mount), dev)
submenu.append(mount_item)
detach_item = gtk.MenuItem("Detach")
detach_item.show()
detach_item.connect("activate", lambda i,d: d_e(d.detach), dev)
submenu.append(detach_item)
else:
submenu = gtk.Menu()
unmount_item = gtk.MenuItem("Unmount")
unmount_item.show()
unmount_item.connect("activate", lambda i,d : d_e(d.unmount), dev)
submenu.append(unmount_item)
item.set_submenu(submenu)
self.menu.append(item)
about = gtk.ImageMenuItem("About")
img = gtk.Image()
img.set_from_stock(gtk.STOCK_ABOUT, gtk.ICON_SIZE_MENU)
about.set_image(img)
about.connect('activate', lambda i: self.show_about())
about.show()
self.menu.append(about)
image = gtk.ImageMenuItem(gtk.STOCK_QUIT)
image.connect("activate", self.quit)
image.show()
self.menu.append(image)
self.menu.show()
self.ind.set_menu(self.menu)
def quit(self, widget, data=None):
gtk.main_quit()
def show_about(self):
self.about = gtk.AboutDialog()
self.about.set_name("Bdin")
self.about.set_version("0.1")
self.about.set_comments("A block device appindicator for ubuntu")
self.about.set_copyright("Copyright (C) 2013 Rodrigo Pinheiro Marques de Araujo")
self.about.set_authors(["Rodrigo Pinheiro Marques de Araujo <[email protected]>"])
self.about.set_license(LICENSE)
self.about.set_program_name("Bdin")
self.about.set_website("github.com/fenrrir/bdin")
self.about.run()
self.about.destroy()
def main():
DBusGMainLoop(set_as_default=True)
app = App()
gtk.main()
if __name__ == "__main__":
main()