OPAL (Object Oriented Parallel Accelerator Library)  2024.1
OPAL
PyParser.cpp
Go to the documentation of this file.
1 //
2 // Python API for Parser
3 //
4 // Copyright (c) 2023, Chris Rogers, STFC Rutherford Appleton Laboratory, Didcot, UK
5 //
6 // This file is part of OPAL.
7 //
8 // OPAL is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
15 //
16 
17 #include <sstream>
18 #include <string>
19 #include <boost/python.hpp>
20 
23 // this define statement tells Main not to define the global gmsg and ippl
24 // objects; they should be defined in Globals
25 #define DONT_DEFINE_IPPL_GMSG 1
26 #include "PyOpal/PyCore/Globals.h"
27 #include "Main.cpp"
28 
29 namespace PyOpal {
30 namespace PyParser {
31 
33 "Initialise from opal file\n"
34 "- file_name: string corresponding to the file name of the OPAL\n"
35 " file.\n"
36 "Note that if file_name is not valid, OPAL may terminate the python script\n"
37 "execution abnormally (without the usual python exit semantics).\n"
38 "\n"
39 "Returns an integer; 0 for successful execution or non-zero if an error\n"
40 "occurred.\n";
41 
42 int initialise_from_opal_file(std::string file_name) {
43  std::string exe("parser");
44  char* argvr[3];
45  // argv must be nullptr terminated array
46  argvr[0] = exe.data();
47  argvr[1] = file_name.data();
48  argvr[2] = nullptr;
49  int error_code = opalMain(2, argvr);
50  return error_code;
51 }
52 
53 std::string list_objects_docstring =
54 "List the objects that are known by the Opal parser, either through calls to\n"
55 "initialise_from_opal_file or through calls directly to the python API\n"
56 "\n"
57 "Returns a list of strings, each one corresponding to the name of a\n"
58 "particular object\n";
59 
60 boost::python::list list_objects() {
61  std::vector<std::string> names = OpalData::getInstance()->getAllNames();
62  boost::python::list pynames;
63  for (size_t i = 0; i < names.size(); ++i) {
64  pynames.append(names[i]);
65  }
66  return pynames;
67 }
68 
69 
70 std::string module_docstring =
71 "The parser module is used to load an OPAL input file from within python";
72 
76  boost::python::scope().attr("__doc__") = module_docstring.c_str();
77  boost::python::def("initialise_from_opal_file",
79  boost::python::args("file_name"),
81  );
82  boost::python::def("list_objects",
85  );
86 }
87 
88 } // namespace PyParser
89 } // namespace PyOpal
static OpalData * getInstance()
Definition: OpalData.cpp:196
std::string initialise_from_opal_file_docstring
Definition: PyParser.cpp:32
void Initialise()
Definition: Globals.cpp:50
boost::python::list list_objects()
Definition: PyParser.cpp:60
int initialise_from_opal_file(std::string file_name)
Definition: PyParser.cpp:42
std::vector< std::string > getAllNames() const
Get a list of all objects.
Definition: OpalData.cpp:615
alter the names
Definition: LICENSE:330
BOOST_PYTHON_MODULE(parser)
Definition: PyParser.cpp:73
std::string list_objects_docstring
Definition: PyParser.cpp:53
int opalMain(int argc, char *argv[])
Definition: Main.cpp:148
std::string module_docstring
Definition: PyParser.cpp:70