PythonInterp.hpp
Go to the documentation of this file.
1/*
2 Copyright 2019 Equinor ASA.
3
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20
21#ifndef PYTHON_INTERP
22#define PYTHON_INTERP
23
24#include <string>
25#include <stdexcept>
26#include <memory>
27
32
33
34#ifdef EMBEDDED_PYTHON
35
36#include <pybind11/embed.h>
37namespace py = pybind11;
38#endif
39
40
41
42namespace Opm {
43
44#ifdef EMBEDDED_PYTHON
45class Parser;
46class Deck;
47
48
49class __attribute__ ((visibility("hidden"))) PythonInterp {
50public:
51 explicit PythonInterp(bool enable);
52 bool exec(const std::string& python_code);
53 bool exec(const std::string& python_code, const Parser& parser, Deck& deck);
54 static bool supported() { return true; };
55 explicit operator bool() const { return bool(this->guard); }
56private:
57 void load_module(const std::string& python_file);
58 bool exec(const std::string& python_code, py::module& context);
59
60 std::unique_ptr<py::scoped_interpreter> guard;
61};
62
63
64#else
65
67public:
68 explicit PythonInterp(bool enable) {
69 if (enable)
70 this->fail();
71 }
72
73 bool exec(const std::string&) {
74 return this->fail();
75 };
76
77 bool exec(const std::string&, const Parser&, Deck&) {
78 return this->fail();
79 }
80
81 bool exec(const Action::PyAction&, EclipseState&, Schedule&, std::size_t, SummaryState& ) {
82 return this->fail();
83 }
84
85 static bool supported() { return false; };
86 explicit operator bool() const { return false; }
87private:
88 bool fail() { throw std::logic_error("The current opm code has been built without Python support;"); }
89};
90
91#endif
92
93}
94
95#endif
const char *const string
Definition: cJSON.h:170
Definition: PyAction.hpp:39
Definition: Deck.hpp:115
Definition: EclipseState.hpp:63
Definition: custom-opm-common/opm-common/opm/parser/eclipse/Parser/Parser.hpp:54
Definition: PythonInterp.hpp:66
bool exec(const Action::PyAction &, EclipseState &, Schedule &, std::size_t, SummaryState &)
Definition: PythonInterp.hpp:81
bool exec(const std::string &)
Definition: PythonInterp.hpp:73
bool exec(const std::string &, const Parser &, Deck &)
Definition: PythonInterp.hpp:77
static bool supported()
Definition: PythonInterp.hpp:85
PythonInterp(bool enable)
Definition: PythonInterp.hpp:68
Definition: Schedule.hpp:113
Definition: SummaryState.hpp:65
char bool
Definition: msvc_stdbool.h:17
Definition: A.hpp:4
class __attribute__((visibility("default"))) PyRunModule
Definition: PyRunModule.hpp:41