Skip to content

Commit

Permalink
Refactor to snake-case
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph2 committed Oct 20, 2019
1 parent 6668cad commit 5424d5a
Show file tree
Hide file tree
Showing 37 changed files with 482 additions and 486 deletions.
2 changes: 1 addition & 1 deletion objutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
__version__ = "0.1.0"

__copyright__ = """
pyObjUtils - Object file library for Python.
objutils - Object file library for Python.
(C) 2010-2019 by Christoph Schueler <cpu12.gems@googlemail.com>
Expand Down
2 changes: 1 addition & 1 deletion objutils/armabi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
__version__ = "0.1.0"

__copyright__ = """
pyObjUtils - Object file library for Python.
objutils - Object file library for Python.
(C) 2010-2016 by Christoph Schueler <cpu12.gems@googlemail.com>
Expand Down
21 changes: 10 additions & 11 deletions objutils/ash.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
__version__ = "0.1.0"

__copyright__ = """
pyObjUtils - Object file library for Python.
objutils - Object file library for Python.
(C) 2010-2019 by Christoph Schueler <cpu12.gems@googlemail.com>
Expand Down Expand Up @@ -52,29 +52,28 @@ class Reader(hexfile.ASCIIHexReader):
"""
VALID_CHARS = re.compile(r"^[a-fA-F0-9 %,\'\$\x02\x03\n\r]*$")

def __init__(self, addressPattern = r'^(?:(?P<stx>[\x02])\s+)?\$A(?P<address>[0-9a-zA-Z]{2,8})[,.]\s*$',
dataPattern = r'^(?:[0-9a-zA-Z]{{2,4}}[{0}]?)*\s*$', etxPattern = r'^q.*$'):
super(Reader, self).__init__(addressPattern, dataPattern, etxPattern, separators = ", %'")
def __init__(self, address_pattern = r'^(?:(?P<stx>[\x02])\s+)?\$A(?P<address>[0-9a-zA-Z]{2,8})[,.]\s*$',
data_pattern = r'^(?:[0-9a-zA-Z]{{2,4}}[{0}]?)*\s*$', etx_pattern = r'^q.*$'):
super(Reader, self).__init__(address_pattern, data_pattern, etx_pattern, separators = ", %'")


class Writer(hexfile.ASCIIHexWriter):

MAX_ADDRESS_BITS = 16
ADDRESS_DESIGNATOR = '$A'

def __init__(self, addressDesignator = '$A'):
super(Writer, self).__init__(addressDesignator)
def __init__(self, address_designator = '$A'):
super(Writer, self).__init__(address_designator)

def composeHeader(self, meta):
def compose_header(self, meta):
self.checksum = 0
self.previousAddress = None
self.previous_address = None
line ="{0} ".format(STX)
return line

def composeFooter(self, meta):
def compose_footer(self, meta):
line = "{0}$${1:04X},".format(ETX, self.checksum % 65536)
return line

def rowCallout(self, address, length, row):
def row_callout(self, address, length, row):
self.checksum += checksum(row)

54 changes: 27 additions & 27 deletions objutils/binfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
__version__ = "0.1.1"

__copyright__ = """
pyObjUtils - Object file library for Python.
objutils - Object file library for Python.
(C) 2010-2019 by Christoph Schueler <cpu12.gems@googlemail.com>
Expand All @@ -34,7 +34,7 @@

from objutils.section import Section
from objutils.image import Image, Builder
from objutils.utils import slicer, createStringBuffer, PYTHON_VERSION
from objutils.utils import slicer, create_string_buffer, PYTHON_VERSION
from objutils.logger import Logger

##
Expand All @@ -57,11 +57,11 @@ def load(self, fp, address = 0x0000):
def loads(self, image, address = 0x0000):
if PYTHON_VERSION.major == 3:
if isinstance(image, str):
return self.load(createStringBuffer(bytes(image, "ascii")), address)
return self.load(create_string_buffer(bytes(image, "ascii")), address)
else:
return self.load(createStringBuffer(image), address)
return self.load(create_string_buffer(image), address)
else:
return self.load(createStringBuffer(image), address)
return self.load(create_string_buffer(image), address)


class Writer(object):
Expand All @@ -78,23 +78,23 @@ def dumps(self, image, filler = b'\xff', **kws):
elif isinstance(filler, int) and filler > 255:
raise ValueError("filler must be in range 0..255")
result = bytearray()
previousAddress = None
previousLength = None
previous_address = None
previous_length = None

if isinstance(image, Builder):
image = image.image # Be tolerant.

if hasattr(image, "sections") and not image.sections:
return b''
sections = sorted(image.sections, key = lambda x: x.startAddress)
sections = sorted(image.sections, key = lambda x: x.start_address)
for section in sections:
if not previousAddress is None:
gap = section.startAddress - (previousAddress + previousLength)
if not previous_address is None:
gap = section.start_address - (previous_address + previous_length)
if gap > 0:
result.extend(filler * gap)
result.extend(section.data)
previousAddress = section.startAddress
previousLength = section.length
previous_address = section.start_address
previous_length = section.length
return result

#
Expand All @@ -117,22 +117,22 @@ def dumps(self, image, **kws):

if hasattr(image, "sections") and not image.sections:
return b''
sections = sorted(image.sections, key = lambda x: x.startAddress)
manifestBuffer = io.StringIO()
outBuffer = io.BytesIO()
#outBuffer = io.StringIO()
print("BUF", outBuffer)
with closing(zipfile.ZipFile(outBuffer, mode = "w")) as outFile:
sections = sorted(image.sections, key = lambda x: x.start_address)
manifest_buffer = io.StringIO()
out_buffer = io.BytesIO()
#out_buffer = io.StringIO()
print("BUF", out_buffer)
with closing(zipfile.ZipFile(out_buffer, mode = "w")) as outFile:
print(outFile)
for idx, section in enumerate(sections):
print(section.startAddress, section.length)
print(section.start_address, section.length)
#print("FN", BinZipWriter.SECTION_FILE_NAME.format(idx))
manifestBuffer.write(BinZipWriter.SECTION_FILE_NAME.format(idx))
manifestBuffer.write("\t")
manifestBuffer.write(str(section.startAddress))
manifestBuffer.write("\t")
manifestBuffer.write(str(section.length))
manifestBuffer.write("\n")
manifestBuffer.seek(0)
print(manifestBuffer.read())
manifest_buffer.write(BinZipWriter.SECTION_FILE_NAME.format(idx))
manifest_buffer.write("\t")
manifest_buffer.write(str(section.start_address))
manifest_buffer.write("\t")
manifest_buffer.write(str(section.length))
manifest_buffer.write("\n")
manifest_buffer.seek(0)
print(manifest_buffer.read())
return ''
6 changes: 3 additions & 3 deletions objutils/checksums.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
__version__ = "0.1.0"

__copyright__ = """
pyObjUtils - Object file library for Python.
objutils - Object file library for Python.
(C) 2010-2016 by Christoph Schueler <cpu12.gems@googlemail.com>
(C) 2010-2019 by Christoph Schueler <cpu12.gems@googlemail.com>
All Rights Reserved
Expand Down Expand Up @@ -83,7 +83,7 @@ def rotatedXOR(values, width, rotator):
cs = rotator(cs)
return cs % (2 ** width)

def nibbleSum(data):
def nibble_sum(data):
result = 0
for d in data:
hn = (d & 0xf0) >> 4
Expand Down
31 changes: 15 additions & 16 deletions objutils/cosmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
__version__ = "0.1.0"

__copyright__ = """
pyObjUtils - Object file library for Python.
objutils - Object file library for Python.
(C) 2010-2016 by Christoph Schueler <cpu12.gems@googlemail.com>
(C) 2010-2019 by Christoph Schueler <cpu12.gems@googlemail.com>
All Rights Reserved
Expand Down Expand Up @@ -43,34 +43,33 @@ class Reader(hexfile.Reader):
(DATA2, "AAAA DD"),
(DATA3, "DD"),
)
previousAddress = 0
previousLength = 0
previous_address = 0
previous_length = 0

def checkLine(self, line, formatType):
def check_line(self, line, format_type):
return True

def isDataLine(self, line, formatType):
if formatType == DATA3:
def is_data_line(self, line, format_type):
if format_type == DATA3:
if line.junk in ("!M", "?M"): # Startsymbol, address ommited.
return False
line.address = self.previousAddress + self.previousLength
self.previousAddress = line.address
self.previousLength = len(line.chunk)
line.address = self.previous_address + self.previous_length
self.previous_address = line.address
self.previous_length = len(line.chunk)
else:
if hasattr(line, 'chunk'):
length = len(line.chunk)
else:
length = 0
self.previousAddress = line.address
self.previousLength = length
return formatType in (DATA0, DATA1, DATA2, DATA3)
self.previous_address = line.address
self.previous_length = length
return format_type in (DATA0, DATA1, DATA2, DATA3)


class Writer(hexfile.Writer):

MAX_ADDRESS_BITS = 16

def composeRow(self, address, length, row):
line = "!M{0:04X} {1}".format(address, Writer.hexBytes(row))
def compose_row(self, address, length, row):
line = "!M{0:04X} {1}".format(address, Writer.hex_bytes(row))
return line

31 changes: 20 additions & 11 deletions objutils/emon52.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
__version__ = "0.1.0"

__copyright__ = """
pyObjUtils - Object file library for Python.
objutils - Object file library for Python.
(C) 2010-2016 by Christoph Schueler <cpu12.gems@googlemail.com>
(C) 2010-2019 by Christoph Schueler <cpu12.gems@googlemail.com>
All Rights Reserved
Expand Down Expand Up @@ -36,42 +36,51 @@


class Codec(object):
def __init__(self, fileLike):
self.fileLike = fileLike
"""
"""

def __init__(self, file_like):
self.file_like = file_like

def readlines(self):
for line in self.fileLike.readlines():
for line in self.file_like.readlines():
yield line

def writelines(self, lines):
for line in lines:
self.fileLike.write(line)
self.file_like.write(line)


class Reader(hexfile.Reader):
"""
"""

FORMAT_SPEC = (
(hexfile.TYPE_FROM_RECORD, "LL AAAA:DD CCCC"),
)
DATA_SEPARATOR = " "

def checkLine(self, line, formatType):
def check_line(self, line, format_type):
if line.length != len(line.chunk):
raise hexfile.InvalidRecordLengthError("Byte count doesn't match length of actual data.")
# todo: factor out checksum calculation from line!!!
checksum = (sum(line.chunk) & 0xffff)
if line.checksum != checksum:
raise hexfile.InvalidRecordChecksumError()

def isDataLine(self, line, formatType):
def is_data_line(self, line, format_type):
return True


class Writer(hexfile.Writer):
"""
"""

MAX_ADDRESS_BITS = 16

def composeRow(self, address, length, row):
def compose_row(self, address, length, row):
checksum = sum(row) % 65536
return "{0:02X} {1:04X}:{2!s} {3:04X}".format(length, address, Writer.hexBytes(row, spaced = True), checksum)

return "{0:02X} {1:04X}:{2!s} {3:04X}".format(length, address, Writer.hex_bytes(row, spaced = True), checksum)
29 changes: 14 additions & 15 deletions objutils/etek.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
__version__ = "0.1.0"

__copyright__ = """
pyObjUtils - Object file library for Python.
objutils - Object file library for Python.
(C) 2010-2016 by Christoph Schueler <cpu12.gems@googlemail.com>
(C) 2010-2019 by Christoph Schueler <cpu12.gems@googlemail.com>
All Rights Reserved
Expand Down Expand Up @@ -47,35 +47,34 @@ class Reader(hexfile.Reader):
(EOF, "%LL8CCAAAAADD"),
)

def checkLine(self, line, formatType):
if formatType == DATA:
def check_line(self, line, format_type):
if format_type == DATA:
line.length = (line.length / 2) - 5
checksum = checksums.nibbleSum(utils.makeList(utils.intToArray(line.address), 6, ((line.length + 5) * 2), line.chunk))
checksum = checksums.nibble_sum(utils.make_list(utils.int_to_array(line.address), 6, ((line.length + 5) * 2), line.chunk))
if line.length != len(line.chunk):
raise hexfile.InvalidRecordLengthError("Byte count doesn't match length of actual data.")
if line.checksum!=checksum:
raise hexfile.InvalidRecordChecksumError()
elif formatType == SYMBOL:
checksum = checksums.nibbleSum(utils.makeList(3, ((line.length + 5) * 2), [ord(b) for b in line.chunk]))
elif format_type == SYMBOL:
checksum = checksums.nibble_sum(utils.make_list(3, ((line.length + 5) * 2), [ord(b) for b in line.chunk]))
chunk = line.chunk.strip()
address = int(chunk[-4 : ], 16)
line.address = address
#if line.checksum!=checksum:
# raise hexfile.InvalidRecordChecksumError()

def isDataLine(self, line, formatType):
return formatType == DATA
def is_data_line(self, line, format_type):
return format_type == DATA

def parseData(self, line, formatType):
return formatType != SYMBOL
def parseData(self, line, format_type):
return format_type != SYMBOL

class Writer(hexfile.Writer):

MAX_ADDRESS_BITS = 24

def composeRow(self, address, length, row):
checksum = checksums.nibbleSum(utils.makeList(utils.intToArray(address), 6, ((length + 5) * 2), row))
def compose_row(self, address, length, row):
checksum = checksums.nibble_sum(utils.make_list(utils.int_to_array(address), 6, ((length + 5) * 2), row))

line = "%{0:02X}6{1:02X}{2:04X}{3!s}".format((length + 5) * 2, checksum, address, Writer.hexBytes(row) )
line = "%{0:02X}6{1:02X}{2:04X}{3!s}".format((length + 5) * 2, checksum, address, Writer.hex_bytes(row) )
return line

5 changes: 2 additions & 3 deletions objutils/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
__version__ = "0.1.0"

__copyright__ = """
pyObjUtils - Object file library for Python.
objutils - Object file library for Python.
(C) 2010-2013 by Christoph Schueler <cpu12.gems@googlemail.com>
(C) 2010-2019 by Christoph Schueler <cpu12.gems@googlemail.com>
All Rights Reserved
Expand All @@ -28,4 +28,3 @@
class FileCorruptedError(Exception): pass

class PrematureEndOfFileError(Exception): pass

0 comments on commit 5424d5a

Please sign in to comment.