# Copyright (c) Alibaba, Inc. and its affiliates.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.# --------------------------------------------------------# Most of the code here has been modified from:# https://github.com/modelscope/modelscope/blob/master/modelscope/utils/registry.py# --------------------------------------------------------
[docs]classRegistry(object):"""This class is used to register some modules to registry by a repo name."""
[docs]def__init__(self,name:str):""" Initialization method. :param name: a registry repo name """self._name=nameself._modules={}
@propertydefname(self):""" Get name of current registry. :return: name of current registry. """returnself._name@propertydefmodules(self):""" Get all modules in current registry. :return: a dict storing modules in current registry. """returnself._modules
[docs]deflist(self):"""Logging the list of module in current registry."""returnlist(self._modules.keys())
[docs]defget(self,module_key):""" Get module named module_key from in current registry. If not found, return None. :param module_key: specified module name :return: module named module_key """returnself._modules.get(module_key,None)
def_register_module(self,module_name=None,module_cls=None,force=False):""" Register module to registry. :param module_name: module name :param module_cls: module class object :param force: Whether to override an existing class with the same name. Default: False. """ifmodule_nameisNone:module_name=module_cls.__name__ifmodule_nameinself._modulesandnotforce:raiseKeyError(f'{module_name} is already registered in {self._name}')self._modules[module_name]=module_clsmodule_cls._name=module_name
[docs]defregister_module(self,module_name:str=None,module_cls:type=None,force=False):""" Register module class object to registry with the specified modulename. :param module_name: module name :param module_cls: module class object :param force: Whether to override an existing class with the same name. Default: False. Example: >>> registry = Registry() >>> @registry.register_module() >>> class TextFormatter: >>> pass >>> class TextFormatter2: >>> pass >>> registry.register_module( module_name='text_formatter2', module_cls=TextFormatter2) """ifnot(module_nameisNoneorisinstance(module_name,str)):raiseTypeError(f'module_name must be either of None, str,'f'got {type(module_name)}')ifmodule_clsisnotNone:self._register_module(module_name=module_name,module_cls=module_cls,force=force)returnmodule_cls# if module_cls is None, should return a decorator functiondef_register(module_cls):""" Register module class object to registry. :param module_cls: module class object :return: module class object. """self._register_module(module_name=module_name,module_cls=module_cls,force=force)returnmodule_clsreturn_register