Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Python/PythonScripting.cpp

    r57388c9 r2cf12a  
    3939#include "CodePatterns/MemDebug.hpp"
    4040
    41 bool executePythonScript(const std::string &pythonfilename_string)
     41bool executePythonScriptFile(const boost::filesystem::path &pythonfilename)
    4242{
    43   boost::filesystem::path pythonfilename(pythonfilename_string);
    4443  if (exists(pythonfilename)) {
    4544    // parse in and execute the local and global config.py
     
    6160      std::string pythonscript( std::istreambuf_iterator<char>(pythonfile),
    6261          (std::istreambuf_iterator<char>()) );
    63       std::string enveloped_script("print \"BEGIN of "+pythonfilename_string+":\"\n");
     62      std::string enveloped_script("print \"BEGIN of "+pythonfilename.string()+":\"\n");
    6463      enveloped_script += pythonscript;
    65       enveloped_script += std::string("print \"END of "+pythonfilename_string+":\"\n");
     64      enveloped_script += std::string("print \"END of "+pythonfilename.string()+":\"\n");
    6665
    6766      boost::python::handle<> ignored(( PyRun_String( enveloped_script.c_str(),
     
    7877  return false;
    7978}
     79
     80bool executePythonScript(const std::string &python_string, const std::string &python_scripttitle)
     81{
     82  try {
     83    PyImport_AppendInittab( "pyMoleCuilder", &initpyMoleCuilder );
     84
     85    Py_Initialize();
     86
     87    boost::python::object main_module((
     88        boost::python::handle<>(boost::python::borrowed(PyImport_AddModule("__main__")))));
     89
     90    boost::python::object main_namespace = main_module.attr("__dict__");
     91
     92    boost::python::object molecuilder_module( (boost::python::handle<>(PyImport_ImportModule("pyMoleCuilder"))) );
     93    main_namespace["pyMoleCuilder"] = molecuilder_module;
     94
     95    std::string enveloped_script("print \"BEGIN of "+python_scripttitle+":\"\n");
     96    enveloped_script += python_string+std::string("\n");
     97    enveloped_script += std::string("print \"END of "+python_scripttitle+":\"\n");
     98
     99    boost::python::handle<> ignored(( PyRun_String( enveloped_script.c_str(),
     100        Py_file_input,
     101        main_namespace.ptr(),
     102        main_namespace.ptr() ) ));
     103
     104  } catch( boost::python::error_already_set ) {
     105    PyErr_Print();
     106    return false;
     107  }
     108  return true;
     109}
Note: See TracChangeset for help on using the changeset viewer.