Skip to content

Commit

Permalink
more (non-breaking) naming updates
Browse files Browse the repository at this point in the history
  • Loading branch information
robamu committed May 9, 2024
1 parent b0d289c commit e23af8e
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 37 deletions.
31 changes: 17 additions & 14 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,51 @@
from unittest import TestCase

from tmtccmd.util import ObjectIdU32
from tmtccmd.util.obj_id import ObjectIdU8, ObjectIdU16
from tmtccmd.util.obj_id import (
ComponentIdU16,
ComponentIdU32,
ComponentIdU8,
)


class TestObjectId(TestCase):
def test_basic(self):
obj_id0 = ObjectIdU32(1, "Some Name")
obj_id0 = ComponentIdU32(1, "Some Name")
self.assertEqual(str(obj_id0), "Object ID Some Name with ID 0x00000001")
self.assertEqual(
obj_id0.__repr__(), "ObjectIdU32(object_id=1, name='Some Name')"
obj_id0.__repr__(), "ComponentIdU32(object_id=1, name='Some Name')"
)
self.assertEqual(obj_id0.as_bytes, bytes([0x00, 0x00, 0x00, 0x01]))
self.assertEqual(obj_id0.as_hex_string, "0x00000001")
self.assertEqual(int(obj_id0), 1)
obj_id1 = ObjectIdU32(1, "Other Name")
obj_id1 = ComponentIdU32(1, "Other Name")
self.assertEqual(obj_id0, obj_id1)
obj_from_raw = ObjectIdU32.from_bytes(obj_id0.as_bytes)
obj_from_raw = ComponentIdU32.from_bytes(obj_id0.as_bytes)
self.assertEqual(obj_from_raw, obj_id0)
with self.assertRaises(ValueError):
ObjectIdU32.from_bytes(bytes())
ComponentIdU32.from_bytes(bytes())
with self.assertRaises(ValueError):
ObjectIdU32.from_bytes(bytes([0, 1, 2]))
ComponentIdU32.from_bytes(bytes([0, 1, 2]))
with self.assertRaises(ValueError):
obj_id1.obj_id = -1

def test_diff_types(self):
obj_id_u8 = ObjectIdU8(1, "U8 ID 0")
obj_id_u8 = ComponentIdU8(1, "U8 ID 0")
self.assertEqual(obj_id_u8.as_bytes, bytes([1]))
self.assertEqual(obj_id_u8.as_hex_string, "0x01")
self.assertEqual(obj_id_u8.byte_len, 1)
obj_id_u16 = ObjectIdU16(2, "U16 ID 2")
obj_id_u16 = ComponentIdU16(2, "U16 ID 2")
self.assertEqual(obj_id_u16.as_bytes, bytes([0, 2]))
self.assertEqual(obj_id_u16.as_hex_string, "0x0002")
self.assertEqual(obj_id_u16.byte_len, 2)
obj_id_u32 = ObjectIdU32(1, "U32 ID 1")
obj_id_u32 = ComponentIdU32(1, "U32 ID 1")
test_dict = dict()
test_dict.update({obj_id_u8: obj_id_u8.name})
test_dict.update({obj_id_u16: obj_id_u16.name})
test_dict.update({obj_id_u32: obj_id_u32.name})
self.assertEqual(len(test_dict), 3)
obj_id_u8_from_raw = ObjectIdU8.from_bytes(obj_id_u8.as_bytes)
obj_id_u8_from_raw = ComponentIdU8.from_bytes(obj_id_u8.as_bytes)
self.assertEqual(obj_id_u8_from_raw, obj_id_u8)
obj_id_u16_from_raw = ObjectIdU16.from_bytes(obj_id_u16.as_bytes)
obj_id_u16_from_raw = ComponentIdU16.from_bytes(obj_id_u16.as_bytes)
self.assertEqual(obj_id_u16_from_raw, obj_id_u16)
obj_id_u32_from_raw = ObjectIdU32.from_bytes(obj_id_u32.as_bytes)
obj_id_u32_from_raw = ComponentIdU32.from_bytes(obj_id_u32.as_bytes)
self.assertEqual(obj_id_u32_from_raw, obj_id_u32)
14 changes: 13 additions & 1 deletion tmtccmd/util/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
from .obj_id import ObjectIdU32, ObjectIdU16, ObjectIdU8, ObjectIdBase, ObjectIdDictT
from .obj_id import (
ComponentIdU32,
ComponentIdU16,
ComponentIdU8,
ComponentIdBase,
ComponentIdMapping,
ObjectIdU32,
ObjectIdU16,
ObjectIdU8,
ObjectIdBase,
ObjectIdMapping,
ObjectIdDictT,
)
from .retval import RetvalDictT
8 changes: 4 additions & 4 deletions tmtccmd/util/conf_util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import collections.abc
import logging
from typing import Tuple, Union
from typing import Any, Tuple, Union
from contextlib import contextmanager


Expand Down Expand Up @@ -62,11 +62,11 @@ def check_args_in_dict(


def __handle_iterable_non_dict(
param: any,
param: Any,
iterable: collections.abc.Iterable,
might_be_integer: bool,
init_res_tuple: Tuple[bool, any],
) -> (bool, any):
init_res_tuple: Tuple[bool, Any],
) -> Tuple[bool, Any]:
param_list = list()
for idx, enum_value in enumerate(iterable):
if isinstance(enum_value.value, str):
Expand Down
3 changes: 2 additions & 1 deletion tmtccmd/util/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import os
import enum
from typing import Any

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -57,7 +58,7 @@ def check_json_file(json_cfg_path: str) -> bool:


def save_to_json_with_prompt(
key: str, value: any, name: str, json_cfg_path: str, json_obj: any
key: str, value: Any, name: str, json_cfg_path: str, json_obj: Any
) -> bool:
save_to_json = input(
f"Do you want to store the {name} to the configuration file? (y/n): "
Expand Down
47 changes: 32 additions & 15 deletions tmtccmd/util/obj_id.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from __future__ import annotations

from typing import Mapping, Union, Optional
from collections.abc import Mapping
from typing import Union, Optional

from deprecated.sphinx import deprecated
from spacepackets.util import UnsignedByteField


class ObjectIdBase(UnsignedByteField):
class ComponentIdBase(UnsignedByteField):
"""Base class for unsigned object IDs with different byte sizes"""

def __init__(self, obj_id: int, byte_len: int, name: Optional[str] = None):
Expand Down Expand Up @@ -45,11 +45,11 @@ def obj_id(self, obj_id: Union[int, bytes]):
self.value = obj_id


class ObjectIdU32(ObjectIdBase):
class ComponentIdU32(ComponentIdBase):
"""A helper object for a unique object identifier which has a raw unsigned
32-bit representation.
>>> obj_id = ObjectIdU32(42, "Object with the answer to everything")
>>> obj_id = ComponentIdU32(42, "Object with the answer to everything")
>>> int(obj_id)
42
>>> obj_id.name
Expand All @@ -69,13 +69,13 @@ def __repr__(self):
)

@classmethod
def from_bytes_typed(cls, obj_id_as_bytes: bytes) -> ObjectIdU32:
obj_id = ObjectIdU32(obj_id=0)
def from_bytes_typed(cls, obj_id_as_bytes: bytes) -> ComponentIdU32:
obj_id = ComponentIdU32(obj_id=0)
obj_id.obj_id = obj_id_as_bytes
return obj_id


class ObjectIdU16(ObjectIdBase):
class ComponentIdU16(ComponentIdBase):
"""A helper object for a unique object identifier which has a raw unsigned
16-bit representation.
"""
Expand All @@ -89,13 +89,13 @@ def __repr__(self):
)

@classmethod
def from_bytes_typed(cls, obj_id_as_bytes: bytes) -> ObjectIdU16:
obj_id = ObjectIdU16(obj_id=0)
def from_bytes_typed(cls, obj_id_as_bytes: bytes) -> ComponentIdU16:
obj_id = ComponentIdU16(obj_id=0)
obj_id.obj_id = obj_id_as_bytes
return obj_id


class ObjectIdU8(ObjectIdBase):
class ComponentIdU8(ComponentIdBase):
"""A helper object for a unique object identifier which has a raw unsigned
8-bit representation.
"""
Expand All @@ -109,12 +109,29 @@ def __repr__(self):
)

@classmethod
def from_bytes_typed(cls, obj_id_as_bytes: bytes) -> ObjectIdU8:
obj_id = ObjectIdU8(obj_id=0)
def from_bytes_typed(cls, obj_id_as_bytes: bytes) -> ComponentIdU8:
obj_id = ComponentIdU8(obj_id=0)
obj_id.obj_id = obj_id_as_bytes
return obj_id


ObjectIdMapping = Mapping[bytes, ObjectIdBase]
"""Deprecated type defintion for :py:class:`ObjectIdMapping`"""
ComponentIdMapping = Mapping[bytes, ComponentIdBase]


ObjectIdBase = ComponentIdBase
"""Deprecated type defintion for :py:class:`ComponentIdBase`"""

ObjectIdU32 = ComponentIdU32
"""Deprecated type defintion for :py:class:`ComponentIdU32`"""

ObjectIdU16 = ComponentIdU16
"""Deprecated type defintion for :py:class:`ComponentIdU16`"""

ObjectIdU8 = ComponentIdU8
"""Deprecated type defintion for :py:class:`ComponentIdU8`"""

ObjectIdMapping = ComponentIdMapping
"""Deprecated type defintion for :py:class:`ComponentIdMapping`"""

ObjectIdDictT = ObjectIdMapping
"""Deprecated type defintion for :py:class:`ObjectIdMapping`"""
7 changes: 5 additions & 2 deletions tmtccmd/util/retval.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict
from collections.abc import Mapping


class RetvalInfo:
Expand All @@ -20,4 +20,7 @@ def unique_id(self) -> int:
return self.id & 0xFF


RetvalDictT = Dict[int, RetvalInfo]
RetvalMapping = Mapping[int, RetvalInfo]

RetvalDictT = RetvalMapping
"""Deprecatd typedef for :py:class:`RetvalMapping`."""

0 comments on commit e23af8e

Please sign in to comment.