From b227d25623fa1ae63b87393dba24cb146094468c Mon Sep 17 00:00:00 2001 From: NickTiny Date: Wed, 14 Jun 2023 17:04:54 -0400 Subject: [PATCH 1/6] Blender Kitsu: Dameon Thread for auto-login at start-up --- scripts-blender/addons/blender_kitsu/auth/ops.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/scripts-blender/addons/blender_kitsu/auth/ops.py b/scripts-blender/addons/blender_kitsu/auth/ops.py index 832b79aa..991a34ef 100644 --- a/scripts-blender/addons/blender_kitsu/auth/ops.py +++ b/scripts-blender/addons/blender_kitsu/auth/ops.py @@ -21,6 +21,7 @@ from typing import Dict, List, Set, Optional, Tuple, Any import bpy +import threading from blender_kitsu import cache, prefs, gazu @@ -123,6 +124,12 @@ def auto_login_on_file_open(): if not session.is_auth(): bpy.ops.kitsu.session_start() + +# 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 ----------. classes = [ @@ -139,7 +146,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(auto_login_on_file_open, first_interval=0.2) + bpy.app.timers.register(thread_login, first_interval=0.2) def unregister(): -- 2.30.2 From 41ecd4aabf223a100fd95dc975d432b5432b9a48 Mon Sep 17 00:00:00 2001 From: NickTiny Date: Thu, 15 Jun 2023 22:26:27 -0400 Subject: [PATCH 2/6] Blender Kitsu: Safely Create Threads during login --- .../addons/blender_kitsu/auth/ops.py | 62 +++++++++++++------ 1 file changed, 43 insertions(+), 19 deletions(-) diff --git a/scripts-blender/addons/blender_kitsu/auth/ops.py b/scripts-blender/addons/blender_kitsu/auth/ops.py index 991a34ef..659b9a05 100644 --- a/scripts-blender/addons/blender_kitsu/auth/ops.py +++ b/scripts-blender/addons/blender_kitsu/auth/ops.py @@ -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 ----------. -- 2.30.2 From 8a037fd2167924ea6c63dc56a604e78d3388898d Mon Sep 17 00:00:00 2001 From: NickTiny Date: Thu, 15 Jun 2023 22:47:56 -0400 Subject: [PATCH 3/6] Blender Kitsu: Move threading login into class --- .../addons/blender_kitsu/auth/ops.py | 70 ++++++++----------- 1 file changed, 28 insertions(+), 42 deletions(-) diff --git a/scripts-blender/addons/blender_kitsu/auth/ops.py b/scripts-blender/addons/blender_kitsu/auth/ops.py index 659b9a05..ee5b06fa 100644 --- a/scripts-blender/addons/blender_kitsu/auth/ops.py +++ b/scripts-blender/addons/blender_kitsu/auth/ops.py @@ -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(): -- 2.30.2 From 6e6777186624cdf9722420d6a5d1a87d1fc35cf0 Mon Sep 17 00:00:00 2001 From: NickTiny Date: Thu, 15 Jun 2023 22:50:31 -0400 Subject: [PATCH 4/6] Blender Kitsu: Session Start remove comment --- scripts-blender/addons/blender_kitsu/auth/ops.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/scripts-blender/addons/blender_kitsu/auth/ops.py b/scripts-blender/addons/blender_kitsu/auth/ops.py index ee5b06fa..478abc93 100644 --- a/scripts-blender/addons/blender_kitsu/auth/ops.py +++ b/scripts-blender/addons/blender_kitsu/auth/ops.py @@ -137,9 +137,6 @@ def auto_login_on_file_open(): bpy.ops.kitsu.session_start() -# TODO move threading logic into kitsu.session_start() so all logins are BG threaded - - # ---------REGISTER ----------. classes = [ -- 2.30.2 From ee7da5d8572348a688c2315ee2b9531fcb0f2075 Mon Sep 17 00:00:00 2001 From: NickTiny Date: Thu, 15 Jun 2023 23:09:57 -0400 Subject: [PATCH 5/6] Blender Kitsu: Session fix active thread logic --- scripts-blender/addons/blender_kitsu/auth/ops.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/scripts-blender/addons/blender_kitsu/auth/ops.py b/scripts-blender/addons/blender_kitsu/auth/ops.py index 478abc93..995dd32e 100644 --- a/scripts-blender/addons/blender_kitsu/auth/ops.py +++ b/scripts-blender/addons/blender_kitsu/auth/ops.py @@ -95,10 +95,11 @@ class KITSU_OT_session_start(bpy.types.Operator): 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() + active_thread._stop() + active_thread = threading.Thread( + target=self.kitsu_session_start(context), daemon=True + ) + active_thread.start() class KITSU_OT_session_end(bpy.types.Operator): -- 2.30.2 From ed2c588b525d3f24bbe32270ce9ab119c612e3bc Mon Sep 17 00:00:00 2001 From: NickTiny Date: Thu, 15 Jun 2023 23:16:44 -0400 Subject: [PATCH 6/6] Blender Kitsu: Session Start remove no-op code --- scripts-blender/addons/blender_kitsu/auth/ops.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/scripts-blender/addons/blender_kitsu/auth/ops.py b/scripts-blender/addons/blender_kitsu/auth/ops.py index 995dd32e..1783f844 100644 --- a/scripts-blender/addons/blender_kitsu/auth/ops.py +++ b/scripts-blender/addons/blender_kitsu/auth/ops.py @@ -22,7 +22,6 @@ from typing import Dict, List, Set, Optional, Tuple, Any import bpy import threading -import time from blender_kitsu import cache, prefs, gazu @@ -35,7 +34,6 @@ logger = LoggerFactory.getLogger() active_thread = False - class KITSU_OT_session_start(bpy.types.Operator): """ Starts the Session, which is stored in blender_kitsu addon preferences. @@ -137,7 +135,6 @@ def auto_login_on_file_open(): if not session.is_auth(): bpy.ops.kitsu.session_start() - # ---------REGISTER ----------. classes = [ -- 2.30.2