Skip to content

Commit 0f331e9

Browse files
committed
Throw exception when partition is not mounted.
Instead of returning strings for partition size value, throw an exception when the partition is not (and presumably cannot be) mounted in `details_udisks2()`. Also, return 0s instead of empty strings in a similar situation in `details_udev()` This changes prevent the conversion error reported in issue #397; however, additional changes will be needed to catch the `PartitionNotMounted` exception now potentially thrown by a call to `details()` on Linux.
1 parent d2099ad commit 0f331e9

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

scripts/usb.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
import pythoncom
2626

2727

28+
class PartitionNotMounted(Exception):
29+
30+
def __init__(self, partition):
31+
self.message = 'Partition is not mounted: {}'.format(partition)
32+
33+
2834
def is_block(usb_disk):
2935
"""
3036
Function to detect if the USB is block device
@@ -290,8 +296,8 @@ def details_udev(usb_disk_part):
290296
fdisk_cmd = 'LANG=C fdisk -l ' + usb_disk_part + \
291297
' | grep "^Disk /" | sed -re "s/.*\s([0-9]+)\sbytes.*/\\1/"'
292298
size_total = subprocess.check_output(fdisk_cmd, shell=True).strip()
293-
size_used = ""
294-
size_free = ""
299+
size_used = 0
300+
size_free = 0
295301
mount_point = ""
296302

297303
return {'uuid': uuid, 'file_system': file_system, 'label': label, 'mount_point': mount_point,
@@ -353,9 +359,7 @@ def details_udisks2(usb_disk_part):
353359
size_used = shutil.disk_usage(mount_point)[1]
354360
size_free = shutil.disk_usage(mount_point)[2]
355361
else:
356-
size_total = str('No_Mount')
357-
size_used = str('No_Mount')
358-
size_free = str('No_Mount')
362+
raise PartitionNotMounted(usb_disk_part)
359363

360364
return {'uuid': uuid, 'file_system': file_system, 'label': label, 'mount_point': mount_point,
361365
'size_total': size_total, 'size_used': size_used, 'size_free': size_free,

0 commit comments

Comments
 (0)