forked from open-cogsci/OpenSesame
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopensesameandroid.py
executable file
·132 lines (106 loc) · 3.11 KB
/
opensesameandroid.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
#!/usr/bin/env python
#-*- coding:utf-8 -*-
"""
This file is part of OpenSesame.
OpenSesame is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenSesame is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenSesame. If not, see <http://www.gnu.org/licenses/>.
"""
import os
import sys
import pygame
import traceback
import yaml
from libopensesame.experiment import experiment, clean_up
try:
import android
except:
android = None
sdcard_folders = [
'/sdcard/',
'/mnt/sdcard/'
]
class stdout_file(object):
"""A class that redirects the standard output to a file"""
def __init__(self, stdout):
"""
Constructor
Arguments:
stdout -- the original standard output
"""
self.stdout = stdout
# Try to auto-detect the sdcard location and create a text file there
for path in sdcard_folders:
if os.path.isdir(path):
break
try:
self.fd = open(os.path.join(path, 'opensesame-debug.txt'), 'w')
except:
self.stdout.write('Failed to create %s' % path)
self.fd = None
def write(self, s):
"""
Write a message to the standard output
Arguments:
s -- the message to write
"""
self.stdout.write(s+'\n')
if self.fd != None:
self.fd.write(s)
self.fd.flush()
def main():
"""The main routine, which is called automatically by pgs4a"""
if android != None:
sys.stdout = stdout_file(sys.stdout)
# First check if an autorun file has been specified. This is a yaml file
# with the experiment path, subject nr, and logfile in it.
for folder in sdcard_folders:
path = os.path.join(folder, 'opensesame-autorun.yml')
print path
if os.path.exists(path):
d = yaml.load(open(path))
experiment_path = d['experiment']
subject_nr = d['subject_nr']
logfile = d['logfile']
break
# If no autorun file has been specified, we launch the menu experiment.
else:
src = 'resources/android/menu.opensesame'
print('Launching %s' % src)
menu = experiment('Experiment', src)
menu.run()
menu.end()
clean_up(menu.debug)
experiment_path = menu._experiment
subject_nr = menu._subject_nr
logfile = menu._logfile
# Next run the actual experiment!
exp = experiment('Experiment', experiment_path)
print('Launching %s' % experiment_path)
exp.set_subject(subject_nr)
exp.logfile = logfile
# Capture exceptions and write them to the standard output so they can be
# inspected
try:
exp.run()
except Exception as e:
for s in traceback.format_exc(e).split("\n"):
print(s)
try:
exp.end()
except Exception as e:
for s in traceback.format_exc(e).split("\n"):
print(s)
clean_up(exp.debug)
pygame.display.quit()
if __name__ == "__main__":
# This is only necessary for testing on a regular PC. On Android, the main()
# function is called automatically.
main()