config.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import yaml
  2. import os
  3. from .util import pushd
  4. class ConfigException(Exception):
  5. pass
  6. class BaseConfig(object):
  7. def __init__(self, path):
  8. self.path = path
  9. if not os.path.exists(path):
  10. raise ConfigException("Base config file not found: {}".format(path))
  11. with open(path) as f:
  12. self._data = yaml.load(f, Loader=yaml.SafeLoader)
  13. self.sources = []
  14. with pushd(os.path.dirname(path)):
  15. for source in self["sources"]:
  16. self.sources.append(RepoConfig(self, source))
  17. def __getitem__(self, name):
  18. return self._data[name]
  19. def __contains__(self, name):
  20. return name in self._data
  21. class RepoConfig(object):
  22. def __init__(self, config, path):
  23. self.path = path
  24. self.config = config
  25. if not os.path.exists(path):
  26. raise ConfigException("Repo config file not found: {}".format(path))
  27. with open(path) as f:
  28. self._data = yaml.load(f, Loader=yaml.SafeLoader)
  29. def __getitem__(self, name):
  30. return self._data[name]
  31. def __contains__(self, name):
  32. return name in self._data