OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
PyParser.cpp
Go to the documentation of this file.
1#include <sstream>
2#include <string>
3#include <boost/python.hpp>
4
7#include "Main.cpp"
8
9namespace PyOpal {
10namespace PyParser {
11
13"Initialise from opal file\n"
14"- file_name: string corresponding to the file name of the OPAL\n"
15" file.\n"
16"Note that if file_name is not valid, OPAL may terminate the python script\n"
17"execution abnormally (without the usual python exit semantics).\n"
18"\n"
19"Returns an integer; 0 for successful execution or non-zero if an error\n"
20"occurred.\n";
21
22int initialise_from_opal_file(std::string file_name) {
23 std::string exe("parser");
24 char* argvr[3];
25 // argv must be NULL terminated array
26 argvr[0] = exe.data();
27 argvr[1] = file_name.data();
28 argvr[2] = nullptr;
29 int error_code = opalMain(2, argvr);
30 return error_code;
31}
32
34"List the objects that are known by the Opal parser, either through calls to\n"
35"initialise_from_opal_file or through calls directly to the python API\n"
36"\n"
37"Returns a list of strings, each one corresponding to the name of a\n"
38"particular object\n";
39
40boost::python::list list_objects() {
41 std::vector<std::string> names = OpalData::getInstance()->getAllNames();
42 boost::python::list pynames;
43 for (size_t i = 0; i < names.size(); ++i) {
44 pynames.append(names[i]);
45 }
46 return pynames;
47}
48
49
50std::string module_docstring =
51"The parser module is used to load an OPAL input file from within python";
52
55 boost::python::scope().attr("__doc__") = module_docstring.c_str();
56 boost::python::def("initialise_from_opal_file",
58 boost::python::args("file_name"),
60 );
61 boost::python::def("list_objects",
64 );
65}
66
67} // namespace PyParser
68} // namespace PyOpal
int opalMain(int argc, char *argv[])
Definition: Main.cpp:133
std::string initialise_from_opal_file_docstring
Definition: PyParser.cpp:12
int initialise_from_opal_file(std::string file_name)
Definition: PyParser.cpp:22
std::string list_objects_docstring
Definition: PyParser.cpp:33
BOOST_PYTHON_MODULE(parser)
Definition: PyParser.cpp:53
std::string module_docstring
Definition: PyParser.cpp:50
boost::python::list list_objects()
Definition: PyParser.cpp:40
std::vector< std::string > getAllNames() const
Get a list of all objects.
Definition: OpalData.cpp:610
static OpalData * getInstance()
Definition: OpalData.cpp:196