Salome HOME
Add insitu example as a test.
[tools/ydefx.git] / src / pydefx / pyscript.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2019  EDF R&D
3 #
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.
8 #
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.
13 #
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
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 from . import sample
21 import py2yacs
22
23 class PyScriptException(Exception):
24   pass
25
26 class PyScript:
27   def __init__(self):
28     self.script = ""
29     self.properties, self.errors = py2yacs.get_properties(self.script)
30
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)
35
36   def loadString(self, script):
37     self.script = script
38     self.properties, self.errors = py2yacs.get_properties(self.script)
39
40   def content(self):
41     return self.script
42
43   def saveFile(self, path):
44     with open(path, "w") as f:
45       f.write(self.script)
46
47   def getAllProperties(self):
48     """
49     functions,errors = myscript.getAllProperties()
50     print(errors)      # list of syntax errors in the script
51     for f in functions:
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
57     """
58     return py2yacs.get_properties(self.script)
59
60   def getFunctionProperties(self, fname = "_exec"):
61     """
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.
70     """
71     fn_properties = next((f for f in self.properties if f.name == fname), None)
72     return fn_properties
73
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
80
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
87
88   def getErrors(self, fname = "_exec"):
89     error_string = ""
90     if len(self.errors) > 0:
91       error_string = "global errors:\n"
92       error_string += '\n'.join(errors)
93     else:
94       properties = self.getFunctionProperties(fname)
95       if properties is None:
96         error_string += "Function {} not found in the script!".format(fname)
97       else:
98         error_string += '\n'.join(properties.errors)
99     return error_string
100
101   def CreateEmptySample(self):
102     """
103     Create a sample with input and output variable names set.
104     """
105     fn = "_exec"
106     errors = self.getErrors(fn)
107     if len(errors) > 0:
108       raise PyScriptException(errors)
109     fn_properties = self.getFunctionProperties(fn)
110     return sample.Sample(fn_properties.inputs, fn_properties.outputs)