1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
8 #include "otk/display.hh"
13 static PyObject *obdict = NULL;
15 void python_init(char *argv0)
17 // start the python engine
18 Py_SetProgramName(argv0);
20 // prepend the openbox directories for python scripts to the sys path
21 PyRun_SimpleString("import sys");
22 PyRun_SimpleString("sys.path.insert(0, '" SCRIPTDIR "')");
23 PyRun_SimpleString(const_cast<char*>(("sys.path.insert(0, '" +
24 otk::expandTilde("~/.openbox/python") +
26 //PyRun_SimpleString("import ob; import otk; import config;");
27 PyRun_SimpleString("import config;");
28 // set up convenience global variables
29 //PyRun_SimpleString("ob.openbox = ob.Openbox_instance()");
30 //PyRun_SimpleString("otk.display = otk.Display_instance()");
32 // set up access to the python global variables
33 PyObject *obmodule = PyImport_AddModule("config");
34 obdict = PyModule_GetDict(obmodule);
42 bool python_exec(const std::string &path)
44 FILE *rcpyfd = fopen(path.c_str(), "r");
46 printf("Failed to load python file %s\n", path.c_str());
49 PyRun_SimpleFile(rcpyfd, const_cast<char*>(path.c_str()));
54 bool python_get_long(const char *name, long *value)
56 PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
57 if (!(val && PyInt_Check(val))) return false;
59 *value = PyInt_AsLong(val);
63 bool python_get_string(const char *name, otk::ustring *value)
65 PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
66 if (!(val && PyString_Check(val))) return false;
68 *value = PyString_AsString(val);
72 bool python_get_stringlist(const char *name, std::vector<otk::ustring> *value)
74 PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
75 if (!(val && PyList_Check(val))) return false;
79 for (int i = 0, end = PyList_Size(val); i < end; ++i) {
80 PyObject *str = PyList_GetItem(val, i);
81 if (PyString_Check(str))
82 value->push_back(PyString_AsString(str));