Skip to content

Commit

Permalink
Integration for Release 2018.5.0 (#87) (#88)
Browse files Browse the repository at this point in the history
* updated travis-ci to only push to pypi from the python 3 build

* python version needs to be in quotations.

* fixing tags deployment

* updated test to properly grab size

* Ignoring pytest caches files.

* Added ability to get the `len` of a Packet.

* added test to verify proper inheritance of Packet class.

* working on updating the inheritance of Packet class

* fixed issue with inheritance.

* Inheritance (#86)

* added test to verify proper inheritance of Packet class.

* working on updating the inheritance of Packet class

* fixed issue with inheritance.

* fixed FlagField documentation.

* Updating to a new release version of <year>.<month>.<minor>
  • Loading branch information
Kronos committed May 9, 2018
1 parent e810c54 commit 0179494
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,5 @@ calpack.egg-info/
*.pyproj
*.pyperf
.vscode/*

\.pytest_cache\*
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ deploy:
distributions: sdist bdist_wheel
repo: KronoSKoderS/CalPack
condition: TRAVIS_EVENT_TYPE != 'cron'
python: "3.6"

- provider: releases
skip_cleanup: true
Expand All @@ -51,6 +52,6 @@ deploy:
file: $(build/*.tar.gz)
on:
repo: KronoSKoderS/CalPack
branch: tags
tags: true
condition: TRAVIS_EVENT_TYPE != 'cron'

27 changes: 20 additions & 7 deletions calpack/models/packets.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ def __new__(mcs, clsname, bases, clsdict):
if PY2:
fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter))

# Get all of the attributes of the base classes and see if the Structure is defined
# if so, then update to the base C Structure to this one. We also need to check
# to see if any of the bases are other Packet types. If so, then 'inherit' that
# Packet's fields. WARNING! If inheriting from multiple Packet types, the fields
# are appended in the order of inheritance.
base_dicts = {}
for base in bases:
base_dicts.update(base.__dict__)
if getattr(base, '_IS_PKT_CLASS', False):
fields_tuple += getattr(base._Packet__c_struct, '_fields_', [])
base_order = getattr(base, 'fields_order', [])
order += base_order
for field_name in base_order:
field = getattr(base, field_name)
class_dict[field_name] = field

# for each 'Field' type we're gonna save the order and prep for the c struct
for name, obj in fields:
order.append(name)
Expand All @@ -88,12 +104,6 @@ def __new__(mcs, clsname, bases, clsdict):
# Here we save the order
class_dict['fields_order'] = order

# Get all of the attributes of the base classes and see if the Structure is defined
# if so, then update to the base C Structure to this one.
base_dicts = {}
for base in bases:
base_dicts.update(base.__dict__)

c_struct_type = base_dicts.get('_c_struct_type', ctypes.Structure)

# Here we create the internal structure
Expand Down Expand Up @@ -129,6 +139,7 @@ class Header(models.Packet):
structure. This MUST have the same :code:`_fields_` as the Packet would normally have in
order for it to work properly.
"""
_IS_PKT_CLASS = True
word_size = typed_property('word_size', int, 16)
fields_order = []
bit_len = 0
Expand Down Expand Up @@ -209,7 +220,6 @@ def set_c_field(self, field_name, val):

setattr(self.__c_pkt, field_name, val)


def get_c_field(self, field_name):
"""
gets the value of the field value of the internal c structure.
Expand All @@ -224,6 +234,9 @@ def __repr__(self):
vals_string = ", ".join(["{}={}".format(name, repr(field)) for name, field in field_pairs])
return f_string.format(name=self.__class__.__name__, fields=vals_string)

def __len__(self):
return ctypes.sizeof(self.__c_struct)


class PacketBigEndian(Packet):
"""
Expand Down
2 changes: 1 addition & 1 deletion docs/user/fields_builtin_doc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ The :code:`BoolField` is used to represent a :code:`bool` object in python. Thi
-----------------

The :code:`FlagField` is used to represent a "flag" in the packet. This is a single bit integer and
can only store a value of 1 or 0. This field is similar to :code:`IntField(bit_len=1)`.
can only store a value of 1 or 0. This field is similar to :code:`IntField8(bit_len=1)`.

:code:`PacketField`
-------------------
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup, find_packages


version = "0.1.3"
version = "2018.5.0"

try:
import pypandoc
Expand Down Expand Up @@ -42,5 +42,5 @@
'Topic :: Utilities',
],
test_suite="tests.get_tests",
packages=find_packages(exclude=['tests'])
packages=find_packages(exclude=['tests', 'docs'])
)
51 changes: 50 additions & 1 deletion tests/test_AdvancedPackets.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class c_PrimaryPacket(ctypes.Structure):
c_pkt.bool_field = True

b_str = pkt.to_bytes()
c_b_str = ctypes.string_at(ctypes.addressof(c_pkt), ctypes.sizeof(c_pkt))
c_b_str = ctypes.string_at(ctypes.addressof(c_pkt), ctypes.sizeof(c_PrimaryPacket))

self.assertEqual(b_str, c_b_str)

Expand All @@ -135,3 +135,52 @@ class c_PrimaryPacket(ctypes.Structure):
self.assertAlmostEqual(parsed_pkt.double_field, -3.14, places=5)
self.assertAlmostEqual(parsed_pkt.long_double_field, 123.456, places=5)
self.assertEqual(parsed_pkt.bool_field, True)

def test_advpkt_inheritance(self):
"""
This test verifies the functionality of Class Inheritance for Packets.
See also GitHub Issue #84 (https://github.com/KronoSKoderS/CalPack/issues/84)
"""

class TemplatePacket(models.Packet):
PKT_UID = 0x00

ID = models.IntField8()
TYPE = models.IntField8()
reserved0 = models.IntField16(bit_len=7)
length = models.IntField16(bit_len=9)
reserved1 = models.IntField32(bit_len=4)
utcTimeUpper = models.IntField32(bit_len=28)
utcTimeLower = models.IntField32()
PacketStatus = models.IntField32()
padding = models.IntField32()

class MyPacket(TemplatePacket):
PKT_UID = 0x0E


t_pkt = TemplatePacket()
m_pkt = MyPacket()

# First let's check the original issue of the packet length:
self.assertEqual(len(t_pkt), len(m_pkt))

# Next, let's for sure verify that the fields are there:
self.assertEqual(t_pkt.ID, m_pkt.ID)
self.assertEqual(t_pkt.length, m_pkt.length)

# Next, let's verify that the packets are indeed different:
t_pkt.PacketStatus = 0xbeefcafe
m_pkt.PacketStatus = 0xdeadbeef
self.assertNotEqual(t_pkt.PacketStatus, m_pkt.PacketStatus)

m_pkt.PacketStatus = 0xbeefcafe

# check to make sure the output of the byte data
# is the same
self.assertEqual(t_pkt.to_bytes(), m_pkt.to_bytes())


# Check to make sure the PKT_UID's remain correct
self.assertEqual(t_pkt.PKT_UID, 0x00)
self.assertEqual(m_pkt.PKT_UID, 0x0E)
15 changes: 15 additions & 0 deletions tests/test_BasicPackets.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,21 @@ class simple_pkt(models.Packet):
self.assertNotEqual(p1.int_field, p2.int_field)
self.assertNotEqual(p1.int_field_signed, p2.int_field_signed)

def test_pkt_len(self):
class simple_pkt(models.Packet):
int_field = models.IntField()
int_field_signed = models.IntField(signed=True)

class c_simple_pkt(ctypes.Structure):
_fields_ = (
('int_field', ctypes.c_uint),
('int_field_signed', ctypes.c_int)
)

p1 = simple_pkt()

self.assertEqual(len(p1), ctypes.sizeof(c_simple_pkt))


class Test_EndianPacket(unittest.TestCase):

Expand Down

0 comments on commit 0179494

Please sign in to comment.