-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpmr2cli.py
253 lines (210 loc) · 7.03 KB
/
pmr2cli.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import json
import os.path
import code
import time
from urllib.error import HTTPError
from urllib.parse import quote_plus
import readline
import traceback
import webbrowser
from pmr2.jsonclient import Client, OAuthCredential
# There are no settings for these. Manually fill this out here with the
# consumer key and secret registered for this at the target PMR2ROOT.
# PMR2ROOT = 'https://models.physiomeproject.org'
PMR2ROOT = 'http://localhost:8280/pmr'
CONSUMER_KEY = 'ovYoqjlJLrpCcEWcIFyxtqRS'
CONSUMER_SECRET = 'fHssEYMWZzgo6JWUBh4l1bhd'
DEFAULT_SCOPE = quote_plus(
'http://localhost:8280/pmr/scope/collection,'
'http://localhost:8280/pmr/scope/search,'
'http://localhost:8280/pmr/scope/workspace_full'
)
HOME = os.path.expanduser('~')
CONFIG_FILENAME = os.path.join(HOME, '.pmr2clirc')
class Cli(object):
token_key = ''
token_secret = ''
active = False
scope = DEFAULT_SCOPE
method = None
_debug = 0
def __init__(self,
pmr2root=PMR2ROOT,
consumer_key=CONSUMER_KEY,
consumer_secret=CONSUMER_SECRET):
self.credential = OAuthCredential(
client=(consumer_key, consumer_secret))
self.client = Client(pmr2root)
self.client.setCredential(self.credential)
@property
def debug(self):
return self._debug
@debug.setter
def debug(self, value):
if isinstance(value, int):
self._debug = value
if isinstance(value, str):
if value.lower() in ('false', 'no', '0',):
self._debug = 0
else:
self._debug = 1
def build_config(self):
return {
'token_key': self.credential.key,
'token_secret': self.credential.secret,
'debug': self.debug,
'scope': DEFAULT_SCOPE,
}
def load_config(self, filename=CONFIG_FILENAME):
try:
fd = open(filename, 'r')
config = json.load(fd)
fd.close()
except IOError:
print("Fail to open configuration file.")
config = self.build_config()
except ValueError:
print("Fail to decode JSON configuration. Using default values.")
config = self.build_config()
self.credential.key = config.get('token_key', '')
self.credential.secret = config.get('token_secret', '')
self.debug = config.get('debug', 0)
self.scope = config.get('scope', DEFAULT_SCOPE)
def save_config(self, filename=CONFIG_FILENAME):
try:
fd = open(filename, 'w')
json.dump(self.build_config(), fd)
fd.close()
except IOError:
print("Error saving configuration")
def get_access(self):
# get user to generate one.
scope = self.scope
try:
self.credential.getTemporaryCredential(callback='oob', scope=scope)
except HTTPError as e:
print('Fail to request temporary credentials.')
return
target = self.credential.getOwnerAuthorizationUrl()
webbrowser.open(target)
verifier = input('Please enter the verifier: ')
self.credential.getAccessCredential(verifier=verifier)
return True
def do_help(self, *a):
"""
Print this message.
"""
print('Basic demo commands:')
print('')
for name in sorted(dir(self)):
if not name.startswith('do_'):
continue
obj = getattr(self, name)
if not callable(obj):
continue
print(name[3:])
print(obj.__doc__)
def do_console(self, *a):
"""
Start the interactive python console.
"""
console = code.InteractiveConsole(locals=locals())
result = console.interact('')
def do_dashboard(self, *a):
"""
List out the features available on the dashboard.
"""
dashboard = self.client.getDashboard()
if not a:
for k, v in dashboard.items():
print('%s\t%s\t%s' % (k, v['label'], v['target']))
return
self.method = self.client.getDashboardMethod(a[0])
print('Acquired method "%s"; use console to interact.' % a[0])
def do_list_workspace(self, *a):
"""
Returns a list of workspaces within your private workspace
container.
"""
method = self.client.getDashboardMethod('workspace-home')
for i in method.raw():
print('"%s"\t%s' % (i['title'], i['target']))
def do_raw(self, *a):
"""
Open a target URL to receive raw API output.
"""
print(self.client.getResponse(*a))
def do_property(self, *a):
"""
Set property for this object.
"""
permitted = ['debug']
if len(a) < 1:
print("need both key and values.")
return
args = list(a)
prop = args.pop(0)
if len(a) < 2:
print('%s = %s' % (prop, getattr(self, prop)))
return
if prop not in permitted:
print("'%s' cannot be set" % prop)
return
setattr(self, prop, ' '.join(args))
def shell(self):
while self.active:
try:
raw = input('pmr2cli> ')
if not raw:
continue
rawargs = raw.split()
command = rawargs.pop(0)
obj = getattr(self, 'do_' + command, None)
if callable(obj):
obj(*rawargs)
else:
print("Invalid command, try 'help'.")
except EOFError:
self.active = False
print('')
except KeyboardInterrupt:
print('\nGot interrupt signal.')
self.active = False
except HTTPError as e:
print('Server responded with error code %d' % e.code)
except:
print(traceback.format_exc())
if self.debug:
import pdb;pdb.post_mortem()
def run(self):
self.load_config()
client = Client(PMR2ROOT)
access = False
if not self.credential.hasAccess():
try:
access = self.get_access()
except HTTPError as e:
print('Fail to validate the verifier.')
else:
access = True
if not access:
self.save_config()
return
try:
self.client.updateDashboard()
except HTTPError as e:
print('Credentials are invalid and are purged. Quitting')
self.credential.key = None
self.credential.secret = None
self.scope = DEFAULT_SCOPE
self.save_config()
return
self.active = True
print('Starting PMR2 Demo Shell...')
self.save_config()
self.shell()
def test(self):
method = client.getDashboardMethod('workspace-add')
if __name__ == '__main__':
cli = Cli()
cli.run()