comparison cherry2 copy 2.py @ 0:032bc65ebafc

added core components
author George Fazekas <gyorgy.fazekas@eecs.qmul.ac.uk>
date Wed, 06 Mar 2013 15:45:48 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:032bc65ebafc
1 """
2 Tutorial - Multiple objects
3
4 This tutorial shows you how to create a site structure through multiple
5 possibly nested request handler objects.
6 """
7
8 import cherrypy, os, random
9 from cherrypy.lib.static import serve_file
10 from templates import *
11
12 from DirectorySession import DirectorySession
13
14 #from pprint import pprint
15
16 PATH = os.path.abspath(os.path.dirname(__file__))
17
18
19 #audiodir = './audio'
20 #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']))]
21 #cherrypy.session['fxs'].remove('dry')
22 #print cherrypy.session['fxs']
23
24 class Root(object): pass
25
26
27 class HomePage:
28 def index(self):
29
30
31
32 return "empty"
33
34
35 index.exposed = True
36
37 class TestPage:
38
39 def getFiles(self):
40 audiodir = './audio'
41 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']))]
42 cherrypy.session['fxs'].remove('dry')
43 print cherrypy.session['fxs']
44
45 def index(self):
46 TestPage.getFiles(self)
47 cherrypy.session['sid'] = None
48 cherrypy.session['sid_filename'] = None
49
50 cherrypy.session['test'] = None
51 cherrypy.session['results'] = None
52 cherrypy.session['tests'] = None
53 cherrypy.session['test_nr'] = None
54 cherrypy.session['audio_pairs'] = None
55 cherrypy.session['page'] = None
56 cherrypy.session['pages'] = 4
57 cherrypy.session['userinfobox'] = None
58 #return '''
59 # <p>
60 # <a href="/test/">start test</a>
61 # </p>'''
62 cherrypy.session['sid'] = cherrypy.session.get_session_path().split('/')[1]
63 print cherrypy.session['sid']
64
65
66
67 return welcome_templ %cherrypy.session['sid']
68
69
70
71 def doStart(self, sessionId, startButton, userinfobox=None):
72 cherrypy.session['userinfobox'] = userinfobox
73 cherrypy.session['sessionId'] = sessionId
74 #print "sid:"
75 #print TestPage.sid
76 #print sessionId
77 if cherrypy.session['sid'] != str(cherrypy.session['sessionId']):
78 return TestPage.sessionError(self)
79 else:
80 return TestPage.index2(self)
81
82
83
84
85 def index2(self):
86 try: print testvar
87 except: pass
88 #testvar = "00000000000000000000000000000000000000000000000000000000000000000000000000"
89 #TestPage.sid = TestPage.getSid(self)
90
91 #if TestPage.sid != TestPage.sidstart:
92 # return TestPage.sessionError(self)
93
94
95 #TestPage.sid_filename = 'results/%s.txt'%TestPage.sid
96 #TestPage.sid_userinfofilename = 'results/%s_info.txt'%TestPage.sid
97 #print TestPage.sid_filename
98
99 #cherrypy.session['userinfo'] = userinfo
100
101
102 #cherrypy.session.write_data(userinfo+'\n','results.txt')
103
104 #f = open(TestPage.sid_userinfofilename, 'w')
105 #f.write('%s\n%s'%(TestPage.sid, str(userinfo)))
106 #f.close()
107
108
109 # TestPage.pageComplete = []
110 # for n in xrange(TestPage.pages):
111 # TestPage.pageComplete.append(0)
112
113
114 #take 2 examples for each effect
115
116 cherrypy.session['audio_pairs'] = []
117
118 # get effected files:
119
120 for cherrypy.session['fx'] in cherrypy.session['fxs']:
121 cherrypy.session['dirlist'] = os.listdir('./audio/%s/'%cherrypy.session['fx'])
122 cherrypy.session['filelist'] = []
123 for cherrypy.session['item'] in cherrypy.session['dirlist']:
124 if cherrypy.session['item'].split('.')[-1] == 'mp3':
125 cherrypy.session['filelist'].append(cherrypy.session['item'].split('.')[0])
126
127 # create audio pairs for this fx
128
129 cherrypy.session['first'] = cherrypy.session['second'] = random.choice(cherrypy.session['filelist'])
130 while cherrypy.session['first'] == cherrypy.session['second']:
131 cherrypy.session['second'] = random.choice(cherrypy.session['filelist'])
132
133 cherrypy.session['dry1'] = 'dry/%s' %cherrypy.session['first'].split('_%s'%cherrypy.session['fx'])[0]
134 cherrypy.session['fx1'] = '%s/%s' %(cherrypy.session['first'], cherrypy.session['fx'])
135 cherrypy.session['dry2'] = 'dry/%s' %cherrypy.session['second'].split('_%s'%cherrypy.session['fx'])[0]
136 cherrypy.session['fx2'] = '%s/%s' %(cherrypy.session['fx'], cherrypy.session['second'])
137 cherrypy.session['audio_pairs'].append([cherrypy.session['fx'], cherrypy.session['dry1'], cherrypy.session['fx1']])
138 cherrypy.session['audio_pairs'].append([cherrypy.session['fx'], cherrypy.session['dry2'], cherrypy.session['fx2']])
139
140 #print TestPage.audio_pairs
141 random.shuffle(cherrypy.session['audio_pairs'])
142
143 cherrypy.session['page'] = 1
144 cherrypy.session['pages'] = 4
145 cherrypy.session['tests'] = 10 * cherrypy.session['pages']
146 cherrypy.session['results'] = []
147 for cherrypy.session['n'] in xrange(cherrypy.session['tests']): cherrypy.session['results'].append([None,0,0,0,0])
148
149
150 # create tuple to fill template
151 cherrypy.session['ts'] = []
152 for cherrypy.session['k'] in xrange(40):
153 cherrypy.session['ts'].append(0)
154 cherrypy.session['ts'].append(cherrypy.session['page'])
155 cherrypy.session['ts'].append(cherrypy.session['pages'])
156 for k in xrange(10):
157 pos = 10 * (cherrypy.session['page'] - 1) + k
158 #print pos
159 for n in xrange(3): cherrypy.session['ts'].append(cherrypy.session['audio_pairs'][pos][1])
160 for n in xrange(3): cherrypy.session['ts'].append(cherrypy.session['audio_pairs'][pos][2])
161 cherrypy.session['ts'].append(cherrypy.session['page'])
162 cherrypy.session['ts'].append(cherrypy.session['sid'])
163 cherrypy.session['ts'] = tuple(cherrypy.session['ts'])
164 print cherrypy.session['ts']
165
166 return audiotempl %cherrypy.session['ts']
167
168
169
170
171
172
173
174 def doAttrib(self, pageId, sessionId, submitButton, attribute1_1=None, attribute1_2=None, attribute1_3=None, attribute1_4=None, attribute2_1=None, attribute2_2=None, attribute2_3=None, attribute2_4=None, attribute3_1=None, attribute3_2=None, attribute3_3=None, attribute3_4=None, attribute4_1=None, attribute4_2=None, attribute4_3=None, attribute4_4=None, attribute5_1=None, attribute5_2=None, attribute5_3=None, attribute5_4=None, attribute6_1=None, attribute6_2=None, attribute6_3=None, attribute6_4=None, attribute7_1=None, attribute7_2=None, attribute7_3=None, attribute7_4=None, attribute8_1=None, attribute8_2=None, attribute8_3=None, attribute8_4=None, attribute9_1=None, attribute9_2=None, attribute9_3=None, attribute9_4=None, attribute10_1=None, attribute10_2=None, attribute10_3=None, attribute10_4=None):
175 #print attribute1 + ' ' + attribute2
176 # Error check
177
178
179
180
181
182 print "################################# %s" %pageId
183 #_pageId = int(pageId) - 1
184
185 if cherrypy.session['page'] != int(pageId):
186 print "################################# BACK BUTTON PRESSED #################################"
187 #resString = ',\n'.join(str(x) for x in TestPage.results)
188 #resString += '\n\nERROR: BACK BUTTON PRESSED'
189 #f = open(TestPage.sid_filename, 'w')
190 #f.write(resString)
191 #f.close()
192 #return '''
193 # <p>
194 # <a href="/test/">nooo! you pressed the back button, please start again!</a>
195 # </p>'''
196
197 # instead: overwrite previously submitted data. WILL THE OLD VALUES BE SUBMITTED AGAIN IN ALL BROWSERS?
198 cherrypy.session['page'] = int(pageId)
199
200
201
202 if cherrypy.session['sid'] != str(sessionId):
203 return TestPage.sessionError(self)
204
205
206
207 #store results
208
209 cherrypy.session['pos'] = 10 * (cherrypy.session['page'] - 1)
210
211 cherrypy.session['results'][cherrypy.session['pos']] = [cherrypy.session['audio_pairs'][cherrypy.session['pos']][2].split('/')[-1], int(attribute1_1), int(attribute1_2), int(attribute1_3), int(attribute1_4)]
212 cherrypy.session['results'][cherrypy.session['pos']+1] = [cherrypy.session['audio_pairs'][cherrypy.session['pos']+1][2].split('/')[-1], int(attribute2_1), int(attribute2_2), int(attribute2_3), int(attribute2_4)]
213 cherrypy.session['results'][cherrypy.session['pos']+2] = [cherrypy.session['audio_pairs'][cherrypy.session['pos']+2][2].split('/')[-1], int(attribute3_1), int(attribute3_2), int(attribute3_3), int(attribute3_4)]
214 cherrypy.session['results'][cherrypy.session['pos']+3] = [cherrypy.session['audio_pairs'][cherrypy.session['pos']+3][2].split('/')[-1], int(attribute4_1), int(attribute4_2), int(attribute4_3), int(attribute4_4)]
215 cherrypy.session['results'][cherrypy.session['pos']+4] = [cherrypy.session['audio_pairs'][cherrypy.session['pos']+4][2].split('/')[-1], int(attribute5_1), int(attribute5_2), int(attribute5_3), int(attribute5_4)]
216 cherrypy.session['results'][cherrypy.session['pos']+5] = [cherrypy.session['audio_pairs'][cherrypy.session['pos']+5][2].split('/')[-1], int(attribute6_1), int(attribute6_2), int(attribute6_3), int(attribute6_4)]
217 cherrypy.session['results'][cherrypy.session['pos']+6] = [cherrypy.session['audio_pairs'][cherrypy.session['pos']+6][2].split('/')[-1], int(attribute7_1), int(attribute7_2), int(attribute7_3), int(attribute7_4)]
218 cherrypy.session['results'][cherrypy.session['pos']+7] = [cherrypy.session['audio_pairs'][cherrypy.session['pos']+7][2].split('/')[-1], int(attribute8_1), int(attribute8_2), int(attribute8_3), int(attribute8_4)]
219 cherrypy.session['results'][cherrypy.session['pos']+8] = [cherrypy.session['audio_pairs'][cherrypy.session['pos']+8][2].split('/')[-1], int(attribute9_1), int(attribute9_2), int(attribute9_3), int(attribute9_4)]
220 cherrypy.session['results'][cherrypy.session['pos']+9] = [cherrypy.session['audio_pairs'][cherrypy.session['pos']+9][2].split('/')[-1], int(attribute10_1), int(attribute10_2), int(attribute10_3), int(attribute10_4)]
221
222
223 resString = cherrypy.session['sid'] + '\n' + cherrypy.session['userinfobox'] + '\n' + ',\n'.join(str(x) for x in cherrypy.session['results'])
224
225 #f = open(TestPage.sid_filename, 'w')
226 #f.write(resString)
227 #f.close()
228
229 cherrypy.session.write_data(resString,'%s.txt'%cherrypy.session['sid'])
230
231 if cherrypy.session['page'] < cherrypy.session['pages']:
232 cherrypy.session['page'] += 1
233
234 # create tuple to fill template
235 cherrypy.session['ts'] = []
236 cherrypy.session['npos'] = cherrypy.session['pos'] + 10
237 for cherrypy.session['k'] in xrange(10):
238 for cherrypy.session['i'] in xrange(4):
239 cherrypy.session['ts'].append(cherrypy.session['results'][cherrypy.session['npos']+cherrypy.session['k']][cherrypy.session['i']+1])
240 cherrypy.session['ts'].append(cherrypy.session['page'])
241 cherrypy.session['ts'].append(cherrypy.session['pages'])
242 for cherrypy.session['n'] in xrange(10):
243 pos = 10 * (cherrypy.session['page'] - 1) + cherrypy.session['n']
244 #print pos
245 for n in xrange(3): cherrypy.session['ts'].append(cherrypy.session['audio_pairs'][pos][1])
246 for n in xrange(3): cherrypy.session['ts'].append(cherrypy.session['audio_pairs'][pos][2])
247 cherrypy.session['ts'].append(cherrypy.session['page'])
248 cherrypy.session['ts'].append(cherrypy.session['sid'])
249 cherrypy.session['ts'] = tuple(cherrypy.session['ts'])
250
251 return audiotempl %cherrypy.session['ts']
252 else:
253 #end_filename = 'results/%s_f.txt'%TestPage.sid
254 #os.rename(TestPage.sid_filename, end_filename)
255
256 print cherrypy.session['results']
257 #_results = []
258
259 cherrypy.session['templ'] = ''
260 for cherrypy.session['n'] in xrange(cherrypy.session['tests']):
261 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])
262 cherrypy.session['templ'] += cherrypy.session['_templ']
263
264 #TestPage.pageComplete += 1
265 cherrypy.session['sid'] = None
266 return endtempl%cherrypy.session['templ'] + """\n<a href="/test/">start test</a>"""
267
268 index.exposed = True
269 doAttrib.exposed = True
270 index2.exposed = True
271 doStart.exposed = True
272
273
274 def sessionError(TestPage):
275 print "################################# SESSION ERROR #################################"
276 cherrypy.session['resString'] = str(cherrypy.session['sid']) + '\n' + cherrypy.session['userinfobox'] + '\n' + ',\n'.join(str(x) for x in cherrypy.session['results'])
277 cherrypy.session['resString'] += '\n\nERROR: SESSION CONFUSION'
278 #f = open(TestPage.sid_filename, 'w')
279 #f.write(resString)
280 #f.close()
281 cherrypy.session.write_data(cherrypy.session['resString'],'%s.txt'%cherrypy.session['sid'])
282
283 return '''
284 <p>
285 <a href="/test/">oh noes! there is something wrong with your session, please start again or go for a coffee!</a>
286 </p>'''
287
288
289 # def getSid(TestPage):
290 # result_txts = os.listdir('results')
291 # print result_txts
292 # sid = int(random.random() * 100000000)
293 # sid_filen = '%s.txt'%sid
294 # while sid_filen in result_txts:
295 # sid = int(random.random() * 100000000)
296 # sid_filen = '%s.txt'%sid
297 # return sid
298
299
300 # Of course we can also mount request handler objects right here!
301 root = HomePage()
302 root.test = TestPage()
303 #root = TestPage()
304
305
306 tutconf = os.path.join(os.path.dirname(__file__), 'conf.conf')
307
308 if __name__ == '__main__':
309 # CherryPy always starts with app.root when trying to map request URIs
310 # to objects, so we need to mount a request handler root. A request
311 # to '/' will be mapped to HelloWorld().index().
312 cherrypy.quickstart(root, config=tutconf)
313 else:
314 # This branch is for the test suite; you can ignore it.
315 cherrypy.tree.mount(root, config=tutconf)
316