From 0f331e9b04d48ee22edf17c1a7a448535337975c Mon Sep 17 00:00:00 2001 From: Alan D Moore Date: Mon, 1 Oct 2018 11:36:16 -0500 Subject: [PATCH] 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. --- scripts/usb.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/scripts/usb.py b/scripts/usb.py index 2e3c3ba..244894e 100644 --- a/scripts/usb.py +++ b/scripts/usb.py @@ -25,6 +25,12 @@ if platform.system() == 'Windows': import pythoncom +class PartitionNotMounted(Exception): + + def __init__(self, partition): + self.message = 'Partition is not mounted: {}'.format(partition) + + def is_block(usb_disk): """ Function to detect if the USB is block device @@ -290,8 +296,8 @@ def details_udev(usb_disk_part): fdisk_cmd = 'LANG=C fdisk -l ' + usb_disk_part + \ ' | grep "^Disk /" | sed -re "s/.*\s([0-9]+)\sbytes.*/\\1/"' size_total = subprocess.check_output(fdisk_cmd, shell=True).strip() - size_used = "" - size_free = "" + size_used = 0 + size_free = 0 mount_point = "" return {'uuid': uuid, 'file_system': file_system, 'label': label, 'mount_point': mount_point, @@ -353,9 +359,7 @@ def details_udisks2(usb_disk_part): size_used = shutil.disk_usage(mount_point)[1] size_free = shutil.disk_usage(mount_point)[2] else: - size_total = str('No_Mount') - size_used = str('No_Mount') - size_free = str('No_Mount') + raise PartitionNotMounted(usb_disk_part) return {'uuid': uuid, 'file_system': file_system, 'label': label, 'mount_point': mount_point, 'size_total': size_total, 'size_used': size_used, 'size_free': size_free,