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
26 dependences=
"python3-dbus python3-dbus.mainloop.qt"
29 import dbus, subprocess, os, os.path, re, time
53 def __init__(self, path, bus, checkable=False):
56 self.
device = bus.get_object(
"org.freedesktop.UDisks", self.
path)
67 self.
devStuff=os.path.abspath(os.path.join(os.path.dirname(p), os.readlink(p)))
74 "1device-mount-paths":QApplication.translate(
"uDisk",
"point de montage",
None, QApplication.UnicodeUTF8),
75 "2device-size":QApplication.translate(
"uDisk",
"taille",
None, QApplication.UnicodeUTF8),
76 "3drive-vendor":QApplication.translate(
"uDisk",
"marque",
None, QApplication.UnicodeUTF8),
77 "4drive-model":QApplication.translate(
"uDisk",
"modèle de disque",
None, QApplication.UnicodeUTF8),
78 "5drive-serial":QApplication.translate(
"uDisk",
"numéro de série",
None, QApplication.UnicodeUTF8),
81 _specialItems={
"0Check":QApplication.translate(
"uDisk",
"cocher",
None, QApplication.UnicodeUTF8)}
83 _ItemPattern=re.compile(
"[0-9]?(.*)")
115 result= list(uDisk._specialItems.keys())+ list(uDisk._itemNames.keys())
116 return sorted(result)
118 return sorted(uDisk._itemNames.keys())
120 headers = staticmethod(headers)
140 return bool(self.
getProp(prop))
142 return self.
getProp(prop)==value
150 return self.
isTrue(
"device-is-removable")
and self.
isTrue(
"drive-connection-interface",
"usb")
and self.
isTrue(
"device-size")
175 fileByPath=self.
getProp(
"device-file-by-path")
176 if isinstance(fileByPath, dbus.Array)
and len(fileByPath)>0:
177 fileByPath=fileByPath[0]
189 paths=self.
getProp(
"device-mount-paths")
190 if isinstance(paths, dbus.Array)
and len(paths)>0:
206 return self.device_prop.Get(
"org.freedesktop.UDisks", name)
216 return self.
getProp(
"id-type")==
"vfat"
223 return bool(self.
getProp(
"device-is-mounted"))
231 prefix=
"\n"+
" "*indent
233 props=[
"device-file-by-id",
234 "device-file-by-path",
235 "device-mount-paths",
236 "device-is-partition-table",
237 "partition-table-count",
238 "device-is-read-only",
240 "device-is-optical-disc",
252 if isinstance(p,dbus.Array):
254 r+=prefix+
"%s = array:" %(prop)
256 r+=prefix+
" "*indent+s
257 elif isinstance(p,dbus.Boolean):
258 r+=prefix+
"%s = %s" %(prop, bool(p))
259 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):
261 r+=prefix+
"%s = %s" %(prop,p)
262 elif p < 10*1024*1024:
263 r+=prefix+
"%s = %s k" %(prop,p/1024)
264 elif p < 10*1024*1024*1024:
265 r+=prefix+
"%s = %s M" %(prop,p/1024/1024)
267 r+=prefix+
"%s = %s G" %(prop,p/1024/1024/1024)
269 r+=prefix+
"%s = %s" %(prop,p)
270 r+=prefix+
"%s = %s" %(
'devStuff', self.
devStuff)
279 return self.
getProp(
"partition-slave")
289 m=uDisk._ItemPattern.match(self.
headers()[n])
309 elif n <= len(propListe):
312 if n < len(propListe):
326 if isinstance(p,dbus.Array):
327 if len(p)>0:
return str(p[0])
329 elif isinstance(p,dbus.Boolean):
331 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):
351 mount_paths=self.
getProp(
"device-mount-paths")
352 if mount_paths==
None:
355 while len(mount_paths)==0
and leftTries >0:
356 leftTries = leftTries - 1
357 path=self.
getProp(
"device-file-by-path")
358 if isinstance(path,dbus.Array)
and len(path)>0:
360 subprocess.call(
"udisks --mount %s > /dev/null" %path,shell=
True)
361 paths=self.
getProp(
"device-mount-paths")
363 return self.
getProp(
"device-mount-paths")[0]
369 raise Exception (
"Could not mount the VFAT after 5 tries.")
371 return mount_paths[0]
402 def __init__(self, checkable=False, access="disk", diskClass=uDisk, diskDict=None):
405 self.
bus = dbus.SystemBus()
406 proxy = self.bus.get_object(
"org.freedesktop.UDisks",
407 "/org/freedesktop/UDisks")
408 iface = dbus.Interface(proxy,
"org.freedesktop.UDisks")
413 ud=diskClass(path, self.
bus, checkable)
417 if bool(ud.getProp(
"device-is-partition-table")) ==
False:
420 self.
disks[ud].append(ud)
423 ud=diskClass(path, self.
bus, checkable)
424 for d
in self.disks.keys():
425 if ud.master() == d.path:
426 self.
disks[d].append(ud)
443 if self.
access==
"firstFat":
462 result=self.
summary()==other.summary()
472 for k
in self.disks.keys():
473 if k.getProp(
"device-file-by-path")==ud.getProp(
"device-file-by-path"):
return True
482 r=
"Available USB discs\n"
483 r+=
"===================\n"
484 for d
in sorted(self.disks.keys(), key=
lambda disk: disk.getFatUuid()):
485 r+=
"%s\n" %(d.title(),)
486 if len(self.
disks[d])>0:
488 for part
in sorted(self.
disks[d], key=
lambda disk: disk.getFatUuid()):
489 r+=
" %s\n" %(part.path,)
498 r=
"Available USB discs\n"
499 r+=
"===================\n"
500 for d
in self.disks.keys():
502 if len(self.
disks[d])>0:
504 for part
in self.
disks[d]:
505 r+=
" %s\n" %(part.path)
506 r+=part.valuableProperties(12)+
"\n"
518 return self.disks.keys()[n]
519 elif self.
access==
"firstFat":
530 return len(self.
disks)
531 elif self.
access==
"firstFat":
549 for d
in self.disks.keys():
550 for p
in self.
disks[d]:
551 if p.isDosFat()
or p==d :
556 self.fatPaths.append(p.title())
573 s=s.replace(
"/org/freedesktop/UDisks/devices/",
"")
575 if p.split(
"/")[-1]==s:
580 if __name__==
"__main__":