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