A corporation manager and dashboard for EVE Online
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

config.py 3.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # -*- coding: utf-8 -*-
  2. import yaml
  3. from .module import Module
  4. __all__ = ["Config"]
  5. class _ModuleIndex(list):
  6. """List class that supports attribute access to its elements by key."""
  7. def __init__(self):
  8. super().__init__()
  9. self._index = {}
  10. def __getitem__(self, key):
  11. try:
  12. return super().__getitem__(key)
  13. except TypeError:
  14. return super().__getitem__(self._index[key])
  15. def __getattr__(self, attr):
  16. return self[self._index[attr]]
  17. def append(self, key, value):
  18. self._index[key] = len(self)
  19. super().append(value)
  20. class Config:
  21. """Stores application-wide configuration info."""
  22. def __init__(self, confdir):
  23. self._dir = confdir
  24. self._filename = confdir / "config.yml"
  25. self._data = None
  26. self._modules = _ModuleIndex()
  27. self._load()
  28. def _load(self):
  29. """Load config from the config file."""
  30. with self._filename.open("rb") as fp:
  31. self._data = yaml.load(fp)
  32. self._modules = _ModuleIndex()
  33. for name in self.get("modules.enabled", []):
  34. self._modules.append(name, Module(self, name))
  35. def get(self, key="", default=None):
  36. """Acts like a dict lookup in the config file.
  37. Dots can be used to separate keys. For example,
  38. config["foo.bar"] == config["foo"]["bar"].
  39. """
  40. obj = self._data
  41. for item in key.split("."):
  42. if item not in obj:
  43. return default
  44. obj = obj[item]
  45. return obj
  46. @property
  47. def modules(self):
  48. """Return a list-like object (a _ModuleIndex) of loaded modules."""
  49. return self._modules
  50. @property
  51. def scheme(self):
  52. """Return the site's configured scheme, either "http" or "https"."""
  53. return "https" if self.get("site.https") else "http"
  54. def install(self, app):
  55. """Install relevant config into the application, including modules."""
  56. app.config["SERVER_NAME"] = self.get("site.canonical")
  57. app.config["PREFERRED_URL_SCHEME"] = self.scheme
  58. app.config["MAKO_MODULE_DIRECTORY"] = str(
  59. self._dir.parent / "templates" / ".cache")
  60. app.secret_key = self.get("auth.session_key")
  61. for module in self.modules:
  62. module.install(app)
  63. def load_module_config(self, name):
  64. """Load and return a module config file.
  65. Returns a YAML parse of {confdir}/modules/{name}.yml, or None.
  66. """
  67. filename = self._dir / "modules" / (name + ".yml")
  68. try:
  69. with filename.open("rb") as fp:
  70. return yaml.load(fp)
  71. except FileNotFoundError:
  72. return None
  73. def collect_scopes(self):
  74. """Return a list of SSO scopes required for all/regular users."""
  75. scopes = set()
  76. for module in self.modules:
  77. scopes |= module.scopes()
  78. if not scopes:
  79. scopes.add("publicData")
  80. return sorted(list(scopes))