/
list_loader.py
382 lines (280 loc) · 11.6 KB
/
list_loader.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
'''
Library for Accessing the OpenMinds API from Python.
Provides two methods of accessing the OpenMinds API:
- Using a user's API key and API secret. This is how
an individual should access the API to get their own data.
- Using an oAuth access token. This is how an app should access
the API on behalf of a user that has granted API access to the app.
Adapted from Adam's version by Ram Narasimhan
'''
import gflags
import oauth2
import time
import httplib
import sys
import csv
from libraries.python.web_util import encode_json, decode_json
import om_utils
DEFAULT_HOST = 'openminds.io'
FLAGS = gflags.FLAGS
gflags.DEFINE_string('om_host', DEFAULT_HOST, 'OpenMinds Host')
gflags.DEFINE_string('om_key', '', 'OpenMinds API user key. Used by an individual to access the OpenMinds API.')
gflags.DEFINE_string('om_secret', '', 'OpenMinds API user secret. Used by an individual to access the OpenMinds API.')
gflags.DEFINE_string('om_access_token', '', 'OpenMindsAPI access token. Used by an app to access the OpenMinds API on behalf of a user. If this flag is defined, om_key and om_secret are ignored.')
class AbstractOpenMindsClient(object):
'''
Abstract OpenMindsClient class. Subclasses should implement the _api_response()
method, which should return the response from an HTTPConnection object.
'''
def __init__(self, host=None):
if host:
self.host = host
else:
self.host = DEFAULT_HOST
def _api_response(self, method, path, body={}):
'''
Implemented by subclasses. Should return a response
from an HTTPConnection object.
'''
return None
def _get_json(self, method, path, body={}, params={}):
response = self._api_response(method, path, body, params).read()
return decode_json(response)
def get_users(self):
return self._get_json('GET', '/api/data/users/')
def get_user(self, user_id):
return self._get_json('GET', '/api/data/users/%s/' % user_id)
def update_user(self, user_id, info):
return self._get_json('PUT', '/api/data/users/%s/' % user_id, info)
def create_user(self, info):
return self._get_json('POST', '/api/data/users/', info)
def get_class(self, class_id):
return self._get_json('GET', '/api/data/classes/%s/' % class_id)
def update_class(self, class_id, info):
return self._get_json('PUT', '/api/data/classes/%s/' % class_id, info)
def create_class(self, info):
return self._get_json('POST', '/api/data/classes/', info)
def get_lists(self, params={}):
return self._get_json('GET', '/api/data/lists/', params=params)
def get_list(self, list_id):
return self._get_json('GET', '/api/data/lists/%s/' % list_id)
def update_list(self, list_id, info):
return self._get_json('PUT', '/api/data/lists/%s/' % list_id, info)
def create_list(self, info):
return self._get_json('POST', '/api/data/lists/', info)
def get_item(self, list_id, item_id):
return self._get_json('GET', '/api/data/lists/%s/%s/' % (list_id, item_id))
def update_item(self, list_id, item_id, info):
return self._get_json('PUT', '/api/data/lists/%s/%s/' % (list_id, item_id), info)
def create_item(self, list_id, info):
return self._get_json('POST', '/api/data/lists/%s/' % list_id, info)
def get_assignment(self, assignment_id):
return self._get_json('GET', '/api/data/assignments/%s/' % (assignment_id))
def update_assignment(self, assignment_id, info):
return self._get_json(
'PUT',
'/api/data/assignments/%s/' % (assignment_id),
info)
def create_assignment(self, info):
return self._get_json('POST', '/api/data/assignments/', info)
def get_assignment_template(self, assignment_template_id):
return self._get_json(
'GET',
'/api/data/assignment_templates/%s/' % (assignment_template_id))
def update_assignment_template(self, assignment_template_id, info):
return self._get_json(
'PUT',
'/api/data/assignment_templates/%s/' % (assignment_template_id),
info)
def create_assignment_template(self, info):
return self._get_json('POST', '/api/data/assignment_templates/', info)
class OpenMindsTwoLeggedClient(AbstractOpenMindsClient):
'''
Client to access the OpenMinds API using oAuth two-legged authentication. The
user provides an API key and API secret, which are used to securely sign the
request.
'''
def __init__(self, key, secret, host=None):
AbstractOpenMindsClient.__init__(self, host)
self.key = key
self.secret = secret
def _get_request(self, method, path, body='', extra_params={}):
consumer = oauth2.Consumer(self.key, self.secret)
params = {
'oauth_version': "1.0",
'oauth_nonce': oauth2.generate_nonce(),
'oauth_timestamp': int(time.time())
}
params.update(extra_params)
if method == 'POST':
params['data'] = body
url = 'http://' + self.host + path
req = oauth2.Request(method=method, url=url, body='', parameters=params)
signature_method = oauth2.SignatureMethod_HMAC_SHA1()
req.sign_request(signature_method, consumer, None)
return req
def _api_response(self, method, path, body={}, params={}):
'''
Signs the request using the oauth2 library.
'''
str_body = encode_json(body)
req = self._get_request(method, path, str_body, extra_params=params)
data = req.to_postdata()
connection = httplib.HTTPConnection(self.host)
if self.host.startswith('localhost') and method == 'POST':
# Workaround for sending api POST requests to local server.
connection.request(method, path, data)
else:
connection.request(method, path + '?' + data, str_body)
return connection.getresponse()
def get_game_url(self, game_id, list_id, params):
path = '/game/%s/%s/' % (game_id, list_id)
req = self._get_request('GET', path, extra_params=params)
return '%s%s?%s' % (self.host, path, req.to_postdata())
class OpenMindsThreeLeggedClient(AbstractOpenMindsClient):
'''
Client to access the OpenMinds API using oAuth three-legged authentication.
We assume the user has already obtained an API access token by granting
an app access through the web interface. The access token is used by the app
to get access to the API on behalf of the user.
'''
def __init__(self, access_token, host=None):
AbstractOpenMindsClient.__init__(self, host)
self.access_token = access_token
def _api_response(self, method, path, body=None):
'''
Includes the access token as a header in the request.
'''
connection = httplib.HTTPConnection(self.host)
if body:
data = encode_json(body)
else:
data = None
headers = {
'X-OpenMinds-Access-Token': self.access_token
}
if method == 'GET':
if data:
path += '?' + data
connection.request(method, path, None, headers)
else:
connection.request(method, path, data, headers)
return connection.getresponse()
# word || The item word.
# pos|| The item part-of-speech.
# defn|| The item definition.
# sentence || An example sentence using the item word.
# otherWords || Alternate words. Array of strings, maximum number of words is 5.
# parts || The item word broken down into parts. Array of strings, maximum number if parts is 5.
def create_item_dicts(textlist):
'''
Each line in CSV file is a new item. It has to have its mandatory fields
and optionally, any of the other item properties for that list format
'''
dictslist = []
WORD = 'word'
POS = 'pos'
DEFINITION = 'defn'
SENTENCE = 'sentence'
ITEM_PROPERTIES = (WORD,POS, DEFINITION,SENTENCE)
for r in textlist:
print r
if r[0].rstrip(" ").lower() =="item":
idict = {}
for index, el in enumerate(r):
if el in ITEM_PROPERTIES:
om_utils.add_to_dict(idict,el,r,index) #will add the token next to curr index
if not WORD in idict:
print("WORD property not specified in CSV. ITEM cannot be created without this mandatory field")
if not DEFINITION in idict:
print("DEFN property not specified in CSV. ITEM cannot be created without this mandatory field")
dictslist.append(idict)
return dictslist # a list of idicts
def create_list_dict(allRows):
'''
Read all lines that begin with List and if a format is found, store it.
Return False if there is no format, or if format is unrecognized
'''
ldict = {}
# title || Name of the list ||
# description || Short list description.
# standard || String code representing the list standard. Example: "L.7.2".
# section || Numerical sub-section of the standard.
TITLE = 'title'
DESCRIPTION = 'description'
FORMAT = 'format'
GRADE = 'grade'
SECTION = 'section'
STANDARD = 'standard'
DUMMY = 'dummy12sger434'
LIST_PROPERTIES = (TITLE, DESCRIPTION, FORMAT,SECTION,STANDARD)
LIST_INTEGER_PROPERTIES = (GRADE, DUMMY)
# if not 'format' in row:
# print('Format type must be specified for new lists.')
# print("useful lines in List file", len(allRows))
for row in allRows:
# print row[0]
if row[0].rstrip(" ").lower() == "list":
for index, item in enumerate(row):
if item in LIST_PROPERTIES:
om_utils.add_to_dict(ldict,item.lower(),row,index) #will add the token next to curr index
# grade || Intended grade level of the list. Number between 0 (K) and 8.
if item in LIST_INTEGER_PROPERTIES:
# print("item %s" % item)
om_utils.add_to_dict_as_int(ldict,item.lower(),row,index)
if not "format" in ldict:
print("List format property not specified in CSV. List cannot be created without this mandatory field")
return ldict
if __name__ == '__main__':
'''
Simple test for the API.
'''
argv = FLAGS(sys.argv)
print argv
print FLAGS.om_key, FLAGS.om_secret
print FLAGS.om_host
if FLAGS.om_access_token:
client = OpenMindsThreeLeggedClient(FLAGS.om_access_token, FLAGS.om_host)
else:
client = OpenMindsTwoLeggedClient(FLAGS.om_key, FLAGS.om_secret, FLAGS.om_host)
# print client.get_user('me')
print client.get_users()
# print(userinfo["username"])
# print client.get_users()
# print client.get_lists({"num":1})
# print client.get_lists({"search":"created"})
# print client.update_user("4fd1269037eaef23070024e7", {"name":"ramtestupdated"} )
# read in the CSV File (skip header row)
ld = {}
textlist = om_utils.read_all_csv_lines()
#one list dictionary
ld = create_list_dict(textlist)
#one dictionary for each item
idictsList = create_item_dicts(textlist)
# create a new list
newlist = client.create_list(ld)
print newlist
# check if the list was successfully created.
if "success" in newlist:
print "List Creation failed"
print "Exiting Script"
sys.exit()
print("New list successfully created")
#get the ID of the newly created List
lid= newlist["id"]
# iteminfo = {"word":"tuber"}
# /data/lists/4ec3c8642324d773c0000057/4f2c68a0d0ccd94e22000000
#create a new item
for iteminfo in idictsList:
print client.create_item(lid,iteminfo)
# add each of the items to that list
## ------- notes follow
# gflags help http://python-gflags.googlecode.com/svn/trunk/gflags.py
# Any flags you don't feel like typing, throw them in a file, one flag per
# line, for instance:
# --myflag=myvalue
# --nomyboolean_flag
# You then specify your file with the special flag '--flagfile=somefile'.
# You CAN recursively nest flagfile= tokens OR use multiple files on the
# command line. Lines beginning with a single hash '#' or a double slash
# '//' are comments in your flagfile.