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.
 
 
 
 
 

61 lines
1.8 KiB

  1. # -*- coding: utf-8 -*-
  2. import importlib
  3. import sys
  4. from types import ModuleType
  5. from flask import Blueprint
  6. from . import baseLogger
  7. __all__ = ["Module"]
  8. class Module:
  9. """Handles common operations for Calefaction's modular components."""
  10. def __init__(self, config, name):
  11. self._config = config
  12. self._name = name
  13. self._logger = baseLogger.getChild("module").getChild(name)
  14. self._module = None
  15. self._blueprint = None
  16. def __getattr__(self, attr):
  17. return getattr(self._module, attr)
  18. def _import(self, app):
  19. """Set up the environment for the module, then import it."""
  20. base = "calefaction.modules"
  21. fullname = base + "." + self._name
  22. self._blueprint = bp = Blueprint(self._name, fullname)
  23. bp.rroute = lambda *a, **kw: (lambda f: bp.route(*a, **kw)(
  24. app.catch_exceptions(app.route_restricted(f))))
  25. provided = ModuleType(base + "._provided")
  26. provided.__package__ = base
  27. provided.app = app
  28. provided.blueprint = bp
  29. provided.config = self._config.load_module_config(self._name)
  30. provided.logger = self._logger
  31. sys.modules[provided.__name__] = provided
  32. self._module = importlib.import_module(fullname)
  33. del sys.modules[provided.__name__]
  34. def install(self, app):
  35. """Load this module and register it with the application."""
  36. self._import(app)
  37. app.register_blueprint(self._blueprint)
  38. def navitem(self):
  39. """Return a navigation bar HTML snippet for this module, or None."""
  40. if hasattr(self._module, "navitem"):
  41. return self._module.navitem()
  42. def scopes(self):
  43. """Return a set of SSO scopes required by this module."""
  44. if hasattr(self._module, "SCOPES"):
  45. return set(self._module.SCOPES)
  46. return set()