1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
#include <Python.h>
#include "config.h"
#include "interpreter.h"
struct BareConfig
{
PyObject *object;
};
PyObject *_bareConfigObject(BareConfig *c)
{
if (c && c->object)
return c->object;
else
Py_RETURN_NONE;
}
BareConfig *bareConfigCreate(Message *m, FILE* outFd, FILE* inFd, FILE* errFd)
{
PyObject *pyout = PyFile_FromFile(((!outFd || fileno(outFd) <= 0) ? stdout : outFd),
"", "w", 0);
PyObject *pyin = PyFile_FromFile(((!inFd || fileno(inFd) <= 0) ? stdin : inFd),
"", "r", 0);
PyObject *pyerr = PyFile_FromFile(((!errFd || fileno(errFd) <= 0) ? stderr : errFd),
"", "w", 0);
PyObject *obj = executeFunction("layman.config", "BareConfig", "OOOO", _messageObject(m), pyout, pyin, pyerr);
Py_DECREF(pyout);
Py_DECREF(pyin);
Py_DECREF(pyerr);
if (!obj)
return NULL;
BareConfig *ret = malloc(sizeof(BareConfig));
ret->object = obj;
return ret;
}
void bareConfigFree(BareConfig* cfg)
{
if (cfg && cfg->object)
{
Py_DECREF(cfg->object);
}
if (cfg)
free(cfg);
}
const char* bareConfigGetDefaultValue(BareConfig* cfg, const char* opt)
{
PyObject *obj = PyObject_CallMethod(cfg->object, "get_defaults", NULL);
if (!obj)
return NULL;
if (PyDict_Contains(obj, PyBytes_FromString(opt)))
{
PyObject *pyopt = PyBytes_FromString(opt);
char *tmp = PyBytes_AsString(PyDict_GetItem(obj, pyopt));
Py_DECREF(pyopt);
char *ret = malloc(sizeof(char) * strlen(tmp));
strcpy(ret, tmp);
Py_DECREF(obj);
return ret;
}
else
return "";
}
const char* bareConfigGetOptionValue(BareConfig* cfg, const char* opt)
{
PyObject *obj = PyObject_CallMethod(cfg->object, "get_option", "(z)", opt);
char *tmp = PyBytes_AsString(obj);
char *ret = malloc(sizeof(char) * (strlen(tmp) + 1));
strcpy(ret, tmp);
Py_DECREF(obj);
return ret;
}
int bareConfigSetOptionValue(BareConfig* cfg, const char* opt, const char* val)
{
PyObject *obj = PyObject_CallMethod(cfg->object, "set_option", "(zz)", opt, val);
int ret;
if (obj)
ret = 1;
else
ret = 0;
Py_DECREF(obj);
return ret;
}
|