Skip to content

Commit

Permalink
working on ArrayField and PacketField
Browse files Browse the repository at this point in the history
  • Loading branch information
KronoSKoderS committed Nov 11, 2017
1 parent 11cd6a1 commit 3d16cfc
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 19 deletions.
59 changes: 49 additions & 10 deletions calpack/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,27 @@ def __init__(self, default_val=None):
def __eq__(self, other):
if isinstance(other, Field):
return self._val == other._val

return self._val == other

def __ne__(self, other):
if isinstance(other, Field):
return self._val != other._val
return self._val != other

def __lt__(self, other):
if isinstance(other, Field):
return self._val < other._val
return self._val < other

def __gt__(self, other):
return other<self

def __ge__(self, other):
return not self<other

def __le__(self, other):
return not other<self

def __get__(self, ins, own):
if ins is not None:
self._val = getattr(ins._c_pkt, self._field_name, self.default_val)
Expand Down Expand Up @@ -213,6 +231,11 @@ def __init__(self, bit_len=16, little_endian=False, signed=False, default_val=0)
else:
self._c_type = ctypes.c_uint64

def __set__(self, ins, val):
if not self.signed and val < 0:
raise TypeError("Signed valued cannot be set for an unsiged IntField!")
return super(IntField, self).__set__(ins, val)

def create_field_tuple(self, name):
return (name, self._c_type, self.bit_len)

Expand All @@ -229,17 +252,14 @@ def __init__(self, packet_cls, default_val=None):
def bit_len(self):
return self.packet_cls.bit_len

def __eq__(self, other):
pass
def create_field_tuple(self, name):
return (name, self.packet_cls._c_struct)

def __get__(self, ins, own):
pass

def __set__(self, ins, val):
pass
if ins is not None:
self._val = self.packet_cls.pkt_from_c_struct(ins, self._field_name)

def create_field_tuple(self, name):
return (name, self._packet_cls._c_struct)
return self


class ArrayField(Field):
Expand All @@ -261,6 +281,9 @@ def __eq__(self, other):
return self._val[:] == other

def __set__(self, ins, val):
if not isinstance(val, ArrayField) and not isinstance(val, list):
raise TypeError("Must be of type ArrayField or list")

temp = getattr(ins._c_pkt, self._field_name)
temp[:] = val
setattr(ins._c_pkt, self._field_name, temp)
Expand Down Expand Up @@ -352,7 +375,23 @@ class Header(models.Packet):
"""
word_size = typed_property('word_size', int, 16)

def __init__(self, **kwargs):
_c_struct = None
_fields_order = None


@classmethod
def pkt_from_c_struct(cls, ins, field_name):
c_struct = getattr(ins._c_struct, field_name)
fields_order = [field[0][1:] for field in ins._c_pkt._fields_]
return cls(c_struct, fields_order)

def __init__(self, c_struct = None, fields_order = None, **kwargs):
if c_struct is not None:
self._c_struct = c_struct

if fields_order is not None:
self._fields_order = fields_order

# create an internal c structure instance for us to interface with.
self._c_pkt = self._c_struct()

Expand Down
15 changes: 6 additions & 9 deletions tests/BasicPackets_Test.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def test_field_raises_TypeError_when_setting_signed_to_nonsigned_int_value(self)
This test verifies that a "TypeErorr" is raised when setting a non-signed value to a
signed value.
"""
p = self.two_int_field_packet()

with self.assertRaises(TypeError):
p.int_field = -123

Expand Down Expand Up @@ -126,11 +128,6 @@ def test_to_bytes_string_then_import_from_binary(self):
self.assertEquals(p.int_field, p2.int_field)
self.assertEquals(p.int_field_signed, p2.int_field_signed)

def test_int_field_raises_KeyError_when_using_invalid_key_words(self):
with self.assertRaises(KeyError):
class invalid_pkt(models.Packet):
inv_field = models.IntField(keyword_that_dont_exist=100)

def test_int_field_with_variable_bit_lenth(self):
class int_packet_with_varied_sized_int_fields(models.Packet):
int_field = models.IntField()
Expand All @@ -145,9 +142,9 @@ class int_packet_with_varied_sized_int_fields(models.Packet):
pkt.int_field_12_bits = 1023

if PY3:
b_str = b'd\x00\x9c\xff\xff?'
b_str = b'd\x00\x9c\xff\x0f\x00\xff\x03'
else:
b_str = b'd\x00\xcf\xf9\xff?'
b_str = b'd\x00\x0f\x00\x9c\xff\xff\x03'

self.assertEquals(b_str, pkt.to_bytes())

Expand Down Expand Up @@ -199,7 +196,7 @@ class multi_int_field_packet(models.Packet):
def test_set_invalid_type_multi_field(self):

class multi_int_field_packet(models.Packet):
list_int_field = models.ArrayField(models.IntField, 10)
list_int_field = models.ArrayField(models.IntField(), 10)

expected_vals = list(range(10))
p = multi_int_field_packet()
Expand Down Expand Up @@ -254,7 +251,7 @@ class simple_pkt(models.Packet):
field1 = models.IntField()

class adv_pkt(models.Packet):
field2 = simple_pkt
field2 = models.PacketField(simple_pkt)

p = adv_pkt()

Expand Down

0 comments on commit 3d16cfc

Please sign in to comment.