source: neercs/trunk/src/python/interpreter.c @ 4071

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