-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpython.cpp
More file actions
155 lines (124 loc) · 4.17 KB
/
Copy pathpython.cpp
File metadata and controls
155 lines (124 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// GAMBIT: Global and Modular BSM Inference Tool
// *********************************************
/// \file
///
/// Make an instance of python scanner and define
/// method to run it
///
/// *********************************************
///
/// Authors (add name and date if you modify):
///
/// \author Gregory Martinez
/// (gregory.david.martinez@gmail.com)
/// \date 2023 May
///
/// \author Andrew Fowlie
/// (andrew.j.fowlie@googlemail.com)
/// \date 2023 May
///
/// \author Anders Kvellestad
/// (anders.kvellestad@fys.uio.no)
/// \date 2023 May
///
/// \author Anders Kvellestad
/// (anders.kvellestad@fys.uio.no)
/// \date 2023 May
///
/// \author Pat Scott
/// (patrickcolinscott@gmail.com)
/// \date 2023 May
///
/// *********************************************
#include "gambit/cmake/cmake_variables.hpp"
#ifdef HAVE_PYBIND11
#ifdef WITH_MPI
#include "gambit/Utils/begin_ignore_warnings_mpi.hpp"
#include "mpi.h"
#include "gambit/Utils/end_ignore_warnings.hpp"
#endif
#include "gambit/Utils/begin_ignore_warnings_pybind11.hpp"
#include <pybind11/embed.h>
#include "gambit/Utils/end_ignore_warnings.hpp"
#include "gambit/Utils/python_interpreter.hpp"
#include "gambit/ScannerBit/scanner_plugin.hpp"
#include "gambit/ScannerBit/python_utils.hpp"
namespace py = pybind11;
namespace Gambit
{
namespace Scanner
{
namespace Plugins
{
namespace ScannerPyPlugin
{
pluginData *&pythonPluginData();
} // end namespace ScannerPyPlugin
} // end namespace Plugins
} // end namespace Scanner
} // end namespace Gambit
scanner_plugin(python, version(1, 0, 0))
{
reqd_headers("Python3");
reqd_headers("pybind11");
Gambit::Utils::python_interpreter_guard g;
/*!
* Instance of python scanner
*/
py::object instance;
py::object run_func;
plugin_constructor
{
// export plugin data to python plugin
Gambit::Scanner::Plugins::ScannerPyPlugin::pythonPluginData() = &__gambit_plugin_namespace__::myData;
// get plugin name
std::string plugin_name = get_inifile_value<std::string>("plugin");
// get yaml as dict
py::kwargs options = yaml_to_dict(get_inifile_node());
// make instance of plugin
py::module file;
std::string pkg = get_inifile_value<std::string>("pkg", "");
try
{
if (pkg == "")
{
Gambit::Scanner::Plugins::plugin_info.load_python_plugins();
decltype(auto) details = Gambit::Scanner::Plugins::plugin_info.load_python_plugin("scanner", plugin_name);
py::list(py::module::import("sys").attr("path")).append(py::cast(details.loc));
file = py::module::import(details.package.c_str());
}
else
{
std::string::size_type pos = pkg.rfind("/");
std::string pkg_name;
if (pos != std::string::npos)
{
pkg_name = pkg.substr(pos+1);
while(pos != 0 && pkg[pos-1] == '/') --pos;
std::string path = pkg.substr(0, pos);
py::list(py::module::import("sys").attr("path")).append(py::cast(path));
}
else
pkg_name = pkg;
py::list(py::module::import("sys").attr("path")).append(py::cast(GAMBIT_DIR "/ScannerBit/src/scanners/python/plugins"));
file = py::module::import(pkg_name.c_str());
}
instance = py::dict(file.attr("__plugins__"))[plugin_name.c_str()](**options);
run_func = instance.attr("run");
}
catch (std::exception &ex)
{
scan_err << "Error loading plugin \"" << plugin_name << "\": " << ex.what() << scan_end;
}
}
int plugin_main()
{
// run scanner
run_func();
return 0;
}
plugin_deconstructor
{
}
}
#endif