Mercurial > hg > env-test-daniele
comparison cherryFxTest.py.orig @ 22:8498a7b84ff1
• Audio files
author | Daniele Barchiesi <daniele.barchiesi@eecs.qmul.ac.uk> |
---|---|
date | Mon, 11 Mar 2013 13:35:29 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
11:15260ae0d6bc | 22:8498a7b84ff1 |
---|---|
1 | |
2 import cherrypy, os, random, smtplib, string | |
3 from cherrypy.lib.static import serve_file | |
4 from templates import * | |
5 | |
6 from DirectorySession import DirectorySession | |
7 from ServerWrapper import ServerWrapper as CherryPyServer | |
8 from ServerWrapper import Config as ServerConfig | |
9 | |
10 from email.MIMEBase import MIMEBase | |
11 from email.MIMEText import MIMEText | |
12 from email.MIMEMultipart import MIMEMultipart | |
13 from email.Utils import COMMASPACE, formatdate | |
14 from email import Encoders | |
15 | |
16 from pprint import pprint | |
17 | |
18 PATH = os.path.abspath(os.path.dirname(__file__)) | |
19 | |
20 #audiodir = './audio' | |
21 #cherrypy.session['fxs'] = [cherrypy.session['f'] for cherrypy.session['f'] in os.listdir(audiodir) if os.path.isdir(os.path.join(audiodir, cherrypy.session['f']))] | |
22 #cherrypy.session['fxs'].remove('dry') | |
23 #print cherrypy.session['fxs'] | |
24 | |
25 class Root(object): pass | |
26 | |
27 | |
28 class HomePage: | |
29 def index(self): | |
30 | |
31 return "move along, nothing to see here!" | |
32 | |
33 index.exposed = True | |
34 | |
35 | |
36 | |
37 class TestPage: | |
38 | |
39 def getAASPFiles(self): | |
40 audioDir = './AASPAudio' | |
41 cherrypy.session['cats'] = [cherrypy.session['f'] for cherrypy.session['f'] in os.listdir(audioDir) if os.path.isdir(os.path.join(audioDir,cherrypy.session['f']))] | |
42 print cherrypy.session['cats'] | |
43 | |
44 def index(self): | |
45 TestPage.getAASPFiles(self) | |
46 cherrypy.session['sid'] = None | |
47 | |
48 cherrypy.session['sid_filename'] = None | |
49 | |
50 | |
51 if cherrypy.request.headers.has_key("X-Forwarded-For"): | |
52 cherrypy.session['ip'] = cherrypy.request.headers["X-Forwarded-For"] | |
53 elif cherrypy.request.headers.has_key("Remote-Addr"): | |
54 cherrypy.session['ip'] = cherrypy.request.headers["Remote-Addr"] | |
55 else : | |
56 cherrypy.session['ip'] = None | |
57 | |
58 cherrypy.session['test'] = None | |
59 cherrypy.session['results'] = None | |
60 cherrypy.session['tests'] = None | |
61 cherrypy.session['test_nr'] = None | |
62 cherrypy.session['audio'] = None | |
63 cherrypy.session['page'] = 0 | |
64 cherrypy.session['pages'] = 6 | |
65 cherrypy.session['userinfobox'] = None | |
66 cherrypy.session['ts'] = None | |
67 #return ''' | |
68 # <p> | |
69 # <a href="/test/">start test</a> | |
70 # </p>''' | |
71 | |
72 #take 2 examples for each effect | |
73 | |
74 cherrypy.session['audio'] = [] | |
75 | |
76 # get effected files: | |
77 cherrypy.session['audio'] = [] | |
78 for cherrypy.session['cat'] in cherrypy.session['cats']: | |
79 cherrypy.session['dirlist'] = os.listdir('./AASPAudio/%s/'%cherrypy.session['cat']) | |
80 for cherrypy.session['item'] in cherrypy.session['dirlist']: | |
81 if cherrypy.session['item'].split('.')[-1] == 'wav': | |
82 cherrypy.session['audio'].append(cherrypy.session['cat'] + '/' + cherrypy.session['item'].split('.')[0]) | |
83 | |
84 | |
85 #print TestPage.audio | |
86 random.shuffle(cherrypy.session['audio']) | |
87 | |
88 print "NUMBER OF TESTS:" | |
89 print len(cherrypy.session['audio']) | |
90 | |
91 cherrypy.session['sid'] = cherrypy.session.get_session_path().split('/')[1] | |
92 print cherrypy.session['sid'] | |
93 | |
94 #print cherrypy.session['audio'] | |
95 pprint(cherrypy.session['audio']) | |
96 | |
97 | |
98 TestPage.outfileid = '%s' %cherrypy.session['sid'] | |
99 ida = 0 | |
100 while os.path.exists('./sessions/%s/%s.txt' %(cherrypy.session['sid'], TestPage.outfileid)): | |
101 TestPage.outfileid += '_%s' %ida | |
102 ida += 1 | |
103 | |
104 | |
105 return welcome_templ %cherrypy.session['sid'] | |
106 | |
107 | |
108 def doStart(self, pageId, sessionId, startButton, uinfo00=None, uinfo01=None, info1=None): | |
109 | |
110 | |
111 | |
112 if cherrypy.session['page'] != int(pageId): | |
113 print "################################# BACK BUTTON PRESSED #################################" | |
114 | |
115 | |
116 if cherrypy.session['sid'] != str(sessionId): | |
117 return TestPage.sessionError(self) | |
118 #cherrypy.session['page'] = int(pageId) | |
119 | |
120 | |
121 | |
122 | |
123 cherrypy.session['userinfobox'] = str([str(uinfo00), str(uinfo01), int(info1)]) + '\n' | |
124 cherrypy.session['sessionId'] = sessionId | |
125 #print "sid:" | |
126 #print TestPage.sid | |
127 #print sessionId | |
128 if cherrypy.session['sid'] != str(cherrypy.session['sessionId']): | |
129 return TestPage.sessionError(self) | |
130 else: | |
131 return TestPage.index2(self) | |
132 | |
133 | |
134 | |
135 def index2(self): | |
136 #try: print testvar | |
137 #except: pass | |
138 | |
139 cherrypy.session['page'] = 1 | |
140 cherrypy.session['pages'] = 10 | |
141 cherrypy.session['tests'] = len(cherrypy.session['audio']) #should be 66 #10 * cherrypy.session['pages'] | |
142 cherrypy.session['results'] = [] | |
143 for cherrypy.session['n'] in xrange(cherrypy.session['tests']): cherrypy.session['results'].append([None]) | |
144 | |
145 # create tuple to fill template | |
146 cherrypy.session['ts'] = [] | |
147 for cherrypy.session['k'] in xrange(10): | |
148 cherrypy.session['ts'].append(0) | |
149 | |
150 return audiotempl%TestPage.makeTuple(self, 10) | |
151 | |
152 def doAttrib(self, pageId, sessionId, submitButton, attribute1=None, attribute2=None, attribute3=None, attribute4=None, attribute5=None, attribute6=None, attribute7=None, attribute8=None, attribute9=None, attribute10=None): | |
153 #print attribute1 + ' ' + attribute2 | |
154 # Error check | |
155 | |
156 print "################################# %s" %pageId | |
157 #_pageId = int(pageId) - 1 | |
158 | |
159 if cherrypy.session['page'] != int(pageId): | |
160 print "################################# BACK BUTTON PRESSED #################################" | |
161 | |
162 cherrypy.session['page'] = int(pageId) | |
163 | |
164 if cherrypy.session['sid'] != str(sessionId): | |
165 return TestPage.sessionError(self) | |
166 | |
167 #store results | |
168 | |
169 cherrypy.session['pos'] = 10 * (cherrypy.session['page'] - 1) | |
170 | |
171 cherrypy.session['results'][cherrypy.session['pos']] = [cherrypy.session['audio'][cherrypy.session['pos']][2].split('/')[-1], int(attribute1)] | |
172 cherrypy.session['results'][cherrypy.session['pos']+1] = [cherrypy.session['audio'][cherrypy.session['pos']+1][2].split('/')[-1], int(attribute2)] | |
173 cherrypy.session['results'][cherrypy.session['pos']+2] = [cherrypy.session['audio'][cherrypy.session['pos']+2][2].split('/')[-1], int(attribute3)] | |
174 cherrypy.session['results'][cherrypy.session['pos']+3] = [cherrypy.session['audio'][cherrypy.session['pos']+3][2].split('/')[-1], int(attribute4)] | |
175 cherrypy.session['results'][cherrypy.session['pos']+4] = [cherrypy.session['audio'][cherrypy.session['pos']+4][2].split('/')[-1], int(attribute5)] | |
176 cherrypy.session['results'][cherrypy.session['pos']+5] = [cherrypy.session['audio'][cherrypy.session['pos']+5][2].split('/')[-1], int(attribute6)] | |
177 cherrypy.session['results'][cherrypy.session['pos']+6] = [cherrypy.session['audio'][cherrypy.session['pos']+6][2].split('/')[-1], int(attribute7)] | |
178 cherrypy.session['results'][cherrypy.session['pos']+7] = [cherrypy.session['audio'][cherrypy.session['pos']+7][2].split('/')[-1], int(attribute8)] | |
179 cherrypy.session['results'][cherrypy.session['pos']+8] = [cherrypy.session['audio'][cherrypy.session['pos']+8][2].split('/')[-1], int(attribute9)] | |
180 cherrypy.session['results'][cherrypy.session['pos']+9] = [cherrypy.session['audio'][cherrypy.session['pos']+9][2].split('/')[-1], int(attribute10)] | |
181 | |
182 | |
183 resString = cherrypy.session['ip'] + '\n' + TestPage.outfileid + '\n' + cherrypy.session['userinfobox'] + '\n' + ',\n'.join(str(x) for x in cherrypy.session['results']) | |
184 | |
185 #f = open(TestPage.sid_filename, 'w') | |
186 #f.write(resString) | |
187 #f.close() | |
188 | |
189 cherrypy.session.write_data(resString,'%s.txt'%TestPage.outfileid) | |
190 | |
191 #if cherrypy.session['page'] < cherrypy.session['pages'] - 1: | |
192 cherrypy.session['page'] += 1 | |
193 # create tuple to fill template | |
194 cherrypy.session['ts'] = [] | |
195 cherrypy.session['npos'] = cherrypy.session['pos'] + 10 | |
196 | |
197 if cherrypy.session['page'] < cherrypy.session['pages']: | |
198 cherrypy.session['m'] = 10 | |
199 else: | |
200 cherrypy.session['m'] = 6 | |
201 | |
202 for cherrypy.session['k'] in xrange(cherrypy.session['m']): | |
203 for cherrypy.session['i'] in xrange(4): | |
204 #print cherrypy.session['k'] | |
205 #print cherrypy.session['i'] | |
206 cherrypy.session['ts'].append(cherrypy.session['results'][cherrypy.session['npos']+cherrypy.session['k']][cherrypy.session['i']+1]) | |
207 | |
208 return audiotempl%TestPage.makeTuple(self, cherrypy.session['m']) | |
209 | |
210 | |
211 index.exposed = True | |
212 doAttrib.exposed = True | |
213 index2.exposed = True | |
214 doStart.exposed = True | |
215 | |
216 | |
217 def doAttrib2(self, pageId, sessionId, submitButton, attribute1=None, attribute2=None, attribute3=None, attribute4=None, attribute5=None, attribute6=None): | |
218 #print attribute1 + ' ' + attribute2 | |
219 # Error check | |
220 | |
221 print "################################# %s" %pageId | |
222 #_pageId = int(pageId) - 1 | |
223 | |
224 if cherrypy.session['page'] != int(pageId): | |
225 print "################################# BACK BUTTON PRESSED #################################" | |
226 | |
227 cherrypy.session['page'] = int(pageId) | |
228 | |
229 if cherrypy.session['sid'] != str(sessionId): | |
230 return TestPage.sessionError(self) | |
231 | |
232 #store results | |
233 | |
234 cherrypy.session['pos'] = 10 * (cherrypy.session['page'] - 1) | |
235 | |
236 cherrypy.session['results'][cherrypy.session['pos']] = [cherrypy.session['audio'][cherrypy.session['pos']][2].split('/')[-1], int(attribute1)] | |
237 cherrypy.session['results'][cherrypy.session['pos']+1] = [cherrypy.session['audio'][cherrypy.session['pos']+1][2].split('/')[-1], int(attribute2)] | |
238 cherrypy.session['results'][cherrypy.session['pos']+2] = [cherrypy.session['audio'][cherrypy.session['pos']+2][2].split('/')[-1], int(attribute3)] | |
239 cherrypy.session['results'][cherrypy.session['pos']+3] = [cherrypy.session['audio'][cherrypy.session['pos']+3][2].split('/')[-1], int(attribute4)] | |
240 cherrypy.session['results'][cherrypy.session['pos']+4] = [cherrypy.session['audio'][cherrypy.session['pos']+4][2].split('/')[-1], int(attribute5)] | |
241 cherrypy.session['results'][cherrypy.session['pos']+5] = [cherrypy.session['audio'][cherrypy.session['pos']+5][2].split('/')[-1], int(attribute6)] | |
242 | |
243 | |
244 | |
245 resString = cherrypy.session['ip'] + '\n' + cherrypy.session['sid'] + '\n' + cherrypy.session['userinfobox'] + '\n' + ',\n'.join(str(x) for x in cherrypy.session['results']) | |
246 | |
247 #f = open(TestPage.sid_filename, 'w') | |
248 #f.write(resString) | |
249 #f.close() | |
250 #os.path.exists() | |
251 | |
252 cherrypy.session.write_data(resString,'%s.txt'%TestPage.outfileid) | |
253 | |
254 | |
255 print cherrypy.session['results'] | |
256 #_results = [] | |
257 | |
258 cherrypy.session['templ'] = '' | |
259 for cherrypy.session['n'] in xrange(cherrypy.session['tests']): | |
260 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" | |
261 #print cherrypy.session['n'] | |
262 cherrypy.session['_templ'] = endtempl2 %(cherrypy.session['results'][cherrypy.session['n']][0], cherrypy.session['results'][cherrypy.session['n']][1], cherrypy.session['results'][cherrypy.session['n']][2], cherrypy.session['results'][cherrypy.session['n']][3], cherrypy.session['results'][cherrypy.session['n']][4]) | |
263 cherrypy.session['templ'] += cherrypy.session['_templ'] | |
264 | |
265 #TestPage.pageComplete += 1 | |
266 try: | |
267 #print 'skipping email' | |
268 TestPage.sendMail(self) | |
269 except: | |
270 print "WARNING: COULD NOT SEND RESULTS" | |
271 pass | |
272 cherrypy.session['sid'] = None | |
273 return endtempl%cherrypy.session['templ'] | |
274 #return lastpage | |
275 | |
276 | |
277 index.exposed = True | |
278 doAttrib.exposed = True | |
279 doAttrib2.exposed = True | |
280 index2.exposed = True | |
281 doStart.exposed = True | |
282 | |
283 | |
284 def makeTuple(TestPage, nr): | |
285 cherrypy.session['nr'] = nr | |
286 cherrypy.session['ts'].append(cherrypy.session['page']) | |
287 cherrypy.session['ts'].append(cherrypy.session['pages']) | |
288 for cherrypy.session['k'] in xrange(cherrypy.session['nr']): | |
289 pos = 10 * (cherrypy.session['page'] - 1) + cherrypy.session['k'] | |
290 #print pos | |
291 cherrypy.session['ts'].append(cherrypy.session['audio'][pos]) | |
292 cherrypy.session['ts'].append(cherrypy.session['page']) | |
293 cherrypy.session['ts'].append(cherrypy.session['sid']) | |
294 cherrypy.session['ts'] = tuple(cherrypy.session['ts']) | |
295 print cherrypy.session['ts'] | |
296 return cherrypy.session['ts'] | |
297 | |
298 def sessionError(TestPage): | |
299 print "################################# SESSION ERROR #################################" | |
300 cherrypy.session['resString'] = cherrypy.session['ip'] + '\n' + str(cherrypy.session['sid']) + '\n' + cherrypy.session['userinfobox'] + '\n' + ',\n'.join(str(x) for x in cherrypy.session['results']) | |
301 cherrypy.session['resString'] += '\n\nERROR: SESSION CONFUSION' | |
302 #f = open(TestPage.sid_filename, 'w') | |
303 #f.write(resString) | |
304 #f.close() | |
305 cherrypy.session.write_data(cherrypy.session['resString'],'%s.txt'%TestPage.outfileid) | |
306 | |
307 return ''' | |
308 <p> | |
309 <a href="/test/">oh noes! there is something wrong with your session, please start again or go for a coffee!</a> | |
310 </p>''' | |
311 | |
312 | |
313 def sendMail(TestPage): | |
314 cherrypy.session['file'] = './sessions/%s/%s.txt'%(cherrypy.session['sid'], TestPage.outfileid) | |
315 username = 'eecsqmul@gmail.com' | |
316 password = 'perceptual' | |
317 fro = 'FX-Test <eecsqmul@gmail.com>' | |
318 to = ['thomas.wilmering@eecs.qmul.ac.uk', 'gyorgy.fazekas@eecs.qmul.ac.uk'] | |
319 msg = MIMEMultipart() | |
320 msg['From'] = fro | |
321 msg['To'] = COMMASPACE.join(to) | |
322 msg['Date'] = formatdate(localtime=True) | |
323 msg['Subject'] = 'fx perceptual listening test' | |
324 msg.attach( MIMEText('Attached are new results from the listening test.')) | |
325 part = MIMEBase('application', "octet-stream") | |
326 part.set_payload( open(cherrypy.session['file'],"rb").read() ) | |
327 Encoders.encode_base64(part) | |
328 part.add_header('Content-Disposition', 'attachment; filename="%s"' % cherrypy.session['file']) | |
329 msg.attach(part) | |
330 server = smtplib.SMTP('smtp.gmail.com:587') | |
331 server.starttls() | |
332 server.login(username,password) | |
333 server.sendmail(fro, to, msg.as_string() ) | |
334 server.close() | |
335 | |
336 | |
337 class OutPage: | |
338 def index(self): | |
339 return ''' | |
340 <html> | |
341 <body> | |
342 <div id="video"> | |
343 <embed id="roll-video" src="http://www.dafk.net/what/ilovethe80s.swf" width="400" height="300"></embed> | |
344 </div> | |
345 </body> | |
346 </html> | |
347 | |
348 ''' | |
349 index.exposed = True | |
350 | |
351 | |
352 # # Of course we can also mount request handler objects right here! | |
353 # root = HomePage() | |
354 # root.test = TestPage() | |
355 # root.out = OutPage() | |
356 # #root = TestPage() | |
357 # | |
358 # tutconf = os.path.join(os.path.dirname(__file__), 'conf.conf') | |
359 | |
360 | |
361 def startFxTest(args): | |
362 | |
363 # check local switch | |
364 if args.count('-local') : | |
365 local = True; args.remove('-local'); | |
366 print 'Startup: Local mode.' | |
367 conf = os.path.join(os.path.dirname(__file__), 'local.conf') | |
368 else: | |
369 conf = os.path.join(os.path.dirname(__file__), 'server.conf') | |
370 local = False | |
371 | |
372 # check string encoding | |
373 print "Startup: String encoding = '%s'" %(str(sys.getdefaultencoding())) | |
374 | |
375 # check ip switch | |
376 ip = '' | |
377 if args.count('-ip') : | |
378 try : | |
379 # here we just see if it's numeric, parsing is done by the server wrapper | |
380 if args[args.index('-ip')+1].replace('.','').replace(':','').isdigit() : | |
381 ip = args[args.index('-ip')+1] | |
382 else : | |
383 print "Startup: Invalid service IP address. Use format: sawa.py -ip 127.0.0.1:8080" | |
384 os._exit(-1) | |
385 except : | |
386 print "Startup: Invalid service IP address. (Insufficient arguments.) Use format: cherryFxTest.py -ip 127.0.0.1:8080" | |
387 os._exit(-1) | |
388 args.remove(args[args.index('-ip')+1]); | |
389 args.remove('-ip') | |
390 | |
391 | |
392 # old command: | |
393 # cherrypy.quickstart(root, config=conf) | |
394 | |
395 # Of course we can also mount request handler objects right here! | |
396 root = HomePage() | |
397 root.test = TestPage() | |
398 root.daniele = TestPage() | |
399 root.out = OutPage() | |
400 #root = TestPage() | |
401 | |
402 server = CherryPyServer(ServerConfig(root=root,local=local,conf=conf)) | |
403 # if local : | |
404 # server = CherryPyServer(ServerConfig(root=root,local=local,conf=conf)) | |
405 # else : | |
406 # server = CherryPyServer(ServerConfig(root=False,local=local,conf=conf)) | |
407 server.mount(root,path_name='') | |
408 server.quickstart(ip) | |
409 | |
410 | |
411 if __name__ == '__main__': | |
412 # CherryPy always starts with app.root when trying to map request URIs | |
413 # to objects, so we need to mount a request handler root. A request | |
414 # to '/' will be mapped to HelloWorld().index(). | |
415 # cherrypy.quickstart(root, config=tutconf) | |
416 import sys | |
417 startFxTest(sys.argv[1:]) | |
418 else: | |
419 # This branch is for the test suite; you can ignore it. | |
420 cherrypy.tree.mount(root, config=tutconf) |