Exemplo n.º 1
0
void python_init(python_ctx& ctx)
{
    try
    {
        Py_Initialize();

        py::class_<stderr_catcher>("stderr_catcher")
            .def("write", &stderr_catcher::write);
        
	
        //      
        ctx.main_module = py::import("__main__");
        ctx.main_namespace = ctx.main_module.attr("__dict__");

        //          -
        //  "sys"      "sys"
        ctx.main_namespace["sys"] = py::import("sys");

        LOG_DEBUG << "configuring pyhton environment";
        
        python_exec(
            "sys.path.append('./')\n"
            "import liburtapi as Ut\n",
            ctx
        );

        //FIXME //HACK :)
        QString py_main = QFileInfo(QSettings(qApp->applicationName(), qApp->applicationName()).fileName()).dir().path();
        py_main += "/scripts/main.py";
        
        LOG_DEBUG << "loading main.py...";
        boost::python::exec_file(py_main.toStdString().c_str(), ctx.main_namespace, ctx.main_namespace);
        ctx.initialized = true;
    }
    catch(py::error_already_set const &)
    {
        throw boost::enable_error_info(std::runtime_error(python_err(ctx))) << init_err("Pyhton initialization failed");
    }
    catch(boost::exception& e)
    {
        e << init_err("Pyhton initialization failed");
        throw;
    }    
    LOG_DEBUG << "python initialized";
}
Exemplo n.º 2
0
void python_exec( std::string script ) {
  python_exec( script.c_str() );
}