Skip to content

Commit

Permalink
Add description metadata to python class.
Browse files Browse the repository at this point in the history
  • Loading branch information
agoessling committed Aug 22, 2023
1 parent 550e34a commit 36786cd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/py_stuff_sack.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +186,27 @@ def get_globals(library, message_spec):
for t in all_types:
if isinstance(t, stuff_sack.Enum):
values = {v.name: v.value for v in t.values}
e = _EnumType(t.name, (
EnumMixin,
ENUM_MAP[t.bytes],
), {
value_descr = [v.description for v in t.values]
attrs = {
'_values_': values,
'__metaclass__': _EnumType
})
'__metaclass__': _EnumType,
'description': t.description,
'value_descriptions':value_descr,
}
e = _EnumType(t.name, (EnumMixin, ENUM_MAP[t.bytes]), attrs)
global_vars[t.name] = e
ctypes_map[t.name] = global_vars[t.name]

if isinstance(t, stuff_sack.Bitfield):
fields = [(f.name, BITFIELD_MAP[t.bytes], f.bits) for f in t.fields]
global_vars[t.name] = type(t.name, (ctypes.Structure,), {'__slots__': [], '_fields_': fields})
field_descr = [f.description for f in t.fields]
attrs = {
'__slots__': [],
'_fields_': fields,
'description': t.description,
'field_descriptions': field_descr,
}
global_vars[t.name] = type(t.name, (ctypes.Structure,), attrs)
ctypes_map[t.name] = global_vars[t.name]

if isinstance(t, stuff_sack.Struct):
Expand All @@ -211,7 +219,13 @@ def get_globals(library, message_spec):

fields.append((field.name, field_type))

attrs = {'__slots__': [], '_fields_': fields}
field_descr = [f.description for f in t.fields]
attrs = {
'__slots__': [],
'_fields_': fields,
'description': t.description,
'field_descriptions': field_descr,
}

base = ctypes.Structure
if isinstance(t, stuff_sack.Message):
Expand Down
22 changes: 22 additions & 0 deletions test/test_py_stuff_sack.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ def test_pack_unpack(self):
self.assertEqual(msg.bitfield.field1, unpacked.bitfield.field1)
self.assertEqual(msg.bitfield.field2, unpacked.bitfield.field2)

def test_description(self):
self.assertEqual('This is a bitfield description.', msg_def.Bitfield4Bytes.description)

def test_field_description(self):
self.assertEqual('This is a bitfield field description.',
msg_def.Bitfield4Bytes.field_descriptions[0])


class TestEnum(unittest.TestCase):

Expand Down Expand Up @@ -123,6 +130,12 @@ def test_enum_behavior(self):
s.enumeration = msg_def.Enum1Bytes.Value10
self.assertEqual('Value10', s.enumeration.name)

def test_description(self):
self.assertEqual('This is an enum description.', msg_def.Enum2Bytes.description)

def test_value_description(self):
self.assertEqual('This is a enum value description', msg_def.Enum2Bytes.value_descriptions[0])


class TestPrimitive(unittest.TestCase):

Expand Down Expand Up @@ -243,6 +256,15 @@ def test_pack_unpack(self):
self.assertEqual(s.field0, unpacked.array_3d[i][j][k].field0)
self.assertEqual(s.field1, unpacked.array_3d[i][j][k].field1)

def test_description(self):
self.assertEqual('This is a struct description.', msg_def.ArrayElem.description)

def test_field_description(self):
self.assertEqual('This is a struct field description.', msg_def.ArrayElem.field_descriptions[0])

def test_message_description(self):
self.assertEqual('This is a message description.', msg_def.ArrayTest.description)


class TestAlias(unittest.TestCase):

Expand Down

0 comments on commit 36786cd

Please sign in to comment.