Issue #2402 Make PythonDecoder more extendable.

Former-commit-id: 19329b4f5a621f20bc45beedbe5acaadc84e9c2f
This commit is contained in:
Ben Steffensmeier 2013-10-03 18:12:19 -05:00
parent 36f5c2c44b
commit 058b32eaec
5 changed files with 51 additions and 76 deletions

View file

@ -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.
# </pre>
#
# @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 = ""

View file

@ -30,7 +30,7 @@
<property name="pluginName" value="warning" />
<property name="pluginFQN" value="com.raytheon.edex.plugin.warning" />
<property name="moduleName" value="WarningDecoder" />
<property name="cache" value="true"/>
<property name="cache" value="false"/>
<property name="recordClassname"
value="com.raytheon.uf.common.dataplugin.warning.WarningRecord" />
</bean>

View file

@ -45,11 +45,12 @@ import com.vividsolutions.jts.io.WKTReader;
*
* <pre>
* 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.
* </pre>
*
* @author njensen
@ -79,6 +80,12 @@ public class PythonDecoder extends AbstractDecoder {
}
public PluginDataObject[] decode(File file) throws Exception {
Map<String, Object> argMap = new HashMap<String, Object>(4);
argMap.put("filePath", file.getPath());
return decode(argMap);
}
public PluginDataObject[] decode(Map<String, Object> args) throws Exception {
List<PluginDataObject> decodedObjects = new ArrayList<PluginDataObject>(
0);
@ -92,10 +99,8 @@ public class PythonDecoder extends AbstractDecoder {
} else {
py = cachedInterpreters.get(id);
}
HashMap<String, Object> argMap = new HashMap<String, Object>();
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()]);
}

View file

@ -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.
*
* </pre>
*
@ -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<String, Object> decoderArgs = new HashMap<String, Object>();
decoderArgs.put("moduleName", moduleName);
decoderArgs.put("fileToDecode", null);
decoderArgs.put("commandArgs", sb.toString());
Map<String, Object> decoderArgs = new HashMap<String, Object>(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<Object>(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<PluginDataObject> decodedObjects = asPluginDataObjects(result);
for (PluginDataObject pdo : decodedObjects) {
pdo.constructDataURI();
}
return decodedObjects.toArray(new PluginDataObject[decodedObjects
.size()]);
}
}

View file

@ -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: