Salome HOME
Copyright update 2020
[modules/yacs.git] / src / py2yacs / py2yacs.hxx
1 // Copyright (C) 2006-2020  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #ifndef _PY2YACS_H_
20 #define _PY2YACS_H_
21
22 #ifdef WIN32
23 #  if defined py2yacslib_EXPORTS
24 #    define PY2YACSLIB_EXPORT __declspec( dllexport )
25 #  else
26 #    define PY2YACSLIB_EXPORT __declspec( dllimport )
27 #  endif
28 #else
29 #  define PY2YACSLIB_EXPORT
30 #endif
31
32
33 #include <string>
34 #include <list>
35 #include <exception>
36
37 class PY2YACSLIB_EXPORT Py2yacsException: std::exception
38 {
39   public:
40     Py2yacsException(const std::string& what);
41     virtual ~Py2yacsException()throw ();
42     virtual const char *what() const throw ();
43   private:
44     std::string _what;
45 };
46
47 namespace YACS
48 {
49   namespace ENGINE
50   {
51     class Proc;
52   };
53 };
54
55 struct PY2YACSLIB_EXPORT FunctionProperties
56 {
57   public:
58     std::string _name;
59     std::list<std::string> _input_ports;
60     std::list<std::string> _output_ports;
61     std::list<std::string> _errors;
62     std::list<std::string> _imports;
63 };
64
65 /*! \brief Converter of a python script to a yacs schema.
66  *  This class converts a string containing a python script to a yacs schema
67  *  containing a python script node.
68  */
69 class PY2YACSLIB_EXPORT Py2yacs
70 {
71   public:
72     Py2yacs();
73     
74     /*!
75      * This constructor can be used if you want to define your own python parser.
76      * The parser function should be a python function and return a tuple of
77      * two lists.
78      * The first list contains the properties of all the functions in the script
79      * and the second one contains global errors.
80      * \param python_parser_module: name of the parser module
81      * \param python_parser_function: name of the parser function
82      */
83     Py2yacs(const std::string& python_parser_module,
84             const std::string& python_parser_function);
85
86     /*!
87      * \param python_code: contains the python code that will be converted
88      *                     to a yacs schema.
89      */
90     void load(const std::string& python_code);
91
92     /*!
93      * \param file_path: path to the xml file where to save the yacs schema.
94      * \param python_function: function defined in the python code that will be
95      *                         called in the yacs node.
96      */
97     void save(const std::string& file_path,
98               const std::string& python_function)const;
99
100     /*!
101      * A new schema is created.
102      * \param python_function: function defined in the python code that will be
103      *                         called in the yacs node.
104      */
105     YACS::ENGINE::Proc* createProc(const std::string& python_function)const;
106     
107     /*!
108      * Syntax errors when parsing python and py2yacs global errors.
109      * \return a list of errors.
110      */
111     const std::list<std::string>& getGlobalErrors() const;
112
113     const std::list<FunctionProperties>& getFunctionProperties()const;
114
115     /*!
116      * Get a string containing global errors and errors specific to py2yacs for
117      * every function in python script.
118      * An empty string means there is no error.
119      */
120     std::string getAllErrors()const;
121
122     /*!
123      * Same as getAllErrors but only for one function.
124      * py2yacs errors for other functions are ignored.
125      * If the function name is not found, you get an error message in the
126      * returned string.
127      */
128     std::string getFunctionErrors(const std::string& functionName)const;
129
130   private:
131     std::string _python_parser_module;
132     std::string _python_parser_function;
133     std::list<FunctionProperties> _functions;
134     std::list<std::string> _global_errors;
135     std::string _python_code;
136 };
137
138 #endif //_PY2YACS_H_