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