3 # Copyright (C) 2007-2024 CEA, EDF
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 class FunctionProperties:
24 def __init__(self, function_name):
25 self.name = function_name
32 result = "Function:" + self.name + "\n"
33 result+= " Inputs:" + str(self.inputs) + "\n"
34 result+= " Outputs:"+ str(self.outputs) + "\n"
35 result+= " Errors:" + str(self.errors) + "\n"
36 result+= " Imports:"+ str(self.imports) + "\n"
39 class VisitAST(ast.NodeVisitor):
40 def visit_Module(self, node):
41 accepted_tokens = ["Import", "ImportFrom", "FunctionDef", "ClassDef", "If"]
44 type_name = type(e).__name__
45 if type_name not in accepted_tokens:
46 error="py2yacs error at line %s: not accepted statement '%s'." % (
48 self.global_errors.append(error)
53 self.generic_visit(node)
55 def visit_FunctionDef(self, node):
57 self.lastfn = FunctionProperties(node.name)
58 self.functions.append(self.lastfn)
61 self.generic_visit(node)
66 def visit_arg(self, node):
67 self.lastfn.inputs.append(node.arg)
69 def visit_Return(self, node):
70 if self.lastfn.outputs is not None :
71 error="py2yacs error at line %s: multiple returns." % node.lineno
72 self.lastfn.errors.append(error)
74 self.lastfn.outputs = []
75 if node.value is None :
77 elif 'Tuple' == type(node.value).__name__ :
78 for e in node.value.elts:
79 if 'Name' == type(e).__name__ :
80 self.lastfn.outputs.append(e.id)
82 error="py2yacs error at line %s: invalid type returned '%s'." % (
83 node.lineno, type(e).__name__)
84 self.lastfn.errors.append(error)
86 if 'Name' == type(node.value).__name__ :
87 self.lastfn.outputs.append(node.value.id)
89 error="py2yacs error at line %s: invalid type returned '%s'." %(
90 node.lineno, type(node.value).__name__)
91 self.lastfn.errors.append(error)
96 def visit_ClassDef(self, node):
100 def visit_Import(self, node):
103 self.lastfn.imports.append(n.name)
104 def visit_ImportFrom(self, node):
111 self.lastfn.imports.append(m+"."+n.name)
113 class vtest(ast.NodeVisitor):
114 def generic_visit(self, node):
115 ast.NodeVisitor.generic_visit(self, node)
117 def create_yacs_schema(text, fn_name, fn_args, fn_returns, file_name):
120 SALOMERuntime.RuntimeSALOME_setRuntime()
121 runtime = pilot.getRuntime()
122 schema = runtime.createProc("schema")
123 node = runtime.createScriptNode("", "default_name")
124 schema.edAddChild(node)
125 fncall = "\n%s=%s(%s)\n"%(",".join(fn_returns),
128 node.setScript(text+fncall)
129 td=schema.getTypeCode("double")
131 newport = node.edAddInputPort(p, td)
134 node.edAddOutputPort(p, td)
135 myContainer=schema.createContainer("Py2YacsContainer")
136 node.setExecutionMode(pilot.InlineNode.REMOTE_STR)
137 node.setContainer(myContainer)
138 schema.saveSchema(file_name)
140 def get_properties(text_file):
142 bt=ast.parse(text_file)
143 except SyntaxError as err:
145 return [], ["".join(traceback.format_exception_only(SyntaxError,err))]
148 return w.functions, w.global_errors
150 def function_properties(python_path, fn_name):
152 python_path : path to a python file
153 fn_name : name of a function in the file
154 return : properties of the function. see class FunctionProperties
156 with open(python_path, 'r') as f:
158 functions,errors = get_properties(text_file)
159 result = [fn for fn in functions if fn.name == fn_name]
161 raise Exception("Function not found: {}".format(fn_name))
165 error_string += "Global errors in file {}\n".format(python_path)
166 error_string += '\n'.join(errors)
167 raise Exception(error_string)
168 if len(result.errors) > 0:
169 error_string += "Errors when parsing function {}\n".format(fn_name)
170 error_string += '\n'.join(result.errors)
171 raise Exception(error_string)
175 def main(python_path, yacs_path, function_name="_exec"):
176 with open(python_path, 'r') as f:
178 fn_name = function_name
179 functions,errors = get_properties(text_file)
182 error_string += "global errors:\n"
183 error_string += '\n'.join(errors)
185 fn_properties = next((f for f in functions if f.name == fn_name), None)
186 if fn_properties is not None :
187 if not fn_properties.errors :
188 create_yacs_schema(text_file, fn_name,
189 fn_properties.inputs, fn_properties.outputs,
192 error_string += '\n'.join(fn_properties.errors)
194 error_string += "Function not found:"
195 error_string += fn_name
198 if __name__ == '__main__':
200 parser = argparse.ArgumentParser(description="Generate a YACS schema from a python file containing a function to run.")
201 parser.add_argument("file", help='Path to the python file')
202 parser.add_argument("-o","--output",
203 help='Path to the output file (yacs_schema.xml by default)',
204 default='yacs_schema.xml')
205 parser.add_argument("-d","--def_name",
206 help='Name of the function to call in the yacs node (_exec by default)',
208 args = parser.parse_args()
209 erreurs = main(args.file, args.output, args.def_name)