r26570: - Trim size of the swig-generated Python bindings by removing a bunch of...
[sfrench/samba-autobuild/.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) { return param_use($self, param); }
61         bool set(const char *parm_name, const char *parm_value) {
62             return lp_set_cmdline($self, parm_name, parm_value);
63         }
64
65         PyObject *get(const char *param_name, const char *service_name)
66         {
67             struct parm_struct *parm = NULL;
68             void *parm_ptr = NULL;
69             int i;
70
71             if (service_name != NULL) {
72                 struct loadparm_service *service;
73                 /* its a share parameter */
74                 service = lp_service($self, service_name);
75                 if (service == NULL) {
76                     return Py_None;
77                 }
78                 if (strchr(param_name, ':')) {
79                     /* its a parametric option on a share */
80                     const char *type = talloc_strndup($self, 
81                                       param_name, 
82                                       strcspn(param_name, ":"));
83                     const char *option = strchr(param_name, ':') + 1;
84                     const char *value;
85                     if (type == NULL || option == NULL) {
86                         return Py_None;
87                     }
88                     value = lp_get_parametric($self, service, type, option);
89                     if (value == NULL) {
90                         return Py_None;
91                     }
92                     return PyString_FromString(value);
93                 }
94
95                 parm = lp_parm_struct(param_name);
96                 if (parm == NULL || parm->class == P_GLOBAL) {
97                     return Py_None;
98                 }
99                 parm_ptr = lp_parm_ptr($self, service, parm);
100             } else if (strchr(param_name, ':')) {
101                 /* its a global parametric option */
102                 const char *type = talloc_strndup($self, 
103                                   param_name, strcspn(param_name, ":"));
104                 const char *option = strchr(param_name, ':') + 1;
105                 const char *value;
106                 if (type == NULL || option == NULL) {
107                     return Py_None;
108                 }
109                 value = lp_get_parametric($self, NULL, type, option);
110                 if (value == NULL)
111                     return Py_None;
112                 return PyString_FromString(value);
113             } else {
114                 /* its a global parameter */
115                 parm = lp_parm_struct(param_name);
116                 if (parm == NULL) {
117                     return Py_None;
118                 }
119                 parm_ptr = lp_parm_ptr($self, NULL, parm);
120             }
121
122             if (parm == NULL || parm_ptr == NULL) {
123                 return Py_None;
124             }
125
126             /* construct and return the right type of python object */
127             switch (parm->type) {
128             case P_STRING:
129             case P_USTRING:
130                 return PyString_FromString(*(char **)parm_ptr);
131             case P_BOOL:
132                 return PyBool_FromLong(*(bool *)parm_ptr);
133             case P_INTEGER:
134             case P_OCTAL:
135             case P_BYTES:
136                 return PyLong_FromLong(*(int *)parm_ptr);
137             case P_ENUM:
138                 for (i=0; parm->enum_list[i].name; i++) {
139                     if (*(int *)parm_ptr == parm->enum_list[i].value) {
140                         return PyString_FromString(parm->enum_list[i].name);
141                     }
142                 }
143                 return Py_None;
144             case P_LIST: 
145                 {
146                     int i;
147                     const char **strlist = *(const char ***)parm_ptr;
148                     PyObject *pylist = PyList_New(str_list_length(strlist));
149                     for (i = 0; strlist[i]; i++) 
150                         PyList_SetItem(pylist, i, 
151                                        PyString_FromString(strlist[i]));
152                     return pylist;
153                 }
154
155                 break;
156             }
157             return Py_None;
158         }
159     }
160 } loadparm_context;
161
162 %nodefaultctor loadparm_service;
163 %nodefaultdtor loadparm_service;
164
165 typedef struct loadparm_service {
166     %extend { 
167         const char *volume_label() { return volume_label($self); }
168         const char *printername() { return lp_printername($self); }
169         int maxprintjobs() { return lp_maxprintjobs($self); } 
170     }
171 } loadparm_service;
172
173 %rename(ParamFile) param_context;
174
175 %talloctype(param_context);
176 typedef struct param_context {
177     %extend { 
178         param(TALLOC_CTX *mem_ctx) { return param_init(mem_ctx); }
179         struct param_section *get_section(const char *name);
180         struct param_section *add_section(const char *name);
181         struct param_opt *get(const char *name, const char *section_name="global");
182         const char *get_string(const char *name, const char *section_name="global");
183         int set_string(const char *param, const char *value, const char *section="global");
184         int set(const char *param, PyObject *ob, const char *section_name="global")
185         {
186             struct param_opt *opt = param_get_add($self, param, section_name);
187
188             talloc_free(opt->value);
189             opt->value = talloc_strdup(opt, PyObject_Str(ob));
190
191             return 0;
192         }
193
194         int read(const char *fn);
195         int write(const char *fn);
196     }
197     %pythoncode {
198         def __getitem__(self, name):
199             ret = self.get_section(name)
200             if ret is None:
201                 raise KeyError("No such section %s" % name)
202             return ret
203     }
204 } param;
205
206 %talloctype(param_opt);
207
208 typedef struct param_opt {
209     %extend {
210 #ifdef SWIGPYTHON
211         const char *__str__() { return $self->value; }
212 #endif
213     }
214 } param_opt;
215
216 %talloctype(param);
217 typedef struct param_section {
218     %extend {
219         struct param_opt *get(const char *name);
220     }
221     %pythoncode {
222         def __getitem__(self, name):
223             ret = self.get(name)
224             if ret is None:
225                 raise KeyError("No such option %s" % name)
226             return ret
227     }
228 } param_section;
229
230 %rename(default_config) global_loadparm;
231 struct loadparm_context *global_loadparm;