Source code for HALF.Utils.Registry
from __future__ import annotations
# https://github.com/niccle27/PyPipeline/blob/master/Libs/Registry/registry.py
[docs]class Registry:
glob_module_registry = dict()
[docs] @classmethod
def register(cls, name: str=None, force:bool=False) -> Registry:
"""Class method to register Executor class to the internal registry.
Args:
name (str, optional): The name of the executor. Defaults to None.
force (bool, optional): If True, write registry key even if it already exists. Defaults to False.
Returns:
The Executor class itself.
"""
def inner_wrapper(wrapped_class):
class_name = wrapped_class.__name__
if name is not None:
key = name
else:
key = class_name
if force or name not in cls.glob_module_registry:
cls.glob_module_registry[key] = wrapped_class
else:
raise Exception(f"Key [{key}] is already stored in registry")
return wrapped_class
return inner_wrapper
def __getitem__(self, key:str):
return Registry.glob_module_registry[key]
REGISTRY = Registry()