diff --git a/src/main/java/edu/ucsb/nceas/mdqengine/dispatch/Dispatcher.java b/src/main/java/edu/ucsb/nceas/mdqengine/dispatch/Dispatcher.java index f0d9894f..46022cdd 100644 --- a/src/main/java/edu/ucsb/nceas/mdqengine/dispatch/Dispatcher.java +++ b/src/main/java/edu/ucsb/nceas/mdqengine/dispatch/Dispatcher.java @@ -132,6 +132,7 @@ public Result dispatch(Map variables, String code) throws Script Object out_py = null; Object out_ids = null; Object out_type = null; + Object out_status = null; // do we have a result object from an R check? try { var_r = engine.get("mdq_result"); @@ -169,8 +170,8 @@ public Result dispatch(Map variables, String code) throws Script } // save the output if (out_py != null && !out_py.toString().equals("")) { - - if (out_py instanceof ArrayList){ + + if (out_py instanceof ArrayList) { ArrayList outputList = new ArrayList<>(); ArrayList out_py_l = (ArrayList) out_py; @@ -179,32 +180,37 @@ public Result dispatch(Map variables, String code) throws Script for (int i = 0; i < out_py_l.size(); i++) { Output o = new Output(out_py_l.get(i).toString()); - + String id = out_ids_l.get(i).toString(); String type = out_type_l.get(i).toString(); - + o.setIdentifier(id); o.setType(type); outputList.add(o); } - dr.setOutput(outputList); + dr.setOutput(outputList); } else { Output o = new Output(out_py.toString()); dr.setOutput(o); } - + } + // if we didn't get any "normal" output from python grab whatever got returned + if (out != null & out_py == null) { + Output o = new Output(out.toString()); + dr.setOutput(o); + } // try to get the global status variable from python try { - out_py = engine.get("status"); + out_status = engine.get("status"); } catch (Exception e) { // catch this silently since we are just fishing // the no result case is handled later log.trace("No result found for python check variable variable status."); } // save the status - if (out_py != null && !out_py.toString().equals("")) { - dr.setStatus(Status.valueOf(out_py.toString())); + if (out_status != null && !out_status.toString().equals("")) { + dr.setStatus(Status.valueOf(out_status.toString())); } else { // if we haven't found anything at this point it probably failed dr.setStatus(Status.FAILURE); diff --git a/src/test/java/edu/ucsb/nceas/mdqengine/dispatch/PythonDispatcherTest.java b/src/test/java/edu/ucsb/nceas/mdqengine/dispatch/PythonDispatcherTest.java index ed6e2617..0389d75a 100644 --- a/src/test/java/edu/ucsb/nceas/mdqengine/dispatch/PythonDispatcherTest.java +++ b/src/test/java/edu/ucsb/nceas/mdqengine/dispatch/PythonDispatcherTest.java @@ -32,13 +32,13 @@ public class PythonDispatcherTest { public static void setupOnce() { try { Dispatcher.setupJep(); - } catch (MetadigException me){ + } catch (MetadigException me) { fail("Setup failed with MetadigException: " + me.getMessage()); } } @Before - public void init(){ + public void init() { dispatcher = Dispatcher.getDispatcher("python"); } @@ -48,7 +48,7 @@ public void testTypes() { names.put("myInt", XMLDialect.retypeObject("2")); names.put("myFloat", XMLDialect.retypeObject("1.5")); - names.put("myBool", XMLDialect.retypeObject("true")); + names.put("myBool", XMLDialect.retypeObject("True")); names.put("myStr", XMLDialect.retypeObject("hello")); String code = "output = (type(myInt) is int) and (type(myFloat) is float) and (type(myBool) is bool)"; @@ -60,7 +60,7 @@ public void testTypes() { e.printStackTrace(); fail(e.getMessage()); } - assertEquals("true", result.getOutput().get(0).getValue()); + assertEquals("True", result.getOutput().get(0).getValue()); } @Test @@ -92,7 +92,7 @@ public void testEquality() { Map names = new HashMap(); names.put("x", 2); names.put("y", 2); - String code = "x == y"; + String code = "def call(): return(x == y)\n"; Result result = null; try { result = dispatcher.dispatch(names, code); @@ -146,7 +146,8 @@ public void testCache() { names.put("y", 2); InputStream library = this.getClass().getResourceAsStream("/code/mdq-cache.py"); - String code = "def call(): \n" + String code = "global output \n" + + "def call(): \n" + " return get('" + dataUrl + "') \n"; Result result = null; @@ -158,6 +159,7 @@ public void testCache() { e.printStackTrace(); fail(e.getMessage()); } + result.getOutput(); // make sure the file is named as expected assertTrue(result.getOutput().get(0).getValue().endsWith(DigestUtils.md5Hex(dataUrl))); }