2014-10-27 16:29:04 -06:00
|
|
|
# -*- coding: cp1252 -*-
|
2022-03-24 10:39:20 -06:00
|
|
|
""" Lightweigth module that saves session position across global config and performs validation of config files. """
|
2014-10-27 16:29:04 -06:00
|
|
|
import os
|
2015-01-18 17:19:39 -06:00
|
|
|
import logging
|
2022-03-24 10:39:20 -06:00
|
|
|
import config
|
|
|
|
import paths
|
2015-01-18 17:19:39 -06:00
|
|
|
log = logging.getLogger("sessionmanager.manager")
|
2018-08-17 05:12:49 -05:00
|
|
|
from sessions import session_exceptions
|
2014-10-27 16:29:04 -06:00
|
|
|
|
|
|
|
manager = None
|
|
|
|
def setup():
|
2022-03-24 10:39:20 -06:00
|
|
|
""" Creates the singleton instance used within TWBlue to access this object. """
|
2021-06-16 16:18:41 -05:00
|
|
|
global manager
|
|
|
|
if not manager:
|
|
|
|
manager = sessionManager()
|
2014-10-27 16:29:04 -06:00
|
|
|
|
|
|
|
class sessionManager(object):
|
|
|
|
|
2021-06-16 16:18:41 -05:00
|
|
|
def get_current_session(self):
|
2022-03-24 10:39:20 -06:00
|
|
|
""" Returns the currently focused session, if valid. """
|
2021-06-16 16:18:41 -05:00
|
|
|
if self.is_valid(config.app["sessions"]["current_session"]):
|
|
|
|
return config.app["sessions"]["current_session"]
|
2014-10-27 16:29:04 -06:00
|
|
|
|
2021-06-16 16:18:41 -05:00
|
|
|
def set_current_session(self, sessionID):
|
|
|
|
config.app["sessions"]["current_session"] = sessionID
|
|
|
|
config.app.write()
|
2014-10-27 16:29:04 -06:00
|
|
|
|
2021-06-16 16:18:41 -05:00
|
|
|
def is_valid(self, id):
|
|
|
|
if not os.path.exists(os.path.join(paths.config_path(), id)):
|
|
|
|
raise session_exceptions.NonExistentSessionError("That session does not exist.")
|
|
|
|
config.app["sessions"]["current_session"] = ""
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|