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

Last change on this file since 4048 was 4048, checked in by Jean-Yves Lamoureux, 11 years ago
  • Created widgets.[c,h] and an unfinished (but working) input box
  • Python interpreter now uses generic input box code
File size: 4.1 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
49    int ret = widget_ibox_handle_key(screen_list->interpreter_props.box, c);
50
51    if (ret == INPUT_BOX_ESC)
52    {
53        widget_ibox_destroy(screen_list->interpreter_props.box);
54        screen_list->interpreter_props.box = NULL;
55        screen_list->python_command = 0;
56    }
57    else if (ret == INPUT_BOX_RET)
58    {
59        python_execute(screen_list);
60    }
61    screen_list->changed = 1;
62
63    return 0;
64}
65
66void draw_python_command(struct screen_list *screen_list)
67{
68    if (!screen_list->interpreter_props.box)
69        screen_list->interpreter_props.box =
70            widget_ibox_init(screen_list->cv, 65, 6);
71
72    widget_ibox_draw(screen_list->interpreter_props.box);
73}
74
75/* Actual Python interpreter stuff */
76int python_init(struct screen_list *sl)
77{
78    Py_Initialize();
79    initNeercsModule(sl);
80
81    return 0;
82}
83
84int python_close(struct screen_list *sl)
85{
86    widget_ibox_destroy(sl->interpreter_props.box);
87    sl->interpreter_props.box = NULL;
88
89    Py_Finalize();
90    return 0;
91}
92
93
94static int python_execute(struct screen_list *sl)
95{
96    char *command = widget_ibox_get_text(sl->interpreter_props.box);
97    if (!command || strlen(command) < 1)
98        return -1;
99    int err = 0;
100
101    debug("py Executing '%s'\n", command);
102
103
104    PyObject *pModule, *pName, *pFunc;
105
106    /* Module from which to call the function */
107    pName = PyUnicode_FromString("neercs");
108    if (!pName)
109    {
110        widget_ibox_set_error(sl->interpreter_props.box, getPythonError());
111        err = 1;
112        debug("py Error 1\n");
113        goto end;
114    }
115
116    pModule = PyImport_Import(pName);
117    Py_DECREF(pName);
118
119    if (pModule != NULL)
120    {
121        PyObject *dictionary = PyModule_GetDict(pModule);
122
123        getExportedValues(dictionary);
124
125        PyObject *o =
126            PyRun_String(command, Py_single_input,
127                         dictionary, NULL);
128        if (!o)
129        {
130            widget_ibox_set_error(sl->interpreter_props.box, getPythonError());
131            err = 1;
132        }
133        else
134        {
135            setExportedValues(dictionary);
136
137            widget_ibox_set_msg(sl->interpreter_props.box, getStringFromPyObject(o));
138            err = 1;
139        }
140        goto end;
141
142    }
143    else
144    {
145        widget_ibox_set_error(sl->interpreter_props.box, getPythonError());
146        err = 1;
147        goto end;
148    }
149
150  end:
151
152    Py_XDECREF(pFunc);
153    Py_DECREF(pModule);
154
155    if (!err)
156    {
157        widget_ibox_destroy(sl->interpreter_props.box);
158        sl->python_command = 0;
159    }
160    sl->changed = 1;
161
162    return 0;
163}
164
165
166
167static char *getPythonError(void)
168{
169    char *err;
170    PyObject *type, *value, *traceback;
171    PyErr_Fetch(&type, &value, &traceback);
172
173    char *evalue = getStringFromPyObject(value);
174
175    int r = asprintf(&err, "%s", evalue);
176    (void)r;
177    return err;
178}
179
180char *getStringFromPyObject(PyObject * p)
181{
182    PyObject *str = PyObject_Repr(p);
183    char *err = PyBytes_AS_STRING(PyUnicode_AsEncodedString(str, "utf-8",
184                                                            "Error ~"));
185
186    char *ret = strdup(err);
187    return ret;
188}
189
190
191#endif
Note: See TracBrowser for help on using the repository browser.