source: trunk/tools/neercs/old/python/interpreter.c @ 1642

Last change on this file since 1642 was 1642, checked in by sam, 8 years ago

neercs: import files from the (unfinished) old neercs code.

  • Property svn:keywords set to Id
File size: 4.4 KB
Line 
1/*
2 *  neercs        console-based window manager
3 *  Copyright (c) 2009-2010 Jean-Yves Lamoureux <jylam@lnxscene.org>
4 *                All Rights Reserved
5 *
6 *  This program is free software. It comes without any warranty, to
7 *  the extent permitted by applicable law. You can redistribute it
8 *  and/or modify it under the terms of the Do What The Fuck You Want
9 *  To Public License, Version 2, as published by Sam Hocevar. See
10 *  http://sam.zoy.org/wtfpl/COPYING for more details.
11 */
12
13#include "config.h"
14
15#ifdef USE_PYTHON
16
17#include <Python.h>
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23
24#include <fcntl.h>
25#include <signal.h>
26#include <sys/ioctl.h>
27#include <sys/socket.h>
28#include <sys/time.h>
29#include <time.h>
30#include <sys/wait.h>
31#include <sys/types.h>
32
33#include <caca.h>
34
35#include "neercs.h"
36#include "py_module.h"
37
38static int python_execute(struct screen_list *sl);
39char *getStringFromPyObject(PyObject * p);
40static char *getPythonError(void);
41
42#if ! defined HAVE_PYTHON26
43static PyObject *PyUnicode_FromString(char const *str)
44{
45    PyObject *tmp, *ret;
46    tmp = PyString_FromString(str);
47    if (!tmp)
48        return NULL;
49    ret = PyString_AsDecodedObject(tmp, "utf-8", "replace");
50    Py_DECREF(tmp);
51    return ret;
52}
53#endif
54
55int python_command_handle_key(struct screen_list *screen_list, unsigned int c)
56{
57    int ret = widget_ibox_handle_key(screen_list->interpreter_props.box, c);
58
59    if (ret == INPUT_BOX_ESC)
60    {
61        widget_ibox_destroy(screen_list->interpreter_props.box);
62        screen_list->interpreter_props.box = NULL;
63        screen_list->modals.python_command = 0;
64    }
65    else if (ret == INPUT_BOX_RET)
66    {
67        python_execute(screen_list);
68    }
69    screen_list->changed = 1;
70
71    return 1;
72}
73
74void draw_python_command(struct screen_list *screen_list)
75{
76    if (!screen_list->interpreter_props.box)
77    {
78        screen_list->interpreter_props.box =
79            widget_ibox_init(screen_list->cv, 65, 6);
80        debug("py Init %p\n", screen_list->interpreter_props.box);
81    }
82    widget_ibox_draw(screen_list->interpreter_props.box);
83}
84
85/* Actual Python interpreter stuff */
86int python_init(struct screen_list *sl)
87{
88    initNeercsModule(sl);
89
90    return 0;
91}
92
93int python_close(struct screen_list *sl)
94{
95    widget_ibox_destroy(sl->interpreter_props.box);
96    sl->interpreter_props.box = NULL;
97
98    Py_Finalize();
99    return 0;
100}
101
102
103static int python_execute(struct screen_list *sl)
104{
105    char *command = widget_ibox_get_text(sl->interpreter_props.box);
106    if (!command || strlen(command) < 1)
107        return -1;
108    int err = 0;
109
110    debug("py Executing '%s'\n", command);
111
112
113    PyObject *pModule, *pName;
114
115    /* Module from which to call the function */
116    pName = PyUnicode_FromString("neercs");
117    if (!pName)
118    {
119        widget_ibox_set_error(sl->interpreter_props.box, getPythonError());
120        err = 1;
121        debug("py Error 1\n");
122        goto end;
123    }
124
125    pModule = PyImport_Import(pName);
126    Py_DECREF(pName);
127
128    if (pModule != NULL)
129    {
130        PyObject *dictionary = PyModule_GetDict(pModule);
131
132        getExportedValues(dictionary);
133
134        PyObject *o =
135            PyRun_String(command, Py_single_input,
136                         dictionary, NULL);
137        if (!o)
138        {
139            widget_ibox_set_error(sl->interpreter_props.box, getPythonError());
140            err = 1;
141        }
142        else
143        {
144            setExportedValues(dictionary);
145
146            widget_ibox_set_msg(sl->interpreter_props.box, getStringFromPyObject(o));
147            err = 1;
148        }
149
150        Py_DECREF(pModule);
151    }
152    else
153    {
154        widget_ibox_set_error(sl->interpreter_props.box, getPythonError());
155        err = 1;
156    }
157
158  end:
159
160    if (!err)
161    {
162        widget_ibox_destroy(sl->interpreter_props.box);
163        sl->interpreter_props.box = NULL;
164        sl->modals.python_command = 0;
165    }
166    sl->changed = 1;
167
168    return 0;
169}
170
171static char *getPythonError(void)
172{
173    char *err;
174    PyObject *type, *value, *traceback;
175    PyErr_Fetch(&type, &value, &traceback);
176
177    char *evalue = getStringFromPyObject(value);
178
179    int r = asprintf(&err, "%s", evalue);
180    (void)r;
181    return err;
182}
183
184char *getStringFromPyObject(PyObject * p)
185{
186    PyObject *str = PyObject_Repr(p);
187    char *tmp;
188#if defined HAVE_PYTHON26
189    tmp = PyBytes_AS_STRING(PyUnicode_AsEncodedString(str, "utf-8", "Error ~"));
190#else
191    tmp = PyString_AsString(str);
192#endif
193    return strdup(tmp);
194}
195
196
197#endif
Note: See TracBrowser for help on using the repository browser.