all repos — openbox @ feaf3ac4e5847d27e3747b09e7443915afa97b0b

openbox fork - make it a bit more like ryudo

src/python.cc (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-

#include "python.hh"
#include "openbox.hh"
#include "actions.hh"
#include "python.hh"
#include "bindings.hh"
#include "otk/display.hh"
#include "otk/util.hh"

extern "C" {
#include <Python.h>

#include "gettext.h"
#define _(str) gettext(str)
}

namespace ob {

static PyObject *get = NULL;

void python_init(char *argv0)
{
  // start the python engine
  Py_SetProgramName(argv0);
  Py_Initialize();
  // prepend the openbox directories for python scripts to the sys path
  PyRun_SimpleString("import sys");
  PyRun_SimpleString("sys.path.insert(0, '" SCRIPTDIR "')");
  PyRun_SimpleString(const_cast<char*>(("sys.path.insert(0, '" +
                                        otk::expandTilde("~/.openbox/python") +
                                        "')").c_str()));

  return;
  PyObject *obmodule = PyImport_ImportModule("config");
  if (obmodule == NULL) {
    PyErr_Print();
    return;
  }
  PyObject *configdict = PyModule_GetDict(obmodule);
  Py_DECREF(obmodule);

  get = PyDict_GetItemString(configdict, "get");
  if (get == NULL) {
    PyErr_Print(); 
    return;
  }
}

void python_destroy()
{
  Py_Finalize();
}

int python_exec(const std::string &path)
{
  FILE *rcpyfd = fopen(path.c_str(), "r");
  if (!rcpyfd) {
    fprintf(stderr, _("Unabled to open python file %s\n"), path.c_str());
    return 1;
  }

  //PyRun_SimpleFile(rcpyfd, const_cast<char*>(path.c_str()));

  PyObject *module = PyImport_AddModule("__main__");
  assert(module);
  PyObject *dict = PyModule_GetDict(module);
  assert(dict);
  PyObject *result = PyRun_File(rcpyfd, const_cast<char*>(path.c_str()),
                                Py_file_input, dict, dict);
  int ret = result == NULL ? 2 : 0;
  if (result == NULL)
    PyErr_Print();
  
  Py_XDECREF(result);
    
  Py_DECREF(dict);

  fclose(rcpyfd);
  return ret;
}

bool python_get_long(const char *name, long *value)
{
  return false;
  if (get == NULL) return false;
  bool ret = false;

  PyObject *val = PyObject_CallFunction(get, "ss", "openbox", name);
  if (val == NULL)
    PyErr_Print();
  else if (PyInt_Check(val)) {
    *value = PyInt_AsLong(val);
    ret = true;
  } else if (PyLong_Check(val)) {
    *value = PyLong_AsLong(val);
    ret = true;
  }
  Py_XDECREF(val);
  return ret;
}

bool python_get_string(const char *name, otk::ustring *value)
{
  return false;
  if (get == NULL) return false;
  bool ret = false;

  PyObject *val = PyObject_CallFunction(get, "ss", "openbox", name);
  if (val == NULL)
    PyErr_Print();
  else if (PyString_Check(val)) {
    *value = std::string(PyString_AsString(val), PyString_Size(val));
    ret = true;
  }
  Py_XDECREF(val);
  return ret;
}

bool python_get_stringlist(const char *name, std::vector<otk::ustring> *value)
{
  return false;
  if (get == NULL) return false;
  bool ret = false;

  PyObject *val = PyObject_CallFunction(get, "ss", "openbox", name);
  if (val == NULL)
    PyErr_Print();
  else if (PyList_Check(val)) {
    for (int i = 0, end = PyList_Size(val); i < end; ++i) {
      PyObject *str = PyList_GET_ITEM(val, i);
      if (PyString_Check(str))
        value->push_back(std::string(PyString_AsString(str),
                                     PyString_Size(str)));
    }
  }
  Py_XDECREF(val);
  return ret;
}

}