HOPS
HOPS class reference
MHO_PyConfigurePath.hh
Go to the documentation of this file.
1 #ifndef MHO_PyConfigurePath_HH__
2  #define MHO_PyConfigurePath_HH_
3 
4  #include <sstream>
5  #include <string>
6 
7  #include "MHO_Message.hh"
8 
9  //pybind11 stuff to interface with python
10  #include <pybind11/embed.h>
11  #include <pybind11/pybind11.h>
12 namespace py = pybind11;
13 
14 namespace hops
15 {
16 
25 static void configure_pypath()
26 {
27  if(Py_IsInitialized() == 0)
28  {
29  //the internal python interpreter has not been started, bail out
30  msg_warn("python_bindings", "python interpreter not running/initialized, cannot configure path " << eom);
31  }
32  else
33  {
34  //make sure our python plugin directories are in our search paths
35  //only do this once on a per-executable level, since these settings are global
36  //(e.g. we don't want each individual class messing with the search paths)
37  std::stringstream pyss;
38  pyss << "import sys\n";
39  std::string default_path = STRING(HOPS_DEFAULT_PLUGINS_DIR);
40  if(default_path.back() != '/')
41  {
42  default_path.push_back('/');
43  }
44  msg_info("main", "adding HOPS_DEFAULT_PLUGINS_DIR to search path: " << default_path << eom);
45  pyss << "sys.path.append(\"" << default_path << "\") \n";
46  const char* user_plugin_env = std::getenv("HOPS_USER_PLUGINS_DIR");
47  if(user_plugin_env != nullptr)
48  {
49  std::string user_specified_path = std::string(user_plugin_env);
50  if(user_specified_path.back() != '/')
51  {
52  user_specified_path.push_back('/');
53  }
54  msg_info("main", "adding HOPS_USER_PLUGINS_DIR to search path: " << user_specified_path << eom);
55  pyss << "sys.path.append(\"" << user_specified_path << "\") \n";
56  }
57 
58  //now lets make sure pyMHO_Containers and pyMHO_Operators are always present
59  pyss << "import pyMHO_Containers\n";
60  pyss << "import pyMHO_Operators\n";
61 
62  //IMPORTANT...if we create additional bindings libraries, we should import them here,
63  //otherwise if a use tries to write a plugin but fails to import what they need, they
64  //will encounter a cryptic error of the form:
65  //terminate called after throwing an instance of 'pybind11::cast_error'
66  // what(): Unable to convert call argument '0' of type '<...something...>' to Python object
67  //Aborted (core dumped)
68 
69 
70 
71  py::exec(pyss.str().c_str());
72  }
73 }
74 
75 } // namespace hops
76 
77 #endif
#define msg_warn(xKEY, xCONTENT)
Definition: MHO_Message.hh:254
#define msg_info(xKEY, xCONTENT)
Definition: MHO_Message.hh:274
#define STRING(str)
Definition: MHO_Message.hh:22
Definition: MHO_ChannelLabeler.hh:17