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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from lodel.context import LodelContext
  2. LodelContext.expose_modules(globals(), {
  3. 'lodel.plugin.plugins': ['Plugin', 'MetaPlugType'],
  4. 'lodel.plugin.exceptions': ['PluginError', 'PluginTypeError',
  5. 'LodelScriptError', 'DatasourcePluginError'],
  6. 'lodel.validator.validator': ['Validator']})
  7. ##@brief SessionHandlerPlugin metaclass designed to implements a wrapper
  8. #between SessionHandlerPlugin classmethod and plugin loader functions
  9. class SessionPluginWrapper(MetaPlugType):
  10. ##@brief Constant that stores all possible session actions
  11. #
  12. #Key is the SessionHandlerPlugin method name and value is SessionHandler
  13. #plugin function name
  14. _ACTIONS = {
  15. 'start': 'start_session',
  16. 'destroy': 'destroy_session',
  17. 'restore': 'restore_session',
  18. 'save': 'save_session',
  19. 'set': 'set_session_value',
  20. 'get': 'get_session_value',
  21. 'del': 'del_session_value'}
  22. def __init__(self, name, bases, attrs):
  23. super().__init__(name, bases, attrs)
  24. self._instance = None
  25. ##@brief Handles wrapper between class method and plugin loader functions
  26. def __getattribute__(self, name):
  27. instance = super().__getattribute__('_instance')
  28. if name in SessionPluginWrapper._ACTIONS:
  29. if instance is None:
  30. raise PluginError("Trying to access to SessionHandler \
  31. functions, but no session handler initialized")
  32. return getattr(
  33. instance.loader_module(),
  34. SessionPluginWrapper._ACTIONS[name])
  35. return super().__getattribute__(name)
  36. _glob_typename = 'session_handler'
  37. ##@brief Singleton class designed to handle session handler plugin
  38. #
  39. #@note This class is a singleton because only one session handler can be
  40. #loaded by instance
  41. class SessionHandlerPlugin(Plugin, metaclass=SessionPluginWrapper):
  42. ##@brief Stores the singleton instance
  43. _instance = None
  44. _plist_confspecs = {
  45. 'section': 'lodel2',
  46. 'key': 'session_handler',
  47. 'default': None,
  48. 'validator': Validator(
  49. 'plugin', none_is_valid=False,ptype = _glob_typename)}
  50. _type_conf_name = _glob_typename
  51. def __init__(self, plugin_name):
  52. if self._instance is None:
  53. super().__init__(plugin_name)
  54. self.__class__._instance = self
  55. else:
  56. raise RuntimeError("A SessionHandler Plugin is already plug")
  57. ##@brief Clear class
  58. #@see plugins.Plugin::clear()
  59. @classmethod
  60. def clear_cls(cls):
  61. if cls._instance is not None:
  62. inst = cls._instance
  63. cls._instance = None
  64. del(inst)