-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonmodel.py
110 lines (80 loc) · 3.3 KB
/
jsonmodel.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
import json
from bottle import PluginError
import inspect
import datetime
class JSONModelPlugin:
name = "jsonmodel"
api = 2
def __init__(self, jsonfile):
self.jsonfile = jsonfile
self.model = Model(self.jsonfile)
def setup(self, app):
for other in app.plugins:
if not isinstance(other, JSONModelPlugin): continue
if other.keyword == self.keyword:
raise PluginError("Found another JSONmodel plugin with "\
"conflicting settings (non-unique keyword).")
def apply(self, callback, context):
# Test if the original callback accepts a 'model' keyword.
# Ignore it if it does not need a database handle.
args = inspect.getargspec(context.callback)[0]
if 'model' not in args:
return callback
def wrapper(*args, **kwargs):
kwargs['model'] = self.model
rv = callback(*args, **kwargs)
return rv
# Replace the route callback with the wrapped one.
return wrapper
class Model:
def __init__(self, jsonfile):
"""Load data from a JSON file to initialise the
data store"""
self.jsonfile = jsonfile
self.reset()
def reset(self):
"""reload the data from the JSON file"""
with open(self.jsonfile) as fd:
self.data = json.load(fd)
print("loaded data from ", self.jsonfile)
def get_observations(self):
return self.data['observations']
def get_users(self):
return self.data['users']
def add_observation(self, observation):
"""Add a new observation to the store"""
fields = ['participant', 'temperature', 'weather', 'wind',
'height', 'girth', 'location',
'leaf_size', 'leaf_shape', 'bark_colour', 'bark_texture']
errors = []
print(observation)
for field in fields:
if not field in observation:
errors.append("Missing required field: " + field)
if not errors == []:
return {'status': "failed", 'errors': errors}
else:
# create a new id (incremental)
observation['id'] = len(self.data['observations']) + 1
# add timestamp
observation['timestamp'] = datetime.datetime.now().isoformat()
# observation['participant'] = int(observation['participant'])
observation['temperature'] = int(observation['temperature'])
observation['height'] = int(observation['height'])
observation['girth'] = float(observation['girth'])
self.data['observations'].append(observation)
return {'status': 'success', 'observation': observation}
def get_observation(self, oid):
"""Return a single observation from the data store
given the id or None if no observation with this id exists"""
for obs in self.data['observations']:
if str(obs['id']) == str(oid):
return obs
return None
def get_user(self, uid):
"""Return a single user record from the data store
given the id or None if no user with this id exists"""
for user in self.data['users']:
if str(user['id']) == str(uid):
return user
return None