1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
8 #include "otk/display.hh"
17 static PyObject *obdict = NULL;
19 void python_init(char *argv0)
21 // start the python engine
22 Py_SetProgramName(argv0);
24 // prepend the openbox directories for python scripts to the sys path
25 PyRun_SimpleString("import sys");
26 PyRun_SimpleString("sys.path.insert(0, '" SCRIPTDIR "')");
27 PyRun_SimpleString(const_cast<char*>(("sys.path.insert(0, '" +
28 otk::expandTilde("~/.openbox/python") +
30 //PyRun_SimpleString("import ob; import otk; import config;");
31 PyRun_SimpleString("import config;");
32 // set up convenience global variables
33 //PyRun_SimpleString("ob.openbox = ob.Openbox_instance()");
34 //PyRun_SimpleString("otk.display = otk.Display_instance()");
36 // set up access to the python global variables
37 PyObject *obmodule = PyImport_AddModule("config");
38 obdict = PyModule_GetDict(obmodule);
46 bool python_exec(const std::string &path)
48 FILE *rcpyfd = fopen(path.c_str(), "r");
50 printf("Failed to load python file %s\n", path.c_str());
53 PyRun_SimpleFile(rcpyfd, const_cast<char*>(path.c_str()));
58 bool python_get_long(const char *name, long *value)
60 PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
61 if (!(val && PyInt_Check(val))) return false;
63 *value = PyInt_AsLong(val);
67 bool python_get_string(const char *name, otk::ustring *value)
69 PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
70 if (!(val && PyString_Check(val))) return false;
72 *value = PyString_AsString(val);
76 bool python_get_stringlist(const char *name, std::vector<otk::ustring> *value)
78 PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
79 if (!(val && PyList_Check(val))) return false;
83 for (int i = 0, end = PyList_Size(val); i < end; ++i) {
84 PyObject *str = PyList_GetItem(val, i);
85 if (PyString_Check(str))
86 value->push_back(PyString_AsString(str));