From 032cd0cf3b58dcbb80d066c5409ea15c2af258ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Elio=20Petten=C3=B2?= Date: Sat, 15 Apr 2017 15:08:33 +0100 Subject: all drivers: make --device optional at the tool level. This requires the drivers to validate their device, but also means they can provide a more explicit error message for the user as to what should be passed. This is a first step to help solving Issue 9. --- glucometer.py | 5 +++-- glucometerutils/drivers/accuchek_reports.py | 5 +++++ glucometerutils/drivers/fsoptium.py | 5 +++++ glucometerutils/drivers/otultra2.py | 5 +++++ glucometerutils/drivers/otultraeasy.py | 5 +++++ glucometerutils/drivers/otverio2015.py | 5 +++++ glucometerutils/drivers/sdcodefree.py | 5 +++++ glucometerutils/exceptions.py | 7 +++++++ glucometerutils/support/freestyle.py | 5 +++++ 9 files changed, 45 insertions(+), 2 deletions(-) diff --git a/glucometer.py b/glucometer.py index be8fb02..63ea8fc 100755 --- a/glucometer.py +++ b/glucometer.py @@ -25,8 +25,9 @@ def main(): '--driver', action='store', required=True, help='Select the driver to use for connecting to the glucometer.') parser.add_argument( - '--device', action='store', required=True, - help='Select the path to the glucometer device.') + '--device', action='store', required=False, + help=('Select the path to the glucometer device. Some devices require this ' + 'argument, others will try autodetection.')) parser.add_argument( '--vlog', action='store', required=False, type=int, diff --git a/glucometerutils/drivers/accuchek_reports.py b/glucometerutils/drivers/accuchek_reports.py index a678736..bf97064 100644 --- a/glucometerutils/drivers/accuchek_reports.py +++ b/glucometerutils/drivers/accuchek_reports.py @@ -46,6 +46,11 @@ _DATETIME_FORMAT = ' '.join((_DATE_FORMAT, _TIME_FORMAT)) class Device(object): def __init__(self, device): + if not device or not os.path.isdir(device): + raise exceptions.CommandLineError( + '--device parameter is required, should point to mount path for the ' + 'meter.') + report_files = glob.glob(os.path.join(device, '*', 'Reports', '*.csv')) if not report_files: raise exceptions.ConnectionFailed( diff --git a/glucometerutils/drivers/fsoptium.py b/glucometerutils/drivers/fsoptium.py index fd4c791..92ee76d 100644 --- a/glucometerutils/drivers/fsoptium.py +++ b/glucometerutils/drivers/fsoptium.py @@ -79,6 +79,11 @@ def _parse_clock(datestr): class Device(object): def __init__(self, device): + if not device: + raise exceptions.CommandLineError( + '--device parameter is required, should point to the serial device ' + 'connected to the meter.') + self.serial_ = serial.Serial( port=device, baudrate=19200, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, diff --git a/glucometerutils/drivers/otultra2.py b/glucometerutils/drivers/otultra2.py index 1c8ee10..21d6743 100644 --- a/glucometerutils/drivers/otultra2.py +++ b/glucometerutils/drivers/otultra2.py @@ -124,6 +124,11 @@ def _parse_datetime(response): class Device(object): def __init__(self, device): + if not device: + raise exceptions.CommandLineError( + '--device parameter is required, should point to the serial device ' + 'connected to the meter.') + self.serial_ = serial.Serial( port=device, baudrate=9600, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, diff --git a/glucometerutils/drivers/otultraeasy.py b/glucometerutils/drivers/otultraeasy.py index 50a6020..ce68625 100644 --- a/glucometerutils/drivers/otultraeasy.py +++ b/glucometerutils/drivers/otultraeasy.py @@ -177,6 +177,11 @@ class _Packet(object): class Device(object): def __init__(self, device): + if not device: + raise exceptions.CommandLineError( + '--device parameter is required, should point to the serial device ' + 'connected to the meter.') + self.serial_ = serial.Serial( port=device, baudrate=9600, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, diff --git a/glucometerutils/drivers/otverio2015.py b/glucometerutils/drivers/otverio2015.py index 80de6ed..55ffbd2 100644 --- a/glucometerutils/drivers/otverio2015.py +++ b/glucometerutils/drivers/otverio2015.py @@ -99,6 +99,11 @@ def _convert_timestamp(timestamp): class Device(object): def __init__(self, device): + if not device: + raise exceptions.CommandLineError( + '--device parameter is required, should point to the disk device ' + 'representing the meter.') + self.device_name_ = device self.scsi_device_ = SCSIDevice(device, readwrite=True) self.scsi_ = SCSI(self.scsi_device_) diff --git a/glucometerutils/drivers/sdcodefree.py b/glucometerutils/drivers/sdcodefree.py index c89d894..19644f6 100644 --- a/glucometerutils/drivers/sdcodefree.py +++ b/glucometerutils/drivers/sdcodefree.py @@ -65,6 +65,11 @@ def xor_checksum(msg): class Device(object): def __init__(self, device): + if not device: + raise exceptions.CommandLineError( + '--device parameter is required, should point to the serial ' + 'device connected to the meter.') + self.serial_ = serial.Serial( port=device, baudrate=38400, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, diff --git a/glucometerutils/exceptions.py b/glucometerutils/exceptions.py index 1290d9a..52c4e94 100644 --- a/glucometerutils/exceptions.py +++ b/glucometerutils/exceptions.py @@ -13,6 +13,13 @@ class Error(Exception): return self.message +class CommandLineError(Error): + """Error with commandline parameters provided.""" + + def __init__(self, message=''): + self.message = message + + class ConnectionFailed(Error): """It was not possible to connect to the meter.""" diff --git a/glucometerutils/support/freestyle.py b/glucometerutils/support/freestyle.py index 307cf64..6c6e294 100644 --- a/glucometerutils/support/freestyle.py +++ b/glucometerutils/support/freestyle.py @@ -69,6 +69,11 @@ class FreeStyleHidDevice(object): TEXT_REPLY_CMD = 0x60 def __init__(self, device): + if not device: + raise exceptions.CommandLineError( + '--device parameter is required, should point to /dev/hidraw ' + 'for the meter') + if not os.path.exists(device): raise exceptions.ConnectionFailed( message='Path %s does not exist.' % device) -- cgit v1.2.3