From: Renaud Barate Date: Wed, 20 Feb 2013 17:03:46 +0000 (+0100) Subject: First part of new HDF5-based persistence (Save is fully implemented, not Load) X-Git-Tag: V7_2_0~16 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=5cdb2aec4a895024354a1fc6c5210385ebddb074;p=modules%2Fparametric.git First part of new HDF5-based persistence (Save is fully implemented, not Load) --- diff --git a/src/PARAMETRIC/PARAMETRIC.py b/src/PARAMETRIC/PARAMETRIC.py index 91e7499..5b6f3e1 100644 --- a/src/PARAMETRIC/PARAMETRIC.py +++ b/src/PARAMETRIC/PARAMETRIC.py @@ -21,6 +21,8 @@ import inspect import traceback import copy import cPickle +import tempfile +import os import salome import SALOME @@ -38,6 +40,7 @@ logger = Logger("PARAMETRIC", color = termcolor.BLUE) logger.setLevel(logging.DEBUG) from salome.parametric import PARAM_STUDY_TYPE_ID, ParametricStudy, parse_entry +from salome.parametric.persistence import load_param_study_dict, save_param_study_dict # module constants MODULE_NAME = "PARAMETRIC" @@ -305,3 +308,42 @@ class PARAMETRIC(PARAMETRIC_ORB__POA.PARAMETRIC_Gen, SALOME_ComponentPy_i, SALOM self.endService("PARAMETRIC.RunStudy") except: self._raiseSalomeError() + + def Save(self, theComponent, theURL, isMultiFile): + try: + # Select parametric studies to save + salomeStudyID = theComponent.GetStudy()._get_StudyId() + componentEntry = theComponent.GetID() + dict_to_save = {} + if salomeStudyID in self.param_study_dict: + for (entry, param_study) in self.param_study_dict[salomeStudyID].iteritems(): + if entry.startswith(componentEntry): + dict_to_save[entry] = param_study + + if len(dict_to_save) > 0: + # Save parametric studies in temporary file + (fd, filename) = tempfile.mkstemp(prefix = "PARAMETRIC_", suffix = ".hdf") + os.close(fd) + save_param_study_dict(dict_to_save, filename) + + # Return the content of the temporary file as a byte sequence + with open(filename) as f: + buf = f.read() + + # Delete the temporary file + #os.remove(filename) + + return buf + else: + return "" + except: + logger.exception("Error while trying to save study") + return "" + + def Load(self, theComponent, theStream, theURL, isMultiFile): + try: + print "PARAMETRIC load" + return 1 + except: + logger.exception("Error while trying to load study") + return 0 diff --git a/src/PARAMETRICGUI/PARAMETRICGUI.py b/src/PARAMETRICGUI/PARAMETRICGUI.py index 51b1644..934d9c0 100644 --- a/src/PARAMETRICGUI/PARAMETRICGUI.py +++ b/src/PARAMETRICGUI/PARAMETRICGUI.py @@ -185,10 +185,15 @@ def new_study(): panel.new_study() def edit_study(): - entry = salome.sg.getSelected(0) - ed = ParametricStudyEditor() - panel = MainPanel() - panel.edit_study(ed.get_parametric_study(entry)) + try: + entry = salome.sg.getSelected(0) + ed = ParametricStudyEditor() + panel = MainPanel() + panel.edit_study(ed.get_parametric_study(entry)) + except Exception, exc: + logger.exception("Error while trying to edit parametric study") + qapp = QtGui.QApplication + QtGui.QMessageBox.critical(sgPyQt.getDesktop(), qapp.translate("edit_study", "Error"), str(exc)) def run_study(): qapp = QtGui.QApplication diff --git a/src/salome/parametric/persistence.py b/src/salome/parametric/persistence.py new file mode 100644 index 0000000..17b033d --- /dev/null +++ b/src/salome/parametric/persistence.py @@ -0,0 +1,83 @@ +# Copyright (C) 2012 EDF +# +# This file is part of SALOME PARAMETRIC module. +# +# SALOME PARAMETRIC module is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# SALOME PARAMETRIC module is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with SALOME PARAMETRIC module. If not, see . + +import h5py + +from study import ParametricStudy + +def save_param_study_dict(param_study_dict, filename): + f = h5py.File(filename) + f.attrs["filetype"] = "salome_parametric" # simple tag used to identify PARAMETRIC files + f.attrs["version"] = "7.2" # version tag used to ensure backwards compatibility + for entry, param_study in param_study_dict.iteritems(): + param_study_group = f.create_group(entry) + if param_study.name is not None: + param_study_group.attrs["name"] = param_study.name + + # Store input and output variables definitions + ordered_vars = [] + input_vars_group = param_study_group.create_group("input_vars") + for input_var in param_study.input_vars: + ordered_vars.append(input_var.name) + var_group = input_vars_group.create_group(input_var.name) + var_group.attrs["min"] = input_var.min + var_group.attrs["max"] = input_var.max + var_group.attrs["step"] = input_var.step + output_vars_group = param_study_group.create_group("output_vars") + for output_varname in param_study.output_vars: + ordered_vars.append(output_varname) + var_group = output_vars_group.create_group(output_varname) + + # Store solver definition + solver_group = param_study_group.create_group("solver") + if param_study.solver_code_type == ParametricStudy.SALOME_COMPONENT: + solver_group.attrs["solver_code_type"] = "salome_component" + if param_study.salome_component_name is not None: + solver_group.attrs["salome_component_name"] = param_study.salome_component_name + if param_study.solver_case_entry is not None: + solver_group.attrs["solver_case_entry"] = param_study.solver_case_entry + else: + solver_group.attrs["solver_code_type"] = "python_script" + if param_study.python_script is not None: + solver_group.attrs["python_script"] = param_study.python_script + + # Execution parameters + execution_group = param_study_group.create_group("execution") + execution_group.attrs["nb_parallel_computations"] = param_study.nb_parallel_computations + + # Data + if param_study.data is not None: + dset = param_study_group.create_dataset("data", (param_study.datasize, len(ordered_vars))) + dset.attrs["ordered_vars"] = ordered_vars + for idx_var, var in enumerate(ordered_vars): + if var in param_study.data and len(param_study.data[var]) > 0: + for idx_value, value in enumerate(param_study.data[var]): + if value is not None: + dset[idx_value, idx_var] = value + + # Error messages + if "__ERROR_MESSAGE__" in param_study.data and len(param_study.data["__ERROR_MESSAGE__"]) > 0: + dt = h5py.special_dtype(vlen=str) + error_set = param_study_group.create_dataset("error_message", (param_study.datasize,), dt) + for idx_value, value in enumerate(param_study.data["__ERROR_MESSAGE__"]): + if value is not None: + error_set[idx_value] = value + + f.close() + +def load_param_study_dict(filename): + pass