def compileScript(infile=None, version=None, outfile=None): """ This function will compile either Python or JS PsychoPy script from .psyexp file. :param infile: The input (psyexp) file to be compiled :param version: The PsychoPy version to use for compiling the script. e.g. 1.84.1. Warning: Cannot set version if module imported. Set version from command line interface only. :param outfile: The output (py) file to be generated (defaults to Python script. """ if __name__ != '__main__' and version not in [None, 'None', 'none', '']: version = None msg = "You cannot set version by calling compileScript() manually. Setting 'version' to None." logging.warning(msg) # Check infile type if isinstance(infile, experiment.Experiment): thisExp = infile else: thisExp = experiment.Experiment() thisExp.loadFromXML(infile) # Write version to experiment init text thisExp.psychopyVersion = version # Set output type, either JS or Python if outfile.endswith(".js"): targetOutput = "PsychoJS" else: targetOutput = "PsychoPy" # Write script script = thisExp.writeScript(outfile, target=targetOutput) outfile.replace('.py', targetOutput[-2:].lower()) # Output script to file with codecs.open(outfile, 'w', 'utf-8') as f: f.write(script.getvalue()) f.close()
def _getExperiment(infile, version): """ Parameters ---------- infile: string, experiment.Experiment object The input (psyexp) file to be compiled version: string The version requested Returns ------- experiment.Experiment The experiment object used for generating the experiment script """ # import PsychoPy experiment and write script with useVersion active from psychopy.app.builder import experiment # Check infile type if isinstance(infile, experiment.Experiment): thisExp = infile else: thisExp = experiment.Experiment() thisExp.loadFromXML(infile) thisExp.psychopyVersion = version return thisExp
def test_blocked(self): # load experiment exp = experiment.Experiment() exp.loadFromXML(join(demosDir, 'builder', 'images_blocks', 'blockedTrials.psyexp')) # try once packaging up the js libs exp.settings.params['JS libs'].val = 'packaged' outFolder = join(self.temp_dir, 'blocked_packaged') self.writeScript(exp, outFolder) print("files in {}".format(outFolder))
def compileScript(infile=None, version=None, outfile=None): """ This function will compile either Python or JS PsychoPy script from .psyexp file. :param infile: The input (psyexp) file to be compiled :param version: The PsychoPy version to use for compiling the script. e.g. 1.84.1. Warning: Cannot set version if module imported. Set version from command line interface only. :param outfile: The output (py) file to be generated (defaults to Python script. """ if __name__ != '__main__' and version not in [None, 'None', 'none', '']: version = None msg = "You cannot set version by calling compileScript() manually. Setting 'version' to None." logging.warning(msg) # Check infile type if isinstance(infile, experiment.Experiment): thisExp = infile else: thisExp = experiment.Experiment() thisExp.loadFromXML(infile) # Write version to experiment init text thisExp.psychopyVersion = version # Set output type, either JS or Python if outfile.endswith(".js"): targetOutput = "PsychoJS" else: targetOutput = "PsychoPy" # Write script if targetOutput == "PsychoJS": # Write module JS code script = thisExp.writeScript(outfile, target=targetOutput, modular=True) # Write no module JS code outfileNoModule = outfile.replace( '.js', 'NoModule.js') # For no JS module script scriptNoModule = thisExp.writeScript(outfileNoModule, target=targetOutput, modular=False) # Store scripts in list scriptDict = {'outfile': script, 'outfileNoModule': scriptNoModule} else: script = thisExp.writeScript(outfile, target=targetOutput) scriptDict = {'outfile': script} # Output script to file for scripts in scriptDict: with codecs.open(eval(scripts), 'w', 'utf-8') as f: f.write(scriptDict[scripts]) f.close()
def test_stroop(self): #load experiment exp = experiment.Experiment() exp.loadFromXML(join(demosDir, 'builder','stroop','stroop.psyexp')) # try once packaging up the js libs exp.settings.params['JS libs'].val = 'packaged' outFolder = join(self.temp_dir, 'stroopJS_packaged') self.writeScript(exp, outFolder) # try once packaging up the js libs exp.settings.params['JS libs'].val = 'remote' outFolder = join(self.temp_dir, 'stroopJS_remote') self.writeScript(exp, outFolder) print("files in {}".format(outFolder))
import argparse parser = argparse.ArgumentParser( description='Compile your python file from here') parser.add_argument('infile', help='The input (psyexp) file to be compiled') parser.add_argument( '--version', '-v', help='The PsychoPy version to use for compiling the script. e.g. 1.84.1') parser.add_argument( '--outfile', '-o', help='The output (py) file to be generated (defaults to the ') args = parser.parse_args() if args.outfile is None: args.outfile = args.infile.replace(".psyexp", ".py") print(args) if args.version: from psychopy import useVersion useVersion(args.version) from psychopy.app.builder import experiment exp = experiment.Experiment(filename=infile) if args.outfile.endswith(".html"): exp.writeScript(args.outfile, target="PsychoJS") else: exp.writeScript(args.outfile, target="PsychoPy")
'--outfile', '-o', help='The output (py) file to be generated (defaults to the ') args = parser.parse_args() if args.outfile is None: args.outfile = args.infile.replace(".psyexp", ".py") # Set version if args.version: from psychopy import useVersion useVersion(args.version) # Import requested version of experiment from psychopy.app.builder import experiment # Set experiment object according to version thisExp = experiment.Experiment() thisExp.loadFromXML(args.infile) # Write version to experiment init text thisExp.psychopyVersion = args.version # Set output type, either JS or Python if args.outfile.endswith(".html"): targetOutput = "PsychoJS" else: targetOutput = "PsychoPy" # Write script script = thisExp.writeScript(args.outfile, target=targetOutput) args.outfile.replace('.py', targetOutput[-2:].lower()) # Output script to file f = codecs.open(args.outfile, 'w', 'utf-8') f.write(script.getvalue()) f.close()