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

Last change on this file since 4047 was 4047, checked in by jylam, 4 years ago
  • Added XCode project (no even compiling, just file hierarchy)
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: 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
42PyObject *PyInit_neercs(void);
43void removeTrailingStuff(char *b);
44
45
46static void addVariableFromConfig(PyObject * dictionary,
47                                  const char *varname, const char *configname)
48{
49    char *v = get_config(configname)->get(screen_list);
50    if (v != NULL)
51    {
52        PyObject *value = Py_BuildValue("s", v);
53        PyDict_SetItemString(dictionary, varname, value);
54    }
55
56    debug("py get '%s' to '%s'\n", varname,
57          get_config(configname)->get(screen_list));
58
59}
60
61void removeTrailingStuff(char *b)
62{
63        if(!b) return;
64    if(b[0]=='\'') 
65    {
66        memmove(b, &b[1], strlen(b)-1);   
67        b[strlen(b)-2] = 0;
68    }
69}
70
71void setExportedValues(PyObject * dictionary)
72{
73    struct config_line *config_option = get_config_option();
74    int i = 0;
75
76    while (strncmp(config_option[i].name, "last", strlen("last")))
77    {
78        /* Get variable */
79        PyObject *res =
80            PyDict_GetItemString(dictionary, config_option[i].name);
81
82        /* Got it */
83        if (res)
84        {
85            /* Get object representation
86             * FIXME : find a way to check object's type */
87            PyObject *str = PyObject_Repr(res);
88           
89            /* Make sure it's a string */
90            char *err =
91                PyBytes_AS_STRING(PyUnicode_AsEncodedString
92                                  (str, "utf-8", "Error ~"));
93                        /* FIXME leak leak leak */
94            char *s = strdup(err);
95
96            if (s != NULL)
97            {
98                /* Representation can include '' around strings */
99                removeTrailingStuff(s);
100                get_config(config_option[i].name)->set(s, screen_list);
101            }
102        }
103        i++;
104    }
105}
106
107void getExportedValues(PyObject * dictionary)
108{
109    struct config_line *config_option = get_config_option();
110    int i = 0;
111    while (strncmp(config_option[i].name, "last", strlen("last")))
112    {
113        addVariableFromConfig(dictionary, config_option[i].name,
114                              config_option[i].name);
115        i++;
116    }
117}
118
119
120static PyObject *neercs_get(PyObject * self, PyObject * args)
121{
122    char *s = NULL;
123
124    debug("Get using list at %p", screen_list);
125
126    if (!PyArg_ParseTuple(args, "s", &s))
127    {
128        PyErr_SetString(PyExc_ValueError, "Can't parse argument");
129        debug("py Can't parse");
130        return NULL;
131    }
132    debug("py Argument : '%s'", s);
133    struct config_line *c = get_config(s);
134
135    if (c)
136        return Py_BuildValue("s", c->get(screen_list));
137
138
139    PyErr_SetString(PyExc_ValueError,
140                    "Can't get value for specified variable");
141    return NULL;
142}
143
144static PyObject *neercs_version(PyObject * self, PyObject * args)
145{
146    return Py_BuildValue("s", PACKAGE_VERSION);
147}
148
149static PyMethodDef NeercsMethods[] = {
150    {"version", neercs_version, METH_NOARGS, "Return the neercs version."},
151    {"get", neercs_get, METH_VARARGS,
152     "Return the specified variable's value."},
153    {NULL, NULL, 0, NULL}
154};
155
156static PyModuleDef NeercsModule = {
157    PyModuleDef_HEAD_INIT, "neercs", NULL, -1, NeercsMethods,
158    NULL, NULL, NULL, NULL
159};
160
161PyObject *PyInit_neercs(void)
162{
163    PyObject *o = PyModule_Create(&NeercsModule);
164    PyImport_AppendInittab("neercs", &PyInit_neercs);
165    return o;
166}
167
168void initNeercsModule(struct screen_list *sl)
169{
170    screen_list = sl;
171    PyInit_neercs();
172}
173
174
175
176#endif
Note: See TracBrowser for help on using the repository browser.