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 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #
  2. # This file is part of Lodel 2 (https://github.com/OpenEdition)
  3. #
  4. # Copyright (C) 2015-2017 Cléo UMS-3287
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as published
  8. # by the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. from lodel.context import LodelContext
  20. LodelContext.expose_modules(globals(), {
  21. 'lodel.plugin.plugins': ['Plugin', 'MetaPlugType'],
  22. 'lodel.plugin.exceptions': ['PluginError', 'PluginTypeError',
  23. 'LodelScriptError', 'DatasourcePluginError'],
  24. 'lodel.validator.validator': ['Validator']})
  25. ##@brief SessionHandlerPlugin metaclass designed to implements a wrapper
  26. #between SessionHandlerPlugin classmethod and plugin loader functions
  27. class SessionPluginWrapper(MetaPlugType):
  28. ##@brief Constant that stores all possible session actions
  29. #
  30. #Key is the SessionHandlerPlugin method name and value is SessionHandler
  31. #plugin function name
  32. _ACTIONS = {
  33. 'start': 'start_session',
  34. 'destroy': 'destroy_session',
  35. 'restore': 'restore_session',
  36. 'save': 'save_session',
  37. 'set': 'set_session_value',
  38. 'get': 'get_session_value',
  39. 'del': 'del_session_value'}
  40. def __init__(self, name, bases, attrs):
  41. super().__init__(name, bases, attrs)
  42. self._instance = None
  43. ##@brief Handles wrapper between class method and plugin loader functions
  44. def __getattribute__(self, name):
  45. instance = super().__getattribute__('_instance')
  46. if name in SessionPluginWrapper._ACTIONS:
  47. if instance is None:
  48. raise PluginError("Trying to access to SessionHandler \
  49. functions, but no session handler initialized")
  50. return getattr(
  51. instance.loader_module(),
  52. SessionPluginWrapper._ACTIONS[name])
  53. return super().__getattribute__(name)
  54. _glob_typename = 'session_handler'
  55. ##@brief Singleton class designed to handle session handler plugin
  56. #
  57. #@note This class is a singleton because only one session handler can be
  58. #loaded by instance
  59. class SessionHandlerPlugin(Plugin, metaclass=SessionPluginWrapper):
  60. ##@brief Stores the singleton instance
  61. _instance = None
  62. _plist_confspecs = {
  63. 'section': 'lodel2',
  64. 'key': 'session_handler',
  65. 'default': None,
  66. 'validator': Validator(
  67. 'plugin', none_is_valid=False,ptype = _glob_typename)}
  68. _type_conf_name = _glob_typename
  69. def __init__(self, plugin_name):
  70. if self._instance is None:
  71. super().__init__(plugin_name)
  72. self.__class__._instance = self
  73. else:
  74. raise RuntimeError("A SessionHandler Plugin is already plug")
  75. ##@brief Clear class
  76. #@see plugins.Plugin::clear()
  77. @classmethod
  78. def clear_cls(cls):
  79. if cls._instance is not None:
  80. inst = cls._instance
  81. cls._instance = None
  82. del(inst)