BlenderKit: Make oauth more thread-safe.

-avoid blender crash in casae of calling hasattr(context, 'view_layer') and others.
-autofix process
This commit is contained in:
Vilem Duha 2019-06-13 00:20:43 +02:00
parent 05f8a2118f
commit b35189e003
3 changed files with 15 additions and 13 deletions

View File

@ -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)))

View File

@ -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,

View File

@ -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):