From e23af8efd6a19facde20d9781c465506eb24d5dc Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 9 May 2024 12:01:32 +0200 Subject: [PATCH] more (non-breaking) naming updates --- tests/test_util.py | 31 ++++++++++++++------------ tmtccmd/util/__init__.py | 14 +++++++++++- tmtccmd/util/conf_util.py | 8 +++---- tmtccmd/util/json.py | 3 ++- tmtccmd/util/obj_id.py | 47 ++++++++++++++++++++++++++------------- tmtccmd/util/retval.py | 7 ++++-- 6 files changed, 73 insertions(+), 37 deletions(-) diff --git a/tests/test_util.py b/tests/test_util.py index 09501666..32c836ef 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -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) diff --git a/tmtccmd/util/__init__.py b/tmtccmd/util/__init__.py index 5d696d93..fdce8f0e 100644 --- a/tmtccmd/util/__init__.py +++ b/tmtccmd/util/__init__.py @@ -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 diff --git a/tmtccmd/util/conf_util.py b/tmtccmd/util/conf_util.py index b942a1cc..33dc01c6 100644 --- a/tmtccmd/util/conf_util.py +++ b/tmtccmd/util/conf_util.py @@ -1,6 +1,6 @@ import collections.abc import logging -from typing import Tuple, Union +from typing import Any, Tuple, Union from contextlib import contextmanager @@ -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): diff --git a/tmtccmd/util/json.py b/tmtccmd/util/json.py index 7ae8494f..b0e8beaa 100644 --- a/tmtccmd/util/json.py +++ b/tmtccmd/util/json.py @@ -2,6 +2,7 @@ import logging import os import enum +from typing import Any _LOGGER = logging.getLogger(__name__) @@ -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): " diff --git a/tmtccmd/util/obj_id.py b/tmtccmd/util/obj_id.py index 0ec582a3..5d11f67a 100644 --- a/tmtccmd/util/obj_id.py +++ b/tmtccmd/util/obj_id.py @@ -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): @@ -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 @@ -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. """ @@ -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. """ @@ -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`""" diff --git a/tmtccmd/util/retval.py b/tmtccmd/util/retval.py index 37a39afc..c5a92dc7 100644 --- a/tmtccmd/util/retval.py +++ b/tmtccmd/util/retval.py @@ -1,4 +1,4 @@ -from typing import Dict +from collections.abc import Mapping class RetvalInfo: @@ -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`."""