Add in write support, and change symbols to be both structures and constants.

This commit is contained in:
Mike Auty
2013-12-28 05:30:54 +00:00
parent cea4969b49
commit 26f8d0f7cc
10 changed files with 179 additions and 88 deletions
+50
View File
@@ -65,27 +65,65 @@ class Integer(PrimitiveObject, int):
def __new__(cls, context, layer_name, offset, symbol_name, struct_format, **kwargs):
return int.__new__(cls, cls._struct_value(struct_format, context, layer_name, offset, symbol_name))
def write(self, value):
"""Writes the object into the layer of the context at the current offset"""
if isinstance(value, int):
data = struct.pack(self._struct_format, value)
return self._context.memory.write(self._layer_name, self._offset, data)
raise TypeError("Integer objects require an integer to be written")
class Float(PrimitiveObject, float):
"""Primitive Object that handles double or floating point numbers"""
def __new__(cls, context, layer_name, offset, symbol_name, struct_format, **kwargs):
return float.__new__(cls, cls._struct_value(struct_format, context, layer_name, offset, symbol_name))
def write(self, value):
"""Writes the object into the layer of the context at the current offset"""
if isinstance(value, float):
data = struct.pack(self._struct_format, value)
return self._context.memory.write(self._layer_name, self._offset, data)
raise TypeError("Float objects require a float to be written")
class Bytes(PrimitiveObject, bytes):
"""Primitive Object that handles specific series of bytes"""
def __init__(self, context, layer_name, offset, symbol_name, size = None, parent = None, length = 1, **kwargs):
bytes.__init__()
PrimitiveObject.__init__(self, context, layer_name, offset, symbol_name, size, parent, struct_format = str(length) + 's')
self.length = length
def __new__(cls, context, layer_name, offset, symbol_name, length = 1, **kwargs):
return bytes.__new__(cls, cls._struct_value(str(length) + "s", context, layer_name, offset, symbol_name))
def write(self, value):
"""Writes the object into the layer of the context at the current offset"""
if isinstance(value, bytes):
data = struct.pack(self._struct_format, value)
return self._context.memory.write(self._layer_name, self._offset, data)
raise TypeError("Bytes objects require a bytes type to be written")
class String(PrimitiveObject, str):
"""Primitive Object that handles string values
length: specifies the maximum possible length that the string could hold in memory
"""
def __init__(self, context, layer_name, offset, symbol_name, size = None, parent = None, length = 1, **kwargs):
str.__init__()
PrimitiveObject.__init__(self, context, layer_name, offset, symbol_name, size, parent, struct_format = str(length) + 's')
self.length = length
def __new__(cls, context, layer_name, offset, symbol_name, length = 1, **kwargs):
return str.__new__(cls, cls._struct_value(str(length) + "s", context, layer_name, offset, symbol_name))
def write(self, value):
"""Writes the object into the layer of the context at the current offset"""
if isinstance(value, str):
data = struct.pack(self._struct_format, value)
return self._context.memory.write(self._layer_name, self._offset, data)
raise TypeError("String objects require a string to be written")
class Pointer(Integer):
"""Pointer which points to another object"""
def __init__(self, context, layer_name, offset, symbol_name, size = None, parent = None, struct_format = None, target = None):
@@ -147,6 +185,9 @@ class BitField(PrimitiveObject, int):
return [arguments['target']]
return []
def write(self, value):
raise NotImplementedError("Writing to BitFields is not yet implemented")
class Enumeration(interfaces.objects.ObjectInterface):
"""Returns an object made up of choices"""
# FIXME: Add in body for the enumeration object
@@ -154,6 +195,9 @@ class Enumeration(interfaces.objects.ObjectInterface):
def template_children(cls, arguments):
return []
def write(self, value):
raise NotImplementedError("Writing to Enumerations is not yet implemented")
class Array(interfaces.objects.ObjectInterface, collections.Sequence):
"""Object which can contain a fixed number of an object type"""
def __init__(self, context, layer_name, offset, symbol_name, size = None, parent = None, count = 0, target = None):
@@ -198,6 +242,9 @@ class Array(interfaces.objects.ObjectInterface, collections.Sequence):
"""Returns the length of the array"""
return self._count
def write(self, value):
raise NotImplementedError("Writing to Arrays is not yet implemented")
class Struct(interfaces.objects.ObjectInterface):
"""Object which can contain members that are other objects"""
@@ -253,3 +300,6 @@ class Struct(interfaces.objects.ObjectInterface):
self._concrete_members[attr] = member
return member
raise AttributeError("'" + self._symbol_name + "' Struct has no attribute '" + attr + "'")
def write(self, value):
raise TypeError("Structs cannot be written to directly, invidivual members must be written instead")
+1 -1
View File
@@ -56,5 +56,5 @@ class ReferenceTemplate(interfaces.objects.Template):
It should not return any attributes
"""
def __call__(self, context, *args, **kwargs):
template = context.symbol_space.resolve(self._symbol_name)
template = context.symbol_space.get_structure(self._symbol_name)
return template(context = context, *args, **kwargs)