# Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
#
# 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.
from abc import abstractmethod
from nvflare.fuel.common.excepts import ConfigError
from nvflare.fuel.utils.class_utils import get_class_path_from_config, instantiate_class
from nvflare.security.logging import secure_format_exception
[docs]
class ConfigType:
COMPONENT = "component"
DICT = "dict"
[docs]
class ComponentBuilder:
[docs]
@abstractmethod
def get_module_scanner(self):
"""Provide the package module scanner.
Returns: module_scanner
"""
pass
[docs]
def is_class_config(self, config_dict: dict) -> bool:
def has_valid_class_path():
try:
_ = self.get_class_path(config_dict)
# we have valid class path
return True
except ConfigError:
# this is not a valid class path
return False
# use config_type to distinguish between components and regular dictionaries
config_type = config_dict.get("config_type", ConfigType.COMPONENT)
if config_type != ConfigType.COMPONENT:
return False
# regardless it has args or not. if path/class_path/name and valid class path, very likely we have
# class config. "class_path" is accepted for consistency with recipe/model config API.
if ("path" in config_dict or "class_path" in config_dict or "name" in config_dict) and has_valid_class_path():
return True
else:
return False
[docs]
def build_component(self, config_dict):
if not config_dict:
return None
if not isinstance(config_dict, dict):
raise ConfigError("component config must be dict but got {}.".format(type(config_dict)))
if config_dict.get("disabled") is True:
return None
class_args = config_dict.get("args", dict())
for k, v in class_args.items():
if isinstance(v, dict) and self.is_class_config(v):
# try to replace the arg with a component
try:
t = self.build_component(v)
class_args[k] = t
except Exception as e:
raise ValueError(f"failed to instantiate class: {secure_format_exception(e)} ")
class_path = self.get_class_path(config_dict)
# Handle the special case, if config pass in the class_attributes, use the user defined class attributes
# parameters directly.
if "class_attributes" in class_args:
class_args = class_args["class_attributes"]
return instantiate_class(class_path, class_args)
[docs]
def get_class_path(self, config_dict):
return get_class_path_from_config(
config_dict,
resolve_name=lambda cn: self.get_module_scanner().get_module_name(cn),
)