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, threading
55 self.
device = bus.get_object(
"org.freedesktop.UDisks", self.
path)
66 self.
devStuff=os.path.abspath(os.path.join(os.path.dirname(p), os.readlink(p)))
73 "1device-mount-paths":QApplication.translate(
"uDisk",
"point de montage",
None, QApplication.UnicodeUTF8),
74 "2device-size":QApplication.translate(
"uDisk",
"taille",
None, QApplication.UnicodeUTF8),
75 "3drive-vendor":QApplication.translate(
"uDisk",
"marque",
None, QApplication.UnicodeUTF8),
76 "4drive-model":QApplication.translate(
"uDisk",
"modèle de disque",
None, QApplication.UnicodeUTF8),
77 "5drive-serial":QApplication.translate(
"uDisk",
"numéro de série",
None, QApplication.UnicodeUTF8),
80 _specialItems={
"0Check":QApplication.translate(
"uDisk",
"cocher",
None, QApplication.UnicodeUTF8)}
82 _ItemPattern=re.compile(
"[0-9]?(.*)")
111 result= list(uDisk._specialItems.keys())+ list(uDisk._itemNames.keys())
112 return sorted(result)
114 headers = staticmethod(headers)
134 return bool(self.
getProp(prop))
136 return self.
getProp(prop)==value
144 return self.
isTrue(
"device-is-removable")
and self.
isTrue(
"drive-connection-interface",
"usb")
and self.
isTrue(
"device-size")
169 fileByPath=self.
getProp(
"device-file-by-path")
170 if isinstance(fileByPath, dbus.Array)
and len(fileByPath)>0:
171 fileByPath=fileByPath[0]
183 paths=self.
getProp(
"device-mount-paths")
184 if isinstance(paths, dbus.Array)
and len(paths)>0:
200 return self.device_prop.Get(
"org.freedesktop.UDisks", name)
210 return self.
getProp(
"id-type")==
"vfat"
217 return bool(self.
getProp(
"device-is-mounted"))
225 prefix=
"\n"+
" "*indent
227 props=[
"device-file-by-id",
228 "device-file-by-path",
229 "device-mount-paths",
230 "device-is-partition-table",
231 "partition-table-count",
232 "device-is-read-only",
234 "device-is-optical-disc",
246 if isinstance(p,dbus.Array):
248 r+=prefix+
"%s = array:" %(prop)
250 r+=prefix+
" "*indent+s
251 elif isinstance(p,dbus.Boolean):
252 r+=prefix+
"%s = %s" %(prop, bool(p))
253 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):
255 r+=prefix+
"%s = %s" %(prop,p)
256 elif p < 10*1024*1024:
257 r+=prefix+
"%s = %s k" %(prop,p/1024)
258 elif p < 10*1024*1024*1024:
259 r+=prefix+
"%s = %s M" %(prop,p/1024/1024)
261 r+=prefix+
"%s = %s G" %(prop,p/1024/1024/1024)
263 r+=prefix+
"%s = %s" %(prop,p)
264 r+=prefix+
"%s = %s" %(
'devStuff', self.
devStuff)
273 return self.
getProp(
"partition-slave")
283 m=uDisk._ItemPattern.match(self.
headers()[n])
303 elif n <= len(propListe):
317 if isinstance(p,dbus.Array):
318 if len(p)>0:
return str(p[0])
320 elif isinstance(p,dbus.Boolean):
322 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):
342 mount_paths=self.
getProp(
"device-mount-paths")
343 if mount_paths==
None:
346 while len(mount_paths)==0
and leftTries >0:
347 leftTries = leftTries - 1
348 path=self.
getProp(
"device-file-by-path")
349 if isinstance(path,dbus.Array)
and len(path)>0:
351 subprocess.call(
"udisks --mount %s > /dev/null" %path,shell=
True)
352 paths=self.
getProp(
"device-mount-paths")
354 return self.
getProp(
"device-mount-paths")[0]
360 raise Exception (
"Could not mount the VFAT after 5 tries.")
362 return mount_paths[0]
390 def __init__(self,access="disk", diskClass=uDisk, diskDict=None):
392 self.
bus = dbus.SystemBus()
393 proxy = self.bus.get_object(
"org.freedesktop.UDisks",
394 "/org/freedesktop/UDisks")
395 iface = dbus.Interface(proxy,
"org.freedesktop.UDisks")
400 ud=diskClass(path, self.
bus)
404 if bool(ud.getProp(
"device-is-partition-table")) ==
False:
407 self.
disks[ud].append(ud)
410 ud=diskClass(path, self.
bus)
411 for d
in self.disks.keys():
412 if ud.master() == d.path:
413 self.
disks[d].append(ud)
430 if self.
access==
"firstFat":
449 result=self.
summary()==other.summary()
459 for k
in self.disks.keys():
460 if k.getProp(
"device-file-by-path")==ud.getProp(
"device-file-by-path"):
return True
469 r=
"Available USB discs\n"
470 r+=
"===================\n"
471 for d
in sorted(self.disks.keys(), key=
lambda disk: disk.getFatUuid()):
472 r+=
"%s\n" %(d.title(),)
473 if len(self.
disks[d])>0:
475 for part
in sorted(self.
disks[d], key=
lambda disk: disk.getFatUuid()):
476 r+=
" %s\n" %(part.path,)
485 r=
"Available USB discs\n"
486 r+=
"===================\n"
487 for d
in self.disks.keys():
489 if len(self.
disks[d])>0:
491 for part
in self.
disks[d]:
492 r+=
" %s\n" %(part.path)
493 r+=part.valuableProperties(12)+
"\n"
505 return self.disks.keys()[n]
506 elif self.
access==
"firstFat":
517 return len(self.
disks)
518 elif self.
access==
"firstFat":
536 for d
in self.disks.keys():
537 for p
in self.
disks[d]:
538 if p.isDosFat()
or p==d :
543 self.fatPaths.append(p.title())
560 s=s.replace(
"/org/freedesktop/UDisks/devices/",
"")
562 if p.split(
"/")[-1]==s:
567 if __name__==
"__main__":
def __init__
Le constructeur.
def getProp
Facilite l'accès aux propriétés à l'aide des mots clés du module udisks.
def getFirstFat
Renvoie la première partition VFAT.
def __getitem__
Renvoye le nième disque.
def getFatUuid
renvoie l'uuid de la première partition FAT après que celle-ci aura été identifiée (utile pour les di...
def showableProp
Renvoie une propriété dans un type "montrable" par QT.
def __len__
Renseigne sur la longueur de la collection.
def finishInit
Fin de l'initialisation.
def uniqueId
renvoie un identifiant unique.
def contains
Permet de déterminer si un disque est dans la collection.
def isUsbDisk
Facilite le réprage des disques USB USB.
une classe pour représenter la collection des disques USB connectés
def getFirstFats
Facilite l'accès aux partitions de type DOS-FAT, et a des effets de bord :
def isTrue
Renvoie la valeur de vérité d'une propriété
def devicePropProxy
renvoie un proxy vers un navigateur de propriétés
def isDosFat
Permet de reconnaitre les partitions DOS-FAT.
def title
Permet d'obtenir un identifiant unique de disque.
def mountFirstFats
fabrique la liste des partitions FAT, monte les partitions FAT si elles ne le sont pas ...
def mountPoint
Permet d'accèder à l'instance par un point de montage.
def valuableProperties
Facilite l'accès aux propriétés intéressantes d'une instance.
une classe pour représenter un disque ou une partition.
def ensureMounted
Permet de s'assurer qu'une partition ou un disque sera bien monté
def __init__
Le constructeur.
def __str__
Fournit une représentation imprimable.
def unNumberProp
retire le numéro des en-têtes pour en faire un nom de propriété valide pour interroger dbus ...
def compare
Sert à comparer deux collections de disques, par exemple une collection passée et une collection prés...
def __str__
Fournit une représentation imprimable.
def summary
Fournit une représentation imprimable d'un résumé
def __getitem__
Renvoie un élément de listage de données internes au disque.
def file
Permet d'accèder à l'instance par un nom de fichier.
def master
renvoie le chemin du disque, dans le cas où self est une partition