-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample.py
141 lines (113 loc) · 4.81 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# -*- coding: utf-8 -*-
"""
@author: Daniel Schreij
This module is distributed under the Apache v2.0 License.
You should have received a copy of the Apache v2.0 License
along with this module. If not, see <http://www.apache.org/licenses/>.
"""
# Python3 compatibility
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from QOpenScienceFramework.compat import *
from QOpenScienceFramework.manager import ConnectionManager
from QOpenScienceFramework import connection as osf
from QOpenScienceFramework import widgets, events
from qtpy import QtWidgets, QtCore
from dotenv import load_dotenv
# Import basics
import sys
import os
import logging
import tempfile
logging.basicConfig(level=logging.INFO)
load_dotenv()
os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"
# Required QT classes
# Widgets
# Event dispatcher and listeners
# CONFIGURE THE CLIENT ID AND REDIRECT URI HERE. REGISTER AT OSF.IO
# You can set the parameters here, or place them as environment variables in a .env file
client_id = os.getenv("OSF_CLIENT_ID", "<YOUR_CLIENT_ID_HERE>")
redirect_uri = os.getenv("OSF_REDIRECT_URI", "<YOUR_REDIRECT_URI_HERE>")
class InvalidateButton(QtWidgets.QWidget):
""" Just a button to tamper with the OSF session and see what the app does
to recover from missing authentication information """
def __init__(self, *args, **kwargs):
super(InvalidateButton, self).__init__(*args, **kwargs)
self.setLayout(QtWidgets.QHBoxLayout())
pb = QtWidgets.QPushButton("Invalidate session")
pb.clicked.connect(self.invalidate_session)
self.layout().addWidget(pb)
def invalidate_session(self):
print("Invalidating session!")
osf.session = osf.create_session()
print(osf.session.token)
class StandAlone(object):
""" Class that opens all available widgets when instantiated for testing
purposes. """
def __init__(self):
# Check if client_id and redirect_uri have been changed
if client_id == "<YOUR_CLIENT_ID_HERE>":
raise RuntimeError("Please insert the client_id you have registered"
" for your app at the OSF")
if redirect_uri == "<YOUR_REDIRECT_URI_HERE>":
raise RuntimeError("Please insert the redirect uri you have registered"
" for your app at the OSF")
# Set OSF server settings
server_settings = {
"client_id" : client_id,
"redirect_uri" : redirect_uri,
}
# Add these settings to the general settings
osf.settings.update(server_settings)
osf.create_session()
tmp_dir = safe_decode(tempfile.gettempdir())
tokenfile = os.path.join(tmp_dir, u"osf_token.json")
# Create manager object
self.manager = ConnectionManager(tokenfile=tokenfile)
# Init and set up user badge
self.user_badge = widgets.UserBadge(self.manager)
self.user_badge.move(850, 100)
# Set-up project tree
project_tree = widgets.ProjectTree(self.manager, use_theme="Faenza")
# Init and set up Project explorer
self.project_explorer = widgets.OSFExplorer(
self.manager, tree_widget=project_tree
)
self.project_explorer.move(50, 100)
# Token file listener writes the token to a json file if it receives
# a logged_in event and removes this file after logout
# Filename of the file to store token information in.
self.tfl = events.TokenFileListener(tokenfile)
self.manager.dispatcher.add_listeners(
[
self.manager, self.tfl, project_tree,
self.user_badge, self.project_explorer
]
)
# Connect click on user badge logout button to osf logout action
self.user_badge.logout_request.connect(self.manager.logout)
self.user_badge.login_request.connect(self.manager.login)
# self.ib = InvalidateButton()
# self.ib.setGeometry(850,200,200,50)
# self.ib.show()
# If a valid token is stored in token.json, use that.
# Otherwise show the login window.
self.manager.login()
# Show the user badge
self.user_badge.show()
self.project_explorer.show()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
print("Using Qt {}".format(QtCore.PYQT_VERSION_STR))
# Enable High DPI display with PyQt5
if hasattr(QtCore.Qt, 'AA_UseHighDpiPixmaps'):
app.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps)
if hasattr(QtCore.Qt, 'AA_EnableHighDpiScaling'):
app.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
test = StandAlone()
exitcode = app.exec_()
logging.info("App exiting with code {}".format(exitcode))
sys.exit(exitcode)