Read a config file and return a dict (arg_name -> val). Args: filename: json or yaml filename Returns: a dictionary maps argument name (str) -> val
(
filename: str,
)
| 26 | |
| 27 | |
| 28 | def read_config_file( |
| 29 | filename: str, |
| 30 | ) -> T.Dict[str, T.Any]: |
| 31 | """ |
| 32 | Read a config file and return a dict (arg_name -> val). |
| 33 | |
| 34 | Args: |
| 35 | filename: |
| 36 | json or yaml filename |
| 37 | |
| 38 | Returns: |
| 39 | a dictionary maps argument name (str) -> val |
| 40 | """ |
| 41 | |
| 42 | if os.path.exists(filename): |
| 43 | ext = os.path.splitext(filename)[1] |
| 44 | if ext == ".json": |
| 45 | with open(filename, "r") as f: |
| 46 | config_dict = json.load(f) |
| 47 | elif ext == ".yaml": |
| 48 | with open(filename, "r") as f: |
| 49 | config_dict = yaml.load(f, Loader=yaml.FullLoader) |
| 50 | else: |
| 51 | raise NotImplementedError(f"{filename} ({ext}) not supported") |
| 52 | else: |
| 53 | raise RuntimeError(f"{filename} not exist") |
| 54 | |
| 55 | return config_dict |
| 56 | |
| 57 | |
| 58 | def compile_argparser_str( |