Config

Attributes

Classes

Config

Config(root=None, name=None, auto_generate=True)

Base Config class

Source code in fedbiomed/common/config.py
def __init__(
    self, root=None, name: Optional[str] = None, auto_generate: bool = True
) -> None:
    """Initializes config"""

    # First try to get component specific config file name, then CONFIG_FILE
    default_config = os.getenv(
        f"{self._COMPONENT_TYPE}_CONFIG_FILE",
        os.getenv("CONFIG_FILE", self._DEFAULT_CONFIG_FILE_NAME),
    )

    self.root = root
    self._cfg = configparser.ConfigParser()
    self.name = name if name else default_config

    if self.root:
        self.path = os.path.join(self.root, CONFIG_FOLDER_NAME, self.name)
        self.root = self.root
    else:
        self.path = os.path.join(CONFIG_DIR, self.name)
        self.root = ROOT_DIR

    # Creates setup folders if not existing
    create_fedbiomed_setup_folders(self.root)

    if auto_generate:
        self.generate()

Attributes

name instance-attribute
name = name if name else default_config
path instance-attribute
path = join(root, CONFIG_FOLDER_NAME, name)
root instance-attribute
root = root

Functions

add_parameters abstractmethod
add_parameters()

"Component specific argument creation

Source code in fedbiomed/common/config.py
@abstractmethod
def add_parameters(self):
    """ "Component specific argument creation"""
generate
generate(force=False, id=None)

"Generate configuration file

Parameters:

Name Type Description Default
force bool

Overwrites existing configration file

False
id Optional[str]

Component ID

None
Source code in fedbiomed/common/config.py
def generate(self, force: bool = False, id: Optional[str] = None) -> bool:
    """ "Generate configuration file

    Args:
        force: Overwrites existing configration file
        id: Component ID
    """

    # Check if configuration is already existing
    if self.is_config_existing() and not force:
        return self.read()

    # Create default section
    component_id = id if id else f"{self._COMPONENT_TYPE}_{uuid.uuid4()}"

    self._cfg["default"] = {
        "id": component_id,
        "component": self._COMPONENT_TYPE,
        "version": str(self._CONFIG_VERSION),
    }

    db_path = os.path.join(
        self.root, VAR_FOLDER_NAME, f"{DB_PREFIX}{component_id}.json"
    )
    self._cfg["default"]["db"] = os.path.relpath(
        db_path, os.path.join(self.root, CONFIG_FOLDER_NAME)
    )


    # Calls child class add_parameterss
    self.add_parameters()

    # Write configuration file
    return self.write()
get
get(section, key, **kwargs)

Returns value for given key and section

Source code in fedbiomed/common/config.py
def get(self, section, key, **kwargs) -> str:
    """Returns value for given key and section"""

    return self._cfg.get(section, key, **kwargs)
is_config_existing
is_config_existing()

Checks if config file exists

Returns:

Type Description
bool

True if config file is already existing

Source code in fedbiomed/common/config.py
def is_config_existing(self) -> bool:
    """Checks if config file exists

    Returns:
        True if config file is already existing
    """

    return os.path.isfile(self.path)
read
read()

Reads configuration file that is already existing in given path

Raises verision compatibility error

Source code in fedbiomed/common/config.py
def read(self) -> bool:
    """Reads configuration file that is already existing in given path

    Raises verision compatibility error
    """

    self._cfg.read(self.path)

    # Validate config version
    raise_for_version_compatibility(
        self._cfg["default"]["version"],
        self._CONFIG_VERSION,
        f"Configuration file {self.path}: found version %s expected version %s",
    )

    return True
refresh
refresh()

Refreshes config file by recreating all the fields without chaning component ID.

Source code in fedbiomed/common/config.py
def refresh(self):
    """Refreshes config file by recreating all the fields without
    chaning component ID.
    """

    if not self.is_config_existing():
        raise FedbiomedError("Can not refresh config file that is not existing")

    # Read the config
    self._cfg.read(self.path)
    id = self._cfg["default"]["id"]

    # Generate by keeping the component ID
    self.generate(force=True, id=id)
sections
sections()

Returns sections of the config

Source code in fedbiomed/common/config.py
def sections(self) -> list:
    """Returns sections of the config"""

    return self._cfg.sections()
set
set(section, key, value)

Sets config section values

Parameters:

Name Type Description Default
section

the name of the config file section as defined by the ini standard

required
key

the name of the attribute to be set

required
value

the value of the attribute to be set

required

Returns:

Name Type Description
value None

the value of the attribute that was just set

Source code in fedbiomed/common/config.py
def set(self, section, key, value) -> None:
    """Sets config section values

    Args:
        section: the name of the config file section as defined by the `ini` standard
        key: the name of the attribute to be set
        value: the value of the attribute to be set

    Returns:
        value: the value of the attribute that was just set
    """
    self._cfg.set(section, key, value)
write
write()

Writes config file

Source code in fedbiomed/common/config.py
def write(self):
    """Writes config file"""

    try:
        with open(self.path, "w", encoding="UTF-8") as f:
            self._cfg.write(f)
    except configparser.Error as exp:
        raise IOError(
            ErrorNumbers.FB600.value + ": cannot save config file: " + self.path
        ) from exp

Functions