Issue #2329 Cleaned up combinations file saving

Change-Id: I61eea505c36e10239dbb3147bfcc46e513504550

Former-commit-id: aff50967f0 [formerly 7dad82b511] [formerly 68f868117f] [formerly aff50967f0 [formerly 7dad82b511] [formerly 68f868117f] [formerly d069127c28 [formerly 68f868117f [formerly aeda31d9b935af3ae0acadc73016f4c22eb8bbd0]]]]
Former-commit-id: d069127c28
Former-commit-id: c2a33a9585 [formerly 86bf3eeca8] [formerly 6f1186c38aec1d3e5b47eb5ed294059389011ecd [formerly 85c5827504]]
Former-commit-id: ade23be39fcd3d20c590752441b61f66d9ca3952 [formerly a4b214e968]
Former-commit-id: d843a1e2b1
This commit is contained in:
Ron Anderson 2013-09-05 13:36:30 -05:00 committed by Steve Harris
parent 199bee9437
commit a8b34aaf40
6 changed files with 151 additions and 264 deletions

View file

@ -28,17 +28,17 @@
# Date Ticket# Engineer Description
# ------------ ---------- ----------- --------------------------
# 07/25/08 njensen Initial Creation.
#
# 09/05/13 #2329 randerso Added error handling
#
#
import sys
import sys, traceback, os, time, LogStream
from java.util import ArrayList
def getCombinations(comboName):
try:
outercombos = ArrayList()
cmd = "md = __import__(\"" + comboName + "\")"
exec cmd
md = __import__(comboName)
comList = md.Combinations
for i in comList:
combos = ArrayList()
@ -48,4 +48,19 @@ def getCombinations(comboName):
outercombos.add(combos)
return outercombos
except AttributeError as e:
filename = md.__file__
if filename.endswith("pyc") or filename.endswith("pyo"):
filename = filename[:-1]
with open(filename,'r') as fd:
filecontents = fd.read()
LogStream.logProblem("\nERROR loading combinations file: "+ comboName +
"\nmd.__file__: " + md.__file__ +
"\ndir(md): " + str(dir(md)) +
"\n" + md.__file__ + " last modified: " + time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime(os.path.getmtime(md.__file__))) +
"\n" + filename + " last modified: " + time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime(os.path.getmtime(filename))) +
"\nContents of " + filename + "\n" + filecontents)
raise e

View file

@ -61,6 +61,8 @@ import com.raytheon.viz.gfe.textformatter.TextProductManager;
* 26 SEP 2012 15423 ryu Fix product correction in practice mode
* 15 MAY 2013 1842 dgilling Change constructor signature to accept a
* DataManager instance.
* 05 SEP 2013 2329 randerso Added call to ZoneCombinerComp.applyZoneCombo when
* when run formatter button is clicked.
*
* </pre>
*
@ -386,12 +388,12 @@ public class ProductAreaComp extends Composite implements
// use
// it, else use the default
String dbId = null;
// zoneCombinerComp.compactList();
zoneCombiner.applyZoneCombo();
dbId = ((FormatterLauncherDialog) productTabCB)
.getSelectedDataSource(productName);
FormatterUtil.runFormatterScript(textProductMgr,
productName, zoneCombiner.getZoneGroupings(),
dbId, vtecMode, ProductAreaComp.this);
productName, dbId, vtecMode,
ProductAreaComp.this);
}
}
});

View file

@ -22,16 +22,12 @@ package com.raytheon.viz.gfe.dialogs.formatterlauncher;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import org.eclipse.jface.preference.IPreferenceStore;
@ -61,6 +57,7 @@ import org.opengis.referencing.FactoryException;
import org.opengis.referencing.operation.TransformException;
import com.raytheon.uf.common.dataplugin.gfe.db.objects.GridLocation;
import com.raytheon.uf.common.dataplugin.gfe.exception.GfeException;
import com.raytheon.uf.common.localization.FileUpdatedMessage;
import com.raytheon.uf.common.localization.FileUpdatedMessage.FileChangeType;
import com.raytheon.uf.common.localization.ILocalizationFileObserver;
@ -70,21 +67,17 @@ import com.raytheon.uf.common.localization.LocalizationContext.LocalizationLevel
import com.raytheon.uf.common.localization.LocalizationContext.LocalizationType;
import com.raytheon.uf.common.localization.LocalizationFile;
import com.raytheon.uf.common.localization.PathManagerFactory;
import com.raytheon.uf.common.localization.exception.LocalizationOpFailedException;
import com.raytheon.uf.common.status.IUFStatusHandler;
import com.raytheon.uf.common.status.UFStatus;
import com.raytheon.uf.common.status.UFStatus.Priority;
import com.raytheon.uf.common.util.FileUtil;
import com.raytheon.uf.common.util.file.FilenameFilters;
import com.raytheon.uf.viz.core.RGBColors;
import com.raytheon.uf.viz.core.VizApp;
import com.raytheon.uf.viz.core.exception.VizException;
import com.raytheon.viz.gfe.Activator;
import com.raytheon.viz.gfe.core.DataManagerUIFactory;
import com.raytheon.viz.gfe.textformatter.CombinationsFileGenerator;
import com.raytheon.viz.gfe.textformatter.CombinationsFileUtil;
import com.raytheon.viz.gfe.textformatter.TextProductManager;
import com.raytheon.viz.gfe.ui.AccessMgr;
import com.raytheon.viz.gfe.ui.zoneselector.ZoneSelector;
/**
@ -100,8 +93,9 @@ import com.raytheon.viz.gfe.ui.zoneselector.ZoneSelector;
* Changes for non-blocking SaveDeleteComboDlg.
* Changes for non-blocking ShuffleZoneGroupsDialog.
* Changes for non-blocking ZoneColorEditorDlg.
*
* Mar 14, 2013 1794 djohnson Consolidate common FilenameFilter implementations.
* Sep 05, 2013 2329 randerso Removed obsolete methods, added ApplyZoneCombo method
*
* </pre>
*
* @author lvenable
@ -307,6 +301,8 @@ public class ZoneCombinerComp extends Composite implements
createMapArea(theSaved);
createBottomControls();
applyButtonState(false);
}
/**
@ -455,6 +451,7 @@ public class ZoneCombinerComp extends Composite implements
@Override
public void widgetSelected(SelectionEvent e) {
zoneSelector.updateCombos(new HashMap<String, Integer>());
applyButtonState(false);
}
});
clearMI.setText("Clear");
@ -731,14 +728,7 @@ public class ZoneCombinerComp extends Composite implements
applyZoneComboBtn.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent event) {
try {
CombinationsFileGenerator.generateAutoCombinationsFile(
zoneSelector.getZoneGroupings(),
getCombinationsFileName() + ".py");
} catch (Exception e) {
statusHandler.handle(Priority.PROBLEM, "Unable to save "
+ getCombinationsFileName(), e);
}
applyZoneCombo();
}
});
Label label = new Label(controlComp, SWT.CENTER);
@ -754,6 +744,25 @@ public class ZoneCombinerComp extends Composite implements
label.setAlignment(SWT.CENTER);
}
/**
* Save zone combo
*/
public void applyZoneCombo() {
if (!buttonState()) {
return;
}
try {
CombinationsFileUtil.generateAutoCombinationsFile(
zoneSelector.getZoneGroupings(), getCombinationsFileName()
+ ".py");
applyButtonState(false);
} catch (Exception e) {
statusHandler.handle(Priority.PROBLEM, "Unable to save "
+ getCombinationsFileName(), e);
}
}
/**
* Display the Color Editor dialog.
*/
@ -845,93 +854,6 @@ public class ZoneCombinerComp extends Composite implements
return file;
}
/**
* Get the names of the combo files at the given level. If level is null,
* get the names of the combo files at all levels. Otherwise, only get the
* names of the files at the given level.
*
* @param level
* @return the save combo files at the given level
*/
public String[] getSavedCombos(LocalizationLevel level) {
String comboDirName = "saved";
String[] combos;
File localFile;
// Accept any file whose name ends with ".py".
FilenameFilter filter = FilenameFilters.byFileExtension(".py");
if (level == null) {
// Aggregate the filenames for all levels.
// Use a set to keep names unique.
Set<String> comboSet = new TreeSet<String>();
LocalizationLevel[] levels = PathManagerFactory.getPathManager()
.getAvailableLevels();
for (int i = levels.length - 1; i >= 0; --i) {
localFile = getLocalization(comboDirName, levels[i]);
if ((localFile != null) && localFile.exists()) {
comboSet.addAll(Arrays.asList(localFile.list(filter)));
}
}
combos = comboSet.toArray(new String[0]);
} else {
// Get only the filenames for USER level.
localFile = getLocalization(comboDirName);
combos = localFile.list(filter);
}
return combos;
}
/**
* Load the combinations file called filename if it is in list or
* filename.py is in list, and return the loaded file as a List of Lists of
* Strings.
*
* @param list
* The list of valid filenames
* @param filename
* The filename to load
* @return the contents of the file, as a List of Lists of Strings.
*/
// public List<List<String>> findCombos(String[] list, String filename) {
// List<List<String>> listOfCombos = null;
// for (int i = 0; i < list.length; i++) {
// if (list[i].equals(filename) || list[i].equals(filename + ".py")) {
// listOfCombos = loadCombinationsFile(filename);
// }
// }
// return listOfCombos;
// }
/**
* Deletes the saved file chosen
*
* @param name
* the combo file name
* @throws LocalizationOpFailedException
* if the server copy of the file cannot be deleted
*/
public void deleteSavedCombos(String name)
throws LocalizationOpFailedException {
String searchName = FileUtil.join(CombinationsFileUtil.COMBO_DIR_PATH,
"saved", name + ".py");
IPathManager pm = PathManagerFactory.getPathManager();
LocalizationContext userContext = pm.getContext(
LocalizationType.CAVE_STATIC, LocalizationLevel.USER);
LocalizationFile userFile = pm.getLocalizationFile(userContext,
searchName);
if (AccessMgr.verifyDelete(userFile.getName(),
LocalizationType.CAVE_STATIC, false)) {
if (userFile.isAvailableOnServer()) {
userFile.delete();
} else if (userFile.exists()) {
File localFile = userFile.getFile();
localFile.delete();
}
}
}
/**
* Returns the localization for the save and delete functions. This is a
* wrapper around getLocalization(String, level).
@ -987,34 +909,40 @@ public class ZoneCombinerComp extends Composite implements
}
public Map<String, Integer> loadCombinationsFile(String comboName) {
Map<String, Integer> dict = new HashMap<String, Integer>();
try {
IPathManager pm = PathManagerFactory.getPathManager();
LocalizationContext ctx = pm.getContext(
LocalizationType.CAVE_STATIC, LocalizationLevel.SITE);
File localFile = pm.getFile(ctx, FileUtil.join(
CombinationsFileUtil.COMBO_DIR_PATH, comboName + ".py"));
List<List<String>> combolist = new ArrayList<List<String>>();
File localFile = PathManagerFactory.getPathManager().getStaticFile(
FileUtil.join(CombinationsFileUtil.COMBO_DIR_PATH, comboName
+ ".py"));
if (localFile != null) {
if (localFile != null && localFile.exists()) {
combolist = CombinationsFileUtil.init(comboName);
} else {
statusHandler.error("Combinations file does not found: "
+ comboName);
}
// reformat combinations into combo dictionary
Map<String, Integer> d = new HashMap<String, Integer>();
try {
int group = 1;
for (List<String> zonelist : combolist) {
for (String z : zonelist) {
d.put(z, group);
dict.put(z, group);
}
group += 1;
}
} catch (Exception e) {
statusHandler.handle(Priority.SIGNIFICANT,
"Combo file is not in combo format: " + comboName);
} catch (GfeException e) {
statusHandler.handle(Priority.SIGNIFICANT, e.getLocalizedMessage(),
e);
return new HashMap<String, Integer>();
}
currentComboFile = FileUtil.join(CombinationsFileUtil.COMBO_DIR_PATH,
comboName + ".py");
return d;
return dict;
}
/**
@ -1060,11 +988,12 @@ public class ZoneCombinerComp extends Composite implements
&& message.getFileName().equalsIgnoreCase(currentComboFile)) {
File file = new File(message.getFileName());
String comboName = file.getName().replace(".py", "");
if (file.getParent().endsWith("saved")) {
comboName = FileUtil.join("saved", comboName);
}
statusHandler
.info("Received FileUpdatedMessage for combinations file: "
+ comboName);
Map<String, Integer> comboDict = loadCombinationsFile(comboName);
this.zoneSelector.updateCombos(comboDict);
applyButtonState(false);
}
}
@ -1085,4 +1014,20 @@ public class ZoneCombinerComp extends Composite implements
});
}
}
private boolean buttonState() {
final boolean[] state = { false };
if (this.applyZoneComboBtn != null
&& !this.applyZoneComboBtn.isDisposed()) {
VizApp.runSync(new Runnable() {
@Override
public void run() {
state[0] = ZoneCombinerComp.this.applyZoneComboBtn
.isEnabled();
}
});
}
return state[0];
}
}

View file

@ -1,108 +0,0 @@
/**
* This software was developed and / or modified by Raytheon Company,
* pursuant to Contract DG133W-05-CQ-1067 with the US Government.
*
* U.S. EXPORT CONTROLLED TECHNICAL DATA
* This software product contains export-restricted data whose
* export/transfer/disclosure is restricted by U.S. law. Dissemination
* to non-U.S. persons whether in the United States or abroad requires
* an export license or other authorization.
*
* Contractor Name: Raytheon Company
* Contractor Address: 6825 Pine Street, Suite 340
* Mail Stop B8
* Omaha, NE 68106
* 402.291.0100
*
* See the AWIPS II Master Rights File ("Master Rights File.pdf") for
* further licensing information.
**/
/**
* Creating the combinations file for the TextFormatter
*
* <pre>
*
* SOFTWARE HISTORY
*
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
*6/12/2008 mnash Initial creation
*
* </pre>
*
* @author mnash
* @version 1
*/
package com.raytheon.viz.gfe.textformatter;
import java.io.File;
import java.io.IOException;
import java.util.List;
import com.raytheon.uf.common.dataplugin.gfe.request.SaveCombinationsFileRequest;
import com.raytheon.uf.common.localization.IPathManager;
import com.raytheon.uf.common.localization.LocalizationContext;
import com.raytheon.uf.common.localization.LocalizationContext.LocalizationLevel;
import com.raytheon.uf.common.localization.LocalizationContext.LocalizationType;
import com.raytheon.uf.common.localization.PathManagerFactory;
import com.raytheon.uf.common.util.FileUtil;
import com.raytheon.viz.gfe.core.DataManager;
import com.raytheon.viz.gfe.core.internal.IFPClient;
public class CombinationsFileGenerator {
/**
* Generates combinations files based on just running the formatter
*
* @param zoneGroupList
* @param filename
* @throws IOException
*/
public static void generateAutoCombinationsFile(
List<List<String>> zoneGroupList, String filename) throws Exception {
generateCombinationsFile(zoneGroupList, filename, "");
}
/**
* Generates combinations files based on user wanting to save
*
* @param zoneGroupList
* @param filename
* @throws IOException
*/
public static void generateSavedCombinationsFile(
List<List<String>> zoneGroupList, String filename) throws Exception {
if (filename.endsWith(".py")) {
generateCombinationsFile(zoneGroupList, filename, "saved"
+ File.separator);
} else {
generateCombinationsFile(zoneGroupList, filename + ".py", "saved"
+ File.separator);
}
}
/**
* Called by both auto and saved functions to actually write file
*
* @param zoneGroupList
* @param filename
* @param loc
* @throws Exception
*/
public static void generateCombinationsFile(
List<List<String>> zoneGroupList, String filename, String loc)
throws Exception {
IFPClient ifpc = DataManager.getCurrentInstance().getClient();
SaveCombinationsFileRequest req = new SaveCombinationsFileRequest();
req.setFileName(FileUtil.join(loc, filename));
req.setCombos(zoneGroupList);
ifpc.makeRequest(req);
IPathManager pm = PathManagerFactory.getPathManager();
LocalizationContext ctx = pm.getContext(LocalizationType.CAVE_STATIC,
LocalizationLevel.SITE);
pm.getFile(ctx, FileUtil.join("gfe", "combinations", filename));
}
}

View file

@ -20,6 +20,7 @@
package com.raytheon.viz.gfe.textformatter;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -33,7 +34,9 @@ import javax.xml.bind.annotation.XmlRootElement;
import jep.JepException;
import com.raytheon.uf.common.dataplugin.gfe.exception.GfeException;
import com.raytheon.uf.common.dataplugin.gfe.python.GfePyIncludeUtil;
import com.raytheon.uf.common.dataplugin.gfe.request.SaveCombinationsFileRequest;
import com.raytheon.uf.common.localization.IPathManager;
import com.raytheon.uf.common.localization.LocalizationContext;
import com.raytheon.uf.common.localization.LocalizationContext.LocalizationLevel;
@ -49,8 +52,9 @@ import com.raytheon.uf.common.serialization.SerializationException;
import com.raytheon.uf.common.serialization.SerializationUtil;
import com.raytheon.uf.common.status.IUFStatusHandler;
import com.raytheon.uf.common.status.UFStatus;
import com.raytheon.uf.common.status.UFStatus.Priority;
import com.raytheon.uf.common.util.FileUtil;
import com.raytheon.viz.gfe.core.DataManagerUIFactory;
import com.raytheon.viz.gfe.core.internal.IFPClient;
import com.raytheon.viz.gfe.textformatter.CombinationsFileUtil.ComboData.Entry;
/**
@ -61,6 +65,9 @@ import com.raytheon.viz.gfe.textformatter.CombinationsFileUtil.ComboData.Entry;
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* Jul 25, 2008 mnash Initial creation
* Sep 05, 2013 #2329 randerso Moved genereateAutoCombinationsFile here
* Cleaned up error handling
*
* </pre>
*
* @author mnash
@ -200,7 +207,7 @@ public class CombinationsFileUtil {
}
@SuppressWarnings("unchecked")
public static List<List<String>> init(String comboName) {
public static List<List<String>> init(String comboName) throws GfeException {
IPathManager pm = PathManagerFactory.getPathManager();
@ -211,7 +218,6 @@ public class CombinationsFileUtil {
File comboFile = new File(comboName);
comboName = comboFile.getName();
String comboPath = GfePyIncludeUtil.getCombinationsIncludePath();
String scriptPath = FileUtil.join(
GfePyIncludeUtil.getUtilitiesLF(baseContext).getFile()
.getPath(), "CombinationsInterface.py");
@ -221,13 +227,15 @@ public class CombinationsFileUtil {
map.put("comboName", comboName);
PythonScript python = null;
try {
python = new PythonScript(scriptPath,
PyUtil.buildJepIncludePath(comboPath));
python = new PythonScript(scriptPath, PyUtil.buildJepIncludePath(
GfePyIncludeUtil.getCombinationsIncludePath(),
GfePyIncludeUtil.getCommonPythonIncludePath()),
CombinationsFileUtil.class.getClassLoader());
Object com = python.execute("getCombinations", map);
combos = (List<List<String>>) com;
} catch (JepException e) {
statusHandler.handle(Priority.CRITICAL,
"Could not get combinations", e);
throw new GfeException("Error loading combinations file: "
+ comboName, e);
} finally {
if (python != null) {
python.dispose();
@ -235,4 +243,30 @@ public class CombinationsFileUtil {
}
return combos;
}
/**
* Generates combinations files based on just running the formatter
*
* @param zoneGroupList
* @param filename
* @throws Exception
* @throws IOException
*/
public static void generateAutoCombinationsFile(
List<List<String>> zoneGroupList, String filename) throws Exception {
IFPClient ifpc = DataManagerUIFactory.getCurrentInstance().getClient();
SaveCombinationsFileRequest req = new SaveCombinationsFileRequest();
req.setFileName(filename);
req.setCombos(zoneGroupList);
try {
statusHandler.info("Saving combinations file: " + filename);
ifpc.makeRequest(req);
statusHandler.info("Successfully saved combinations file: "
+ filename);
} catch (Exception e) {
statusHandler.error("Error saving combinations file: " + filename,
e);
throw e;
}
}
}

View file

@ -20,7 +20,6 @@
package com.raytheon.viz.gfe.textformatter;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.TimeZone;
import com.raytheon.uf.common.status.IUFStatusHandler;
@ -42,6 +41,7 @@ import com.raytheon.viz.gfe.tasks.TaskManager;
* ------------ ---------- ----------- --------------------------
* Sep 8, 2008 njensen Initial creation
* Jan 15, 2010 3395 ryu Fix &quot;issued by&quot; functionality
* Sep 05, 2013 2329 randerso Removed save of combinations file
*
* </pre>
*
@ -63,21 +63,20 @@ public class FormatterUtil {
* the formatter instance to use
* @param productName
* the name of the text product
* @param zoneList
* the list of zones to produce the product for
* @param dbId
* source database
* @param vtecMode
* VTEC mode
* @param finish
* listener to fire when formatter finishes generating product
*/
public static void runFormatterScript(TextProductManager productMgr,
String productName, List<List<String>> zoneList, String dbId,
String vtecMode, TextProductFinishListener finish) {
String productName, String dbId, String vtecMode,
TextProductFinishListener finish) {
try {
String filename = productMgr.getCombinationsFileName(productName);
boolean mapRequired = productMgr.mapRequired(productName);
if (filename != null && mapRequired) {
String filenameExt = filename + ".py";
CombinationsFileGenerator.generateAutoCombinationsFile(
zoneList, filenameExt);
productMgr.reloadModule(filename);
}
} catch (Exception e) {