[Bf-extensions-cvs] [b35189e0] master: BlenderKit: Make oauth more thread-safe. -avoid blender crash in casae of calling hasattr(context, 'view_layer') and others. -autofix process

Vilem Duha noreply at git.blender.org
Thu Jun 13 00:23:13 CEST 2019


Commit: b35189e003e14301df22c264d6c762d6acaa9e2f
Author: Vilem Duha
Date:   Thu Jun 13 00:20:43 2019 +0200
Branches: master
https://developer.blender.org/rBAb35189e003e14301df22c264d6c762d6acaa9e2f

BlenderKit: Make oauth more thread-safe.
-avoid blender crash in casae of calling hasattr(context, 'view_layer') and others.
-autofix process

===================================================================

M	blenderkit/bkit_oauth.py
M	blenderkit/download.py
M	blenderkit/utils.py

===================================================================

diff --git a/blenderkit/bkit_oauth.py b/blenderkit/bkit_oauth.py
index 5350ae74..2b732bb1 100644
--- a/blenderkit/bkit_oauth.py
+++ b/blenderkit/bkit_oauth.py
@@ -42,15 +42,15 @@ PORTS = [62485, 1234]
 
 
 def login_thread(signup=False):
-    thread = threading.Thread(target=login, args=([signup]), daemon=True)
+    r_url = paths.get_oauth_landing_url()
+    url = paths.get_bkit_url()
+    thread = threading.Thread(target=login, args=([signup, url, r_url]), daemon=True)
     thread.start()
 
 
-def login(signup):
-    r_url = paths.get_oauth_landing_url()
-
-    authenticator = oauth.SimpleOAuthAuthenticator(server_url=paths.get_bkit_url(), client_id=CLIENT_ID, ports=PORTS)
-    auth_token, refresh_token = authenticator.get_new_token(register = signup, redirect_url=r_url)
+def login(signup, url, r_url):
+    authenticator = oauth.SimpleOAuthAuthenticator(server_url=url, client_id=CLIENT_ID, ports=PORTS)
+    auth_token, refresh_token = authenticator.get_new_token(register=signup, redirect_url=r_url)
     utils.p('tokens retrieved')
     tasks_queue.add_task((write_tokens, (auth_token, refresh_token)))
 
@@ -58,12 +58,13 @@ def login(signup):
 def refresh_token_thread():
     preferences = bpy.context.preferences.addons['blenderkit'].preferences
     if len(preferences.api_key_refresh) > 0:
-        thread = threading.Thread(target=refresh_token, args=([preferences.api_key_refresh]), daemon=True)
+        url = paths.get_bkit_url()
+        thread = threading.Thread(target=refresh_token, args=([preferences.api_key_refresh, url]), daemon=True)
         thread.start()
 
 
-def refresh_token(api_key_refresh):
-    authenticator = oauth.SimpleOAuthAuthenticator(server_url=paths.get_bkit_url(), client_id=CLIENT_ID, ports=PORTS)
+def refresh_token(api_key_refresh, url):
+    authenticator = oauth.SimpleOAuthAuthenticator(server_url=url, client_id=CLIENT_ID, ports=PORTS)
     auth_token, refresh_token = authenticator.get_refreshed_token(api_key_refresh)
     if auth_token is not None and refresh_token is not None:
         tasks_queue.add_task((write_tokens, (auth_token, refresh_token)))
diff --git a/blenderkit/download.py b/blenderkit/download.py
index 06342db6..647c15ce 100644
--- a/blenderkit/download.py
+++ b/blenderkit/download.py
@@ -544,7 +544,7 @@ class Downloader(threading.Thread):
         if check_existing(asset_data) and not tcom.passargs.get('delete'):
             # this sends the thread for processing, where another check should occur, since the file might be corrupted.
             tcom.downloaded = 100
-            print('not downloading, trying to append again')
+            utils.p('not downloading, trying to append again')
             return;
         file_name = paths.get_download_filenames(asset_data)[0]  # prefer global dir if possible.
         # for k in asset_data:
@@ -646,7 +646,7 @@ def check_existing(asset_data):
 
     file_names = paths.get_download_filenames(asset_data)
 
-    print('check if file allready exists')
+    utils.p('check if file allready exists')
     if len(file_names) == 2:
         # TODO this should check also for failed or running downloads.
         # If download is running, assign just the running thread. if download isn't running but the file is wrong size,
diff --git a/blenderkit/utils.py b/blenderkit/utils.py
index 2b13533f..c2c83648 100644
--- a/blenderkit/utils.py
+++ b/blenderkit/utils.py
@@ -180,7 +180,7 @@ def load_prefs():
 
 def save_prefs(self, context):
     # first check context, so we don't do this on registration or blender startup
-    if not bpy.app.background and hasattr(bpy.context, 'view_layer'):
+    if not bpy.app.background: #(hasattr kills blender)
         user_preferences = bpy.context.preferences.addons['blenderkit'].preferences
         # we test the api key for lenght, so not a random accidentaly typed sequence gets saved.
         lk = len(user_preferences.api_key)
@@ -200,7 +200,8 @@ def save_prefs(self, context):
         f = open(fpath, 'w')
         with open(fpath, 'w') as s:
             json.dump(prefs, s)
-        bpy.ops.wm.save_userpref()
+        # this was crashing blender 2.8 since some point, probably not needed since autosave is in preferences.
+        # bpy.ops.wm.save_userpref()
 
 
 def get_hidden_image(tpath, bdata_name, force_reload=False):



More information about the Bf-extensions-cvs mailing list