source: neercs/trunk/src/python/py_module.c @ 4165

Last change on this file since 4165 was 4165, checked in by sam, 4 years ago

Start porting the Python interpreter to Python 2.4/2.5.

File size: 4.9 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: py_module.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#include "py_module.h"
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26
27#include <fcntl.h>
28#include <signal.h>
29#include <sys/ioctl.h>
30#include <sys/socket.h>
31#include <sys/time.h>
32#include <time.h>
33#include <sys/wait.h>
34#include <sys/types.h>
35#include <caca.h>
36
37#include "neercs.h"
38
39/* FIXME : Find a way to pass a user pointer to PyModuleDef or something */
40static struct screen_list *screen_list;
41
42#if defined HAVE_PYTHON3
43static PyObject *PyInit_neercs(void);
44#else
45static void PyInit_neercs(void);
46#endif
47static void removeTrailingStuff(char *b);
48
49
50static void addVariableFromConfig(PyObject * dictionary,
51                                  const char *varname, const char *configname)
52{
53    char *v = get_config(configname)->get(screen_list);
54    if (v != NULL)
55    {
56        PyObject *value = Py_BuildValue("s", v);
57        PyDict_SetItemString(dictionary, varname, value);
58    }
59
60    debug("py get '%s' to '%s'\n", varname,
61          get_config(configname)->get(screen_list));
62}
63
64static void removeTrailingStuff(char *b)
65{
66    if(!b)
67        return;
68    if(b[0]=='\'') 
69    {
70        memmove(b, &b[1], strlen(b)-1);   
71        b[strlen(b)-2] = 0;
72    }
73}
74
75void setExportedValues(PyObject * dictionary)
76{
77    struct config_line *config_option = get_config_option();
78    int i = 0;
79
80    while (strncmp(config_option[i].name, "last", strlen("last")))
81    {
82        /* Get variable */
83        PyObject *res =
84            PyDict_GetItemString(dictionary, config_option[i].name);
85
86        /* Got it */
87        if (res)
88        {
89            /* Get object representation
90             * FIXME : find a way to check object's type */
91            PyObject *str = PyObject_Repr(res);
92           
93            /* Make sure it's a string */
94            char *err =
95#if defined HAVE_PYTHON3
96                PyBytes_AS_STRING(PyUnicode_AsEncodedString
97                                  (str, "utf-8", "Error ~"));
98#elif defined HAVE_PYTHON2
99                PyString_AsString(str);
100#endif
101            /* FIXME leak leak leak */
102            char *s = strdup(err);
103
104            if (s != NULL)
105            {
106                /* Representation can include '' around strings */
107                removeTrailingStuff(s);
108                get_config(config_option[i].name)->set(s, screen_list);
109            }
110        }
111        i++;
112    }
113}
114
115void getExportedValues(PyObject * dictionary)
116{
117    struct config_line *config_option = get_config_option();
118    int i = 0;
119    while (strncmp(config_option[i].name, "last", strlen("last")))
120    {
121        addVariableFromConfig(dictionary, config_option[i].name,
122                              config_option[i].name);
123        i++;
124    }
125}
126
127static PyObject *neercs_get(PyObject * self, PyObject * args)
128{
129    char *s = NULL;
130
131    debug("Get using list at %p", screen_list);
132
133    if (!PyArg_ParseTuple(args, "s", &s))
134    {
135        PyErr_SetString(PyExc_ValueError, "Can't parse argument");
136        debug("py Can't parse");
137        return NULL;
138    }
139    debug("py Argument : '%s'", s);
140    struct config_line *c = get_config(s);
141
142    if (c)
143        return Py_BuildValue("s", c->get(screen_list));
144
145
146    PyErr_SetString(PyExc_ValueError,
147                    "Can't get value for specified variable");
148    return NULL;
149}
150
151static PyObject *neercs_version(PyObject * self, PyObject * args)
152{
153    return Py_BuildValue("s", PACKAGE_VERSION);
154}
155
156static PyMethodDef NeercsMethods[] =
157{
158    { "version", neercs_version, METH_NOARGS, "Return the neercs version." },
159    { "get", neercs_get, METH_VARARGS,
160      "Return the specified variable's value." },
161    { NULL, NULL, 0, NULL }
162};
163
164#if defined HAVE_PYTHON3
165static PyObject *PyInit_neercs(void)
166{
167    static PyModuleDef NeercsModule =
168    {
169        PyModuleDef_HEAD_INIT, "neercs", NULL, -1, NeercsMethods,
170        NULL, NULL, NULL, NULL
171    };
172
173    return PyModule_Create(&NeercsModule);
174}
175
176#elif defined HAVE_PYTHON2
177static void PyInit_neercs(void)
178{
179    PyMethodDef *m = NeercsMethods;
180    PyObject *mod = PyModule_New("neercs");
181    PyModule_AddStringConstant(mod, "__file__", "<synthetic>");
182
183    for (m = NeercsMethods; m->ml_name; m++)
184        PyModule_AddObject(mod, m->ml_name, PyCFunction_New(m, NULL));
185}
186#endif
187
188void initNeercsModule(struct screen_list *sl)
189{
190    screen_list = sl;
191    PyImport_AppendInittab("neercs", &PyInit_neercs);
192    Py_Initialize();
193}
194
195#endif
Note: See TracBrowser for help on using the repository browser.