A corporation manager and dashboard for EVE Online
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

108 lines
3.1 KiB

  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, basedir):
  23. self._dir = basedir
  24. self._filename = basedir / "config" / "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 dir(self):
  48. """Return the application's base directory."""
  49. return self._dir
  50. @property
  51. def modules(self):
  52. """Return a list-like object (a _ModuleIndex) of loaded modules."""
  53. return self._modules
  54. @property
  55. def scheme(self):
  56. """Return the site's configured scheme, either "http" or "https"."""
  57. return "https" if self.get("site.https") else "http"
  58. def install(self, app):
  59. """Install relevant config into the application, including modules."""
  60. app.config["SERVER_NAME"] = self.get("site.canonical")
  61. app.config["PREFERRED_URL_SCHEME"] = self.scheme
  62. app.config["MAKO_MODULE_DIRECTORY"] = str(
  63. self._dir / "templates" / ".cache")
  64. app.secret_key = self.get("auth.session_key")
  65. for module in self.modules:
  66. module.install(app)
  67. def load_module_config(self, name):
  68. """Load and return a module config file.
  69. Returns a YAML parse of {confdir}/modules/{name}.yml, or None.
  70. """
  71. filename = self._dir / "config" / "modules" / (name + ".yml")
  72. try:
  73. with filename.open("rb") as fp:
  74. return yaml.load(fp)
  75. except FileNotFoundError:
  76. return None
  77. def collect_scopes(self):
  78. """Return a list of SSO scopes required for all/regular users."""
  79. scopes = set()
  80. for module in self.modules:
  81. scopes |= module.scopes()
  82. if not scopes:
  83. scopes.add("publicData")
  84. return sorted(list(scopes))