3 # Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
5 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
26 class FunctionProperties:
27 def __init__(self, function_name):
28 self.name = function_name
35 result = "Function:" + self.name + "\n"
36 result+= " Inputs:" + str(self.inputs) + "\n"
37 result+= " Outputs:"+ str(self.outputs) + "\n"
38 result+= " Errors:" + str(self.errors) + "\n"
39 result+= " Imports:"+ str(self.imports) + "\n"
42 class v(ast.NodeVisitor):
43 def visit_Module(self, node):
44 #print(type(node).__name__, ":")
45 accepted_tokens = ["Import", "ImportFrom", "FunctionDef", "ClassDef"]
49 type_name = type(e).__name__
50 if type_name not in accepted_tokens:
51 error="py2yacs error at line %s: not accepted statement '%s'." % (
53 self.global_errors.append(error)
55 #print("------------------------------------------------------------------")
60 self.generic_visit(node)
62 def visit_FunctionDef(self, node):
63 #print(type(node).__name__, ":", node.name)
65 self.lastfn = FunctionProperties(node.name)
66 self.functions.append(self.lastfn)
69 self.generic_visit(node)
74 def visit_arg(self, node):
75 #print(type(node).__name__, ":", node.arg)
76 self.lastfn.inputs.append(node.arg)
78 def visit_Return(self, node):
79 #print(type(node).__name__, ":", node.value)
80 if self.lastfn.outputs is not None :
81 error="py2yacs error at line %s: multiple returns." % node.lineno
82 self.lastfn.errors.append(error)
84 self.lastfn.outputs = []
85 if node.value is None :
87 elif 'Tuple' == type(node.value).__name__ :
88 for e in node.value.elts:
89 if 'Name' == type(e).__name__ :
90 self.lastfn.outputs.append(e.id)
92 error="py2yacs error at line %s: invalid type returned '%s'." % (
93 node.lineno, type(e).__name__)
94 self.lastfn.errors.append(error)
96 if 'Name' == type(node.value).__name__ :
97 self.lastfn.outputs.append(node.value.id)
99 error="py2yacs error at line %s: invalid type returned '%s'." %(
100 node.lineno, type(node.value).__name__)
101 self.lastfn.errors.append(error)
106 def visit_ClassDef(self, node):
107 # just ignore classes
110 def visit_Import(self, node):
113 self.lastfn.imports.append(n.name)
114 def visit_ImportFrom(self, node):
118 self.lastfn.imports.append(m+"."+n.name)
120 class vtest(ast.NodeVisitor):
121 def generic_visit(self, node):
122 #print type(node).__name__
123 ast.NodeVisitor.generic_visit(self, node)
125 def create_yacs_schema(text, fn_name, fn_args, fn_returns, file_name):
129 SALOMERuntime.RuntimeSALOME_setRuntime()
130 runtime = pilot.getRuntime()
131 schema = runtime.createProc("schema")
132 node = runtime.createFuncNode("", "default_name")
133 schema.edAddChild(node)
134 fncall = "\n%s=%s(%s)\n"%(",".join(fn_returns),
137 node.setScript(text+fncall)
138 node.setFname(fn_name)
139 td=schema.getTypeCode("double")
141 node.edAddInputPort(p, td)
143 node.edAddOutputPort(p, td)
144 schema.saveSchema(file_name)
146 def get_properties(text_file):
147 bt=ast.parse(text_file)
150 return w.functions, w.global_errors
152 if __name__ == '__main__':
154 parser = argparse.ArgumentParser(description="Generate a YACS schema from a python file containing a function to run.")
155 parser.add_argument("file", help='Path to the python file')
156 parser.add_argument("-o","--output",
157 help='Path to the output file (yacs_schema.xml by default)',
158 default='yacs_schema.xml')
159 parser.add_argument("-d","--def_name",
160 help='Name of the function to call in the yacs node (_exec by default)',
162 args = parser.parse_args()
163 with open(args.file, 'r') as f:
165 #bt=ast.parse(text_file)
169 #print "global errors:", w.global_errors
170 #for f in w.functions:
173 fn_name = args.def_name
174 functions,errors = get_properties(text_file)
175 print("global errors:", errors)
179 fn_properties = next((f for f in functions if f.name == fn_name), None)
180 if fn_properties is not None :
181 if not fn_properties.errors :
182 create_yacs_schema(text_file, fn_name,
183 fn_properties.inputs, fn_properties.outputs,
186 print("\n".join(fn_properties.errors))
188 print("Function not found:", fn_name)