Coverage for physioblocks / configuration / base.py: 98%

42 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-09 16:40 +0100

1# SPDX-FileCopyrightText: Copyright INRIA 

2# 

3# SPDX-License-Identifier: LGPL-3.0-only 

4# 

5# Copyright INRIA 

6# 

7# This file is part of PhysioBlocks, a library mostly developed by the 

8# [Ananke project-team](https://team.inria.fr/ananke) at INRIA. 

9# 

10# Authors: 

11# - Colin Drieu 

12# - Dominique Chapelle 

13# - François Kimmig 

14# - Philippe Moireau 

15# 

16# PhysioBlocks is free software: you can redistribute it and/or modify it under the 

17# terms of the GNU Lesser General Public License as published by the Free Software 

18# Foundation, version 3 of the License. 

19# 

20# PhysioBlocks is distributed in the hope that it will be useful, but WITHOUT ANY 

21# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 

22# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 

23# 

24# You should have received a copy of the GNU Lesser General Public License along with 

25# PhysioBlocks. If not, see <https://www.gnu.org/licenses/>. 

26 

27""" 

28Define the base configurations objects 

29""" 

30 

31from __future__ import annotations 

32 

33from collections.abc import ItemsView, Iterator, KeysView, Mapping, ValuesView 

34from dataclasses import dataclass, field 

35from typing import Any 

36 

37 

38@dataclass 

39class Configuration(Mapping[str, Any]): 

40 """ 

41 Define a generic configuration object that can easily be serialized. 

42 

43 .. note:: 

44 

45 The ``configuration_items`` can also be :class:`~.Configuration` instances. 

46 """ 

47 

48 label: str 

49 """The item type name""" 

50 

51 configuration_items: dict[str, Any] = field(default_factory=dict) 

52 """Configuration values""" 

53 

54 def __getitem__(self, key: str) -> Any: 

55 return self.configuration_items[key] 

56 

57 def __setitem__(self, key: str, value: Any) -> None: 

58 self.configuration_items[key] = value 

59 

60 def __contains__(self, key: object) -> bool: 

61 return key in self.configuration_items 

62 

63 def __iter__(self) -> Iterator[str]: 

64 yield from self.configuration_items 

65 

66 def __eq__(self, config: Any) -> bool: 

67 if isinstance(config, Configuration): 

68 if config.label != self.label: 

69 return False 

70 if len(config.configuration_items) != len(self.configuration_items): 

71 return False 

72 for key, val in self.items(): 

73 if key not in config: 

74 return False 

75 if config[key] != val: 

76 return False 

77 return True 

78 return False 

79 

80 def __len__(self) -> int: 

81 return len(self.configuration_items) 

82 

83 def items(self) -> ItemsView[str, Any]: 

84 """ 

85 Get a view on configuration key and value pairs. 

86 

87 :return: a view on configuration key-value 

88 :rtype: ItemsView[str, Any] 

89 """ 

90 return self.configuration_items.items() 

91 

92 def keys(self) -> KeysView[str]: 

93 """ 

94 Get a view on configuration keys 

95 

96 :return: a view on configuration keys 

97 :rtype: KeysView[str] 

98 """ 

99 return self.configuration_items.keys() 

100 

101 def values(self) -> ValuesView[Any]: 

102 """ 

103 Get an iterable on configuration values 

104 

105 :return: a view on configuration values 

106 :rtype: ValuesView[Any] 

107 """ 

108 return self.configuration_items.values() 

109 

110 def copy(self) -> Configuration: 

111 """ 

112 Get a copy of the configuration. 

113 

114 :return: a copy of the configuration item. 

115 :rtype: Configuration 

116 """ 

117 return Configuration(self.label, self.configuration_items.copy()) 

118 

119 

120class ConfigurationError(Exception): 

121 """ 

122 Error raised when a configuration can not be loaded or saved. 

123 """