diff --git a/bumble/core.py b/bumble/core.py index aa8aee5..1cc10ec 100644 --- a/bumble/core.py +++ b/bumble/core.py @@ -152,7 +152,12 @@ class UUID: BASE_UUID = bytes.fromhex('00001000800000805F9B34FB')[::-1] # little-endian UUIDS: List[UUID] = [] # Registry of all instances created - def __init__(self, uuid_str_or_int, name=None): + uuid_bytes: bytes + name: Optional[str] + + def __init__( + self, uuid_str_or_int: Union[str, int], name: Optional[str] = None + ) -> None: if isinstance(uuid_str_or_int, int): self.uuid_bytes = struct.pack(' UUID: # Register this object in the class registry, and update the entry's name if # it wasn't set already for uuid in self.UUIDS: @@ -196,22 +201,22 @@ class UUID: raise ValueError('only 2, 4 and 16 bytes are allowed') @classmethod - def from_16_bits(cls, uuid_16, name=None): + def from_16_bits(cls, uuid_16: int, name: Optional[str] = None) -> UUID: return cls.from_bytes(struct.pack(' UUID: return cls.from_bytes(struct.pack(' Tuple[int, UUID]: return len(uuid_as_bytes), cls.from_bytes(uuid_as_bytes[offset:]) @classmethod - def parse_uuid_2(cls, uuid_as_bytes, offset): + def parse_uuid_2(cls, uuid_as_bytes: bytes, offset: int) -> Tuple[int, UUID]: return offset + 2, cls.from_bytes(uuid_as_bytes[offset : offset + 2]) - def to_bytes(self, force_128=False): + def to_bytes(self, force_128: bool = False) -> bytes: ''' Serialize UUID in little-endian byte-order ''' @@ -227,7 +232,7 @@ class UUID: else: assert False, "unreachable" - def to_pdu_bytes(self): + def to_pdu_bytes(self) -> bytes: ''' Convert to bytes for use in an ATT PDU. According to Vol 3, Part F - 3.2.1 Attribute Type: @@ -236,11 +241,11 @@ class UUID: ''' return self.to_bytes(force_128=(len(self.uuid_bytes) == 4)) - def to_hex_str(self) -> str: + def to_hex_str(self, separator: str = '') -> str: if len(self.uuid_bytes) == 2 or len(self.uuid_bytes) == 4: return bytes(reversed(self.uuid_bytes)).hex().upper() - return ''.join( + return separator.join( [ bytes(reversed(self.uuid_bytes[12:16])).hex(), bytes(reversed(self.uuid_bytes[10:12])).hex(), @@ -250,10 +255,10 @@ class UUID: ] ).upper() - def __bytes__(self): + def __bytes__(self) -> bytes: return self.to_bytes() - def __eq__(self, other): + def __eq__(self, other: object) -> bool: if isinstance(other, UUID): return self.to_bytes(force_128=True) == other.to_bytes(force_128=True) @@ -262,35 +267,19 @@ class UUID: return False - def __hash__(self): + def __hash__(self) -> int: return hash(self.uuid_bytes) - def __str__(self): + def __str__(self) -> str: + result = self.to_hex_str(separator='-') if len(self.uuid_bytes) == 2: - uuid = struct.unpack(' None: + assert UUID("b5ea").to_hex_str() == "B5EA" + assert UUID("df5ce654").to_hex_str() == "DF5CE654" + assert ( + UUID("df5ce654-e059-11ed-b5ea-0242ac120002").to_hex_str() + == "DF5CE654E05911EDB5EA0242AC120002" + ) + assert UUID("b5ea").to_hex_str('-') == "B5EA" + assert UUID("df5ce654").to_hex_str('-') == "DF5CE654" + assert ( + UUID("df5ce654-e059-11ed-b5ea-0242ac120002").to_hex_str('-') + == "DF5CE654-E059-11ED-B5EA-0242AC120002" + ) + + # ----------------------------------------------------------------------------- if __name__ == '__main__': test_ad_data() + test_get_dict_key_by_value() + test_uuid_to_hex_str()