2018-08-16 16:57:37 -05:00
# -*- coding: utf-8 -*-
""" A base class to be derived in possible new sessions for TWBlue and services. """
2018-11-22 13:35:19 -06:00
from __future__ import absolute_import
2019-06-06 11:52:23 -05:00
from __future__ import unicode_literals
from builtins import str
from builtins import object
import os
2018-08-16 16:57:37 -05:00
import paths
import output
import time
import sound
import logging
import config_utils
2021-06-23 13:40:21 -05:00
import sqlitedict
2018-08-16 16:57:37 -05:00
import application
import os
2018-11-22 13:35:19 -06:00
from . import session_exceptions as Exceptions
2018-08-16 16:57:37 -05:00
log = logging . getLogger ( " sessionmanager.session " )
2021-06-23 13:40:21 -05:00
2018-08-16 16:57:37 -05:00
class baseSession ( object ) :
2021-06-16 16:18:41 -05:00
""" toDo: Decorators does not seem to be working when using them in an inherited class. """
2018-08-16 16:57:37 -05:00
2021-06-16 16:18:41 -05:00
# Decorators.
2018-08-16 16:57:37 -05:00
2021-06-16 16:18:41 -05:00
def _require_login ( fn ) :
""" Decorator for checking if the user is logged in.
Some functions may need this to avoid making unneeded calls . """
def f ( self , * args , * * kwargs ) :
if self . logged == True :
fn ( self , * args , * * kwargs )
else :
raise Exceptions . NotLoggedSessionError ( " You are not logged in yet. " )
return f
2018-08-16 16:57:37 -05:00
2021-06-16 16:18:41 -05:00
def _require_configuration ( fn ) :
""" Check if the user has a configured session. """
def f ( self , * args , * * kwargs ) :
if self . settings != None :
fn ( self , * args , * * kwargs )
else :
raise Exceptions . NotConfiguredSessionError ( " Not configured. " )
return f
2018-08-16 16:57:37 -05:00
2021-06-16 16:18:41 -05:00
def __init__ ( self , session_id ) :
""" session_id (str): The name of the folder inside the config directory where the session is located. """
super ( baseSession , self ) . __init__ ( )
self . session_id = session_id
self . logged = False
self . settings = None
self . db = { }
2018-08-16 16:57:37 -05:00
2021-06-16 16:18:41 -05:00
@property
def is_logged ( self ) :
return self . logged
2018-08-16 16:57:37 -05:00
2021-06-16 16:18:41 -05:00
def get_configuration ( self ) :
""" Get settings for a session. """
file_ = " %s /session.conf " % ( self . session_id , )
log . debug ( " Creating config file %s " % ( file_ , ) )
self . settings = config_utils . load_config ( os . path . join ( paths . config_path ( ) , file_ ) , os . path . join ( paths . app_path ( ) , " Conf.defaults " ) )
self . init_sound ( )
2021-06-25 23:35:33 -05:00
self . load_persistent_data ( )
2018-08-16 16:57:37 -05:00
2021-06-16 16:18:41 -05:00
def init_sound ( self ) :
try : self . sound = sound . soundSystem ( self . settings [ " sound " ] )
except : pass
2018-08-16 16:57:37 -05:00
2021-06-16 16:18:41 -05:00
@_require_configuration
def login ( self , verify_credentials = True ) :
pass
2018-08-16 16:57:37 -05:00
2021-06-16 16:18:41 -05:00
@_require_configuration
def authorise ( self ) :
pass
2018-08-16 16:57:37 -05:00
2021-06-25 23:35:33 -05:00
def save_persistent_data ( self ) :
""" Save the data to a persistant sqlite backed file. . """
dbname = os . path . join ( paths . config_path ( ) , str ( self . session_id ) , " cache.db " )
# persist_size set to 0 means not saving data actually.
2021-06-16 16:18:41 -05:00
if self . settings [ " general " ] [ " persist_size " ] == 0 :
2021-06-25 23:35:33 -05:00
if os . path . exists ( dbname ) :
os . remove ( dbname )
2021-06-16 16:18:41 -05:00
return
try :
2021-06-23 13:40:21 -05:00
self . db . commit ( )
2021-06-16 16:18:41 -05:00
except :
2021-06-25 23:35:33 -05:00
output . speak ( _ ( " An exception occurred while saving the {app} database. It will be deleted and rebuilt automatically. If this error persists, send the error log to the {app} developers. " ) . format ( app = application . name ) , True )
log . exception ( " Exception while saving {} " . format ( dbname ) )
os . remove ( dbname )
2021-06-16 16:18:41 -05:00
2021-06-25 23:35:33 -05:00
def load_persistent_data ( self ) :
""" Import data from a database file from user config. """
dbname = os . path . join ( paths . config_path ( ) , str ( self . session_id ) , " cache.db " )
# If persist_size is set to 0, we should remove the db file as we are no longer going to save anything.
2021-06-16 16:18:41 -05:00
if self . settings [ " general " ] [ " persist_size " ] == 0 :
2021-06-25 23:35:33 -05:00
if os . path . exists ( dbname ) :
os . remove ( dbname )
# Let's return from here, as we are not loading anything.
2021-06-16 16:18:41 -05:00
return
2021-06-25 23:35:33 -05:00
# try to load the db file.
2021-06-16 16:18:41 -05:00
try :
2021-06-25 23:35:33 -05:00
self . db = sqlitedict . SqliteDict ( os . path . join ( paths . config_path ( ) , dbname ) , ' c ' )
2021-06-23 13:40:21 -05:00
if self . db . get ( " cursors " ) == None :
cursors = dict ( direct_messages = - 1 )
self . db [ " cursors " ] = cursors
2021-06-16 16:18:41 -05:00
except :
2021-06-25 23:35:33 -05:00
output . speak ( _ ( " An exception occurred while loading the {app} database. It will be deleted and rebuilt automatically. If this error persists, send the error log to the {app} developers. " ) . format ( app = application . name ) , True )
log . exception ( " Exception while loading {} " . format ( dbname ) )
2021-06-16 16:18:41 -05:00
try :
2021-06-25 23:35:33 -05:00
os . remove ( dbname )
2021-06-16 16:18:41 -05:00
except :
pass
2018-08-16 16:57:37 -05:00