Skip to content

Commit 0135ff9

Browse files
committed
Merge pull request #3 from smathot/master
Make Unicode safe
2 parents 2f6945b + aa8ce9d commit 0135ff9

File tree

5 files changed

+75
-89
lines changed

5 files changed

+75
-89
lines changed

QOpenScienceFramework/__init__.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
__version__ = "1.0.3"
22
__author__ = "Daniel Schreij"
33

4+
import os
5+
import sys
6+
from QOpenScienceFramework.compat import *
7+
dirname = safe_decode(os.path.dirname(__file__),
8+
enc=sys.getfilesystemencoding())
9+
410
import QOpenScienceFramework.connection
511
import QOpenScienceFramework.manager
6-
import QOpenScienceFramework.widgets
12+
import QOpenScienceFramework.widgets

QOpenScienceFramework/connection.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from __future__ import division
1212
from __future__ import print_function
1313
from __future__ import unicode_literals
14+
from QOpenScienceFramework.compat import *
1415

1516
# Import basics
1617
import os
@@ -27,9 +28,10 @@
2728
from oauthlib.oauth2 import MobileApplicationClient
2829
# Easier function decorating
2930
from functools import wraps
31+
from QOpenScienceFramework import dirname
3032

3133
# Load settings file containing required OAuth2 parameters
32-
with open(os.path.join(os.path.dirname(__file__), 'settings.json')) as fp:
34+
with open(os.path.join(dirname, 'settings.json')) as fp:
3335
settings = json.load(fp)
3436
base_url = settings['base_url']
3537
api_base_url = settings['api_base_url']
@@ -49,7 +51,7 @@ class OSFInvalidResponse(Exception):
4951
def create_session():
5052
""" Creates/resets and OAuth 2 session, with the specified data. """
5153
global session
52-
global settings
54+
global settings
5355

5456
try:
5557
client_id = settings['client_id']
@@ -107,7 +109,7 @@ def check_for_active_session():
107109
if session is None:
108110
raise RuntimeError("Session is not yet initialized, use connection."
109111
"session = connection.create_session()")
110-
112+
111113

112114
#%%--------------------------- Oauth communiucation ----------------------------
113115

@@ -256,9 +258,3 @@ def direct_api_call(api_call):
256258

257259
if __name__ == "__main__":
258260
print(get_authorization_url())
259-
260-
261-
262-
263-
264-

QOpenScienceFramework/loginwindow.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030
import QOpenScienceFramework.connection as osf
3131
# Python 2 and 3 compatiblity settings
3232
from QOpenScienceFramework.compat import *
33+
from QOpenScienceFramework import dirname
3334

34-
osf_logo_path = os.path.join(os.path.dirname(__file__), 'img/cos-white2.png')
35+
osf_logo_path = os.path.join(dirname, 'img/cos-white2.png')
3536

3637
# Dummy function later to be replaced for translation
3738
_ = lambda s: s
@@ -96,7 +97,7 @@ def checkResponse(self, reply):
9697
self.token = osf.parse_token_from_url(r_url)
9798
except ValueError as e:
9899
logging.warning(e)
99-
else:
100+
else:
100101
self.logged_in.emit()
101102
self.hide()
102103

@@ -117,6 +118,6 @@ def check_URL(self, url):
117118
self.token = osf.parse_token_from_url(url_string)
118119
except ValueError as e:
119120
logging.warning(e)
120-
else:
121+
else:
121122
self.logged_in.emit()
122123
self.hide()

QOpenScienceFramework/manager.py

+21-22
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,9 @@ def check_for_stored_token(self, tokenfile):
198198
def check_network_accessibility(func):
199199
""" Decorator function, not to be called directly.
200200
Checks if network is accessible and buffers the network request so
201-
that it can be sent again if it fails the first time, due to an invalidated
202-
OAuth2 token. In this case the user will be presented with the login
203-
screen again. If the same user successfully logs in again, the request
201+
that it can be sent again if it fails the first time, due to an invalidated
202+
OAuth2 token. In this case the user will be presented with the login
203+
screen again. If the same user successfully logs in again, the request
204204
will be resent. """
205205

206206
@wraps(func)
@@ -220,7 +220,7 @@ def func_wrapper(inst, *args, **kwargs):
220220
# Add tuple with current user, and request to be performed
221221
# to the pending request dictionary
222222
inst.pending_requests[request_id] = (
223-
inst.logged_in_user['data']['id'],
223+
inst.logged_in_user['data']['id'],
224224
current_request)
225225
# Add current request id to kwargs of function being called
226226
kwargs['_request_id'] = request_id
@@ -271,7 +271,7 @@ def __check_request_parameters(self, url, callback):
271271
if not isinstance(url, QtCore.QUrl) and not isinstance(url, basestring):
272272
raise TypeError("url should be a string or QUrl object")
273273
if not isinstance(url, QtCore.QUrl):
274-
url = get_QUrl(url)
274+
url = QtCore.QUrl(url)
275275
if not callable(callback):
276276
raise TypeError("callback should be a function or callable.")
277277
return url
@@ -322,7 +322,7 @@ def get(self, url, callback, *args, **kwargs):
322322
if not self.add_token(request):
323323
self.warning_message.emit('Warning',
324324
_(u"Token could not be added to the request"))
325-
325+
326326
# Check if this is a redirect and keep a count to prevent endless
327327
# redirects. If redirect_count is not set, init it to 0
328328
kwargs['redirect_count'] = kwargs.get('redirect_count',0)
@@ -681,10 +681,10 @@ def download_file(self, url, destination, *args, **kwargs):
681681
kwargs['destination'] = destination
682682
kwargs['download_url'] = url
683683
# Extra call to get() to make sure OAuth2 token is still valid before download
684-
# is initiated. If not, this way the request can be repeated after the user
684+
# is initiated. If not, this way the request can be repeated after the user
685685
# reauthenticates
686686
self.get_logged_in_user(self.__download, *args, **kwargs)
687-
687+
688688
def upload_file(self, url, source_file, *args, **kwargs):
689689
""" Upload a file to the specified destination on the OSF
690690
@@ -716,7 +716,7 @@ def upload_file(self, url, source_file, *args, **kwargs):
716716
Any other keywoard arguments that you want to have passed to the callback
717717
"""
718718
# Extra call to get() to make sure OAuth2 token is still valid before download
719-
# is initiated. If not, this way the request can be repeated after the user
719+
# is initiated. If not, this way the request can be repeated after the user
720720
# reauthenticates
721721
kwargs['upload_url'] = url
722722
kwargs['source_file'] = source_file
@@ -730,7 +730,7 @@ def __reply_finished(self, callback, *args, **kwargs):
730730
# Get the error callback function, if set
731731
errorCallback = kwargs.get('errorCallback', None)
732732
# Get the request id, if set (only for authenticated requests, if a user
733-
# is logged in), so it can be repeated if the user is required to
733+
# is logged in), so it can be repeated if the user is required to
734734
# reauthenticate.
735735
current_request_id = kwargs.pop('_request_id', None)
736736

@@ -794,7 +794,7 @@ def __reply_finished(self, callback, *args, **kwargs):
794794
return
795795
# Perform another request with the redirect_url and pass on the callback
796796
redirect_url = reply.attribute(request.RedirectionTargetAttribute)
797-
# For now, the redirects only work for GET operations (but to my
797+
# For now, the redirects only work for GET operations (but to my
798798
# knowledge, those are the only operations they occur for)
799799
if reply.operation() == self.GetOperation:
800800
self.get(redirect_url, callback, *args, **kwargs)
@@ -813,8 +813,8 @@ def __reply_finished(self, callback, *args, **kwargs):
813813
reply.deleteLater()
814814

815815
def __create_progress_dialog(self, text, filesize):
816-
""" Creates a progress dialog
817-
816+
""" Creates a progress dialog
817+
818818
Parameters
819819
----------
820820
text : str
@@ -838,7 +838,7 @@ def __transfer_progress(self, transfered, total):
838838
self.sender().property('progressDialog').setValue(transfered)
839839

840840
def __download(self, reply, download_url, *args, **kwargs):
841-
""" The real download function, that is a callback for get_logged_in_user()
841+
""" The real download function, that is a callback for get_logged_in_user()
842842
in download_file() """
843843
# Create tempfile
844844
tmp_file = QtCore.QTemporaryFile()
@@ -858,11 +858,11 @@ def __download(self, reply, download_url, *args, **kwargs):
858858

859859
# Callback function for when bytes are received
860860
kwargs['readyRead'] = self.__download_readyRead
861-
# Download the file with a get request
861+
# Download the file with a get request
862862
self.get(download_url, self.__download_finished, *args, **kwargs)
863863

864864
def __download_readyRead(self, *args, **kwargs):
865-
""" callback for a reply object to indicate that data is ready to be
865+
""" callback for a reply object to indicate that data is ready to be
866866
written to a buffer. """
867867

868868
reply = self.sender()
@@ -912,7 +912,6 @@ def __upload(self, reply, upload_url, source_file, *args, **kwargs):
912912
""" Callback for get_logged_in_user() in upload_file(). Does the real
913913
uploading. """
914914
# Put checks for the url to be a string or QUrl
915-
916915
# Check source file
917916
if isinstance(source_file, basestring):
918917
# Check if the specified file exists, because a situation is possible in which
@@ -941,7 +940,7 @@ def __upload(self, reply, upload_url, source_file, *args, **kwargs):
941940
kwargs['uploadProgress'] = self.__transfer_progress
942941

943942
source_file.open(QtCore.QIODevice.ReadOnly)
944-
self.put(upload_url, self.__upload_finished, data_to_send=source_file,
943+
self.put(upload_url, self.__upload_finished, data_to_send=source_file,
945944
*args, **kwargs)
946945

947946
def __upload_finished(self, reply, *args, **kwargs):
@@ -950,7 +949,7 @@ def __upload_finished(self, reply, *args, **kwargs):
950949
progressDialog = kwargs.pop('progressDialog', None)
951950
if isinstance(progressDialog, QtWidgets.QWidget):
952951
progressDialog.deleteLater()
953-
if not 'data_to_send' in kwargs or not isinstance(kwargs['data_to_send'],
952+
if not 'data_to_send' in kwargs or not isinstance(kwargs['data_to_send'],
954953
QtCore.QIODevice):
955954
raise AttributeError("No valid open file handle")
956955
# Close the source file
@@ -963,13 +962,13 @@ def __upload_finished(self, reply, *args, **kwargs):
963962

964963
def __close_file_handles(self, *args, **kwargs):
965964
""" Closes any open file handles after a failed transfer. Called by
966-
__reply_finished when a HTTP response code indicating an error has been
965+
__reply_finished when a HTTP response code indicating an error has been
967966
received """
968967
# When a download is failed, close the file handle stored in tmp_file
969968
tmp_file = kwargs.pop('tmp_file', None)
970969
if isinstance(tmp_file, QtCore.QIODevice):
971970
tmp_file.close()
972-
# File uploads are stored in data_to_send
971+
# File uploads are stored in data_to_send
973972
data_to_send = kwargs.pop('data_to_send', None)
974973
if isinstance(data_to_send, QtCore.QIODevice):
975974
data_to_send.close()
@@ -992,4 +991,4 @@ def set_logged_in_user(self, user_data):
992991
for (user_id, request) in self.pending_requests.values():
993992
if user_id == self.logged_in_user['data']['id']:
994993
request()
995-
self.pending_requests = {}
994+
self.pending_requests = {}

0 commit comments

Comments
 (0)