From 6a5a32d29fb6fbb9f884d48b35b0a4c44b401d01 Mon Sep 17 00:00:00 2001 From: Arvid Norlander Date: Wed, 5 Aug 2020 18:42:34 +0200 Subject: [PATCH] Various small and easy cleanup found by inspection in PyCharm --- bin/validity-led-dance | 6 +++--- scripts/dbus-cmd.py | 8 +++++--- scripts/prototype.py | 4 ++-- validitysensor/db.py | 10 +++++----- validitysensor/flash.py | 13 ++++++------ validitysensor/hw_tables.py | 4 ++-- validitysensor/init.py | 2 +- validitysensor/init_db.py | 4 ++-- validitysensor/init_flash.py | 2 +- validitysensor/sensor.py | 26 ++++++++++++------------ validitysensor/sid.py | 2 +- validitysensor/timeslot.py | 36 +++++++++++++++++----------------- validitysensor/tls.py | 22 ++++++++++----------- validitysensor/upload_fwext.py | 4 ++-- validitysensor/usb.py | 4 ++-- validitysensor/util.py | 2 +- 16 files changed, 76 insertions(+), 73 deletions(-) diff --git a/bin/validity-led-dance b/bin/validity-led-dance index 4c5fc2c..4612938 100755 --- a/bin/validity-led-dance +++ b/bin/validity-led-dance @@ -22,9 +22,9 @@ if __name__ == "__main__": sleep(0.05) led_script = unhexlify( - '39ff100000ff03000001ff002000000000ffff0000ffff0000ff03000001ff00' \ - '200000000000000000ffff0000ff03000001ff002000000000ffff0000000000' \ - '0000000000000000000000000000000000000000000000000000000000000000' \ + '39ff100000ff03000001ff002000000000ffff0000ffff0000ff03000001ff00' + '200000000000000000ffff0000ff03000001ff002000000000ffff0000000000' + '0000000000000000000000000000000000000000000000000000000000000000' '0000000000000000000000000000000000000000000000000000000000') assert_status(tls.app(led_script)) diff --git a/scripts/dbus-cmd.py b/scripts/dbus-cmd.py index 93b44bc..3774e00 100755 --- a/scripts/dbus-cmd.py +++ b/scripts/dbus-cmd.py @@ -1,12 +1,14 @@ #!/usr/bin/env python3 -import dbus.mainloop.glib -dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) import sys +import dbus.mainloop.glib + +dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) + bus = dbus.SystemBus() o = bus.get_object('net.reactivated.Fprint', '/net/reactivated/Fprint/Manager', introspect=False) o = o.GetDefaultDevice() o = bus.get_object('net.reactivated.Fprint', o, introspect=False) o = dbus.Interface(o, 'net.reactivated.Fprint.Device') -print(o.RunCmd(sys.argv[1])) \ No newline at end of file +print(o.RunCmd(sys.argv[1])) diff --git a/scripts/prototype.py b/scripts/prototype.py index 9ffabe5..3a64e40 100644 --- a/scripts/prototype.py +++ b/scripts/prototype.py @@ -3,8 +3,8 @@ from binascii import hexlify from validitysensor.sensor import sensor -#usb.trace_enabled = True -#tls.trace_enabled = True +# usb.trace_enabled = True +# tls.trace_enabled = True def identify(): diff --git a/validitysensor/db.py b/validitysensor/db.py index 3431fe0..6853e1c 100644 --- a/validitysensor/db.py +++ b/validitysensor/db.py @@ -8,7 +8,7 @@ from .util import assert_status from .winbio_constants import finger_names -class UserStorage(): +class UserStorage: def __init__(self, dbid, name): self.dbid = dbid self.name = name @@ -19,7 +19,7 @@ class UserStorage(): self.name), repr(self.users)) -class User(): +class User: def __init__(self, dbid, identity): self.dbid = dbid self.identity = identity @@ -112,7 +112,7 @@ def identity_to_bytes(identity: str): raise Exception('Don' 't know how to handle identity %s' % repr(identity)) -class DbRecord(): +class DbRecord: def __init__(self): self.dbid = 0 self.type = 0 @@ -125,8 +125,8 @@ class DbRecord(): self.dbid, self.type, self.storage, repr(self.value), repr(self.children)) -class Db(): - class Info(): +class Db: + class Info: def __init__(self, total, used, free, records, roots): self.total = total # partition size self.used = used # used (not deleted) diff --git a/validitysensor/flash.py b/validitysensor/flash.py index 2db41a3..de0befb 100644 --- a/validitysensor/flash.py +++ b/validitysensor/flash.py @@ -6,9 +6,10 @@ from .tls import tls from .util import assert_status, unhex -class FlashInfo(): +class FlashInfo: def __init__(self, ic, blocks, unknown0, blocksize, unknown1, partitions): - self.ic, self.blocks, self.unknown0, self.blocksize, self.unknown1, self.partitions = ic, blocks, unknown0, blocksize, unknown1, partitions + self.ic, self.blocks, self.unknown0, self.blocksize, self.unknown1, self.partitions = \ + ic, blocks, unknown0, blocksize, unknown1, partitions def __repr__(self): return 'FlashInfo(%s, 0x%x, 0x%x, 0x%x, 0x%x, %s)' % (repr( @@ -20,7 +21,7 @@ class FlashInfo(): # access lvl: # 2 -- write only # 7 -- can read/write even without TLS -class PartitionInfo(): +class PartitionInfo: def __init__(self, id, type, access_lvl, offset, size): self.id, self.type, self.access_lvl, self.offset, self.size = id, type, access_lvl, offset, size @@ -39,7 +40,7 @@ def get_flash_info(): ic = flash_ic_table_lookup(jid0, jid1, blocks * blocksize) - if ic == None: + if ic is None: raise Exception('Unknown flash IC. JEDEC id=%x:%x, size=%dx%d' % (jid0, jid1, blocks, blocksize)) @@ -63,7 +64,7 @@ def get_flash_info(): # 0200 2377 0000 0100 802f0000 # 0200 6637 0100 0c00 f0220200 # 0100 2556 0000 0100 60040000 -class ModuleInfo(): +class ModuleInfo: def __init__(self, type, subtype, major, minor, size): self.type, self.subtype, self.major, self.minor, self.size = type, subtype, major, minor, size @@ -72,7 +73,7 @@ class ModuleInfo(): self.minor, self.size) -class FirmwareInfo(): +class FirmwareInfo: def __init__(self, major, minor, buildtime, modules): self.major, self.minor, self.buildtime, self.modules = major, minor, buildtime, modules diff --git a/validitysensor/hw_tables.py b/validitysensor/hw_tables.py index 868d2ed..7c657a9 100644 --- a/validitysensor/hw_tables.py +++ b/validitysensor/hw_tables.py @@ -1,4 +1,4 @@ -class DeviceInfo(): +class DeviceInfo: def __init__(self, major, type, version, version_mask, name): self.major, self.type, self.version, self.version_mask, self.name = major, type, version, version_mask, name @@ -445,7 +445,7 @@ def dev_info_lookup(major, ver): return fuzzy_match -class FlashIcInfo(): +class FlashIcInfo: def __init__(self, name, size, f18, jid0, jid1, f1b, f1c, f1e, secror_size, sector_erase_cmd, f25, f26): self.name, self.size, self.f18, self.jid0, self.jid1, self.f1b, self.f1c, self.f1e, self.secror_size, self.sector_erase_cmd, self.f25, self.f26 = name, size, f18, jid0, jid1, f1b, f1c, f1e, secror_size, sector_erase_cmd, f25, f26 diff --git a/validitysensor/init.py b/validitysensor/init.py index 909c3b8..a990993 100644 --- a/validitysensor/init.py +++ b/validitysensor/init.py @@ -28,7 +28,7 @@ def close(): def open_common(): init_flash() usb.send_init() - tls.parseTlsFlash(read_tls_flash()) + tls.parse_tls_flash(read_tls_flash()) tls.open() upload_fwext() sensor.open() diff --git a/validitysensor/init_db.py b/validitysensor/init_db.py index d9b9211..4ae1de2 100644 --- a/validitysensor/init_db.py +++ b/validitysensor/init_db.py @@ -32,8 +32,8 @@ def init_machine_guid(machine_guid='e7260876-58db-4d27-8c40-8d13110d6a71'): def init_db(): stg = db.get_user_storage(name='StgWindsor') - if stg == None: + if stg is None: logging.info('Creating a new user storage object') db.new_user_storate() - #init_machine_guid() + # init_machine_guid() diff --git a/validitysensor/init_flash.py b/validitysensor/init_flash.py index e427206..ee46909 100644 --- a/validitysensor/init_flash.py +++ b/validitysensor/init_flash.py @@ -157,7 +157,7 @@ def init_flash(): erase_flash(4) # Persist certs and keys on cert partition. - write_flash(1, 0, tls.makeTlsFlash()) + write_flash(1, 0, tls.make_tls_flash()) # Reboot. # The device will disconnect and our service will be started by udev as soon as it is connected again. diff --git a/validitysensor/sensor.py b/validitysensor/sensor.py index 4c220ca..2e8dec7 100644 --- a/validitysensor/sensor.py +++ b/validitysensor/sensor.py @@ -87,7 +87,7 @@ def factory_reset(): reboot() -class RomInfo(): +class RomInfo: @classmethod def get(cls): rsp = tls.cmd(b'\x01') @@ -173,10 +173,10 @@ def bitpack(b): # convert back to bytes b = b.to_bytes((u * l + 7) // 8, 'little') - return (u, m, b) + return u, m, b -class Line(): +class Line: mask = None flags = None data = None @@ -217,7 +217,7 @@ class CaptureMode(Enum): ENROLL = 3 -class Sensor(): +class Sensor: calib_data = b'' def open(self): @@ -417,7 +417,7 @@ class Sensor(): tst = self.patch_timeslot_again(tst) c[1] = self.get_key_line() + tst[self.type_info.line_width:] - #---------------- Reply Configuration --------------- + # ---------------- Reply Configuration --------------- chunks += [[0x17, b'']] if mode == CaptureMode.IDENTIFY: @@ -448,7 +448,7 @@ class Sensor(): 0x2e, unhexlify('0200180023000000700070004d010000a0008c003c32321e3c0a0202') ]] - #---------------- Interleave --------------- + # ---------------- Interleave --------------- chunks += [[0x44, pack(' 0: l.data += b'\0' * (4 - pad) - #---------------- Line Update --------------- + # ---------------- Line Update --------------- line_update = pack('> 0x14) <= 1]) chunks += [[0x30, line_update]] - #---------------- Line Update Transform --------------- + # ---------------- Line Update Transform --------------- update_transform = b''.join([ pack('> 0x14) > 1 @@ -525,7 +525,7 @@ class Sensor(): tst = self.patch_timeslot_again(tst) c[1] = tst - #---------------- Reply Configuration --------------- + # ---------------- Reply Configuration --------------- chunks += [[0x17, b'']] if mode == CaptureMode.IDENTIFY: @@ -728,7 +728,7 @@ class Sensor(): if error != 0: raise Exception('Scanning problem: %04x' % error) - return (x, y, w1, w2) + return x, y, w1, w2 finally: tls.app(unhexlify('04')) # capture stop if still running, cleanup @@ -788,7 +788,7 @@ class Sensor(): res = res[magic_len + l:] - return (header, template, tid) + return header, template, tid def make_finger_data(self, subtype, template, tid): template = pack('> 3, b[0] & 7) + return 14, 1, (b[0] & 0x38) >> 3, b[0] & 7 elif b[0] & 0xc0 == 0xc0: - return (15, 2, (b[0] & 0x38) >> 3, b[0] & 7, 0x100 if b[1] == 0 else b[1]) + return 15, 2, (b[0] & 0x38) >> 3, b[0] & 7, 0x100 if b[1] == 0 else b[1] else: raise Exception('Unhandled instruction %02x' % b) @@ -158,7 +158,7 @@ def find_nth_insn(b, opcode, n): if op == opcode: n -= 1 if n == 0: - return (pc, b[:sz]) + return pc, b[:sz] b = b[sz:] pc += sz @@ -177,7 +177,7 @@ def find_nth_regwrite(b, reg_addr, n): if addr == reg_addr: n -= 1 if n == 0: - return (pc, b[:sz]) + return pc, b[:sz] b = b[sz:] pc += sz diff --git a/validitysensor/tls.py b/validitysensor/tls.py index e9f9827..388f1ca 100644 --- a/validitysensor/tls.py +++ b/validitysensor/tls.py @@ -86,7 +86,7 @@ def unpad(b): # TODO assert the right state transitions -class Tls(): +class Tls: def __init__(self, usb): self.usb = usb self.reset() @@ -407,7 +407,7 @@ class Tls(): def make_ext(self, id, b): return pack('>H', id) + with_2bytes_size(b) - def parseTlsFlash(self, reply): + def parse_tls_flash(self, reply): while len(reply) > 0: hdr, reply = reply[:4], reply[4:] hs, reply = reply[:0x20], reply[0x20:] @@ -440,20 +440,20 @@ class Tls(): else: self.trace('unhandled block id %04x (%d bytes): %s' % (id, sz, hexlify(body))) - def makeTlsFlashBlock(self, id, body): + def make_tls_flash_block(self, id, body): m = sha256() m.update(body) hdr = pack('