From 6db5cede06dfb580c5e4f6cdeb1796256969b4fb Mon Sep 17 00:00:00 2001 From: c0rejump Date: Tue, 21 Jan 2020 21:30:50 +0100 Subject: [PATCH] tools/pydfu.py: Clean up syntax, update comments and docstrings. Some parts of code have been aligned to increase readability. In general '' instead of "" were used wherever possible to keep the same convention for entire file. Import inspect line has been moved to the top according to hints reported by pep8 tools. A few extra spaces were removed, a few missing spaces were added. Comments have been updated, mostly in "read_dfu_file" function. Some other comments have been capitalized and/or slightly updated. A few docstrings were fixed as well. No real code changes intended. --- tools/pydfu.py | 195 ++++++++++++++++++++++++++----------------------- 1 file changed, 104 insertions(+), 91 deletions(-) diff --git a/tools/pydfu.py b/tools/pydfu.py index 658ce59ae6..962ba2b7f0 100755 --- a/tools/pydfu.py +++ b/tools/pydfu.py @@ -15,6 +15,7 @@ from __future__ import print_function import argparse import collections +import inspect import re import struct import sys @@ -67,7 +68,6 @@ __DFU_INTERFACE = 0 # Python 3 deprecated getargspec in favour of getfullargspec, but # Python 2 doesn't have the latter, so detect which one to use -import inspect getargspec = getattr(inspect, 'getfullargspec', inspect.getargspec) if 'length' in getargspec(usb.util.get_string).args: @@ -82,9 +82,17 @@ else: def find_dfu_cfg_descr(descr): if len(descr) == 9 and descr[0] == 9 and descr[1] == _DFU_DESCRIPTOR_TYPE: - nt = collections.namedtuple('CfgDescr', - ['bLength', 'bDescriptorType', 'bmAttributes', - 'wDetachTimeOut', 'wTransferSize', 'bcdDFUVersion']) + nt = collections.namedtuple( + 'CfgDescr', + [ + 'bLength', + 'bDescriptorType', + 'bmAttributes', + 'wDetachTimeOut', + 'wTransferSize', + 'bcdDFUVersion' + ] + ) return nt(*struct.unpack(' 1: - raise ValueError("Multiple DFU devices found") + raise ValueError('Multiple DFU devices found') __dev = devices[0] __dev.set_configuration() @@ -141,57 +149,56 @@ def get_status(): """Get the status of the last operation.""" stat = __dev.ctrl_transfer(0xA1, __DFU_GETSTATUS, 0, __DFU_INTERFACE, 6, 20000) - # print (__DFU_STAT[stat[4]], stat) return stat[4] def mass_erase(): - """Performs a MASS erase (i.e. erases the entire device.""" + """Performs a MASS erase (i.e. erases the entire device).""" # Send DNLOAD with first byte=0x41 __dev.ctrl_transfer(0x21, __DFU_DNLOAD, 0, __DFU_INTERFACE, - "\x41", __TIMEOUT) + '\x41', __TIMEOUT) # Execute last command if get_status() != __DFU_STATE_DFU_DOWNLOAD_BUSY: - raise Exception("DFU: erase failed") + raise Exception('DFU: erase failed') # Check command state if get_status() != __DFU_STATE_DFU_DOWNLOAD_IDLE: - raise Exception("DFU: erase failed") + raise Exception('DFU: erase failed') def page_erase(addr): """Erases a single page.""" if __verbose: - print("Erasing page: 0x%x..." % (addr)) + print('Erasing page: 0x%x...' % (addr)) # Send DNLOAD with first byte=0x41 and page address - buf = struct.pack("