Source code for HALF.Utils.Config
from glob import glob
from HALF.Commons.SingletonMeta import SingletonMeta
from omegaconf import OmegaConf
from typing import Union
import sys
[docs]class Config(metaclass=SingletonMeta):
"""
Config class with all parameters to configure the framework, built upon OmegaConf
"""
CONFIG = None
[docs] @staticmethod
def setup(file: Union[OmegaConf, str]):
"""Create the config class
Args:
file (Union[OmegaConf, str]): path where to find the config file or OmegaConf object
"""
conf_merge = None
conf_file = Config._get_file_conf(file)
sys.argv = sys.argv[1:]
conf_cli = OmegaConf.from_cli(sys.argv)
if (conf_file is not None):
conf_merge = OmegaConf.merge(conf_file, conf_cli)
elif (conf_cli is not None):
conf_merge = conf_cli
Config.CONFIG = conf_merge
@property
def data(self):
return Config.CONFIG
@staticmethod
def _get_file_conf(file: Union[OmegaConf, str]):
"""Get the file
Args:
file (Union[OmegaConf, str]): path where to find the config file or OmegaConf object
"""
conf_file = None
if file is not None:
if isinstance(file, OmegaConf):
conf_file = file
elif isinstance(file, str):
conf_file = OmegaConf.load(file)
return conf_file