def load_yaml_opts(source): """ Load options dictionary from a yaml file """ result = {} if isinstance(source, str): with open(source, 'r') as yaml_file: options_dict = yaml.safe_load(yaml_file) elif isinstance(source, dict): options_dict = source else: raise TypeError('Unsupported source type: {}'.format(type(source))) includes = options_dict.get('__include__', False) if includes: if type(includes) != list: includes = [includes] for include in includes: filename = '{}/{}'.format(os.path.dirname(source), include) if os.path.isfile(filename): parent = Options.load_yaml_opts(filename) else: parent = Options.load_yaml_opts(include) merge_dictionaries(result, parent) merge_dictionaries( result, options_dict ) # to be sure the main options overwrite the parent options result.pop('__include__', None) result = OptionsDict(result) return result
def test_merge_dictionaries(): """ Merge two dictionnary """ dict1 = {'exp': {'dir': 'lol1', 'resume': None}} dict2 = {'exp': {'dir': 'lol2'}} dict1 = OptionsDict(dict1) dict2 = OptionsDict(dict2) merge_dictionaries(dict1, dict2) assert (dict1 == OptionsDict( {'exp': OptionsDict({ 'dir': 'lol2', 'resume': None })}))
def load_yaml_opts(path_yaml): """ Load options dictionary from a yaml file """ result = {} print("debug", path_yaml) with open(path_yaml, 'r') as yaml_file: options_yaml = yaml.load(yaml_file, Loader=yaml.FullLoader) includes = options_yaml.get('__include__', False) if includes: if type(includes) != list: includes = [includes] for include in includes: parent = Options.load_yaml_opts('{}/{}'.format( os.path.dirname(path_yaml), include)) merge_dictionaries(result, parent) merge_dictionaries( result, options_yaml ) # to be sure the main options overwrite the parent options result.pop('__include__', None) return result
def load_yaml_opts(path_yaml): """ Load options dictionary from a yaml file """ # TODO: include the parent options when parsed, instead of after having loaded the main options result = {} with open(path_yaml, 'r') as yaml_file: options_yaml = yaml.load(yaml_file) includes = options_yaml.get('__include__', False) if includes: if type(includes) != list: includes = [includes] for include in includes: parent = Options.load_yaml_opts('{}/{}'.format( os.path.dirname(path_yaml), include)) merge_dictionaries(result, parent) merge_dictionaries( result, options_yaml ) # to be sure the main options overwrite the parent options result.pop('__include__', None) result = OptionsDict(result) return result