style: format python files with isort and double-quote-string-fixer

This commit is contained in:
Fu Hanxi
2021-01-26 10:49:01 +08:00
parent dc8402ea61
commit 0146f258d7
276 changed files with 8241 additions and 8162 deletions

View File

@@ -6,8 +6,8 @@ class Section(object):
"""
One Section of section table. contains info about section name, address and raw data
"""
SECTION_START_PATTERN = re.compile(b"Contents of section (.+?):")
DATA_PATTERN = re.compile(b"([0-9a-f]{4,8})")
SECTION_START_PATTERN = re.compile(b'Contents of section (.+?):')
DATA_PATTERN = re.compile(b'([0-9a-f]{4,8})')
def __init__(self, name, start_address, data):
self.name = name
@@ -16,8 +16,8 @@ class Section(object):
def __contains__(self, item):
""" check if the section name and address match this section """
if (item["section"] == self.name or item["section"] == "any") \
and (self.start_address <= item["address"] < (self.start_address + len(self.data))):
if (item['section'] == self.name or item['section'] == 'any') \
and (self.start_address <= item['address'] < (self.start_address + len(self.data))):
return True
else:
return False
@@ -36,7 +36,7 @@ class Section(object):
return self.data[item]
def __str__(self):
return "%s [%08x - %08x]" % (self.name, self.start_address, self.start_address + len(self.data))
return '%s [%08x - %08x]' % (self.name, self.start_address, self.start_address + len(self.data))
__repr__ = __str__
@@ -47,12 +47,12 @@ class Section(object):
:param raw_data: lines of raw data generated by `objdump -s`
:return: one section, un-processed lines
"""
name = ""
data = ""
name = ''
data = ''
start_address = 0
# first find start line
for i, line in enumerate(raw_data):
if b"Contents of section " in line: # do strcmp first to speed up
if b'Contents of section ' in line: # do strcmp first to speed up
match = cls.SECTION_START_PATTERN.search(line)
if match is not None:
name = match.group(1)
@@ -60,11 +60,11 @@ class Section(object):
break
else:
# do some error handling
raw_data = [b""] # add a dummy first data line
raw_data = [b''] # add a dummy first data line
def process_data_line(line_to_process):
# first remove the ascii part
hex_part = line_to_process.split(b" ")[0]
hex_part = line_to_process.split(b' ')[0]
# process rest part
data_list = cls.DATA_PATTERN.findall(hex_part)
try:
@@ -74,12 +74,12 @@ class Section(object):
def hex_to_str(hex_data):
if len(hex_data) % 2 == 1:
hex_data = b"0" + hex_data # append zero at the beginning
hex_data = b'0' + hex_data # append zero at the beginning
_length = len(hex_data)
return "".join([chr(int(hex_data[_i:_i + 2], base=16))
return ''.join([chr(int(hex_data[_i:_i + 2], base=16))
for _i in range(0, _length, 2)])
return _address, "".join([hex_to_str(x) for x in data_list[1:]])
return _address, ''.join([hex_to_str(x) for x in data_list[1:]])
# handle first line:
address, _data = process_data_line(raw_data[0])
@@ -107,14 +107,14 @@ class SectionTable(object):
""" elf section table """
def __init__(self, file_name):
with open(file_name, "rb") as f:
with open(file_name, 'rb') as f:
raw_data = f.readlines()
self.table = []
while raw_data:
section, raw_data = Section.parse_raw_data(raw_data)
self.table.append(section)
def get_unsigned_int(self, section, address, size=4, endian="LE"):
def get_unsigned_int(self, section, address, size=4, endian='LE'):
"""
get unsigned int from section table
:param section: section name; use "any" will only match with address
@@ -124,19 +124,19 @@ class SectionTable(object):
:return: int or None
"""
if address % 4 != 0 or size % 4 != 0:
print("warning: try to access without 4 bytes aligned")
key = {"address": address, "section": section}
print('warning: try to access without 4 bytes aligned')
key = {'address': address, 'section': section}
for section in self.table:
if key in section:
tmp = section[address:address + size]
value = 0
for i in range(size):
if endian == "LE":
if endian == 'LE':
value += ord(tmp[i]) << (i * 8)
elif endian == "BE":
elif endian == 'BE':
value += ord(tmp[i]) << ((size - i - 1) * 8)
else:
print("only support LE or BE for parameter endian")
print('only support LE or BE for parameter endian')
assert False
break
else:
@@ -151,7 +151,7 @@ class SectionTable(object):
:return: string or None
"""
value = None
key = {"address": address, "section": section}
key = {'address': address, 'section': section}
for section in self.table:
if key in section:
value = section[address:]