Esempio n. 1
0
void initPython(int argc, char **argv)
{
    assert(!pythonInitialized);

    try
    {
        Py_Initialize();
        python_main_module    = python::import("__main__");
        python_main_namespace = python_main_module.attr("__dict__");
    }
    catch(python::error_already_set const &)
    {
        printf("Error in Python initialization\r\n");
        PyErr_Print();
        assert(0 && "Halting on Python error");
    }

    pythonInitialized = true;

    // Set up general stuff to allow Python to be used
    EXEC_PYTHON("import sys, os");
    // TODO: remove sys.path in binary builds, leave only a tiny subset of all of CPy
    #ifdef INTENSITY_INSTALL_ROOT
        printf("Changing directory to install root: %s\r\n", INTENSITY_INSTALL_ROOT);
        EXEC_PYTHON("os.chdir('" + std::string(INTENSITY_INSTALL_ROOT) + "')");
    #endif
    EXEC_PYTHON("sys.path = ['', 'src', os.path.join('src', 'python') ] + sys.path");
    EXEC_PYTHON("print 'Python path:', sys.path");
    EXEC_PYTHON("from intensity.c_module import *");

    #if 0 //def WINDOWS - TODO: Probably need to place in BAT file
        // For Windows, use pre-prepared DLL files in windows/dll
        EXEC_PYTHON("os.environ['PATH'] = os.path.join(os.getcwd(), 'windows', 'dll') + ';' + os.environ['PATH']");
    #endif

    // Pass C commandline arguments untouched to Python
    try
    {
        boost::python::list args;
        for (int i = 0; i < argc; i++)
            args.append(std::string(argv[i]));
        REFLECT_PYTHON( set_python_args )
        set_python_args(args);
    } catch(boost::python::error_already_set const &)
    {
        printf("Error in Python execution of setting args\r\n");
        PyErr_Print();
        assert(0 && "Halting on Python error");
    }
}
Esempio n. 2
0
python::object PythonConsoleViewImpl::getMemberObject(std::vector<string>& moduleNames, python::object& parentObject)
{
    if(moduleNames.size() == 0){
        return parentObject;
    }else{
        string moduleName = moduleNames.front();
        moduleNames.erase(moduleNames.begin());
        std::vector<string> memberNames = getMemberNames(parentObject);
        if(std::find(memberNames.begin(),memberNames.end(),moduleName) == memberNames.end()){
            return python::object();
        }else{
            python::object childObject = parentObject.attr(moduleName.c_str());
            return getMemberObject(moduleNames,childObject);
        }
    }
}
Esempio n. 3
0
    void PyRegisterService( //const Service::ClassType& name,
			    const python::object& py_service_class) {
      
      Service::ClassType serv_type;
      try {
	serv_type = python::extract<Service::ClassType>
	  ( py_service_class.attr("__name__"));
      }
      catch ( ... )
	{
	  PyErr_Print();
	  PyErr_Clear();
	  Logger::failure("PyRegister: Unable to register python service");
	  return;
	}
      theServiceManager().registerPyService( serv_type, py_service_class );
    }
Esempio n. 4
0
    void PyRegisterEntity( //const Entity::ClassType& name,
			   const python::object& py_entity_class) {
      
      Entity::ClassType ent_type;
      try {
	ent_type = python::extract<Entity::ClassType>
	  ( py_entity_class.attr("__name__"));
      }
      catch ( ... )
	{
	  PyErr_Print();
	  PyErr_Clear();
	  Logger::failure("PyRegister: Unable to register python entity");
	  return;
	}
      theEntityManager().registerPyEntity( ent_type, py_entity_class );
    }