Salome HOME
Update copyrights
[modules/med.git] / src / MEDCalculator / Swig / SPythonInterpreter.hxx
1 // Copyright (C) 2007-2019  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 // Author : Anthony Geay (CEA/DEN)
20
21 #ifndef __SPYTHONINTERPRETER_HXX__
22 #define __SPYTHONINTERPRETER_HXX__
23
24 // rnv: avoid compilation warning on Linux : "_POSIX_C_SOURCE" and "_XOPEN_SOURCE" are redefined
25 #ifdef _POSIX_C_SOURCE
26 #undef _POSIX_C_SOURCE
27 #endif 
28
29 #ifdef _XOPEN_SOURCE
30 #undef _XOPEN_SOURCE
31 #endif 
32
33 #include <Python.h>
34
35 #include "MedCalculatorSPythonDefines.hxx"
36
37 #include <string>
38 #include <vector>
39 #include <set>
40
41 namespace MEDCoupling
42 {
43   class MEDCALCULATORSPYTHON_EXPORT SPythonInterpreter
44   {
45   public:
46     SPythonInterpreter(PyObject *globals, PyObject *locals);
47     void initialize();
48     bool run(const char *str, bool& isSPython);
49     bool finishSession();
50   private:
51     bool checkIndentCoherency(const std::string& s, std::size_t p);
52     void checkPythonInterp(PyObject *r);
53     bool isIndenter(const std::string& s, std::size_t p);
54     bool isSPythonExpression(const std::string& s);
55     bool isSPythonExpressionLev1(const std::string& s, std::string& result);
56   public:
57     static std::string strip(const std::string& s);
58     static bool isSpythonManipulationNeeded(const std::string& s, std::string& pythonLines);
59     static bool isCandidateParenthesis(const std::string& s, std::size_t p1, std::size_t& n);
60   public:
61     static const int NB_OF_INDENT=7;
62     static const char *INDENT_TOKEN[];
63     static const char NUMBERS[];
64   private:
65     std::string _cmd;
66     std::set<int> _indent_pos;
67     bool _indent_must_change;
68     PyObject *_glob;
69     PyObject *_loc;
70   };
71 }
72
73 #endif