From 908e1f30e504a01cea0c02797584147596214da7 Mon Sep 17 00:00:00 2001 From: Alexey Kondratyev Date: Mon, 11 Oct 2021 10:57:46 +0300 Subject: [PATCH] [bos #26444] [EDF] (2021) SHAPER: Improve treatment of parameters Add parameter errors in Python console. Add tests and update existing test. --- src/ParametersAPI/ParametersAPI.i | 10 ++++++++ src/ParametersAPI/ParametersAPI_Parameter.cpp | 10 +++++++- .../Test/TestParameterCreationError.py | 20 ++++++++++++++++ .../Test/TestParameterErrorMsg.py | 24 ++++++++++++------- src/PythonAPI/Test/TestInsertParameter.py | 3 ++- .../model/parameter/import_parameter.py | 5 +++- 6 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 src/ParametersPlugin/Test/TestParameterCreationError.py diff --git a/src/ParametersAPI/ParametersAPI.i b/src/ParametersAPI/ParametersAPI.i index 8d8a59c74..66ba5fb3d 100644 --- a/src/ParametersAPI/ParametersAPI.i +++ b/src/ParametersAPI/ParametersAPI.i @@ -45,5 +45,15 @@ // shared pointers %shared_ptr(ParametersAPI_Parameter) +// exception handler +%exception addParameter { + try { + $action + } catch (const std::string& str) { + PyErr_SetString(PyExc_SyntaxError, str.c_str()); + return NULL; + } +} + // all supported interfaces %include "ParametersAPI_Parameter.h" diff --git a/src/ParametersAPI/ParametersAPI_Parameter.cpp b/src/ParametersAPI/ParametersAPI_Parameter.cpp index 9079094a4..5c2c7130c 100644 --- a/src/ParametersAPI/ParametersAPI_Parameter.cpp +++ b/src/ParametersAPI/ParametersAPI_Parameter.cpp @@ -90,7 +90,15 @@ ParameterPtr addParameter(const std::shared_ptr & thePart, const std::wstring & theComment) { std::shared_ptr aFeature = thePart->addFeature(ParametersAPI_Parameter::ID()); - return ParameterPtr(new ParametersAPI_Parameter(aFeature, theName, theExpression, theComment)); + ParameterPtr aParam(new ParametersAPI_Parameter(aFeature, theName, theExpression, theComment)); + + if (!aParam->feature()->error().empty()) + { + std::string anError("Error with parameter \""); + anError += theName + "\": " + aParam->feature()->error(); + throw anError; + } + return aParam; } //-------------------------------------------------------------------------------------- diff --git a/src/ParametersPlugin/Test/TestParameterCreationError.py b/src/ParametersPlugin/Test/TestParameterCreationError.py new file mode 100644 index 000000000..08b2746e3 --- /dev/null +++ b/src/ParametersPlugin/Test/TestParameterCreationError.py @@ -0,0 +1,20 @@ +### +### SHAPER component +### + +from salome.shaper import model + +model.begin() +partSet = model.moduleDocument() + +### Create Part +Part_1 = model.addPart(partSet) +Part_1_doc = Part_1.document() +aPart1 = model.addParameter(Part_1_doc, "Doing", "123", "Longueur de la pièce") +try: + aPart2 = model.addParameter(Part_1_doc, "Doing", "323", "Long") +except SyntaxError as anError: + assert(anError != "") + assert(str(anError).find("Variable name is not unique.") != -1) + +model.end() diff --git a/src/ParametersPlugin/Test/TestParameterErrorMsg.py b/src/ParametersPlugin/Test/TestParameterErrorMsg.py index bc7443528..d21b360de 100644 --- a/src/ParametersPlugin/Test/TestParameterErrorMsg.py +++ b/src/ParametersPlugin/Test/TestParameterErrorMsg.py @@ -22,15 +22,23 @@ from salome.shaper import model model.begin() partSet = model.moduleDocument() # check error on empty name -Param1 = model.addParameter(partSet, "", "100") -assert(Param1.feature().error() != "") +try: + Param1 = model.addParameter(partSet, "", "100") +except SyntaxError as anError: + assert(str(anError).find("Attribute \"variable\" value is empty.") != -1) # check error on empty value -Param2 = model.addParameter(partSet, "L", "") -assert(Param2.feature().error() != "") +try: + Param2 = model.addParameter(partSet, "L", "") +except SyntaxError as anError: + assert(str(anError).find("Expression is empty.") != -1) # check error if name is not variable -Param3 = model.addParameter(partSet, "100", "100") -assert(Param3.feature().error() != "") +try: + Param3 = model.addParameter(partSet, "100", "100") +except SyntaxError as anError: + assert(str(anError).find("Incorrect variable name.") != -1) # check error on wrong value expression -Param4 = model.addParameter(partSet, "N", "+-.so&@") -assert(Param4.feature().error() != "") +try: + Param4 = model.addParameter(partSet, "N", "+-.so&@") +except SyntaxError as anError: + assert(str(anError).find("invalid syntax") != -1) model.end() diff --git a/src/PythonAPI/Test/TestInsertParameter.py b/src/PythonAPI/Test/TestInsertParameter.py index 97b7e13f5..739206e4e 100644 --- a/src/PythonAPI/Test/TestInsertParameter.py +++ b/src/PythonAPI/Test/TestInsertParameter.py @@ -49,8 +49,9 @@ aFile.write("Longueur2 36. #\"Comment\" #Comm #Comm\n") aFile.close() aListOfParameters = model.importParameters(Part_1_doc, nameFile) + Box_1 = model.addBox(Part_1_doc, "Longueur", "Largeur", "Hauteur") assert(len(Box_1.feature().error()) == 0) assert(len(aListOfParameters) > 0) -assert(len(aListOfParameters) == 8) +assert(len(aListOfParameters) == 5) diff --git a/src/PythonAPI/model/parameter/import_parameter.py b/src/PythonAPI/model/parameter/import_parameter.py index cc5b21427..d844f7880 100644 --- a/src/PythonAPI/model/parameter/import_parameter.py +++ b/src/PythonAPI/model/parameter/import_parameter.py @@ -49,7 +49,10 @@ def importParameters(theDocument, theFileName): aComment = aLine if(len(aName) > 0): - aResult.append(model.addParameter(theDocument, aName, aParameter.strip(), aComment.strip())) + try: + aResult.append(model.addParameter(theDocument, aName, aParameter.strip(), aComment.strip())) + except SyntaxError as anError: + anError aFile.close() return aResult -- 2.39.2