2014-11-12 20:41:29 -06:00
# -*- coding: utf-8 -*-
2022-03-24 10:39:20 -06:00
""" Module to perform session actions such as addition, removal or display of the global settings dialogue. """
2014-11-12 20:41:29 -06:00
import time
import os
2015-01-18 17:19:39 -06:00
import logging
2022-02-24 12:25:45 -06:00
import widgetUtils
2018-08-17 12:16:51 -05:00
import sessions
2022-02-24 12:25:45 -06:00
import output
import paths
2015-03-30 10:55:56 -06:00
import config_utils
2015-01-02 09:55:27 -06:00
import config
2022-02-24 12:25:45 -06:00
from pubsub import pub
2021-09-26 03:58:25 -05:00
from tweepy . errors import TweepyException
2022-02-24 12:25:45 -06:00
from controller import settings
from sessions . twitter import session as TwitterSession
from sessions . mastodon import session as MastodonSession
from . import manager
from . import wxUI as view
2015-01-18 17:19:39 -06:00
log = logging . getLogger ( " sessionmanager.sessionManager " )
2014-11-12 20:41:29 -06:00
class sessionManagerController ( object ) :
2022-02-24 12:25:45 -06:00
def __init__ ( self , started : bool = False ) :
""" Class constructor.
Creates the SessionManager class controller , responsible for the accounts within TWBlue . From this dialog , users can add / Remove accounts , or load the global settings dialog .
: param started : Indicates whether this object is being created during application startup ( when no other controller has been instantiated ) or not . It is important for us to know this , as we won ' t show the button to open global settings dialog if the application has been started. Users must choose the corresponding option in the menu bar.
: type started : bool
"""
2021-06-16 16:18:41 -05:00
super ( sessionManagerController , self ) . __init__ ( )
log . debug ( " Setting up the session manager. " )
self . started = started
2022-02-24 13:35:47 -06:00
# Initialize the manager, responsible for storing session objects.
2021-06-16 16:18:41 -05:00
manager . setup ( )
self . view = view . sessionManagerWindow ( )
2022-02-24 12:25:45 -06:00
pub . subscribe ( self . manage_new_account , " sessionmanager.new_account " )
pub . subscribe ( self . remove_account , " sessionmanager.remove_account " )
2021-06-16 16:18:41 -05:00
if self . started == False :
2022-02-24 12:25:45 -06:00
pub . subscribe ( self . configuration , " sessionmanager.configuration " )
2021-06-16 16:18:41 -05:00
else :
self . view . hide_configuration ( )
2022-02-24 12:25:45 -06:00
# Store a temporary copy of new and removed sessions, so we will perform actions on them during call to on_ok.
2021-06-16 16:18:41 -05:00
self . new_sessions = { }
self . removed_sessions = [ ]
2014-11-12 20:41:29 -06:00
2021-06-16 16:18:41 -05:00
def fill_list ( self ) :
2022-02-24 12:25:45 -06:00
""" Fills the session list with all valid sessions that could be found in config path. """
2021-06-16 16:18:41 -05:00
sessionsList = [ ]
reserved_dirs = [ " dicts " ]
log . debug ( " Filling the sessions list. " )
self . sessions = [ ]
for i in os . listdir ( paths . config_path ( ) ) :
if os . path . isdir ( os . path . join ( paths . config_path ( ) , i ) ) and i not in reserved_dirs :
log . debug ( " Adding session %s " % ( i , ) )
strconfig = " %s /session.conf " % ( os . path . join ( paths . config_path ( ) , i ) )
config_test = config_utils . load_config ( strconfig )
if len ( config_test ) == 0 :
try :
log . debug ( " Deleting session %s " % ( i , ) )
shutil . rmtree ( os . path . join ( paths . config_path ( ) , i ) )
continue
except :
output . speak ( " An exception was raised while attempting to clean malformed session data. See the error log for details. If this message persists, contact the developers. " , True )
os . exception ( " Exception thrown while removing malformed session " )
continue
2022-02-24 12:25:45 -06:00
if config_test . get ( " twitter " ) != None :
name = _ ( " {account_name} (Twitter) " ) . format ( account_name = config_test [ " twitter " ] [ " user_name " ] )
if config_test [ " twitter " ] [ " user_key " ] != " " and config_test [ " twitter " ] [ " user_secret " ] != " " :
sessionsList . append ( name )
2022-02-24 14:00:07 -06:00
self . sessions . append ( dict ( type = " twitter " , id = i ) )
2022-02-24 12:25:45 -06:00
elif config_test . get ( " mastodon " ) != None :
name = _ ( " {account_name} (Mastodon) " ) . format ( account_name = config_test [ " mastodon " ] [ " user_name " ] )
if config_test [ " mastodon " ] [ " instance " ] != " " and config_test [ " mastodon " ] [ " access_token " ] != " " :
sessionsList . append ( name )
2022-02-24 14:00:07 -06:00
self . sessions . append ( dict ( type = " mastodon " , id = i ) )
2021-06-16 16:18:41 -05:00
else :
try :
log . debug ( " Deleting session %s " % ( i , ) )
shutil . rmtree ( os . path . join ( paths . config_path ( ) , i ) )
except :
output . speak ( " An exception was raised while attempting to clean malformed session data. See the error log for details. If this message persists, contact the developers. " , True )
os . exception ( " Exception thrown while removing malformed session " )
self . view . fill_list ( sessionsList )
2014-11-12 20:41:29 -06:00
2021-06-16 16:18:41 -05:00
def show ( self ) :
2022-02-24 13:35:47 -06:00
""" Displays the session manager dialog. """
2021-06-16 16:18:41 -05:00
if self . view . get_response ( ) == widgetUtils . OK :
self . do_ok ( )
2015-04-03 16:57:08 -06:00
# else:
2021-06-16 16:18:41 -05:00
self . view . destroy ( )
2014-11-12 20:41:29 -06:00
2021-06-16 16:18:41 -05:00
def do_ok ( self ) :
log . debug ( " Starting sessions... " )
for i in self . sessions :
2022-02-24 14:00:07 -06:00
# Skip already created sessions. Useful when session manager controller is not created during startup.
if sessions . sessions . get ( i . get ( " id " ) ) != None :
continue
# Create the session object based in session type.
if i . get ( " type " ) == " twitter " :
s = TwitterSession . Session ( i . get ( " id " ) )
elif i . get ( " type " ) == " mastodon " :
s = MastodonSession . Session ( i . get ( " id " ) )
2021-06-16 16:18:41 -05:00
s . get_configuration ( )
2022-02-24 14:00:07 -06:00
if i . get ( " id " ) not in config . app [ " sessions " ] [ " ignored_sessions " ] :
2021-06-16 16:18:41 -05:00
try :
s . login ( )
2021-09-26 03:58:25 -05:00
except TweepyException :
2021-06-16 16:18:41 -05:00
self . show_auth_error ( s . settings [ " twitter " ] [ " user_name " ] )
continue
2022-02-24 14:00:07 -06:00
sessions . sessions [ i . get ( " id " ) ] = s
self . new_sessions [ i . get ( " id " ) ] = s
2015-04-03 16:57:08 -06:00
# self.view.destroy()
2014-11-12 20:41:29 -06:00
2021-06-16 16:18:41 -05:00
def show_auth_error ( self , user_name ) :
error = view . auth_error ( user_name )
2020-05-29 11:17:18 -05:00
2022-02-24 12:25:45 -06:00
def manage_new_account ( self , type ) :
# Generic settings for all account types.
location = ( str ( time . time ( ) ) [ - 6 : ] )
log . debug ( " Creating %s session in the %s path " % ( type , location ) )
if type == " twitter " :
s = TwitterSession . Session ( location )
elif type == " mastodon " :
s = MastodonSession . Session ( location )
manager . manager . add_session ( location )
s . get_configuration ( )
s . authorise ( )
2022-02-24 14:00:07 -06:00
self . sessions . append ( dict ( id = location , type = type ) )
2022-02-24 12:25:45 -06:00
self . view . add_new_session_to_list ( )
s . settings . write ( )
2015-04-03 16:57:08 -06:00
2022-02-24 12:25:45 -06:00
def remove_account ( self , index ) :
selected_account = self . sessions [ index ]
self . view . remove_session ( index )
2022-02-24 14:00:07 -06:00
self . removed_sessions . append ( selected_account . get ( " id " ) )
2022-02-24 12:25:45 -06:00
self . sessions . remove ( selected_account )
2022-02-24 14:00:07 -06:00
shutil . rmtree ( path = os . path . join ( paths . config_path ( ) , selected_account . get ( " id " ) ) , ignore_errors = True )
2015-04-22 15:34:44 -05:00
2022-02-24 12:25:45 -06:00
def configuration ( self ) :
2021-06-16 16:18:41 -05:00
""" Opens the global settings dialogue. """
d = settings . globalSettingsController ( )
if d . response == widgetUtils . OK :
d . save_configuration ( )