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 8a037fd216 - Show all commits

View File

@ -33,40 +33,9 @@ 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):
"""
Starts the Session, which is stored in blender_kitsu addon preferences.
@ -87,9 +56,8 @@ class KITSU_OT_session_start(bpy.types.Operator):
return True
def execute(self, context: bpy.types.Context) -> Set[str]:
global active_thread
thread_login()
if not logged_in:
self.thread_login(context)
if not prefs.session_get(context).is_auth():
self.report({"ERROR"}, "Login data not correct")
logger.error("Login data not correct")
return {"CANCELLED"}
@ -105,12 +73,33 @@ class KITSU_OT_session_start(bpy.types.Operator):
# Check frame range.
ops_playblast.load_post_handler_check_frame_range(None)
# 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,
}
def kitsu_session_start(self, context):
session = prefs.session_get(context)
session.set_config(self.get_config(context))
try:
session_data = session.start()
self.report({"INFO"}, f"Logged in as {session_data.user['full_name']}")
finally:
return
def thread_login(self, context):
global active_thread
if active_thread:
active_thread.kill()
context = bpy.context
x = threading.Thread(target=self.kitsu_session_start(context), daemon=True)
x.start()
class KITSU_OT_session_end(bpy.types.Operator):
"""
@ -136,9 +125,6 @@ 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"}
@ -170,7 +156,7 @@ def register():
# it automatically un-registers after it runs.
# FIXME: XXX This makes Blender hang if there is no Internet connectivity
# TODO: Rewrite this, so the 'auto' login happens out of the main thread
bpy.app.timers.register(thread_login, first_interval=0.2)
bpy.app.timers.register(auto_login_on_file_open, first_interval=0.2)
def unregister():