nvflare.fuel.utils.config_service module¶
- class ConfigService[source]¶
Bases:
object
The ConfigService provides a global configuration service that can be used by any component at any layer. The ConfigService manages config information and makes it available to any component, in two ways: 1. Config info is preloaded into predefined sections. Callers can get the config data by a section name. 2. Manages config path (a list of directories) and loads file from the path.
Only JSON file loading is supported.
- classmethod add_section(section_name: str, data: dict, overwrite_existing: bool = True)[source]¶
Add a section to the config data.
- Parameters:
section_name – name of the section to be added
data – data of the section
overwrite_existing – if section already exists, whether to overwrite
Returns: None
- classmethod find_file(file_basename: str) None | str [source]¶
Find specified file from the config path. Caller is responsible for loading/processing the file. This is useful for non-JSON files.
- Parameters:
file_basename – base name of the file to be found
Returns: full name of the file if found; None if not.
- classmethod get_bool_var(name: str, conf=None, default=None)[source]¶
Get configured bool value of the specified var
- Parameters:
name – name of the var
conf – source config
default – value to return if the var is not found
Returns: configured value of the var, or the default value if var is not configured
- classmethod get_dict_var(name: str, conf=None, default=None)[source]¶
Get configured dict value of the specified var
- Parameters:
name – name of the var
conf – source config
default – value to return if the var is not found
Returns: configured value of the var, or the default value if var is not configured
- classmethod get_float_var(name: str, conf=None, default=None)[source]¶
Get configured float value of the specified var
- Parameters:
name – name of the var
conf – source config
default – value to return if the var is not found
Returns: configured value of the var, or the default value if var is not configured
- classmethod get_int_var(name: str, conf=None, default=None)[source]¶
Get configured int value of the specified var
- Parameters:
name – name of the var
conf – source config
default – value to return if the var is not found
Returns: configured value of the var, or the default value if var is not configured
- classmethod get_section(name: str)[source]¶
Get the specified section.
- Parameters:
name – name of the section
Returns: the section of the specified name, or None if the section is not found.
- classmethod get_str_var(name: str, conf=None, default=None)[source]¶
Get configured str value of the specified var
- Parameters:
name – name of the var
conf – source config
default – value to return if the var is not found
Returns: configured value of the var, or the default value if var is not configured
- classmethod initialize(section_files: Dict[str, str], config_path: List[str], parsed_args=None, var_dict=None)[source]¶
Initialize the ConfigService. Configuration is divided into sections, and each section must have a JSON config file. Only specify the base name of the config file. Config path is provided to locate config files. Files are searched in the order of provided config_dirs. If multiple directories contain the same file name, then the first one is used.
- Parameters:
section_files – dict: section name => config file
config_path – list of config directories
parsed_args – command args for starting the program
var_dict – dict for additional vars
Returns:
- classmethod load_config_dict(file_basename: str, search_dirs: List | None = None, raise_exception: bool = True) Dict | None [source]¶
Load a specified config file ( ignore extension)
- Parameters:
raise_exception – if True raise exception when error occurs
file_basename – base name of the config file to be loaded.
example (for) – file_basename = config_fed_server.json
matches (what the function does is to search for config file that)
config_fed_server.[json|json.default|conf|conf.default|yml|yml.default]
directories (in given search) – cls._config_path
found; (if json or json.default is not)
switch (then switch to Pyhoncon [.conf] or corresponding default file; if still not found; then we)
YAML (to YAML files. We use OmegaConf to load)
search_dirs – which directories to search.
- Returns: Dictionary from the configuration
if not found, exception will be raised.
- classmethod load_configuration(file_basename: str) Config | None [source]¶
Load config data from the specified file basename. The full name of the config file will be determined by ConfigFactory.
- Parameters:
file_basename – the basename of the config file.
Returns: config data loaded, or None if the config file is not found.
- logger = <Logger nvflare.fuel.utils.config_service.ConfigService (WARNING)>¶
- find_file_in_dir(file_basename, path) None | str [source]¶
Find a file from a directory and return the full path of the file, if found
- Parameters:
file_basename – base name of the file to be found
path – the directory from where the file is to be found
Returns: the full path of the file, if found; None if not found
- search_file(file_basename: str, dirs: List[str]) None | str [source]¶
Find a file by searching a list of dirs and return the one in the last dir.
- Parameters:
file_basename – base name of the file to be found
dirs – list of directories to search
Returns: the full path of the file, if found; None if not found