-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
filesystem.py
268 lines (205 loc) · 9.56 KB
/
filesystem.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
"""
Copyright (c) 2017-2022, Vanessa Sochat
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
from flask import session
from expfactory.utils import write_json, mkdir_p
from expfactory.defaults import EXPFACTORY_SUBID, EXPFACTORY_DATA
from glob import glob
import uuid
import os
import sys
# DEFAULT FLAT #################################################################
# Default "database" is flat files written to the system
################################################################################
# This is an Expfactory Flask Server database plugin. It implements common
# functions (generate_subid, save_data, init_db) that should prepare a
# database and perform actions to save data to it. The functions are added
# to the main application upon initialization of the server.
def generate_subid(self, token=None):
"""assumes a flat (file system) database, organized by experiment id, and
subject id, with data (json) organized by subject identifier
"""
# Not headless auto-increments
if not token:
token = str(uuid.uuid4())
# Headless doesn't use any folder_id, just generated token folder
return "%s/%s" % (self.study_id, token)
def list_users(self):
"""list users, each associated with a filesystem folder"""
folders = glob("%s/*" % (self.database))
folders.sort()
return [self.print_user(x) for x in folders]
def print_user(self, user):
"""print a filesystem database user. A "database" folder that might end with
the participant status (e.g. _finished) is extracted to print in format
[folder] [identifier][studyid]
/scif/data/expfactory/xxxx-xxxx xxxx-xxxx[studyid]
"""
status = "active"
if user.endswith("_finished"):
status = "finished"
elif user.endswith("_revoked"):
status = "revoked"
subid = os.path.basename(user)
for ext in ["_revoked", "_finished"]:
subid = subid.replace(ext, "")
subid = "%s\t%s[%s]" % (user, subid, status)
print(subid)
return subid
# Actions ######################################################################
def generate_user(self, subid=None):
"""generate a new user on the filesystem, still session based so we
create a new identifier. This function is called from the users new
entrypoint, and it assumes we want a user generated with a token.
since we don't have a database proper, we write the folder name to
the filesystem
"""
# Only generate token if subid being created
if subid is None:
token = str(uuid.uuid4())
subid = self.generate_subid(token=token)
if os.path.exists(self.data_base): # /scif/data
data_base = "%s/%s" % (self.data_base, subid)
# expfactory/00001
if not os.path.exists(data_base):
mkdir_p(data_base)
return data_base
def finish_user(self, subid, ext="finished"):
"""finish user will append "finished" (or other) to the data folder when
the user has completed (or been revoked from) the battery.
For headless, this means that the session is ended and the token
will not work again to rewrite the result. If the user needs to update
or redo an experiment, this can be done with a new session. Note that if
this function is called internally by the application at experiment
finish, the subid includes a study id (e.g., expfactory/xxxx-xxxx)
but if called by the user, it may not (e.g., xxxx-xxxx). We check
for this to ensure it works in both places.
"""
if os.path.exists(self.data_base): # /scif/data
# Only relevant to filesystem save - the studyid is the top folder
if subid.startswith(self.study_id):
data_base = "%s/%s" % (self.data_base, subid)
else:
data_base = "%s/%s/%s" % (self.data_base, self.study_id, subid)
# The renamed file will be here
finished = "%s_%s" % (data_base, ext)
# Participant already finished
if os.path.exists(finished):
self.logger.warning("[%s] is already finished: %s" % (subid, data_base))
# Exists and can finish
elif os.path.exists(data_base):
os.rename(data_base, finished)
# Not finished, doesn't exist
else:
finished = None
self.logger.warning(
"%s does not exist, cannot finish. %s" % (data_base, subid)
)
return finished
def restart_user(self, subid):
"""restart user will remove any "finished" or "revoked" extensions from
the user folder to restart the session. This command always comes from
the client users function, so we know subid does not start with the
study identifer first
"""
if os.path.exists(self.data_base): # /scif/data/<study_id>
data_base = "%s/%s" % (self.data_base, subid)
for ext in ["revoked", "finished"]:
folder = "%s_%s" % (data_base, ext)
if os.path.exists(folder):
os.rename(folder, data_base)
self.logger.info("Restarting %s, folder is %s." % (subid, data_base))
self.logger.warning(
"%s does not have revoked or finished folder, no changes necessary."
% (subid)
)
return data_base
self.logger.warning(
"%s does not exist, cannot restart. %s" % (self.database, subid)
)
# Tokens #######################################################################
def validate_token(self, token):
"""retrieve a subject based on a token. Valid means we return a participant
invalid means we return None
"""
# A token that is finished or revoked is not valid
subid = None
if not token.endswith(("finished", "revoked")):
subid = self.generate_subid(token=token)
data_base = "%s/%s" % (self.data_base, subid)
if not os.path.exists(data_base):
subid = None
return subid
def refresh_token(self, subid):
"""refresh or generate a new token for a user. If the user is finished,
this will also make the folder available again for using."""
if os.path.exists(self.data_base): # /scif/data
data_base = "%s/%s" % (self.data_base, subid)
if os.path.exists(data_base):
refreshed = "%s/%s" % (self.database, str(uuid.uuid4()))
os.rename(data_base, refreshed)
return refreshed
self.logger.warning("%s does not exist, cannot rename %s" % (data_base, subid))
else:
self.logger.warning(
"%s does not exist, cannot rename %s" % (self.database, subid)
)
def revoke_token(self, subid):
"""revoke a presently active token, meaning append _revoked to it."""
return self.finish_user(subid, ext="revoked")
def save_data(self, session, exp_id, content):
"""save data will obtain the current subid from the session, and save it
depending on the database type. Currently we just support flat files"""
subid = session.get("subid")
# We only attempt save if there is a subject id, set at start
data_file = None
if subid is not None:
data_base = "%s/%s" % (self.data_base, subid)
# If not running in headless, ensure path exists
if not self.headless and not os.path.exists(data_base):
mkdir_p(data_base)
# Conditions for saving:
do_save = False
# If headless with token pre-generated OR not headless
if self.headless and os.path.exists(data_base) or not self.headless:
do_save = True
if data_base.endswith(("revoked", "finished")):
do_save = False
# If headless with token pre-generated OR not headless
if do_save is True:
data_file = "%s/%s-results.json" % (data_base, exp_id)
if os.path.exists(data_file):
self.logger.warning("%s exists, and is being overwritten." % data_file)
write_json(content, data_file)
return data_file
def init_db(self):
"""init_db for the filesystem ensures that the base folder (named
according to the studyid) exists.
"""
self.session = None
if not os.path.exists(self.data_base):
mkdir_p(self.data_base)
self.database = "%s/%s" % (self.data_base, self.study_id)
if not os.path.exists(self.database):
mkdir_p(self.database)