Lazy init session storage and deprecate enable_session
from __future__ import absolute_import, unicode_literals
import six
+import warnings
import werobot
@@ -41,34 +42,39 @@ class BaseRoBot(object):
session_storage = ConfigAttribute("SESSION_STORAGE")
def __init__(self, token=None, logger=None,
- enable_session=True, session_storage=None,
+ enable_session=None, session_storage=None,
app_id=None, app_secret=None, encoding_aes_key=None,
**kwargs):
- self.config = Config(_DEFAULT_CONFIG)
self._handlers = dict((k, []) for k in self.message_types)
self._handlers['all'] = []
self.make_error_page = make_error_page
+
if logger is None:
import werobot.logger
logger = werobot.logger.logger
self.logger = logger
- if enable_session and session_storage is None:
- from .session.sqlitestorage import SQLiteStorage
- session_storage = SQLiteStorage()
+ self.config = Config(_DEFAULT_CONFIG)
self.config.update(
TOKEN=token,
- SESSION_STORAGE=session_storage,
APP_ID=app_id,
APP_SECRET=app_secret,
ENCODING_AES_KEY=encoding_aes_key
)
-
- self.use_encryption = False
-
for k, v in kwargs.items():
self.config[k.upper()] = v
+ if enable_session is not None:
+ warnings.warn(
+ "enable_session is deprecated. set SESSION_STORAGE to False if you want to disable Session",
+ DeprecationWarning,
+ stacklevel=2
+ )
+ if not enable_session:
+ self.config["SESSION_STORAGE"] = False
+
+ self.use_encryption = False
+
@property
def crypto(self):
if hasattr(self, "_crypto"):
@@ -102,6 +108,27 @@ class BaseRoBot(object):
self._client = Client(self.config)
return self._client
+ @property
+ def session_storage(self):
+ if hasattr(self, "_session_storage"):
+ return self._session_storage
+ if self.config["SESSION_STORAGE"] is False:
+ return None
+ if not self.config["SESSION_STORAGE"]:
+ from .session.sqlitestorage import SQLiteStorage
+ self.config["SESSION_STORAGE"] = SQLiteStorage()
+ self._session_storage = self.config["SESSION_STORAGE"]
+ return self._session_storage
+
+ @session_storage.setter
+ def session_storage(self, value):
+ warnings.warn(
+ "You should set session storage in config",
+ DeprecationWarning,
+ stacklevel=2
+ )
+ self.config["SESSION_STORAGE"] = value
+
def handler(self, f):
"""
Decorator to add a handler function for every messages
