view 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
line wrap: on
line source
"""
Tutorial - Multiple objects

This tutorial shows you how to create a site structure through multiple
possibly nested request handler objects.
"""

import cherrypy, os, random
from cherrypy.lib.static import serve_file
from templates import *

from DirectorySession import DirectorySession

#from pprint import pprint

PATH = os.path.abspath(os.path.dirname(__file__))


audiodir = './audio'
fxs = [f for f in os.listdir(audiodir) if os.path.isdir(os.path.join(audiodir, f))]
fxs.remove('dry')
print fxs
	
class Root(object): pass


class HomePage:
	def index(self):
		#return ''' <p> <a href="/test/">start test </a> </p>'''
		
		#print cherrypy.session.get_session_name()
		#cherry.session.write_data()

		#print cherrypy.session['gfdxgfdx']

		#cherrypy.session['x'] = [1,2,3]
		#print cherrypy.session['x']
		#x = cherrypy.session['x']
		#x.append(5)
		#print cherrypy.session['x']
		return "empty"
		

	index.exposed = True

class TestPage:

	sid = None
	sid_filename = None

	test = None
	results = None
	tests = None
	test_nr = None
	audio_pairs = None
	page = None
	pages = 4
	pageComplete = None
	userInfo = None
	
	def index(self):
		#return '''
		#	<p>
		#	<a href="/test/">start test</a>
		#	</p>'''
		TestPage.sid = TestPage.getSid(self)
		print TestPage.sid
		return welcome_templ %TestPage.sid

	
	def doStart(self, sessionId, startButton, userinfobox=None):
		print "sid:"
		print TestPage.sid
		print sessionId
		if TestPage.sid != int(sessionId):
			return TestPage.sessionError(self)
		else:
			return TestPage.index2(self, userinfobox)




	def index2(self, userinfo):
		
		#TestPage.sid = TestPage.getSid(self)
		
		#if TestPage.sid != TestPage.sidstart:
		#	return TestPage.sessionError(self)
		
		
		TestPage.sid_filename = 'results/%s.txt'%TestPage.sid
		TestPage.sid_userinfofilename = 'results/%s_info.txt'%TestPage.sid
		print TestPage.sid_filename
		
		f = open(TestPage.sid_userinfofilename, 'w')
		f.write('%s\n%s'%(TestPage.sid, str(userinfo)))
		f.close()
		
		
		TestPage.pageComplete = []
		for n in xrange(TestPage.pages): 
			TestPage.pageComplete.append(0)
		
		
		#take 2 examples for each effect

		TestPage.audio_pairs = []
		
		# get effected files:
			
		for fx in fxs:
			dirlist = os.listdir('./audio/%s/'%fx)
			filelist = []
			for item in dirlist:
				if item.split('.')[-1] == 'mp3':
					filelist.append(item.split('.')[0])
			
			# create audio pairs for this fx
			
			first = second = random.choice(filelist)
			while first==second:
				second = random.choice(filelist)

			dry1 = 'dry/%s' %first.split('_%s'%fx)[0]
			fx1 = '%s/%s' %(fx, first)
			dry2 = 'dry/%s' %second.split('_%s'%fx)[0]
			fx2 = '%s/%s' %(fx, second)
			TestPage.audio_pairs.append([fx, dry1, fx1])
			TestPage.audio_pairs.append([fx, dry2, fx2])
		
		#print TestPage.audio_pairs
		random.shuffle(TestPage.audio_pairs)

		TestPage.page = 1
		TestPage.pages = 4
		TestPage.tests = 10 * TestPage.pages
		TestPage.results = []
		for n in xrange(TestPage.tests): TestPage.results.append([None,0,0,0,0])

		# create tuple to fill template
		ts = []
		for k in xrange(40):
			ts.append(0)
		ts.append(TestPage.page)
		ts.append(TestPage.pages)

		for k in xrange(10):
			pos = 10 * (TestPage.page - 1) + k
			#print pos
			for n in xrange(3): ts.append(TestPage.audio_pairs[pos][1])
			for n in xrange(3): ts.append(TestPage.audio_pairs[pos][2])
			
		ts.append(TestPage.page)
		ts.append(TestPage.sid)
		print ts
		ts = tuple(ts)
		
		return audiotempl %ts
	
	

	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):
		#print attribute1 + ' ' + attribute2
		# Error check
 			
		print "################################# %s" %pageId
		_pageId = int(pageId) - 1

		if TestPage.page != int(pageId):
			print "################################# BACK BUTTON PRESSED #################################"
			#resString = ',\n'.join(str(x) for x in TestPage.results)
			#resString += '\n\nERROR: BACK BUTTON PRESSED'
			#f = open(TestPage.sid_filename, 'w')
			#f.write(resString)
			#f.close()
			#return '''
			#	<p>
			#	<a href="/test/">nooo! you pressed the back button, please start again!</a>
			#	</p>'''
			
			# instead: overwrite previously submitted data. WILL THE OLD VALUES BE SUBMITTED AGAIN IN ALL BROWSERS?
		TestPage.page = int(pageId)
		
		
				
		if TestPage.sid != int(sessionId):
			return TestPage.sessionError(self)
			

		
		#store results
		
		pos = 10 * (TestPage.page - 1)		
	 
		TestPage.results[pos] = [TestPage.audio_pairs[pos][2].split('/')[-1], int(attribute1_1), int(attribute1_2), int(attribute1_3), int(attribute1_4)]
		TestPage.results[pos+1] = [TestPage.audio_pairs[pos+1][2].split('/')[-1], int(attribute2_1), int(attribute2_2), int(attribute2_3), int(attribute2_4)]
		TestPage.results[pos+2] = [TestPage.audio_pairs[pos+2][2].split('/')[-1], int(attribute3_1), int(attribute3_2), int(attribute3_3), int(attribute3_4)]
		TestPage.results[pos+3] = [TestPage.audio_pairs[pos+3][2].split('/')[-1], int(attribute4_1), int(attribute4_2), int(attribute4_3), int(attribute4_4)]
		TestPage.results[pos+4] = [TestPage.audio_pairs[pos+4][2].split('/')[-1], int(attribute5_1), int(attribute5_2), int(attribute5_3), int(attribute5_4)]
		TestPage.results[pos+5] = [TestPage.audio_pairs[pos+5][2].split('/')[-1], int(attribute6_1), int(attribute6_2), int(attribute6_3), int(attribute6_4)]
		TestPage.results[pos+6] = [TestPage.audio_pairs[pos+6][2].split('/')[-1], int(attribute7_1), int(attribute7_2), int(attribute7_3), int(attribute7_4)]
		TestPage.results[pos+7] = [TestPage.audio_pairs[pos+7][2].split('/')[-1], int(attribute8_1), int(attribute8_2), int(attribute8_3), int(attribute8_4)]
		TestPage.results[pos+8] = [TestPage.audio_pairs[pos+8][2].split('/')[-1], int(attribute9_1), int(attribute9_2), int(attribute9_3), int(attribute9_4)]
		TestPage.results[pos+9] = [TestPage.audio_pairs[pos+9][2].split('/')[-1], int(attribute10_1), int(attribute10_2), int(attribute10_3), int(attribute10_4)]
		
		resString = ',\n'.join(str(x) for x in TestPage.results)
		
		f = open(TestPage.sid_filename, 'w')
		f.write(resString)
		f.close()
		
		
		if TestPage.page < TestPage.pages:
			TestPage.page += 1
			
			# create tuple to fill template
			ts = []
			
			npos = pos + 10
			for k in xrange(10):
				for i in xrange(4):
					ts.append(TestPage.results[npos+k][i+1])

			ts.append(TestPage.page)
			ts.append(TestPage.pages)
			
			for k in xrange(10):
				pos = 10 * (TestPage.page - 1) + k
				#print pos
				for n in xrange(3): ts.append(TestPage.audio_pairs[pos][1])
				for n in xrange(3): ts.append(TestPage.audio_pairs[pos][2])
			ts.append(TestPage.page)
			ts.append(TestPage.sid)
			ts = tuple(ts)
		
			#print TestPage.audio_pairs[TestPage.test-1][2]
			#TestPage.pageComplete += 1
			return audiotempl %ts
		else:
			end_filename = 'results/%s_f.txt'%TestPage.sid
			os.rename(TestPage.sid_filename, end_filename)
			
			print TestPage.results
			_results = []
			
			templ = ''
			for n in xrange(TestPage.tests):
				_templ = endtempl2 %(TestPage.results[n][0], TestPage.results[n][1], TestPage.results[n][2], TestPage.results[n][3], TestPage.results[n][4])
				templ += _templ 

			#TestPage.pageComplete += 1
			TestPage.sid = None
			return endtempl%templ
			
	index.exposed = True
	doAttrib.exposed = True
	index2.exposed = True
	doStart.exposed = True
	
	
	def sessionError(TestPage):
		print "################################# SESSION ERROR #################################"
		resString = ',\n'.join(str(x) for x in TestPage.results)
		resString += '\n\nERROR: SESSION CONFUSION'
		f = open(TestPage.sid_filename, 'w')
		f.write(resString)
		f.close()
		return '''
			<p>
			<a href="/test/">oh noes! there is something wrong with your session, please start again or go for a coffee!</a>
			</p>'''
			
	
	def getSid(TestPage):
		result_txts = os.listdir('results')
		print result_txts
		sid = int(random.random() * 100000000)
		sid_filen = '%s.txt'%sid
		while sid_filen in result_txts:
			sid = int(random.random() * 100000000)
			sid_filen = '%s.txt'%sid
		return sid
		

# Of course we can also mount request handler objects right here!
root = HomePage()
root.test = TestPage()
#root = TestPage()


tutconf = os.path.join(os.path.dirname(__file__), 'conf.conf')

if __name__ == '__main__':
	# CherryPy always starts with app.root when trying to map request URIs
	# to objects, so we need to mount a request handler root. A request
	# to '/' will be mapped to HelloWorld().index().
	cherrypy.quickstart(root, config=tutconf)
else:
	# This branch is for the test suite; you can ignore it.
	cherrypy.tree.mount(root, config=tutconf)