added datafile and filename to script actions
This commit is contained in:
2013-09-23 15:34:31 +02:00
parent 3025f597c0
commit 879ec1d657
3 changed files with 58 additions and 4 deletions
@@ -513,6 +513,7 @@ public class Acquisition {
if(configuration.getData()!=null){ // Safety
manipulation.setVariable("FILENAME", configuration.getData().getFileName());
manipulation.setVariable("DATAFILE", datafile.getAbsoluteFile());
}
this.manipulations.add(manipulation);
@@ -626,7 +627,9 @@ public class Acquisition {
}
else if(a instanceof ShellAction){
ShellAction sa = (ShellAction) a;
ch.psi.fda.core.actions.ShellAction action = new ch.psi.fda.core.actions.ShellAction(sa.getCommand());
String com = sa.getCommand().replaceAll("\\$\\{DATAFILE\\}", datafile.getAbsolutePath());
com = com.replaceAll("\\$\\{FILENAME\\}", datafile.getName().replaceAll("\\.\\w*$", ""));
ch.psi.fda.core.actions.ShellAction action = new ch.psi.fda.core.actions.ShellAction(com);
action.setCheckExitValue(sa.isCheckExitValue());
action.setExitValue(sa.getExitValue());
alist.add(action);
@@ -634,6 +637,9 @@ public class Acquisition {
else if(a instanceof ScriptAction){
ScriptAction sa = (ScriptAction) a;
// TODO set global variables DATAFILE and FILENAME
// TODO create Jython Action
List<JythonParameterMappingChannel> mapping = new ArrayList<JythonParameterMappingChannel>();
for(ChannelParameterMapping ma: sa.getMapping()){
@@ -650,7 +656,11 @@ public class Acquisition {
logger.warning("Channel type ["+ma.getType()+"] is not supported for mapping");
}
}
alist.add(new ch.psi.fda.core.actions.JythonAction(sa.getScript(), mapping));
ch.psi.fda.core.actions.JythonAction ja = new ch.psi.fda.core.actions.JythonAction(sa.getScript(), mapping);
ja.setVariable("FILENAME", datafile.getName().replaceAll("\\.\\w*$", ""));
ja.setVariable("DATAFILE", datafile.getAbsoluteFile());
alist.add(ja);
}
}
return(alist);
@@ -21,7 +21,9 @@ package ch.psi.fda.core.actions;
import gov.aps.jca.CAException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -60,6 +62,8 @@ public class JythonAction implements Action {
* Jython entry call
*/
private String jythonCall;
private Map<String,Object> gvariables = new HashMap<String,Object>();
public JythonAction(String script, List<JythonParameterMappingChannel> mapping){
@@ -76,7 +80,12 @@ public class JythonAction implements Action {
String[] functionParameters = null;
if(matcher.find() && matcher.groupCount()==1){
logger.finest("Entry function '"+entryFunctionPattern+"' found - Identified parameters: "+matcher.group(1));
functionParameters = matcher.group(1).split(" *, *");
if(matcher.group(1).matches(" *")){
functionParameters = new String[0];
}
else{
functionParameters = matcher.group(1).split(" *, *");
}
}
else{
throw new IllegalArgumentException("Cannot determine entry function: "+entryFunctionPattern);
@@ -112,7 +121,7 @@ public class JythonAction implements Action {
StringBuffer buffer = new StringBuffer();
buffer.append(entryFunction);
buffer.append("(");
buffer.append("( "); // Need to have trailing space as otherwise there will be a problem if no paramters are specified
for(JythonParameterMappingChannel b: mapping){
// Create channel
@@ -142,6 +151,15 @@ public class JythonAction implements Action {
*/
@Override
public void execute() {
// Set global variables - WORKAROUND gvariables
// This block is not in initialization as we want to assure that all invocations
// of this manipulation will get the same value (i.e. to prevent inconsistent behaviour
// if variable was changed during an execution of the manipulation)
for(String k: gvariables.keySet()){
engine.put(k, gvariables.get(k));
}
try {
engine.eval(jythonCall);
} catch (ScriptException e) {
@@ -166,4 +184,13 @@ public class JythonAction implements Action {
// Nothing to be done
}
/**
* Workaround to put variables into the jython engine.
* @param name
* @param value
*/
public void setVariable(String name, Object value){
gvariables.put(name, value);
}
}
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration numberOfExecution="0" xmlns="http://www.psi.ch/~ebner/models/scan/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.psi.ch/~ebner/models/scan/1.0 ../../src/model-v1.xsd">
<!--
1D Scan reading out scalar channels and the timestamp for each point
-->
<data format="txt"/>
<scan id="">
<preAction xsi:type="ShellAction" command="/bin/echo hello ${DATAFILE} ${FILENAME}"/>
<preAction xsi:type="ScriptAction">
<script>
def process( ):
print "filename __ %s" % FILENAME
print "data __ %s" % DATAFILE
</script>
</preAction>
</scan>
</configuration>