Blender Kitsu: Use Background Thread for Kitsu Login #79

Merged
Nick Alberelli merged 6 commits from fix/blender-kitsu-bg-threading into main 2023-06-16 17:44:21 +02:00
Showing only changes of commit 41ecd4aabf - Show all commits

View File

@ -22,6 +22,7 @@ from typing import Dict, List, Set, Optional, Tuple, Any
import bpy
import threading
import time
from blender_kitsu import cache, prefs, gazu
@ -32,6 +33,39 @@ from blender_kitsu.logger import LoggerFactory
logger = LoggerFactory.getLogger()
logged_in = False
active_thread = False
def get_config(context: bpy.types.Context) -> Dict[str, str]:
addon_prefs = prefs.addon_prefs_get(context)
return {
"email": addon_prefs.email,
"host": addon_prefs.host,
"passwd": addon_prefs.passwd,
}
def kitsu_session_start(context):
global logged_in
session = prefs.session_get(context)
session.set_config(get_config(context))
try:
session_data = session.start()
logged_in = True
# self.report({"INFO"}, f"Logged in as {session_data.user['full_name']}")
finally:
return logged_in
def thread_login():
global active_thread
if active_thread:
active_thread.kill()
context = bpy.context
x = threading.Thread(target=kitsu_session_start(context), daemon=True)
x.start()
class KITSU_OT_session_start(bpy.types.Operator):
"""
@ -53,14 +87,9 @@ class KITSU_OT_session_start(bpy.types.Operator):
return True
def execute(self, context: bpy.types.Context) -> Set[str]:
session = prefs.session_get(context)
session.set_config(self.get_config(context))
try:
session_data = session.start()
except gazu.exception.AuthFailedException:
global active_thread
thread_login()
if not logged_in:
self.report({"ERROR"}, "Login data not correct")
logger.error("Login data not correct")
return {"CANCELLED"}
@ -77,17 +106,11 @@ class KITSU_OT_session_start(bpy.types.Operator):
# Check frame range.
ops_playblast.load_post_handler_check_frame_range(None)
self.report({"INFO"}, f"Logged in as {session_data.user['full_name']}")
# TODO Fix reporting
# self.report({"INFO"}, f"Logged in as {session_data.user['full_name']}")
self.report({"INFO"}, f"Logged in!")
return {"FINISHED"}
def get_config(self, context: bpy.types.Context) -> Dict[str, str]:
addon_prefs = prefs.addon_prefs_get(context)
return {
"email": addon_prefs.email,
"host": addon_prefs.host,
"passwd": addon_prefs.passwd,
}
class KITSU_OT_session_end(bpy.types.Operator):
"""
@ -113,6 +136,9 @@ class KITSU_OT_session_end(bpy.types.Operator):
# Clear startup variables.
cache.clear_startup_variables()
global logged_in
logged_in = False
self.report({"INFO"}, "Logged out")
return {"FINISHED"}
@ -126,8 +152,6 @@ def auto_login_on_file_open():
# TODO move threading logic into kitsu.session_start() so all logins are BG threaded
def thread_login():
x = threading.Thread(target=auto_login_on_file_open, daemon=True)
# ---------REGISTER ----------.