No Description
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.

sessionhandler.py 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from .plugins import Plugin, MetaPlugType
  2. from .exceptions import *
  3. from lodel.settings.validator import SettingValidator
  4. ##@brief SessionHandlerPlugin metaclass designed to implements a wrapper
  5. #between SessionHandlerPlugin classmethod and plugin loader functions
  6. class SessionPluginWrapper(MetaPlugType):
  7. ##@brief Constant that stores all possible session actions
  8. #
  9. #Key is the SessionHandlerPlugin method name and value is SessionHandler
  10. #plugin function name
  11. _ACTIONS = {
  12. 'start': 'start_session',
  13. 'destroy': 'destroy_session',
  14. 'restore': 'restore_session',
  15. 'save': 'save_session'}
  16. def __init__(self, name, bases, attrs):
  17. super().__init__(name, bases, attrs)
  18. self._instance = None
  19. ##@brief Handles wrapper between class method and plugin loader functions
  20. def __getattribute__(self, name):
  21. instance = super().__getattribute__('_instance')
  22. if name in SessionPluginWrapper._ACTIONS:
  23. if instance is None:
  24. raise PluginError("Trying to access to SessionHandler \
  25. functions, but no session handler initialized")
  26. return getattr(
  27. instance.loader_module(),
  28. SessionPluginWrapper._ACTIONS[name])
  29. return super().__getattribute__(name)
  30. ##@page lodel2_plugins Lodel2 plugins system
  31. #
  32. # @par Plugin structure
  33. #A plugin is a package (a folder containing, at least, an __init__.py file.
  34. #This file should expose multiple things :
  35. # - a CONFSPEC variable containing configuration specifications
  36. # - an _activate() method that returns True if the plugin can be activated (
  37. # optionnal)
  38. #
  39. class SessionHandlerPlugin(Plugin, metaclass=SessionPluginWrapper):
  40. ##@brief Stores the singleton instance
  41. _instance = None
  42. _plist_confspecs = {
  43. 'section': 'lodel2',
  44. 'key': 'session_handler',
  45. 'default': None,
  46. 'validator': SettingValidator('string', none_is_valid=False)}
  47. _type_conf_name = 'session_handler'
  48. def __init__(self, plugin_name):
  49. if self._instance is None:
  50. super().__init__(plugin_name)
  51. self.__class__._instance = self
  52. else:
  53. raise RuntimeError("A SessionHandler Plugin is already plug")