Skip to content

Commit

Permalink
Enable almost all Flake8 checks (#548)
Browse files Browse the repository at this point in the history
* fix W391

* E262

* E703

* W293

* some E

* E231

* some more E

* E225

* more E

* E252

* no tabs

* more tabs

* E701

* uds.py

* almost all of them

* only e265 left

* not sure why this is triggering on commented out code

* ignore esptool
  • Loading branch information
adeebshihadeh committed Jun 1, 2020
1 parent 3d5a717 commit d7f7b14
Show file tree
Hide file tree
Showing 71 changed files with 448 additions and 431 deletions.
4 changes: 3 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ repos:
- id: flake8
exclude: '^(tests/automated)/'
args:
- --select=F
- --ignore=E111,E114,E121,E501,E302,E305,W504
- --exclude=python/esptool.py,tests/gmbitbang/*
- --statistics
- repo: local
hooks:
- id: pylint
Expand Down
4 changes: 2 additions & 2 deletions board/tools/enter_download_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ def enter_download_mode(device):
def find_first_panda(context=None):
context = context or usb1.USBContext()
for device in context.getDeviceList(skip_on_error=True):
if device.getVendorID() == 0xbbaa and device.getProductID()&0xFF00 == 0xdd00:
if device.getVendorID() == 0xbbaa and device.getProductID() & 0xFF00 == 0xdd00:
return device

if __name__ == "__main__":
panda_dev = find_first_panda()
if panda_dev == None:
if panda_dev is None:
print("no device found")
sys.exit(0)
print("found device")
Expand Down
10 changes: 4 additions & 6 deletions crypto/getcertheader.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ def modinv(a, m):

def to_c_string(x):
mod = (hex(x)[2:-1].rjust(0x100, '0'))
hh = ''.join('\\x'+mod[i:i+2] for i in range(0, 0x100, 2))
hh = ''.join('\\x' + mod[i:i + 2] for i in range(0, 0x100, 2))
return hh

def to_c_uint32(x):
nums = []
for i in range(0x20):
nums.append(x%(2**32))
nums.append(x % (2**32))
x //= (2**32)
return "{"+'U,'.join(map(str, nums))+"U}"
return "{" + 'U,'.join(map(str, nums)) + "U}"

for fn in sys.argv[1:]:
rsa = RSA.importKey(open(fn).read())
Expand All @@ -35,11 +35,9 @@ def to_c_uint32(x):

cname = fn.split("/")[-1].split(".")[0] + "_rsa_key"

print('RSAPublicKey '+cname+' = {.len = 0x20,')
print('RSAPublicKey ' + cname + ' = {.len = 0x20,')
print(' .n0inv = %dU,' % n0inv)
print(' .n = %s,' % to_c_uint32(rsa.n))
print(' .rr = %s,' % to_c_uint32(rr))
print(' .exponent = %d,' % rsa.e)
print('};')


3 changes: 1 addition & 2 deletions crypto/sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@
dd = hashlib.sha1(dat).digest()

print("hash:", str(binascii.hexlify(dd), "utf-8"))
dd = b"\x00\x01" + b"\xff"*0x69 + b"\x00" + dd
dd = b"\x00\x01" + b"\xff" * 0x69 + b"\x00" + dd
rsa_out = pow(int.from_bytes(dd, byteorder='big', signed=False), rsa.d, rsa.n)
sig = (hex(rsa_out)[2:].rjust(0x100, '0'))
x += binascii.unhexlify(sig)
f.write(x)

7 changes: 5 additions & 2 deletions examples/can_bit_transition.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

class Message():
"""Details about a specific message ID."""

def __init__(self, message_id):
self.message_id = message_id
self.ones = [0] * 8 # bit set if 1 is always seen
Expand Down Expand Up @@ -32,7 +33,8 @@ def load(self, filename, start, end):
reader = csv.reader(input)
next(reader, None) # skip the CSV header
for row in reader:
if not len(row): continue
if not len(row):
continue
time = float(row[0])
bus = int(row[2])
if time < start or bus > 127:
Expand Down Expand Up @@ -76,7 +78,8 @@ def PrintUnique(log_file, low_range, high_range):
if message_id in low.messages:
high.messages[message_id].printBitDiff(low.messages[message_id])
found = True
if not found: print('No messages that transition from always low to always high found!')
if not found:
print('No messages that transition from always low to always high found!')

if __name__ == "__main__":
if len(sys.argv) < 4:
Expand Down
6 changes: 3 additions & 3 deletions examples/can_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ def can_logger():

try:
p = Panda("WIFI")
except:
except Exception:
print("WiFi connection timed out. Please make sure your Panda is connected and try again.")
sys.exit(0)

try:
outputfile = open('output.csv', 'w')
csvwriter = csv.writer(outputfile)
#Write Header
# Write Header
csvwriter.writerow(['Bus', 'MessageID', 'Message', 'MessageLength'])
print("Writing csv file output.csv. Press Ctrl-C to exit...\n")

Expand All @@ -33,7 +33,7 @@ def can_logger():
while True:
can_recv = p.can_recv()

for address, _, dat, src in can_recv:
for address, _, dat, src in can_recv:
csvwriter.writerow([str(src), str(hex(address)), f"0x{dat.hex()}", len(dat)])

if src == 0:
Expand Down
1 change: 1 addition & 0 deletions examples/can_unique.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

class Message():
"""Details about a specific message ID."""

def __init__(self, message_id):
self.message_id = message_id
self.data = {} # keyed by hex string encoded message data
Expand Down
1 change: 0 additions & 1 deletion examples/get_panda_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def get_panda_password():
sys.exit(0)

wifi = p.get_serial()
#print('[%s]' % ', '.join(map(str, wifi)))
print("SSID: " + wifi[0])
print("Password: " + wifi[1])

Expand Down
20 changes: 13 additions & 7 deletions examples/query_fw_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

if __name__ == "__main__":
addrs = [0x700 + i for i in range(256)]
addrs += [0x18da0000 + (i<<8) + 0xf1 for i in range(256)]
addrs += [0x18da0000 + (i << 8) + 0xf1 for i in range(256)]
results = {}

panda = Panda()
Expand All @@ -31,37 +31,43 @@

try:
data = uds_client.read_data_by_identifier(DATA_IDENTIFIER_TYPE.BOOT_SOFTWARE_IDENTIFICATION)
if data: resp[DATA_IDENTIFIER_TYPE.BOOT_SOFTWARE_IDENTIFICATION] = data
if data:
resp[DATA_IDENTIFIER_TYPE.BOOT_SOFTWARE_IDENTIFICATION] = data
except NegativeResponseError:
pass

try:
data = uds_client.read_data_by_identifier(DATA_IDENTIFIER_TYPE.APPLICATION_SOFTWARE_IDENTIFICATION)
if data: resp[DATA_IDENTIFIER_TYPE.APPLICATION_SOFTWARE_IDENTIFICATION] = data
if data:
resp[DATA_IDENTIFIER_TYPE.APPLICATION_SOFTWARE_IDENTIFICATION] = data
except NegativeResponseError:
pass

try:
data = uds_client.read_data_by_identifier(DATA_IDENTIFIER_TYPE.APPLICATION_DATA_IDENTIFICATION)
if data: resp[DATA_IDENTIFIER_TYPE.APPLICATION_DATA_IDENTIFICATION] = data
if data:
resp[DATA_IDENTIFIER_TYPE.APPLICATION_DATA_IDENTIFICATION] = data
except NegativeResponseError:
pass

try:
data = uds_client.read_data_by_identifier(DATA_IDENTIFIER_TYPE.BOOT_SOFTWARE_FINGERPRINT)
if data: resp[DATA_IDENTIFIER_TYPE.BOOT_SOFTWARE_FINGERPRINT] = data
if data:
resp[DATA_IDENTIFIER_TYPE.BOOT_SOFTWARE_FINGERPRINT] = data
except NegativeResponseError:
pass

try:
data = uds_client.read_data_by_identifier(DATA_IDENTIFIER_TYPE.APPLICATION_SOFTWARE_FINGERPRINT)
if data: resp[DATA_IDENTIFIER_TYPE.APPLICATION_SOFTWARE_FINGERPRINT] = data
if data:
resp[DATA_IDENTIFIER_TYPE.APPLICATION_SOFTWARE_FINGERPRINT] = data
except NegativeResponseError:
pass

try:
data = uds_client.read_data_by_identifier(DATA_IDENTIFIER_TYPE.APPLICATION_DATA_FINGERPRINT)
if data: resp[DATA_IDENTIFIER_TYPE.APPLICATION_DATA_FINGERPRINT] = data
if data:
resp[DATA_IDENTIFIER_TYPE.APPLICATION_DATA_FINGERPRINT] = data
except NegativeResponseError:
pass

Expand Down
15 changes: 6 additions & 9 deletions examples/query_vin_and_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

def get_current_data_for_pid(pid):
# 01 xx = Show current data
isotp_send(panda, b"\x01"+ bytes([pid]), 0x7e0)
isotp_send(panda, b"\x01" + bytes([pid]), 0x7e0)
return isotp_recv(panda, 0x7e8)

def get_supported_pids():
ret = []
pid = 0
while 1:
supported = struct.unpack(">I", get_current_data_for_pid(pid)[2:])[0]
for i in range(1+pid, 0x21+pid):
for i in range(1 + pid, 0x21 + pid):
if supported & 0x80000000:
ret.append(i)
supported <<= 1
Expand All @@ -45,16 +45,13 @@ def get_supported_pids():
print("DTCs:", "".join(map(chr, dtcs[:2])))

supported_pids = get_supported_pids()
print("Supported PIDs:",supported_pids)
print("Supported PIDs:", supported_pids)

while 1:
speed = struct.unpack(">B", get_current_data_for_pid(13)[2:])[0] # kph
rpm = struct.unpack(">H", get_current_data_for_pid(12)[2:])[0]/4.0 # revs
throttle = struct.unpack(">B", get_current_data_for_pid(17)[2:])[0]/255.0 * 100 # percent
rpm = struct.unpack(">H", get_current_data_for_pid(12)[2:])[0] / 4.0 # revs
throttle = struct.unpack(">B", get_current_data_for_pid(17)[2:])[0] / 255.0 * 100 # percent
temp = struct.unpack(">B", get_current_data_for_pid(5)[2:])[0] - 40 # degrees C
load = struct.unpack(">B", get_current_data_for_pid(4)[2:])[0]/255.0 * 100 # percent
load = struct.unpack(">B", get_current_data_for_pid(4)[2:])[0] / 255.0 * 100 # percent
print("%d KPH, %d RPM, %.1f%% Throttle, %d deg C, %.1f%% load" % (speed, rpm, throttle, temp, load))
time.sleep(0.2)



4 changes: 3 additions & 1 deletion examples/tesla_tester.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env python3
# flake8: noqa

import sys
import binascii
from panda import Panda
Expand Down Expand Up @@ -45,7 +47,7 @@ def tesla_tester():
while True:
#Read the VIN
can_recv = p.can_recv()
for address, _, dat, src in can_recv:
for address, _, dat, src in can_recv:
if src == body_bus_num:
if address == 1384: # 0x568 is VIN
vin_index = int(binascii.hexlify(dat)[:2]) # first byte is the index, 00, 01, 02
Expand Down
Loading

0 comments on commit d7f7b14

Please sign in to comment.