Coverage for src / bluetooth_sig / registry / uuids / declarations.py: 81%

31 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-11 20:14 +0000

1"""Declarations registry for Bluetooth SIG declaration UUIDs.""" 

2 

3from __future__ import annotations 

4 

5from bluetooth_sig.registry.base import BaseUUIDRegistry 

6from bluetooth_sig.types.registry.declarations import DeclarationInfo 

7from bluetooth_sig.types.uuid import BluetoothUUID 

8 

9 

10class DeclarationsRegistry(BaseUUIDRegistry[DeclarationInfo]): 

11 """Registry for Bluetooth SIG GATT attribute declarations.""" 

12 

13 def _load_yaml_path(self) -> str: 

14 """Return the YAML file path relative to bluetooth_sig/ root.""" 

15 return "assigned_numbers/uuids/declarations.yaml" 

16 

17 def _create_info_from_yaml(self, uuid_data: dict[str, str], uuid: BluetoothUUID) -> DeclarationInfo: 

18 """Create DeclarationInfo from YAML data.""" 

19 return DeclarationInfo( 

20 uuid=uuid, 

21 name=uuid_data["name"], 

22 id=uuid_data["id"], 

23 ) 

24 

25 def _create_runtime_info(self, entry: object, uuid: BluetoothUUID) -> DeclarationInfo: 

26 """Create runtime DeclarationInfo from entry.""" 

27 return DeclarationInfo( 

28 uuid=uuid, 

29 name=getattr(entry, "name", ""), 

30 id=getattr(entry, "id", ""), 

31 ) 

32 

33 def get_declaration_info(self, uuid: str | BluetoothUUID) -> DeclarationInfo | None: 

34 """Get declaration information by UUID. 

35 

36 Args: 

37 uuid: The UUID to look up (string, int, or BluetoothUUID) 

38 

39 Returns: 

40 DeclarationInfo if found, None otherwise 

41 """ 

42 return self.get_info(uuid) 

43 

44 def get_declaration_info_by_name(self, name: str) -> DeclarationInfo | None: 

45 """Get declaration information by name (case insensitive). 

46 

47 Args: 

48 name: The declaration name to look up 

49 

50 Returns: 

51 DeclarationInfo if found, None otherwise 

52 """ 

53 self._ensure_loaded() 

54 for info in self._canonical_store.values(): 

55 if info.name.lower() == name.lower(): 

56 return info 

57 return None 

58 

59 def get_declaration_info_by_id(self, declaration_id: str) -> DeclarationInfo | None: 

60 """Get declaration information by declaration ID. 

61 

62 Args: 

63 declaration_id: The declaration ID to look up 

64 

65 Returns: 

66 DeclarationInfo if found, None otherwise 

67 """ 

68 self._ensure_loaded() 

69 for info in self._canonical_store.values(): 

70 if info.id == declaration_id: 

71 return info 

72 return None 

73 

74 def is_declaration_uuid(self, uuid: str | BluetoothUUID) -> bool: 

75 """Check if a UUID corresponds to a known declaration. 

76 

77 Args: 

78 uuid: The UUID to check 

79 

80 Returns: 

81 True if the UUID is a known declaration, False otherwise 

82 """ 

83 return self.get_info(uuid) is not None 

84 

85 def get_all_declarations(self) -> list[DeclarationInfo]: 

86 """Get all declarations in the registry. 

87 

88 Returns: 

89 List of all DeclarationInfo objects 

90 """ 

91 self._ensure_loaded() 

92 return list(self._canonical_store.values()) 

93 

94 

95# Global instance for convenience 

96declarations_registry = DeclarationsRegistry.get_instance()