-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmisc.py
76 lines (65 loc) · 1.81 KB
/
misc.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
from functools import update_wrapper
from flask import request, jsonify
import simplejson, time
"""
Checks if given parameters are available as GET or POST variables.
"""
def parameters_given(params):
def decorator(fn):
def wrapped_function(*args, **kwargs):
for param in params:
if request.method == 'POST' and not paramExists(param):
message = { 'success': False, 'status': 'Please define all non-optional arguments.' }
resp = jsonify(message)
resp.status_code = 403
return resp
return fn(*args, **kwargs)
return update_wrapper(wrapped_function, fn)
return decorator
def getParam(name):
return request.form[name]
def paramExists(param):
return param in request.form
"""
Determine whether a string represents a boolean true or false.
"""
def isTrue(param):
if param.lower() == 'true' or param == '1':
return True
elif param.lower() == 'false' or param == '0':
return False
else:
raise TypeError("Parameter could not be converted to boolean.")
"""
General http errors
"""
# FIXME: use some kind of global conversion of error messages (to json)
def error405():
resp = jsonify({'success': False, 'status': 'Method not allowed.'})
resp.status_code = 405
return resp
def error504():
resp = jsonify({'success': False, 'status': 'Time-out reached.'})
resp.status_code = 504
return resp
"""
Returns dictionary with requested status info.
"""
def getStatus(filename):
with open(filename, 'r') as f:
status = simplejson.loads(f.read())
return status
"""
Updates given status info.
"""
def setStatus(filename, newStatus):
oldStatus = getStatus(filename)
try:
del oldStatus["lastUpdate"]
except KeyError:
pass
if oldStatus != newStatus:
newStatus["lastUpdate"] = int(time.time())
with open(filename, 'w') as f:
f.write(simplejson.dumps(newStatus))
return { 'success': True }