40 Just like `yaml.load(open(filename))`, but inherit attributes from its
43 filename (str): the file name of the current config. Will be used to
44 find the base config file.
45 allow_unsafe (bool): whether to allow loading the config file with
48 (dict): the loaded yaml
50 with PathManager.open(filename,
"r")
as f:
52 cfg = yaml.safe_load(f)
53 except yaml.constructor.ConstructorError:
56 logger = logging.getLogger(__name__)
58 "Loading config {} with yaml.unsafe_load. Your machine may "
59 "be at risk if the file contains malicious content.".format(
64 with open(filename,
"r")
as f:
65 cfg = yaml.unsafe_load(f)
67 def merge_a_into_b(a, b):
69 for k, v
in a.items():
70 if isinstance(v, dict)
and k
in b:
73 ),
"Cannot inherit key '{}' from base!".format(k)
74 merge_a_into_b(v, b[k])
79 base_cfg_file = cfg[BASE_KEY]
80 if base_cfg_file.startswith(
"~"):
81 base_cfg_file = os.path.expanduser(base_cfg_file)
83 map(base_cfg_file.startswith, [
"/",
"https://",
"http://"])
86 base_cfg_file = os.path.join(
87 os.path.dirname(filename), base_cfg_file
89 base_cfg = CfgNode.load_yaml_with_base(
90 base_cfg_file, allow_unsafe=allow_unsafe
94 merge_a_into_b(cfg, base_cfg)
100 Merge configs from a given yaml file.
102 cfg_filename: the file name of the yaml config.
103 allow_unsafe: whether to allow loading the config file with
106 loaded_cfg = CfgNode.load_yaml_with_base(
107 cfg_filename, allow_unsafe=allow_unsafe
109 loaded_cfg = type(self)(loaded_cfg)