(yaml_path=None, yaml_config=None, yaml_dir=None, mode="full")
| 367 | |
| 368 | |
| 369 | def load_yaml_config(yaml_path=None, yaml_config=None, yaml_dir=None, mode="full"): |
| 370 | if mode == "simple": |
| 371 | constructor_fn = ignore_constructor |
| 372 | elif mode == "full": |
| 373 | constructor_fn = import_function |
| 374 | |
| 375 | # Add the import_function constructor to the YAML loader |
| 376 | yaml.add_constructor("!function", constructor_fn) |
| 377 | if yaml_config is None: |
| 378 | with open(yaml_path, "rb") as file: |
| 379 | yaml_config = yaml.full_load(file) |
| 380 | |
| 381 | if yaml_dir is None: |
| 382 | yaml_dir = os.path.dirname(yaml_path) |
| 383 | |
| 384 | assert yaml_dir is not None |
| 385 | |
| 386 | if "include" in yaml_config: |
| 387 | include_path = yaml_config["include"] |
| 388 | del yaml_config["include"] |
| 389 | |
| 390 | if isinstance(include_path, str): |
| 391 | include_path = [include_path] |
| 392 | |
| 393 | # Load from the last one first |
| 394 | include_path.reverse() |
| 395 | final_yaml_config = {} |
| 396 | for path in include_path: |
| 397 | # Assumes that path is a full path. |
| 398 | # If not found, assume the included yaml |
| 399 | # is in the same dir as the original yaml |
| 400 | if not os.path.isfile(path): |
| 401 | path = os.path.join(yaml_dir, path) |
| 402 | |
| 403 | try: |
| 404 | included_yaml_config = load_yaml_config(yaml_path=path, mode=mode) |
| 405 | final_yaml_config.update(included_yaml_config) |
| 406 | except Exception as ex: |
| 407 | # If failed to load, ignore |
| 408 | raise ex |
| 409 | |
| 410 | final_yaml_config.update(yaml_config) |
| 411 | return final_yaml_config |
| 412 | return yaml_config |
| 413 | |
| 414 | |
| 415 | def regex_replace(string, pattern, repl, count: int = 0): |
nothing calls this directly
no outgoing calls
no test coverage detected