python: Accept loadparm filename rather than loadparm object, too.
[kai/samba.git] / source4 / param / param.i
1 /* 
2    Unix SMB/CIFS implementation.
3    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
4    
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9    
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14    
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 %module(package="samba.param") param
20
21 %{
22 #include <stdint.h>
23 #include <stdbool.h>
24
25 #include "includes.h"
26 #include "param/param.h"
27 #include "param/loadparm.h"
28
29 typedef struct param_context param;
30 typedef struct loadparm_context loadparm_context;
31 typedef struct loadparm_service loadparm_service;
32 typedef struct param_section param_section;
33 typedef struct param_opt param_opt;
34 %}
35
36 %import "stdint.i"
37 %import "carrays.i"
38 %import "typemaps.i"
39 %import "../lib/talloc/talloc.i"
40
41 %typemap(default,noblock=1) struct loadparm_context * {
42     $1 = loadparm_init(NULL);
43 }
44
45 %rename(LoadParm) loadparm_context;
46
47 %talloctype(loadparm_context);
48
49 typedef struct loadparm_context {
50     %extend {
51         loadparm_context(TALLOC_CTX *mem_ctx) { return loadparm_init(mem_ctx); }
52         bool load(const char *filename) { return lp_load($self, filename); }
53 #ifdef SWIGPYTHON
54         int __len__() { return lp_numservices($self); }
55         struct loadparm_service *__getitem__(const char *name) { return lp_service($self, name); }
56 #endif
57         const char *configfile() { return lp_configfile($self); }
58         bool is_mydomain(const char *domain) { return lp_is_mydomain($self, domain); }
59         bool is_myname(const char *name) { return lp_is_myname($self, name); }
60         int use(struct param_context *param_ctx) { return param_use($self, param_ctx); }
61         bool set(const char *parm_name, const char *parm_value) {
62             if (parm_value == NULL)
63                 return false;
64             return lp_set_cmdline($self, parm_name, parm_value);
65         }
66
67         PyObject *get(const char *param_name, const char *service_name)
68         {
69             struct parm_struct *parm = NULL;
70             void *parm_ptr = NULL;
71             int i;
72
73             if (service_name != NULL) {
74                 struct loadparm_service *service;
75                 /* its a share parameter */
76                 service = lp_service($self, service_name);
77                 if (service == NULL) {
78                     return Py_None;
79                 }
80                 if (strchr(param_name, ':')) {
81                     /* its a parametric option on a share */
82                     const char *type = talloc_strndup($self, 
83                                       param_name, 
84                                       strcspn(param_name, ":"));
85                     const char *option = strchr(param_name, ':') + 1;
86                     const char *value;
87                     if (type == NULL || option == NULL) {
88                         return Py_None;
89                     }
90                     value = lp_get_parametric($self, service, type, option);
91                     if (value == NULL) {
92                         return Py_None;
93                     }
94                     return PyString_FromString(value);
95                 }
96
97                 parm = lp_parm_struct(param_name);
98                 if (parm == NULL || parm->class == P_GLOBAL) {
99                     return Py_None;
100                 }
101                 parm_ptr = lp_parm_ptr($self, service, parm);
102             } else if (strchr(param_name, ':')) {
103                 /* its a global parametric option */
104                 const char *type = talloc_strndup($self, 
105                                   param_name, strcspn(param_name, ":"));
106                 const char *option = strchr(param_name, ':') + 1;
107                 const char *value;
108                 if (type == NULL || option == NULL) {
109                     return Py_None;
110                 }
111                 value = lp_get_parametric($self, NULL, type, option);
112                 if (value == NULL)
113                     return Py_None;
114                 return PyString_FromString(value);
115             } else {
116                 /* its a global parameter */
117                 parm = lp_parm_struct(param_name);
118                 if (parm == NULL) {
119                     return Py_None;
120                 }
121                 parm_ptr = lp_parm_ptr($self, NULL, parm);
122             }
123
124             if (parm == NULL || parm_ptr == NULL) {
125                 return Py_None;
126             }
127
128             /* construct and return the right type of python object */
129             switch (parm->type) {
130             case P_STRING:
131             case P_USTRING:
132                 return PyString_FromString(*(char **)parm_ptr);
133             case P_BOOL:
134                 return PyBool_FromLong(*(bool *)parm_ptr);
135             case P_INTEGER:
136             case P_OCTAL:
137             case P_BYTES:
138                 return PyLong_FromLong(*(int *)parm_ptr);
139             case P_ENUM:
140                 for (i=0; parm->enum_list[i].name; i++) {
141                     if (*(int *)parm_ptr == parm->enum_list[i].value) {
142                         return PyString_FromString(parm->enum_list[i].name);
143                     }
144                 }
145                 return Py_None;
146             case P_LIST: 
147                 {
148                     int j;
149                     const char **strlist = *(const char ***)parm_ptr;
150                     PyObject *pylist = PyList_New(str_list_length(strlist));
151                     for (j = 0; strlist[j]; j++) 
152                         PyList_SetItem(pylist, j, 
153                                        PyString_FromString(strlist[j]));
154                     return pylist;
155                 }
156
157                 break;
158             }
159             return Py_None;
160         }
161     }
162 } loadparm_context;
163
164 %nodefaultctor loadparm_service;
165 %nodefaultdtor loadparm_service;
166
167 typedef struct loadparm_service {
168     %extend { 
169         const char *volume_label() { return volume_label($self); }
170         const char *printername() { return lp_printername($self); }
171         int maxprintjobs() { return lp_maxprintjobs($self); } 
172     }
173 } loadparm_service;
174
175 %rename(ParamFile) param_context;
176
177 %talloctype(param_context);
178 typedef struct param_context {
179     %extend { 
180         param(TALLOC_CTX *mem_ctx) { return param_init(mem_ctx); }
181         struct param_section *get_section(const char *name);
182         struct param_section *add_section(const char *name);
183         struct param_opt *get(const char *name, const char *section_name="global");
184         const char *get_string(const char *name, const char *section_name="global");
185         int set_string(const char *param, const char *value, const char *section="global");
186 #ifdef SWIGPYTHON
187         int set(const char *parameter, PyObject *ob, const char *section_name="global")
188         {
189             struct param_opt *opt = param_get_add($self, parameter, section_name);
190
191             talloc_free(opt->value);
192             opt->value = talloc_strdup(opt, PyString_AsString(PyObject_Str(ob)));
193
194             return 0;
195         }
196         
197 #endif
198
199         struct param_section *first_section() { return $self->sections; }
200         struct param_section *next_section(struct param_section *s) { return s->next; }
201
202         int read(const char *fn);
203         int write(const char *fn);
204     }
205     %pythoncode {
206         def __getitem__(self, name):
207             ret = self.get_section(name)
208             if ret is None:
209                 raise KeyError("No such section %s" % name)
210             return ret
211
212         class SectionIterator:
213             def __init__(self, param):
214                 self.param = param
215                 self.key = None
216
217             def __iter__(self):
218                 return self
219                 
220             def next(self):
221                 if self.key is None:
222                     self.key = self.param.first_section()
223                     if self.key is None:
224                         raise StopIteration
225                     return self.key
226                 else:
227                     self.key = self.param.next_section(self.key)
228                     if self.key is None:
229                         raise StopIteration
230                     return self.key
231
232         def __iter__(self):
233             return self.SectionIterator(self)
234     }
235 } param;
236
237 %talloctype(param_opt);
238
239 typedef struct param_opt {
240     %immutable key;
241     %immutable value;
242     const char *key, *value;
243     %extend {
244 #ifdef SWIGPYTHON
245         const char *__str__() { return $self->value; }
246 #endif
247     }
248 } param_opt;
249
250 %talloctype(param);
251 typedef struct param_section {
252     %immutable name;
253     const char *name;
254     %extend {
255         struct param_opt *get(const char *name);
256         struct param_opt *first_parameter() { return $self->parameters; }
257         struct param_opt *next_parameter(struct param_opt *s) { return s->next; }
258     }
259     %pythoncode {
260         def __getitem__(self, name):
261             ret = self.get(name)
262             if ret is None:
263                 raise KeyError("No such option %s" % name)
264             return ret
265
266         class ParamIterator:
267             def __init__(self, section):
268                 self.section = section
269                 self.key = None
270
271             def __iter__(self):
272                 return self
273                 
274             def next(self):
275                 if self.key is None:
276                     self.key = self.section.first_parameter()
277                     if self.key is None:
278                         raise StopIteration
279                     return self.key
280                 else:
281                     self.key = self.section.next_parameter(self.key)
282                     if self.key is None:
283                         raise StopIteration
284                     return self.key
285
286         def __iter__(self):
287             return self.ParamIterator(self)
288     }
289 } param_section;
290
291 %rename(default_config) global_loadparm;
292 struct loadparm_context *global_loadparm;
293
294 %{
295
296 struct loadparm_context *lp_from_py_object(PyObject *py_obj)
297 {
298     struct loadparm_context *lp_ctx;
299     if (PyString_Check(py_obj)) {
300         lp_ctx = loadparm_init(NULL);
301         if (!lp_load(lp_ctx, PyString_AsString(py_obj))) {
302             talloc_free(lp_ctx);
303             return NULL;
304         }
305         return lp_ctx;
306     }
307
308     if (SWIG_ConvertPtr(py_obj, (void *)&lp_ctx, SWIGTYPE_p_loadparm_context, 0 |  0 ) < 0)
309         return NULL;
310     return lp_ctx;
311 }
312
313 %}