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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
/*
Unix SMB/CIFS implementation.
Python wrapper for reading smb.conf files
Copyright (C) Jelmer Vernooij 2007
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "includes.h"
#include "Python.h"
#include "param/param.h"
staticforward PyTypeObject param_ParamFileType;
typedef struct {
PyObject_HEAD
struct param_context *param_ctx;
} param_ParamFileObject;
static param_ParamFileObject *py_param_init(void)
{
param_ParamFileObject *param;
param = PyObject_New(param_ParamFileObject, ¶m_ParamFileType);
param->param_ctx = param_init(NULL);
return param;
}
static PyObject *py_param_load(PyObject *self, PyObject *args)
{
char *filename = NULL;
param_ParamFileObject *param;
if (!PyArg_ParseTuple(args, "|s:new", &filename))
return NULL;
param = py_param_init();
if (filename != NULL) {
int ret = param_read(param->param_ctx, filename);
if (ret == -1) {
PyErr_SetString(PyExc_TypeError, "reading file failed");
return NULL;
}
}
return (PyObject *)param;
}
static void
param_dealloc(PyObject* self)
{
PyObject_Del(self);
}
static PyObject *py_param_get(PyObject *_self, PyObject *args)
{
struct param_opt *param;
const char *section_name = NULL, *param_name = NULL;
param_ParamFileObject *self = (param_ParamFileObject *)_self;
if (!PyArg_ParseTuple(args, (char *)"s|s", ¶m_name, §ion_name))
return NULL;
param = param_get(self->param_ctx, section_name, param_name);
if (param == NULL)
return Py_None;
return PyString_FromString(param->value);
}
static PyObject *py_param_set(PyObject *_self, PyObject *args)
{
param_ParamFileObject *self = (param_ParamFileObject *)_self;
const char *section_name = NULL, *param_name = NULL, *param_value = NULL;
if (!PyArg_ParseTuple(args, "ss|s", ¶m_name, ¶m_value, §ion_name))
return NULL;
if (section_name == NULL)
section_name = GLOBAL_NAME;
if (param_set_string(self->param_ctx, section_name, param_name, param_value) != 0) {
PyErr_SetString(PyExc_TypeError, "setting variable failed");
return NULL;
}
return Py_None;
}
static PyObject *py_param_save(PyObject *_self, PyObject *args)
{
param_ParamFileObject *self = (param_ParamFileObject *)_self;
const char *filename = NULL;
if (!PyArg_ParseTuple(args, "s", &filename))
return NULL;
if (param_write(self->param_ctx, filename) != 0) {
PyErr_SetString(PyExc_TypeError, "unable to save");
return NULL;
}
return Py_None;
}
static PyObject *py_param_use(PyObject *_self, PyObject *args)
{
param_ParamFileObject *self = (param_ParamFileObject *)_self;
if (!PyArg_ParseTuple(args, ""))
return NULL;
if (param_use(global_loadparm, self->param_ctx) != 0) {
PyErr_SetString(PyExc_TypeError, "unable to use");
return NULL;
}
return Py_None;
}
static PyMethodDef param_methods[] = {
{"get", (PyCFunction)py_param_get, METH_VARARGS,
"Get a parameter."},
{"set", (PyCFunction)py_param_set, METH_VARARGS,
"Set a parameter."},
{"save", (PyCFunction)py_param_save, METH_VARARGS,
"Save file" },
{"use", (PyCFunction)py_param_use, METH_VARARGS,
"Use param file" },
{NULL, NULL, 0, NULL}
};
static PyObject *
param_getattr(PyTypeObject *obj, char *name)
{
return Py_FindMethod(param_methods, (PyObject *)obj, name);
}
static PyTypeObject param_ParamFileType = {
PyObject_HEAD_INIT(NULL) 0,
.tp_name = "ParamFile",
.tp_basicsize = sizeof(param_ParamFileObject),
.tp_dealloc = param_dealloc,
.tp_getattr = param_getattr,
};
static PyMethodDef methods[] = {
{ "ParamFile", (PyCFunction)py_param_load, METH_VARARGS, NULL},
{ NULL, NULL }
};
PyDoc_STRVAR(param_doc, "Simple wrappers around the smb.conf parsers");
PyMODINIT_FUNC initparam(void)
{
PyObject *mod = Py_InitModule3("param", methods, param_doc);
if (mod == NULL)
return;
PyModule_AddObject(mod, "configfile",
PyString_FromString(lp_configfile(global_loadparm)));
}
|