From 058b32eaecc9224f2d9b0c66611956ead00eb69b Mon Sep 17 00:00:00 2001 From: Ben Steffensmeier Date: Thu, 3 Oct 2013 18:12:19 -0500 Subject: [PATCH] Issue #2402 Make PythonDecoder more extendable. Former-commit-id: 19329b4f5a621f20bc45beedbe5acaadc84e9c2f --- .../WarningDecoder.py | 39 ++++++++-------- .../res/spring/activetable-common.xml | 2 +- .../uf/edex/python/decoder/PythonDecoder.java | 26 ++++++----- .../python/decoder/TimeOffsetDecoder.java | 45 ++++--------------- .../base/python/DecoderInterface.py | 15 +++---- 5 files changed, 51 insertions(+), 76 deletions(-) diff --git a/edexOsgi/com.raytheon.edex.plugin.warning/WarningDecoder.py b/edexOsgi/com.raytheon.edex.plugin.warning/WarningDecoder.py index d51f38ec3a..2314710f95 100644 --- a/edexOsgi/com.raytheon.edex.plugin.warning/WarningDecoder.py +++ b/edexOsgi/com.raytheon.edex.plugin.warning/WarningDecoder.py @@ -28,14 +28,20 @@ # # SOFTWARE HISTORY # -# Date Ticket# Engineer Description -# ------------ ---------- ----------- -------------------------- -# Initial creation -# Feb 19, 2013 1636 rferrel Use TimeTools to get file timestamp. -# May 07, 2013 1973 rferrel Adjust Issue and Purge times to be relative to start time. -# Jun 24, 2013 DR 16317 D. Friedman If no storm line, parse storm motion from event text. -# Aug 21, 2013 DR16501 m.gamazaychikov Adjusted calculation of Purge time in NoVTECWarningDecoder. -# Sep 12, 2013 DR2249 rferrel When incoming file from warngen adjust start time from file's timestamp. +# Date Ticket# Engineer Description +# ------------- -------- -------------- -------------------------- +# Initial creation +# Feb 19, 2013 1636 rferrel Use TimeTools to get file timestamp. +# May 07, 2013 1973 rferrel Adjust Issue and Purge times to be +# relative to start time. +# Jun 24, 2013 16317 D. Friedman If no storm line, parse storm motion +# from event text. +# Aug 21, 2013 16501 mgamazaychikov Adjusted calculation of Purge time in +# NoVTECWarningDecoder. +# Sep 12, 2013 2249 rferrel When incoming file from warngen adjust +# start time from file's timestamp. +# Oct 03, 2013 2402 bsteffen Make PythonDecoder more extendable. + # # # @author rferrel @@ -1068,7 +1074,12 @@ class WarningDecoder(): self._badVtecRE = r'^\s?/.*/\s?$' self._stdWarningDecode = None - self._command = command + if str == type(command): + command = command.split() + if command and str == type(command[0]) and command[0].startswith('-'): + self._command = [''] + command + else: + self._command = command def decode(self): #discover which type of warning to decode @@ -1082,15 +1093,7 @@ class WarningDecoder(): else: #print "using no vtec warning decoder" decoder = NoVTECWarningDecoder(self.text, self.filePath, self._command) - return decoder.decode() - - def setCommand(self, args): - if str == type(args): - args = args.split() - if args and str == type(args[0]) and args[0].startswith('-'): - args = [''] + args - self._command = args - + return decoder.decode() def _checkForVTEC(self): contents = "" diff --git a/edexOsgi/com.raytheon.uf.edex.activetable/res/spring/activetable-common.xml b/edexOsgi/com.raytheon.uf.edex.activetable/res/spring/activetable-common.xml index ca80e04b60..2c57d6eb99 100644 --- a/edexOsgi/com.raytheon.uf.edex.activetable/res/spring/activetable-common.xml +++ b/edexOsgi/com.raytheon.uf.edex.activetable/res/spring/activetable-common.xml @@ -30,7 +30,7 @@ - + diff --git a/edexOsgi/com.raytheon.uf.edex.python.decoder/src/com/raytheon/uf/edex/python/decoder/PythonDecoder.java b/edexOsgi/com.raytheon.uf.edex.python.decoder/src/com/raytheon/uf/edex/python/decoder/PythonDecoder.java index 753979938d..d2f42fcb94 100644 --- a/edexOsgi/com.raytheon.uf.edex.python.decoder/src/com/raytheon/uf/edex/python/decoder/PythonDecoder.java +++ b/edexOsgi/com.raytheon.uf.edex.python.decoder/src/com/raytheon/uf/edex/python/decoder/PythonDecoder.java @@ -45,11 +45,12 @@ import com.vividsolutions.jts.io.WKTReader; * *
  * SOFTWARE HISTORY
- * Date         Ticket#    Engineer    Description
- * ------------ ---------- ----------- --------------------------
- * Sep 22, 2008            njensen     Initial creation
- * Nov 24, 2008            chammack    Camel Refactor
- * Aug 30, 2013 2298       rjpeter     Make getPluginName abstract
+ * Date          Ticket#  Engineer    Description
+ * ------------- -------- ----------- --------------------------
+ * Sep 22, 2008           njensen     Initial creation
+ * Nov 24, 2008           chammack    Camel Refactor
+ * Aug 30, 2013  2298     rjpeter     Make getPluginName abstract
+ * Oct 03, 2013  2402     bsteffen    Make PythonDecoder more extendable.
  * 
* * @author njensen @@ -79,6 +80,12 @@ public class PythonDecoder extends AbstractDecoder { } public PluginDataObject[] decode(File file) throws Exception { + Map argMap = new HashMap(4); + argMap.put("filePath", file.getPath()); + return decode(argMap); + } + + public PluginDataObject[] decode(Map args) throws Exception { List decodedObjects = new ArrayList( 0); @@ -92,10 +99,8 @@ public class PythonDecoder extends AbstractDecoder { } else { py = cachedInterpreters.get(id); } - HashMap argMap = new HashMap(); - argMap.put("moduleName", moduleName); - argMap.put("fileToDecode", file.getPath()); - List result = (List) py.execute("decode", argMap); + args.put("moduleName", moduleName); + List result = (List) py.execute("decode", args); decodedObjects = asPluginDataObjects(result); } catch (JepException e) { @@ -113,9 +118,6 @@ public class PythonDecoder extends AbstractDecoder { } } - for (PluginDataObject pdo : decodedObjects) { - pdo.constructDataURI(); - } return decodedObjects.toArray(new PluginDataObject[decodedObjects .size()]); } diff --git a/edexOsgi/com.raytheon.uf.edex.python.decoder/src/com/raytheon/uf/edex/python/decoder/TimeOffsetDecoder.java b/edexOsgi/com.raytheon.uf.edex.python.decoder/src/com/raytheon/uf/edex/python/decoder/TimeOffsetDecoder.java index d20692c416..979cdefdb6 100644 --- a/edexOsgi/com.raytheon.uf.edex.python.decoder/src/com/raytheon/uf/edex/python/decoder/TimeOffsetDecoder.java +++ b/edexOsgi/com.raytheon.uf.edex.python.decoder/src/com/raytheon/uf/edex/python/decoder/TimeOffsetDecoder.java @@ -20,16 +20,11 @@ package com.raytheon.uf.edex.python.decoder; import java.io.File; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.Map; -import jep.JepException; - import com.raytheon.edex.esb.Headers; import com.raytheon.uf.common.dataplugin.PluginDataObject; -import com.raytheon.uf.common.python.PythonScript; /** * A PythonDecoder, modified to allow a time offset string (as passed in -z @@ -41,9 +36,10 @@ import com.raytheon.uf.common.python.PythonScript; * * SOFTWARE HISTORY * - * Date Ticket# Engineer Description - * ------------ ---------- ----------- -------------------------- - * Jun 27, 2011 wldougher Initial creation + * Date Ticket# Engineer Description + * ------------- -------- ----------- -------------------------- + * Jun 27, 2011 wldougher Initial creation + * Oct 03, 2013 2402 bsteffen Make PythonDecoder more extendable. * * * @@ -77,9 +73,6 @@ public class TimeOffsetDecoder extends PythonDecoder { public PluginDataObject[] decode(File file, Headers headers) throws Exception { - String moduleName = getModuleName(); - String pluginFQN = getPluginFQN(); - StringBuilder sb = new StringBuilder("cmd -f "); sb.append(file.getPath()); Boolean notifyGFE = (Boolean) headers.get("notifygfe"); @@ -92,33 +85,11 @@ public class TimeOffsetDecoder extends PythonDecoder { } // create an argument map to run the decoder - Map decoderArgs = new HashMap(); - decoderArgs.put("moduleName", moduleName); - decoderArgs.put("fileToDecode", null); - decoderArgs.put("commandArgs", sb.toString()); + Map decoderArgs = new HashMap(4); + decoderArgs.put("filePath", null); + decoderArgs.put("command", sb.toString()); + return decode(decoderArgs); - // We need a new DecoderInterface every time - PythonScript python = PythonDecoderFactory.makePythonDecoder(pluginFQN, - moduleName); - - List result = new ArrayList(0); - try { - // DecoderInterface calls module ctor and module.decode() - result = (List) python.execute("decode", decoderArgs); - } catch (JepException e) { - throw new Exception("Python exception: " + e.getMessage(), e); - } finally { - python.dispose(); - } - - List decodedObjects = asPluginDataObjects(result); - - for (PluginDataObject pdo : decodedObjects) { - pdo.constructDataURI(); - } - - return decodedObjects.toArray(new PluginDataObject[decodedObjects - .size()]); } } diff --git a/edexOsgi/com.raytheon.uf.edex.python.decoder/utility/common_static/base/python/DecoderInterface.py b/edexOsgi/com.raytheon.uf.edex.python.decoder/utility/common_static/base/python/DecoderInterface.py index b40a6ae53b..cd1ee5bd13 100644 --- a/edexOsgi/com.raytheon.uf.edex.python.decoder/utility/common_static/base/python/DecoderInterface.py +++ b/edexOsgi/com.raytheon.uf.edex.python.decoder/utility/common_static/base/python/DecoderInterface.py @@ -28,11 +28,12 @@ from java.util import ArrayList # # # -# SOFTWARE HISTORY +# SOFTWARE HISTORY # -# Date Ticket# Engineer Description -# ------------ ---------- ----------- -------------------------- -# 09/22/08 njensen Initial Creation. +# Date Ticket# Engineer Description +# ------------- -------- ----------- -------------------------- +# Sep 22, 2008 njensen Initial Creation. +# Oct 03, 2013 2402 bsteffen Make PythonDecoder more extendable. # # # @@ -43,11 +44,9 @@ def loadModule(pluginDir, pluginFQN, moduleName): jar = zipimport.zipimporter(jarname) jar.load_module(moduleName) -def decode(moduleName, fileToDecode, commandArgs=None): +def decode(moduleName, **kwargs): mod = sys.modules[moduleName] - exec 'dec = mod.' + moduleName + '(filePath=fileToDecode)' - if commandArgs is not None: - dec.setCommand(commandArgs) + exec 'dec = mod.' + moduleName + '(**kwargs)' result = dec.decode() resultList = ArrayList() if result is not None: