import os import yaml class Loader(yaml.SafeLoader): def __init__(self, stream): self._root = os.path.split(stream.name)[0] super(Loader, self).__init__(stream) def include(self, node): filename = os.path.join(self._root, self.construct_scalar(node)) with open(filename, "r") as f: return yaml.load(f, Loader) Loader.add_constructor("!include", Loader.include) def merge_dicts(a, b): """Recursively merge dict b into dict a.""" for key, value in b.items(): if key in a and isinstance(a[key], dict) and isinstance(value, dict): merge_dicts(a[key], value) else: a[key] = value return a