r26503: Change order of arguments in param interface so it's easier to make the
[ab/samba.git/.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
28 typedef struct param_context param;
29 typedef struct loadparm_context loadparm_context;
30 typedef struct loadparm_service loadparm_service;
31 typedef struct param_section param_section;
32 typedef struct param_opt param_opt;
33 %}
34
35 %import "stdint.i"
36 %import "carrays.i"
37 %import "typemaps.i"
38 %import "../lib/talloc/talloc.i"
39
40 %typemap(default) struct loadparm_context * {
41     $1 = loadparm_init(NULL);
42 }
43
44 %rename(LoadParm) loadparm_context;
45
46 %talloctype(loadparm_context);
47
48 typedef struct loadparm_context {
49     %extend {
50         loadparm_context(TALLOC_CTX *mem_ctx) { return loadparm_init(mem_ctx); }
51         bool load(const char *filename) { return lp_load($self, filename); }
52 #ifdef SWIGPYTHON
53         int __len__() { return lp_numservices($self); }
54         struct loadparm_service *__getitem__(const char *name) { return lp_service($self, name); }
55 #endif
56         const char *configfile() { return lp_configfile($self); }
57         bool is_mydomain(const char *domain) { return lp_is_mydomain($self, domain); }
58         bool is_myname(const char *name) { return lp_is_myname($self, name); }
59         int use(struct param_context *param) { return param_use($self, param); }
60     }
61 } loadparm_context;
62
63 %nodefaultctor loadparm_service;
64 %nodefaultdtor loadparm_service;
65
66 typedef struct loadparm_service {
67     %extend { 
68         const char *volume_label() { return volume_label($self); }
69         const char *printername() { return lp_printername($self); }
70         int maxprintjobs() { return lp_maxprintjobs($self); } 
71     }
72 } loadparm_service;
73
74 %rename(ParamFile) param_context;
75
76 %talloctype(param_context);
77 typedef struct param_context {
78     %extend { 
79         param(TALLOC_CTX *mem_ctx) { return param_init(mem_ctx); }
80         struct param_section *get_section(const char *name);
81         struct param_section *add_section(const char *name);
82         struct param_opt *get(const char *name, const char *section_name="global");
83         const char *get_string(const char *name, const char *section_name="global");
84         int set_string(const char *param, const char *value, const char *section="global");
85         int set(const char *param, PyObject *ob, const char *section_name="global")
86         {
87             struct param_opt *opt = param_get_add($self, param, section_name);
88
89             talloc_free(opt->value);
90             opt->value = talloc_strdup(opt, PyObject_Str(ob));
91
92             return 0;
93         }
94
95         int read(const char *fn);
96         int write(const char *fn);
97     }
98     %pythoncode {
99         def __getitem__(self, name):
100             ret = self.get_section(name)
101             if ret is None:
102                 raise KeyError("No such section %s" % name)
103             return ret
104     }
105 } param;
106
107 %talloctype(param_opt);
108
109 typedef struct param_opt {
110     %extend {
111 #ifdef SWIGPYTHON
112         const char *__str__() { return $self->value; }
113 #endif
114     }
115 } param_opt;
116
117 %talloctype(param);
118 typedef struct param_section {
119     %extend {
120         struct param_opt *get(const char *name);
121     }
122     %pythoncode {
123         def __getitem__(self, name):
124             ret = self.get(name)
125             if ret is None:
126                 raise KeyError("No such option %s" % name)
127             return ret
128     }
129 } param_section;
130
131 %rename(default_config) global_loadparm;
132 struct loadparm_context *global_loadparm;