7 this file is part of the project scolasync
9 Copyright (C) 2010 Georges Khaznadar <georgesk@ofset.org>
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 licence[
'en']=licence_en
27 import dbus, subprocess, os.path, re, time
51 def __init__(self, path, bus, checkable=False):
53 self.
device = bus.get_object(
"org.freedesktop.UDisks", self.
path)
65 "1device-mount-paths":QApplication.translate(
"uDisk",
"point de montage",
None, QApplication.UnicodeUTF8),
66 "2device-size":QApplication.translate(
"uDisk",
"taille",
None, QApplication.UnicodeUTF8),
67 "3drive-vendor":QApplication.translate(
"uDisk",
"marque",
None, QApplication.UnicodeUTF8),
68 "4drive-model":QApplication.translate(
"uDisk",
"modèle de disque",
None, QApplication.UnicodeUTF8),
69 "5drive-serial":QApplication.translate(
"uDisk",
"numéro de série",
None, QApplication.UnicodeUTF8),
72 _specialItems={
"0Check":QApplication.translate(
"uDisk",
"cocher",
None, QApplication.UnicodeUTF8)}
74 _ItemPattern=re.compile(
"[0-9]?(.*)")
106 result=uDisk._specialItems.keys()+ uDisk._itemNames.keys()
107 return sorted(result)
109 return sorted(uDisk._itemNames.keys())
111 headers = staticmethod(headers)
131 return bool(self.
getProp(prop))
133 return self.
getProp(prop)==value
141 return self.
isTrue(
"device-is-removable")
and self.
isTrue(
"drive-connection-interface",
"usb")
and self.
isTrue(
"device-size")
166 fileById=self.
getProp(
"device-file-by-id")
167 if isinstance(fileById, dbus.Array): fileById=fileById[0]
176 paths=self.
getProp(
"device-mount-paths")
177 if isinstance(paths, dbus.Array)
and len(paths)>0:
190 return self.device_prop.Get(
"org.freedesktop.UDisks", name)
200 return self.
getProp(
"id-type")==
"vfat"
207 return bool(self.
getProp(
"device-is-mounted"))
215 prefix=
"\n"+
" "*indent
217 props=[
"device-file-by-id",
218 "device-mount-paths",
219 "device-is-partition-table",
220 "partition-table-count",
221 "device-is-read-only",
223 "device-is-optical-disc",
235 if isinstance(p,dbus.Array):
237 r+=prefix+
"%s = array:" %(prop)
239 r+=prefix+
" "*indent+s
240 elif isinstance(p,dbus.Boolean):
241 r+=prefix+
"%s = %s" %(prop, bool(p))
242 elif isinstance(p,dbus.Int16)
or isinstance(p,dbus.Int32)
or isinstance(p,dbus.Int64)
or isinstance(p,dbus.UInt16)
or isinstance(p,dbus.UInt32)
or isinstance(p,dbus.UInt64)
or isinstance(p,int):
244 r+=prefix+
"%s = %s" %(prop,p)
245 elif p < 10*1024*1024:
246 r+=prefix+
"%s = %s k" %(prop,p/1024)
247 elif p < 10*1024*1024*1024:
248 r+=prefix+
"%s = %s M" %(prop,p/1024/1024)
250 r+=prefix+
"%s = %s G" %(prop,p/1024/1024/1024)
252 r+=prefix+
"%s = %s" %(prop,p)
261 return self.
getProp(
"partition-slave")
271 m=uDisk._ItemPattern.match(self.
headers()[n])
291 elif n <= len(propListe):
294 if n < len(propListe):
308 if isinstance(p,dbus.Array):
309 if len(p)>0:
return str(p[0])
311 elif isinstance(p,dbus.Boolean):
313 elif isinstance(p,dbus.Int16)
or isinstance(p,dbus.Int32)
or isinstance(p,dbus.Int64)
or isinstance(p,dbus.UInt16)
or isinstance(p,dbus.UInt32)
or isinstance(p,dbus.UInt64)
or isinstance(p,int):
333 mount_paths=self.
getProp(
"device-mount-paths")
334 if mount_paths==
None:
337 while len(mount_paths)==0
and leftTries >0:
338 leftTries = leftTries - 1
339 path=self.
getProp(
"device-file-by-id")
340 if isinstance(path,dbus.Array)
and len(path)>0:
342 subprocess.call(
"udisks --mount %s > /dev/null" %path,shell=
True)
343 paths=self.
getProp(
"device-mount-paths")
345 return self.
getProp(
"device-mount-paths")[0]
351 raise Exception,
"Could not mount the VFAT after 5 tries."
353 return mount_paths[0]
384 def __init__(self, checkable=False, access="disk", diskClass=uDisk, diskDict=None):
388 self.
bus = dbus.SystemBus()
389 proxy = self.bus.get_object(
"org.freedesktop.UDisks",
390 "/org/freedesktop/UDisks")
391 iface = dbus.Interface(proxy,
"org.freedesktop.UDisks")
396 ud=diskClass(path, self.
bus, checkable)
400 if bool(ud.getProp(
"device-is-partition-table")) ==
False:
403 self.
disks[ud].append(ud)
406 ud=diskClass(path, self.
bus, checkable)
407 for d
in self.disks.keys():
408 if ud.master() == d.path:
409 self.
disks[d].append(ud)
426 if self.
access==
"firstFat":
445 result=self.
summary()==other.summary()
455 for k
in self.disks.keys():
456 if k.getProp(
"device-file-by-id")==ud.getProp(
"device-file-by-id"):
return True
465 r=
"Available USB discs\n"
466 r+=
"===================\n"
467 for d
in sorted(self.disks.keys(), key=
lambda disk: disk.getFatUuid()):
468 r+=
"%s\n" %(d.title(),)
469 if len(self.
disks[d])>0:
471 for part
in sorted(self.
disks[d], key=
lambda disk: disk.getFatUuid()):
472 r+=
" %s\n" %(part.path,)
481 r=
"Available USB discs\n"
482 r+=
"===================\n"
483 for d
in self.disks.keys():
485 if len(self.
disks[d])>0:
487 for part
in self.
disks[d]:
488 r+=
" %s\n" %(part.path)
489 r+=part.valuableProperties(12)+
"\n"
501 return self.disks.keys()[n]
502 elif self.
access==
"firstFat":
513 return len(self.
disks)
514 elif self.
access==
"firstFat":
532 for d
in self.disks.keys():
533 for p
in self.
disks[d]:
534 if p.isDosFat()
or p==d :
539 self.fatPaths.append(p.title())
556 s=s.replace(
"/org/freedesktop/UDisks/devices/",
"")
558 if p.split(
"/")[-1]==s:
563 if __name__==
"__main__":