2 Unix SMB
/CIFS implementation.
3 Copyright
(C
) Jelmer Vernooij
<jelmer@samba.org
> 2007
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.
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.
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
/>.
19 %module
(docstring
="Parsing and writing Samba configuration files.",package
="samba.param") param
26 #include
"param/param.h"
27 #include
"param/loadparm.h"
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
;
39 %import
"../lib/talloc/talloc.i"
41 %typemap
(default
,noblock
=1) struct loadparm_context
* {
42 $
1 = loadparm_init
(NULL);
45 %rename
(LoadParm
) loadparm_context
;
47 %talloctype
(loadparm_context
);
49 typedef struct loadparm_context
{
51 loadparm_context
(TALLOC_CTX
*mem_ctx
) { return loadparm_init
(mem_ctx
); }
52 struct loadparm_service
*default_service
() { return lp_default_service
($self
); }
53 %feature
("docstring") load
"S.load(filename) -> None\n" \
54 "Load specified file.";
55 bool load
(const char
*filename
) { return lp_load
($self
, filename
); }
56 %feature
("docstring") load_default
"S.load_default() -> None\n" \
57 "Load default smb.conf file.";
58 bool load_default
() { return lp_load_default
($self
); }
60 int __len__
() { return lp_numservices
($self
); }
61 struct loadparm_service
*__getitem__
(const char
*name
) { return lp_service
($self
, name
); }
63 %feature
("docstring") configfile
"S.configfile() -> string\n" \
64 "Return name of last config file that was loaded.";
65 const char
*configfile
() { return lp_configfile
($self
); }
66 %feature
("docstring") is_mydomain
"S.is_mydomain(domain_name) -> bool\n" \
67 "Check whether the specified name matches our domain name.";
68 bool is_mydomain
(const char
*domain
) { return lp_is_mydomain
($self
, domain
); }
69 %feature
("docstring") is_myname
"S.is_myname(netbios_name) -> bool\n" \
70 "Check whether the specified name matches one of our netbios names.";
71 bool is_myname
(const char
*name
) { return lp_is_myname
($self
, name
); }
72 int use
(struct param_context
*param_ctx
) { return param_use
($self
, param_ctx
); }
73 %feature
("docstring") set
"S.set(name, value) -> bool\n" \
74 "Change a parameter.";
75 bool set
(const char
*parm_name
, const char
*parm_value
) {
76 if
(parm_value
== NULL)
78 return lp_set_cmdline
($self
, parm_name
, parm_value
);
81 %feature
("docstring") set
"S.get(name, service_name) -> value\n" \
82 "Find specified parameter.";
83 PyObject
*get
(const char
*param_name
, const char
*service_name
)
85 struct parm_struct
*parm
= NULL;
86 void
*parm_ptr
= NULL;
89 if
(service_name
!= NULL) {
90 struct loadparm_service
*service
;
91 /* its a share parameter
*/
92 service
= lp_service
($self
, service_name
);
93 if
(service
== NULL) {
96 if
(strchr
(param_name
, '
:'
)) {
97 /* its a parametric option on a share
*/
98 const char
*type
= talloc_strndup
($self
,
100 strcspn
(param_name
, ":"));
101 const char
*option
= strchr
(param_name
, '
:'
) + 1;
103 if
(type
== NULL || option
== NULL) {
106 value
= lp_get_parametric
($self
, service
, type
, option
);
110 return PyString_FromString
(value
);
113 parm
= lp_parm_struct
(param_name
);
114 if
(parm
== NULL || parm-
>class
== P_GLOBAL
) {
117 parm_ptr
= lp_parm_ptr
($self
, service
, parm
);
118 } else if
(strchr
(param_name
, '
:'
)) {
119 /* its a global parametric option
*/
120 const char
*type
= talloc_strndup
($self
,
121 param_name
, strcspn
(param_name
, ":"));
122 const char
*option
= strchr
(param_name
, '
:'
) + 1;
124 if
(type
== NULL || option
== NULL) {
127 value
= lp_get_parametric
($self
, NULL, type
, option
);
130 return PyString_FromString
(value
);
132 /* its a global parameter
*/
133 parm
= lp_parm_struct
(param_name
);
137 parm_ptr
= lp_parm_ptr
($self
, NULL, parm
);
140 if
(parm
== NULL || parm_ptr
== NULL) {
144 /* construct and return the right type of python object
*/
145 switch
(parm-
>type
) {
148 return PyString_FromString
(*(char
**)parm_ptr
);
150 return PyBool_FromLong
(*(bool
*)parm_ptr
);
154 return PyLong_FromLong
(*(int
*)parm_ptr
);
156 for
(i
=0; parm-
>enum_list
[i
].name
; i
++) {
157 if
(*(int
*)parm_ptr
== parm-
>enum_list
[i
].value
) {
158 return PyString_FromString
(parm-
>enum_list
[i
].name
);
165 const char
**strlist
= *(const char
***)parm_ptr
;
166 PyObject
*pylist
= PyList_New
(str_list_length
(strlist
));
167 for
(j
= 0; strlist
[j
]; j
++)
168 PyList_SetItem
(pylist
, j
,
169 PyString_FromString
(strlist
[j
]));
180 %nodefaultctor loadparm_service
;
181 %nodefaultdtor loadparm_service
;
183 typedef struct loadparm_service
{
185 const char
*volume_label
(struct loadparm_service
*sDefault
) { return volume_label
($self
, sDefault
); }
186 const char
*printername
(struct loadparm_service
*sDefault
) { return lp_printername
($self
, sDefault
); }
187 int maxprintjobs
(struct loadparm_service
*sDefault
) { return lp_maxprintjobs
($self
, sDefault
); }
191 %rename
(ParamFile
) param_context
;
193 %talloctype
(param_context
);
194 typedef struct param_context
{
196 param
(TALLOC_CTX
*mem_ctx
) { return param_init
(mem_ctx
); }
197 %feature
("docstring") add_section
"S.get_section(name) -> section\n"
198 "Get an existing section.";
199 struct param_section
*get_section
(const char
*name
);
200 %feature
("docstring") add_section
"S.add_section(name) -> section\n"
201 "Add a new section.";
202 struct param_section
*add_section
(const char
*name
);
203 struct param_opt
*get
(const char
*name
, const char
*section_name
="global");
204 const char
*get_string
(const char
*name
, const char
*section_name
="global");
205 int set_string
(const char
*param
, const char
*value
, const char
*section
="global");
207 int set
(const char
*parameter
, PyObject
*ob
, const char
*section_name
="global")
209 struct param_opt
*opt
= param_get_add
($self
, parameter
, section_name
);
211 talloc_free
(opt-
>value
);
212 opt-
>value
= talloc_strdup
(opt
, PyString_AsString
(PyObject_Str
(ob
)));
219 %feature
("docstring") first_section
"S.first_section() -> section\n"
220 "Find first section";
221 struct param_section
*first_section
() { return $self-
>sections
; }
222 %feature
("docstring") next_section
"S.next_section(prev) -> section\n"
224 struct param_section
*next_section
(struct param_section
*s
) { return s-
>next
; }
226 %feature
("docstring") read
"S.read(filename) -> bool\n"
228 int read
(const char
*fn
);
229 %feature
("docstring") read
"S.write(filename) -> bool\n"
230 "Write this object to a file.";
231 int write
(const char
*fn
);
234 def __getitem__
(self
, name
):
235 ret
= self.get_section
(name
)
237 raise KeyError
("No such section %s" % name
)
240 class SectionIterator
:
241 def __init__
(self
, param
):
250 self.key
= self.param.first_section
()
255 self.key
= self.param.next_section
(self.key
)
261 return self.SectionIterator
(self
)
265 %talloctype
(param_opt
);
267 typedef struct param_opt
{
270 const char
*key
, *value
;
273 const char
*__str__
() { return $self-
>value
; }
279 typedef struct param_section
{
283 struct param_opt
*get
(const char
*name
);
284 struct param_opt
*first_parameter
() { return $self-
>parameters
; }
285 struct param_opt
*next_parameter
(struct param_opt
*s
) { return s-
>next
; }
288 def __getitem__
(self
, name
):
291 raise KeyError
("No such option %s" % name
)
295 def __init__
(self
, section
):
296 self.section
= section
304 self.key
= self.section.first_parameter
()
309 self.key
= self.section.next_parameter
(self.key
)
315 return self.ParamIterator
(self
)
319 %rename
(default_config
) global_loadparm
;
320 struct loadparm_context
*global_loadparm
;
324 struct loadparm_context
*lp_from_py_object
(PyObject
*py_obj
)
326 struct loadparm_context
*lp_ctx
;
327 if
(PyString_Check
(py_obj
)) {
328 lp_ctx
= loadparm_init
(NULL);
329 if
(!lp_load
(lp_ctx
, PyString_AsString
(py_obj
))) {
336 if
(py_obj
== Py_None
) {
337 lp_ctx
= loadparm_init
(NULL);
338 if
(!lp_load_default
(lp_ctx
)) {
345 if
(SWIG_ConvertPtr
(py_obj
, (void
*)&lp_ctx, SWIGTYPE_p_loadparm_context, 0 | 0 ) < 0)