1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2019 EDF R&D
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 class PyScriptException(Exception):
29 self.properties, self.errors = py2yacs.get_properties(self.script)
31 def loadFile(self,path):
32 with open(path, "r") as f:
33 self.script = f.read()
34 self.properties, self.errors = py2yacs.get_properties(self.script)
36 def loadString(self, script):
38 self.properties, self.errors = py2yacs.get_properties(self.script)
43 def saveFile(self, path):
44 with open(path, "w") as f:
47 def getAllProperties(self):
49 functions,errors = myscript.getAllProperties()
50 print(errors) # list of syntax errors in the script
52 print(f.name) # function name
53 print(f.inputs) # list of input variable names
54 print(f.outputs) # list of output variable names
55 print(f.errors) # list of py2yacs errors in the function
56 print(f.imports) # list of import statements in the function
58 return py2yacs.get_properties(self.script)
60 def getFunctionProperties(self, fname = "_exec"):
62 Properties of the _exec function:
63 fn_properties = myscript.getFunctionProperties()
64 fn_properties.name : "_exec"
65 fn_properties.inputs : list of input variable names
66 fn_properties.outputs : list of output variable names
67 fn_properties.errors : list of py2yacs errors in the function
68 fn_properties.imports : list of import statements in the function
69 fn_properties is None if the "_exec" function does not exist.
71 fn_properties = next((f for f in self.properties if f.name == fname), None)
74 def getOutputNames(self, fname = "_exec"):
75 errorsText = self.getErrors(fname)
76 if len(errorsText) > 0:
77 raise PyScriptException(errorsText)
78 fnProperties = self.getFunctionProperties(fname)
79 return fnProperties.outputs
81 def getInputNames(self, fname = "_exec"):
82 errorsText = self.getErrors(fname)
83 if len(errorsText) > 0:
84 raise PyScriptException(errorsText)
85 fnProperties = self.getFunctionProperties(fname)
86 return fnProperties.inputs
88 def getErrors(self, fname = "_exec"):
90 if len(self.errors) > 0:
91 error_string = "global errors:\n"
92 error_string += '\n'.join(errors)
94 properties = self.getFunctionProperties(fname)
95 if properties is None:
96 error_string += "Function {} not found in the script!".format(fname)
98 error_string += '\n'.join(properties.errors)
101 def CreateEmptySample(self):
103 Create a sample with input and output variable names set.
106 errors = self.getErrors(fn)
108 raise PyScriptException(errors)
109 fn_properties = self.getFunctionProperties(fn)
110 return sample.Sample(fn_properties.inputs, fn_properties.outputs)