refactor: Separate sessions into multiple files
This commit is contained in:
parent
97a740417b
commit
82a3e729f1
40
src/ankisyncd/sessions/__init__.py
Normal file
40
src/ankisyncd/sessions/__init__.py
Normal file
@ -0,0 +1,40 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import importlib
|
||||
import inspect
|
||||
from ankisyncd import logging
|
||||
from ankisyncd.sessions.simple_manager import SimpleSessionManager
|
||||
from ankisyncd.sessions.sqlite_manager import SqliteSessionManager
|
||||
|
||||
logger = logging.get_logger(__name__)
|
||||
|
||||
|
||||
def get_session_manager(config):
|
||||
if "session_db_path" in config and config["session_db_path"]:
|
||||
logger.info(
|
||||
"Found session_db_path in config, using SqliteSessionManager for auth"
|
||||
)
|
||||
return SqliteSessionManager(config["session_db_path"])
|
||||
elif "session_manager" in config and config["session_manager"]: # load from config
|
||||
logger.info(
|
||||
"Found session_manager in config, using {} for persisting sessions".format(
|
||||
config["session_manager"]
|
||||
)
|
||||
)
|
||||
|
||||
module_name, class_name = config["session_manager"].rsplit(".", 1)
|
||||
module = importlib.import_module(module_name.strip())
|
||||
class_ = getattr(module, class_name.strip())
|
||||
|
||||
if not SimpleSessionManager in inspect.getmro(class_):
|
||||
raise TypeError(
|
||||
""""session_manager" found in the conf file but it doesn''t
|
||||
inherit from SimpleSessionManager"""
|
||||
)
|
||||
return class_(config)
|
||||
else:
|
||||
logger.warning(
|
||||
"Neither session_db_path nor session_manager set, "
|
||||
"ankisyncd will lose sessions on application restart"
|
||||
)
|
||||
return SimpleSessionManager()
|
||||
19
src/ankisyncd/sessions/simple_manager.py
Normal file
19
src/ankisyncd/sessions/simple_manager.py
Normal file
@ -0,0 +1,19 @@
|
||||
class SimpleSessionManager:
|
||||
"""A simple session manager that keeps the sessions in memory."""
|
||||
|
||||
def __init__(self):
|
||||
self.sessions = {}
|
||||
|
||||
def load(self, hkey, session_factory=None):
|
||||
return self.sessions.get(hkey)
|
||||
|
||||
def load_from_skey(self, skey, session_factory=None):
|
||||
for i in self.sessions:
|
||||
if self.sessions[i].skey == skey:
|
||||
return self.sessions[i]
|
||||
|
||||
def save(self, hkey, session):
|
||||
self.sessions[hkey] = session
|
||||
|
||||
def delete(self, hkey):
|
||||
del self.sessions[hkey]
|
||||
@ -1,30 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import os
|
||||
import logging
|
||||
from sqlite3 import dbapi2 as sqlite
|
||||
|
||||
logger = logging.getLogger("ankisyncd.sessions")
|
||||
|
||||
|
||||
class SimpleSessionManager:
|
||||
"""A simple session manager that keeps the sessions in memory."""
|
||||
|
||||
def __init__(self):
|
||||
self.sessions = {}
|
||||
|
||||
def load(self, hkey, session_factory=None):
|
||||
return self.sessions.get(hkey)
|
||||
|
||||
def load_from_skey(self, skey, session_factory=None):
|
||||
for i in self.sessions:
|
||||
if self.sessions[i].skey == skey:
|
||||
return self.sessions[i]
|
||||
|
||||
def save(self, hkey, session):
|
||||
self.sessions[hkey] = session
|
||||
|
||||
def delete(self, hkey):
|
||||
del self.sessions[hkey]
|
||||
from ankisyncd.sessions.simple_manager import SimpleSessionManager
|
||||
|
||||
|
||||
class SqliteSessionManager(SimpleSessionManager):
|
||||
@ -128,36 +104,3 @@ class SqliteSessionManager(SimpleSessionManager):
|
||||
|
||||
cursor.execute(self.fs("DELETE FROM session WHERE hkey=?"), (hkey,))
|
||||
conn.commit()
|
||||
|
||||
|
||||
def get_session_manager(config):
|
||||
if "session_db_path" in config and config["session_db_path"]:
|
||||
logger.info(
|
||||
"Found session_db_path in config, using SqliteSessionManager for auth"
|
||||
)
|
||||
return SqliteSessionManager(config["session_db_path"])
|
||||
elif "session_manager" in config and config["session_manager"]: # load from config
|
||||
logger.info(
|
||||
"Found session_manager in config, using {} for persisting sessions".format(
|
||||
config["session_manager"]
|
||||
)
|
||||
)
|
||||
import importlib
|
||||
import inspect
|
||||
|
||||
module_name, class_name = config["session_manager"].rsplit(".", 1)
|
||||
module = importlib.import_module(module_name.strip())
|
||||
class_ = getattr(module, class_name.strip())
|
||||
|
||||
if not SimpleSessionManager in inspect.getmro(class_):
|
||||
raise TypeError(
|
||||
""""session_manager" found in the conf file but it doesn''t
|
||||
inherit from SimpleSessionManager"""
|
||||
)
|
||||
return class_(config)
|
||||
else:
|
||||
logger.warning(
|
||||
"Neither session_db_path nor session_manager set, "
|
||||
"ankisyncd will lose sessions on application restart"
|
||||
)
|
||||
return SimpleSessionManager()
|
||||
Loading…
Reference in New Issue
Block a user