]> Dogcows Code - chaz/openbox/blob - src/python.cc
time to refactor shit hard
[chaz/openbox] / src / python.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #include "python.hh"
4 #include "openbox.hh"
5 #include "actions.hh"
6 #include "python.hh"
7 #include "bindings.hh"
8 #include "otk/display.hh"
9 #include "otk/util.hh"
10
11 extern "C" {
12 #include <Python.h>
13
14 #include "gettext.h"
15 #define _(str) gettext(str)
16 }
17
18 namespace ob {
19
20 static PyObject *get = NULL;
21
22 void python_init(char *argv0)
23 {
24 // start the python engine
25 Py_SetProgramName(argv0);
26 Py_Initialize();
27 // prepend the openbox directories for python scripts to the sys path
28 PyRun_SimpleString("import sys");
29 PyRun_SimpleString("sys.path.insert(0, '" SCRIPTDIR "')");
30 PyRun_SimpleString(const_cast<char*>(("sys.path.insert(0, '" +
31 otk::expandTilde("~/.openbox/python") +
32 "')").c_str()));
33
34 return;
35 PyObject *obmodule = PyImport_ImportModule("config");
36 if (obmodule == NULL) {
37 PyErr_Print();
38 return;
39 }
40 PyObject *configdict = PyModule_GetDict(obmodule);
41 Py_DECREF(obmodule);
42
43 get = PyDict_GetItemString(configdict, "get");
44 if (get == NULL) {
45 PyErr_Print();
46 return;
47 }
48 }
49
50 void python_destroy()
51 {
52 Py_Finalize();
53 }
54
55 int python_exec(const std::string &path)
56 {
57 FILE *rcpyfd = fopen(path.c_str(), "r");
58 if (!rcpyfd) {
59 fprintf(stderr, _("Unabled to open python file %s\n"), path.c_str());
60 return 1;
61 }
62
63 //PyRun_SimpleFile(rcpyfd, const_cast<char*>(path.c_str()));
64
65 PyObject *module = PyImport_AddModule("__main__");
66 assert(module);
67 PyObject *dict = PyModule_GetDict(module);
68 assert(dict);
69 PyObject *result = PyRun_File(rcpyfd, const_cast<char*>(path.c_str()),
70 Py_file_input, dict, dict);
71 int ret = result == NULL ? 2 : 0;
72 if (result == NULL)
73 PyErr_Print();
74
75 Py_XDECREF(result);
76
77 Py_DECREF(dict);
78
79 fclose(rcpyfd);
80 return ret;
81 }
82
83 bool python_get_long(const char *name, long *value)
84 {
85 return false;
86 if (get == NULL) return false;
87 bool ret = false;
88
89 PyObject *val = PyObject_CallFunction(get, "ss", "openbox", name);
90 if (val == NULL)
91 PyErr_Print();
92 else if (PyInt_Check(val)) {
93 *value = PyInt_AsLong(val);
94 ret = true;
95 } else if (PyLong_Check(val)) {
96 *value = PyLong_AsLong(val);
97 ret = true;
98 }
99 Py_XDECREF(val);
100 return ret;
101 }
102
103 bool python_get_string(const char *name, otk::ustring *value)
104 {
105 return false;
106 if (get == NULL) return false;
107 bool ret = false;
108
109 PyObject *val = PyObject_CallFunction(get, "ss", "openbox", name);
110 if (val == NULL)
111 PyErr_Print();
112 else if (PyString_Check(val)) {
113 *value = std::string(PyString_AsString(val), PyString_Size(val));
114 ret = true;
115 }
116 Py_XDECREF(val);
117 return ret;
118 }
119
120 bool python_get_stringlist(const char *name, std::vector<otk::ustring> *value)
121 {
122 return false;
123 if (get == NULL) return false;
124 bool ret = false;
125
126 PyObject *val = PyObject_CallFunction(get, "ss", "openbox", name);
127 if (val == NULL)
128 PyErr_Print();
129 else if (PyList_Check(val)) {
130 for (int i = 0, end = PyList_Size(val); i < end; ++i) {
131 PyObject *str = PyList_GET_ITEM(val, i);
132 if (PyString_Check(str))
133 value->push_back(std::string(PyString_AsString(str),
134 PyString_Size(str)));
135 }
136 }
137 Py_XDECREF(val);
138 return ret;
139 }
140
141 }
This page took 0.040752 seconds and 5 git commands to generate.