Coverage for physioblocks / registers / save_function_register.py: 100%

23 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 decorators to register any function as a specific save function 

29of a configuration object. 

30""" 

31 

32from collections.abc import Callable 

33from typing import Any 

34 

35from physioblocks.base.registers import register 

36 

37__save_functions_register: dict[type, Callable[..., Any]] = {} 

38 

39 

40def saves( 

41 saved_type: type, 

42) -> Callable[[Callable[..., Any]], Callable[..., Any]]: 

43 """ 

44 Decorator to register a method that saves a registered type. 

45 

46 :param saved_type: the saved type by the method 

47 :type saved_type: str 

48 

49 :return: the class decorator 

50 :rtype: Callable 

51 """ 

52 

53 def register_decorator( 

54 save_function: Callable[..., Any], 

55 ) -> Callable[..., Any]: 

56 register(__save_functions_register, saved_type, save_function) 

57 return save_function 

58 

59 return register_decorator 

60 

61 

62def has_save_function(key: type) -> bool: 

63 """ 

64 Check if the type has a save function 

65 

66 :param key: the key to test 

67 :type key: Any 

68 

69 :return: True if the key is registered, False otherwise 

70 :rtype: bool 

71 """ 

72 key_type = _get_closest_type(key) 

73 return key_type in __save_functions_register 

74 

75 

76def get_save_function(saved_type: type) -> Callable[..., Any]: 

77 """ 

78 Get a registered save function. 

79 

80 :param type_id: the registered type id 

81 :type type_id: str 

82 

83 :raise KeyError: Raises a key error if the type is not registered. 

84 

85 :return: the registered type 

86 :rtype: type 

87 """ 

88 key_type = _get_closest_type(saved_type) 

89 

90 if key_type is None: 

91 raise KeyError( 

92 str.format( 

93 "No save function registered for type {0}", 

94 saved_type.__name__, 

95 ) 

96 ) 

97 return __save_functions_register[key_type] 

98 

99 

100def _get_closest_type(searched_type: type | None) -> None | type: 

101 if searched_type is None: 

102 return None 

103 elif searched_type in __save_functions_register: 

104 return searched_type 

105 else: 

106 return _get_closest_type(searched_type.__base__)