import yaml import os from .util import pushd class ConfigException(Exception): pass class BaseConfig(object): def __init__(self, path): self.path = path if not os.path.exists(path): raise ConfigException("Base config file not found: {}".format(path)) with open(path) as f: self._data = yaml.load(f, Loader=yaml.SafeLoader) self.sources = [] with pushd(os.path.dirname(path)): for source in self["sources"]: self.sources.append(RepoConfig(self, source)) def __getitem__(self, name): return self._data[name] def __contains__(self, name): return name in self._data class RepoConfig(object): def __init__(self, config, path): self.path = path self.config = config if not os.path.exists(path): raise ConfigException("Repo config file not found: {}".format(path)) with open(path) as f: self._data = yaml.load(f, Loader=yaml.SafeLoader) def __getitem__(self, name): return self._data[name] def __contains__(self, name): return name in self._data