diff options
author | Vilem Duha <vilem.duha@gmail.com> | 2019-06-13 01:20:43 +0300 |
---|---|---|
committer | Vilem Duha <vilem.duha@gmail.com> | 2019-06-13 01:22:31 +0300 |
commit | b35189e003e14301df22c264d6c762d6acaa9e2f (patch) | |
tree | f41d24b460cc2276e63b0070482fb1f98ac06e2e /blenderkit | |
parent | 05f8a2118ff12b966f34acd08d1bc3a9c0b7c96a (diff) |
BlenderKit: Make oauth more thread-safe.
-avoid blender crash in casae of calling hasattr(context, 'view_layer') and others.
-autofix process
Diffstat (limited to 'blenderkit')
-rw-r--r-- | blenderkit/bkit_oauth.py | 19 | ||||
-rw-r--r-- | blenderkit/download.py | 4 | ||||
-rw-r--r-- | blenderkit/utils.py | 5 |
3 files changed, 15 insertions, 13 deletions
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): |