config.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import yaml
  2. import os
  3. from glob import glob
  4. from slugify import slugify
  5. from .util import pushd
  6. class ConfigException(Exception):
  7. pass
  8. class BaseConfig(object):
  9. def __getitem__(self, name):
  10. return self._data[name]
  11. def __contains__(self, name):
  12. return name in self._data
  13. def __str__(self):
  14. return yaml.dump(self._data, default_flow_style=False)
  15. class ChildConfig(BaseConfig):
  16. def __init__(self, config, path):
  17. self.path = path
  18. self.config = config
  19. self.id = slugify(path, max_length=255)
  20. self.name = os.path.splitext(os.path.basename(path))[0]
  21. if not os.path.exists(path):
  22. raise ConfigException("Repo config file not found: {}".format(path))
  23. with open(path) as f:
  24. self._data = yaml.load(f, Loader=yaml.SafeLoader) or {}
  25. self._data['name'] = self.name
  26. class Config(BaseConfig):
  27. def __init__(self, path):
  28. self.path = os.path.join(path, 'pacman-repo.yml')
  29. if not os.path.exists(self.path):
  30. self.path = os.path.join(path, 'pacman-repo.yaml')
  31. if not os.path.exists(self.path):
  32. raise ConfigException("Base config file not found: {}".format(path))
  33. with open(self.path) as f:
  34. self._data = yaml.load(f, Loader=yaml.SafeLoader) or {}
  35. self.repos = []
  36. self.servers = []
  37. with pushd(path):
  38. for source in self["repos"]:
  39. for path in glob('{}/*.yml'.format(source)) + \
  40. glob('{}/*.yaml'.format(source)):
  41. self.repos.append(RepoConfig(self, path))
  42. for server in self["servers"]:
  43. for path in glob('{}/*.yml'.format(server)) + \
  44. glob('{}/*.yaml'.format(server)):
  45. self.servers.append(ServerConfig(self, path))
  46. class RepoConfig(ChildConfig):
  47. pass
  48. class ServerConfig(ChildConfig):
  49. pass