-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconspyre.py
432 lines (387 loc) · 15.6 KB
/
conspyre.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
from types import ModuleType
import sys
import time
import requests
try:
import json
except:
import simplejson as json
import fix_json
import requests
conspyre_modules = {}
class ConspyreException(Exception):
def __str__(self):
return """There was an ambiguous exception that occurred while attempting to communicate with the server."""
class HTTPError(ConspyreException):
def __str__(self):
return """An HTTP error occurred. Please check the server to confirm that it is running correctly."""
class ServerError(ConspyreException):
def __str__(self):
return """There was an error on the server, check the server's logs for errors."""
class ConnectionError(ConspyreException):
def __str__(self):
return """A Connection error occurred. Recheck your internet connection and make sure the server is running."""
class Timeout(ConspyreException):
def __str__(self):
return """The request timed out."""
class UserNotFoundException(ConspyreException):
def __str__(self):
return """That user was not found in the database. Perhaps they have not registered?"""
class LoginException(ConspyreException):
def __str__(self):
return """You must log in before you make that call."""
class ModuleNotFoundException(ConspyreException):
def __str__(self):
return """That module does not fully exist yet, and it could not be created. Please fix it's setup on the server."""
class PasswordMismatchException(ConspyreException):
def __str__(self):
return """The username and password do not match."""
class UsernameExistsException(ConspyreException):
def __str__(self):
return """That username already exists."""
class ResetNotApprovedException(ConspyreException):
def __str__(self):
return """Your teacher hasn't approved your password reset yet."""
class ResetAlreadyRequestedException(ConspyreException):
def __str__(self):
return """You have already requested a password reset from this teacher."""
exceptions = {"PasswordMismatch": PasswordMismatchException,
"ModuleNotFound": ModuleNotFoundException,
"UserNotFound": UserNotFoundException,
"UsernameExists": UsernameExistsException,
"ResetNotApproved": ResetNotApprovedException,
"ResetAlreadyRequested": ResetAlreadyRequestedException}
class ConspyreConnection(object):
"""
This is the main class for connecting to a server. Besides the
explicitly defined functions, you can make other calls to the server by
using conspyre.<module_name>.<method_name>(). All parameters must be
named.
*connected*
*logged_in*
*teacher*
"""
def raise_error_from_status(self, request_object):
status = request_object.status_code
if status == 500:
raise HTTPError()
else:
request_object.raise_for_status()
def __init__(self):
self.module = self.__class__.__name__
self.id = None
self.name = ""
self.email = ""
self.password = ""
self.teacher = ""
self.server = ""
self.connected = False
self.logged_in = False
self.devel = True
def log(self, message):
"""
Logs a *message* string into the module's log (stored in JSON on the
server). This could be used to easily dump crash logs, for instance.
"""
id = -1 if self.id is None else self.id
data = { 'metadata' : {'email' : self.email,
'password' : self.password,
'id' : id,
'teacher' : self.teacher,
'module' : self.module,
'time' : time.time()
},
'message' : message}
print data
try:
r = requests.post(self.server+'/logging/'+self.module,
data = json.dumps(data))
except requests.exceptions.ConnectionError, ce:
raise ConnectionError()
if r.status_code != 200:
r.raise_for_status()
r = json.loads(r.content)
if r['type'] == "error":
raise Exception(r['error'])
def login(self, username, password, teacher_id = None):
"""
Logs the user named by the *username* into the server using the
*password*. This method must be called before any methods that
assume a user.
Optionally, a teacher's id can also be passed in. This will create
a Teaching association between this student and teacher.
"""
params={"email" : username,
"password" : password}
if password is not None:
params['teacher'] = teacher_id;
try:
r = requests.get(self.server+"/conspyre/login/",
params=params)
except requests.exceptions.ConnectionError, ce:
raise ConnectionError()
except requests.exceptions.Timeout, to:
raise Timeout()
except requests.exceptions.HTTPError, he:
raise HTTPError()
if r.status_code != 200:
self.raise_error_from_status(r);
r = json.loads(r.content)
if r['type'] == "error":
if r['error'] in exceptions:
raise exceptions[r['error']]
else:
raise Exception(r['error'])
self.email = username
self.password = password
self.name = r['data']['name']
self.id = int(r['data']['id'])
self.logged_in = True
def reset_password(self, username, password):
"""
Changes *username*'s password to *password*. After this function is called,
the user will also be logged in.
Does not require login.
"""
params={"email" : username,
"password" : password}
try:
r = requests.get(self.server+"/conspyre/reset_password/",
params=params)
except requests.exceptions.ConnectionError, ce:
raise ConnectionError()
except requests.exceptions.Timeout, to:
raise Timeout()
except requests.exceptions.HTTPError, he:
raise HTTPError()
if r.status_code != 200:
r.raise_for_status()
r = json.loads(r.content)
if r['type'] == "error":
if r['error'] in exceptions:
raise exceptions[r['error']]
else:
raise Exception(r['error'])
self.email = username
self.password = password
self.name = r['data']['name']
self.id = int(r['data']['id'])
def register(self, username, name, password, teacher_id = None):
"""
Creates a new user with the *username*, *name*, and *password*. After this
function is called, the user will also be logged in.
Optionally, a teacher's id can also be passed in. This will create
a Teaching association between this student and teacher.
Does not require login.
"""
params={"email" : username,
"name" : name,
"password" : password}
if password is not None:
params['teacher'] = teacher_id;
try:
r = requests.get(self.server+"/conspyre/register/",
params= params)
except requests.exceptions.ConnectionError, ce:
raise ConnectionError()
except requests.exceptions.Timeout, to:
raise Timeout()
except requests.exceptions.HTTPError, he:
raise HTTPError()
if r.status_code != 200:
r.raise_for_status()
r = json.loads(r.content)
if r['type'] == "error":
if r['error'] in exceptions:
raise exceptions[r['error']]
else:
raise Exception(r['error'])
self.email = username
self.password = password
self.name = name
self.id = int(r['data']['id'])
def request_reset(self, username):
"""
Sends a request to the current teacher to reset the password for
*username*.
Does not require login.
"""
try:
r = requests.get(self.server+"/conspyre/request_reset/",
params={"student" : username,
"teacher" : self.teacher})
except requests.exceptions.ConnectionError, ce:
raise ConnectionError()
except requests.exceptions.Timeout, to:
raise Timeout()
except requests.exceptions.HTTPError, he:
raise HTTPError()
if r.status_code != 200:
r.raise_for_status()
r = json.loads(r.content)
if r['type'] == "error":
if r['error'] in exceptions:
raise exceptions[r['error']]
else:
raise Exception(r['error'])
def password_is_resetable(self, username):
"""
Returns whether the current teacher has approved a reset for *username*.
Does not require login.
"""
try:
r = requests.get(self.server+"/conspyre/password_resetable/",
params={"student" : username})
except requests.exceptions.ConnectionError, ce:
raise ConnectionError()
except requests.exceptions.Timeout, to:
raise Timeout()
except requests.exceptions.HTTPError, he:
raise HTTPError()
if r.status_code != 200:
r.raise_for_status()
r = json.loads(r.content)
return r['data']['status']
def logout(self):
"""
Logs the currently logged in user out of the system, most likely so that
another user can log in. This does not actually communicate with the
server.
"""
if not self.logged_in:
raise LoginException()
self.id = None
self.name = ""
self.email = ""
self.password = ""
self.logged_in = False
def get_teacher_list(self):
"""
Returns a list of all the teachers. This will be a list of dictionaries
with the keys "id" (a number that uniquely identifies a teacher), "name"
(a name that children will recognize as their teacher), and "username"
(a unique name that identifies them, but will probably not be
recognizeable to a child).
Does not require login.
"""
try:
r = requests.get(self.server+"/conspyre/teacher_list/")
except requests.exceptions.ConnectionError, ce:
raise ConnectionError()
except requests.exceptions.Timeout, to:
raise Timeout()
except requests.exceptions.HTTPError, he:
raise HTTPError()
if r.status_code != 200:
r.raise_for_status()
r = json.loads(r.content)
if r['type'] == "error":
if r['error'] in exceptions:
raise exceptions[r['error']]
else:
raise Exception(r['error'])
return r['data']['teachers']
def get_student_list(self, teacher):
"""
Returns a list of all the students associated with a specific teacher.
This will be a list of dictionaries with the keys "id" (a number that
uniquely identifies a student), "username" (a unique name that
identifies them and hopefully they've memorized), and a "name" (a name
that they will recognize as their own but may not be unique).
Does not require login.
"""
if teacher is None:
teacher = ""
try:
r = requests.get(self.server+"/conspyre/student_list/",
params={"teacher" : teacher})
except requests.exceptions.ConnectionError, ce:
raise ConnectionError()
except requests.exceptions.Timeout, to:
raise Timeout()
except requests.exceptions.HTTPError, he:
raise HTTPError()
if r.status_code != 200:
r.raise_for_status()
r = json.loads(r.content)
if r['type'] == "error":
if r['error'] in exceptions:
raise exceptions[r['error']]
else:
raise Exception(r['error'])
return r['data']['students']
def connect(self, server = 'http://127.0.0.1:8080', devel=True):
"""
Establishes a connection to the *server*. This function must be called
before any others.
"""
self.server = server
self.devel = devel
try:
r = requests.get(self.server+"/test/"+self.module)
except requests.exceptions.ConnectionError, ce:
raise ConnectionError()
except requests.exceptions.Timeout, to:
raise Timeout()
except requests.exceptions.HTTPError, he:
raise HTTPError()
if r.status_code != 200:
return self.raise_error_from_status(r)
r = json.loads(r.content)
if r['type'] == "error":
if r['error'] in exceptions:
raise exceptions[r['error']]
else:
raise Exception(r['error'])
self.connected = True
def disconnect(self):
"""
Disconnects from the server. This does not communicate with the server.
"""
self.connected = False
def _make_server_command(self, command):
def _server_command(**kwargs):
metadata = { 'metadata' : {'email' : self.email,
'password' : self.password,
'id' : self.id,
'teacher' : self.teacher,
'module' : self.module,
'time' : time.time(),
'command' : command},
'data' : dict(kwargs)
}
if not self.logged_in:
raise LoginException()
r = requests.post(self.server+'/modules/'+self.module+'/'+command,
data = json.dumps(metadata))
if r.status_code != 200:
return self.raise_error_from_status(r)
r = json.loads(r.content)
if r['type'] == 'error':
raise Exception(r['error'], r['data'])
if len(r['data']) == 0:
return None
if len(r['data']) == 1:
return r['data'][0]
else:
return r['data']
return _server_command
def __getattr__(self, name):
return self._make_server_command(name)
class module(ModuleType):
def __getattr__(self, name):
# if we have already loaded the module, return it
if name in conspyre_modules:
return conspyre_modules[name]
# otherwise create an instance of it now
c = type(name, (ConspyreConnection,), dict())
conspyre_modules[name] = c()
return conspyre_modules[name]
old_module = sys.modules['conspyre']
new_module = sys.modules['conspyre'] = module('conspyre')
new_module.__dict__.update({
'__file__' : __file__,
'__package__' : 'conspyre',
# '__path__' : __path__,
'__doc__' : __doc__
})