forked from open-cogsci/OpenSesame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopensesamerun
executable file
·132 lines (121 loc) · 4.55 KB
/
opensesamerun
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/>.
"""
if __name__ == u"__main__":
# First, load a minimum number of modules and show an empty app window. This
# gives the user the feeling of a snappy response.
import os, sys
# Change the working directory on Windows. Depending on whether the
# application has been frozen by py2exe or not we need to use a different
# method of deducing the name to the main script.
# See also http://www.py2exe.org/index.cgi/HowToDetermineIfRunningFromExe
if os.name == "nt":
import imp
if (hasattr(sys, "frozen") or hasattr(sys, "importers") or \
imp.is_frozen("__main__")):
path = os.path.dirname(sys.executable)
else:
path = os.path.dirname(__file__)
if path != '':
os.chdir(path)
if path not in sys.path:
sys.path.append(path)
import libopensesame.misc
libopensesame.misc.parse_environment_file()
import libopensesame.experiment
from libopensesame.py3compat import *
# Parse the command line options
options = libopensesame.misc.opensesamerun_options()
app = None
# If the command line options haven't provided sufficient information to
# run right away, present a GUI
while not libopensesame.misc.opensesamerun_ready(options):
# If PyQt4 is not available (e.g., this might be the case on Mac OS)
# give an error instead of showing a GUI. This makes sure that even
# without PyQt4, people can still run experiments.
try:
# Change Qt API
import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)
from PyQt4 import QtGui, QtCore
except:
libopensesame.misc.messagebox(u"OpenSesame Run",
u"Incorrect or missing options.\n\nRun 'opensesame --help' from a terminal (or command prompt) to see a list of available options, or install Python Qt4 to enable the graphical user interface.")
sys.exit()
# Create the GUI and show it
import libqtopensesame.qtopensesamerun
if app is None:
app = QtGui.QApplication(sys.argv)
myapp = libqtopensesame.qtopensesamerun.qtopensesamerun(options)
QtCore.QObject.connect(app, QtCore.SIGNAL(u"sys.exit()"), myapp.close)
myapp.show()
app.exec_()
# Update the options from the GUI
options = myapp.options
# Exit if the GUI was canceled
if not myapp.run:
sys.exit()
# Decode the experiment path and logfile
experiment = os.path.abspath(options.experiment)
if isinstance(experiment, str):
experiment = safe_decode(experiment,
enc=libopensesame.misc.filesystem_encoding(), errors=u'ignore')
# experiment_path = os.path.dirname(experiment)
logfile = options.logfile
if isinstance(logfile, str):
logfile = safe_decode(logfile,
enc=libopensesame.misc.filesystem_encoding(), errors=u'ignore')
if options.debug:
# In debug mode, don't try to catch any exceptions
exp = libopensesame.experiment.experiment(u"Experiment",
experiment, experiment_path=experiment_path)
exp.set_subject(options.subject)
exp.var.fullscreen = options.fullscreen
exp.logfile = logfile
exp.run()
exp.end()
else:
# Try to parse the experiment from a file
experiment_path = safe_decode(os.path.abspath(options.experiment),
enc=libopensesame.misc.filesystem_encoding())
try:
exp = libopensesame.experiment.experiment(u"Experiment",
experiment, experiment_path=experiment_path)
except Exception as e:
libopensesame.misc.messagebox(u"OpenSesame Run",
libopensesame.misc.strip_tags(e))
sys.exit()
# Set some options
exp.set_subject(options.subject)
exp.var.fullscreen = options.fullscreen
exp.logfile = logfile
# Initialize random number generator
import random
random.seed()
# Try to run the experiment
try:
exp.run()
except Exception as e:
# Try to nicely end the experiment, even though an exception
# occurred.
try:
exp.end()
except Exception as f:
libopensesame.misc.messagebox(u"OpenSesame Run",
libopensesame.misc.strip_tags(f))
libopensesame.misc.messagebox(u"OpenSesame Run",
libopensesame.misc.strip_tags(e))
libopensesame.experiment.clean_up(exp.debug)