Skip to content

Commit

Permalink
Fix Image() constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph2 committed Oct 21, 2019
1 parent d646268 commit d2c5f25
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
40 changes: 36 additions & 4 deletions objutils/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class InvalidAddressError(Exception):
"""Raised if address information is out of range.
"""

#
# TODO: Use crypto hashes (comparison, optimized storage, ...)
#

## Adress-space constants.
AS_16 = 0
AS_24 = 1
Expand All @@ -63,7 +67,14 @@ class Image(object):
def __init__(self, sections = None, meta = None, valid = False):
if meta is None:
meta = {}
self.sections = sections if sections else []
if not sections:
self.sections = []
elif isinstance(sections, Section):
self.sections = tuple(sections)
elif hasattr(sections, "__iter__"):
self.sections = sections
else:
raise TypeError("Argument section is of wrong type '{}'".format(sections))
_validate_sections(self.sections)
self.meta = meta
self.valid = valid
Expand All @@ -74,6 +85,8 @@ def __repr__(self):
result.append(repr(segment))
return '\n'.join(result)

__str__ = __repr__

def __len__(self):
return len(self.sections)

Expand Down Expand Up @@ -187,10 +200,23 @@ def split(self, at = None, equal_parts = None, remap = None):


class Builder(object):
"""Construct and :class:`Image` object.
"""Construct an :class:`Image` object.
Parameters
----------
sections: iteratable (typically list or tuple)
auto_join: bool
Automatically join adjacent sections.
auto_sort: bool
Sort sections by startaddress (ascending).
"""

def __init__(self, sections = None, auto_join = False, auto_sort = False):
def __init__(self, sections = None, auto_join = True, auto_sort = False):
#print("\nBuilder c-tor: sections-type: {} image: '{}'\n".format(type(sections), Image(sections)))
self._image = Image(sections)

if auto_sort:
self._need_sorting = True
if sections:
Expand All @@ -206,7 +232,7 @@ def __init__(self, sections = None, auto_join = False, auto_sort = False):
self.address = 0
self.auto_join = auto_join

def add_section(self, data, address = None, dont_join = False): # TODO: 'polymorphic' signature, move 'dontJoin' to segment!
def add_section(self, data, address = None, dont_join = False):
address = address if address else self.address # If Address omitted, create continuous address space.
if isinstance(data, str):
data = [ord(x) for x in data] # array.array('B',data)
Expand All @@ -230,6 +256,12 @@ def hexdump(self, fp = sys.stdout):
def image(self):
return Image(self._sections)

def __str__(self):
return str(self.image)

def __repr__(self):
return str(self.image)


def _validate_sections(sections):
"""Test for required protocol
Expand Down
23 changes: 23 additions & 0 deletions objutils/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,29 @@ def tearDown(self):
del self.b0
del self.b1


class TestImageConstructors(unittest.TestCase):

def testEmpty(self):
img = Image()
self.assertEqual(img.sections, [])

def testNone(self):
img = Image(None)
self.assertEqual(img.sections, [])

def testSingle(self):
sec0 = Section(data = "hello", start_address = 0x100)
img = Image(sec0)
self.assertEqual(len(img.sections), 1)

def testTwo(self):
sec0 = Section(data = "hello", start_address = 0x100)
sec1 = Section(data = "world", start_address = 0x200)
img = Image((sec0, sec1))
self.assertEqual(len(img.sections), 2)


class Equality(BaseTest):

def testEqualImagesShallCompareEqualCase1(self):
Expand Down

0 comments on commit d2c5f25

Please sign in to comment.