Coverage for physioblocks / registers / type_register.py: 100%
17 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-09 16:40 +0100
« 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/>.
27"""
28Define decorators to register any type with a name in a dedicated register.
29"""
31from collections.abc import Callable
32from typing import Any, TypeVar
34from physioblocks.base.registers import check_key_value_type, register
36__type_register: dict[Any, Any] = {}
39T = TypeVar("T")
42def register_type(type_id: str) -> Callable[[T], T]:
43 """
44 Class decorator to register the class type with the given name.
46 :param type_id: the defined type id for the class type
47 :type type_id: str
49 :return: the class decorator
50 :rtype: Callable
51 """
53 def class_decorator(registered_type: T) -> T:
54 check_key_value_type(type_id, str, registered_type, type)
55 register(__type_register, type_id, registered_type)
56 return registered_type
58 return class_decorator
61def get_registered_type(type_id: str) -> Any:
62 """
63 Get a registered type.
65 :param type_id: the registered type id
66 :type type_id: str
68 :return: the registered type
69 :rtype: type
70 """
71 return __type_register[type_id]
74def get_registered_type_id(registered_type: type) -> Any:
75 """
76 Get a registered type name for the matching type.
78 :param registered_type: the registered type id
79 :type registered_type: type
81 :return: the registered type id
82 :rtype: str
83 """
84 return __type_register[registered_type]
87def is_registered(key: Any) -> bool:
88 """
89 Get if the given value is registered as a type or type id.
91 :param key: the key to test
92 :type key: Any
94 :return: True if the key is registered, False otherwise
95 :rtype: bool
96 """
97 return key in __type_register