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.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. 'set': 'set_session_value',
  17. 'get': 'get_session_value',
  18. 'del': 'del_session_value'}
  19. def __init__(self, name, bases, attrs):
  20. super().__init__(name, bases, attrs)
  21. self._instance = None
  22. ##@brief Handles wrapper between class method and plugin loader functions
  23. def __getattribute__(self, name):
  24. instance = super().__getattribute__('_instance')
  25. if name in SessionPluginWrapper._ACTIONS:
  26. if instance is None:
  27. raise PluginError("Trying to access to SessionHandler \
  28. functions, but no session handler initialized")
  29. return getattr(
  30. instance.loader_module(),
  31. SessionPluginWrapper._ACTIONS[name])
  32. return super().__getattribute__(name)
  33. _glob_typename = 'session_handler'
  34. ##@brief Singleton class designed to handle session handler plugin
  35. #
  36. #@note This class is a singleton because only one session handler can be
  37. #loaded by instance
  38. class SessionHandlerPlugin(Plugin, metaclass=SessionPluginWrapper):
  39. ##@brief Stores the singleton instance
  40. _instance = None
  41. _plist_confspecs = {
  42. 'section': 'lodel2',
  43. 'key': 'session_handler',
  44. 'default': None,
  45. 'validator': SettingValidator(
  46. 'plugin', none_is_valid=False,ptype = _glob_typename)}
  47. _type_conf_name = _glob_typename
  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")
  54. ##@brief Clear class
  55. #@see plugins.Plugin::clear()
  56. @classmethod
  57. def clear_cls(cls):
  58. if cls._instance is not None:
  59. inst = cls._instance
  60. cls._instance = None
  61. del(inst)