r26662: pidl/python: Add constructors for struct/union types.
[ira/wip.git] / source / pidl / lib / Parse / Pidl / Samba4 / Python.pm
1 ###################################################
2 # Python function wrapper generator
3 # Copyright jelmer@samba.org 2007
4 # released under the GNU GPL
5
6 package Parse::Pidl::Samba4::Python;
7
8 use Exporter;
9 @ISA = qw(Exporter);
10
11 use strict;
12 use Parse::Pidl::Typelist;
13 use Parse::Pidl::Util qw(has_property ParseExpr);
14
15 use vars qw($VERSION);
16 $VERSION = '0.01';
17
18 sub new($) {
19         my ($class) = @_;
20         my $self = { res => "", res_hdr => "", tabs => "", constants => {}};
21         bless($self, $class);
22 }
23
24 sub pidl_hdr ($$)
25 {
26         my $self = shift;
27         $self->{res_hdr} .= shift;
28 }
29
30 sub pidl($$)
31 {
32         my ($self, $d) = @_;
33         if ($d) {
34                 $self->{res} .= $self->{tabs};
35                 $self->{res} .= $d;
36         }
37         $self->{res} .= "\n";
38 }
39
40 sub indent($)
41 {
42         my ($self) = @_;
43         $self->{tabs} .= "\t";
44 }
45
46 sub deindent($)
47 {
48         my ($self) = @_;
49         $self->{tabs} = substr($self->{tabs}, 0, -1);
50 }
51
52 sub Import
53 {
54         my $self = shift;
55         my @imports = @_;
56         foreach (@imports) {
57                 s/\.idl\"$//;
58                 s/^\"//;
59                 $self->pidl_hdr("#include \"librpc/gen_ndr/py_$_\.h\"\n");
60         }
61 }
62
63 sub Const($$)
64 {
65     my ($self, $const) = @_;
66     $self->{constants}->{$const->{NAME}} = [$const->{DATA}->{TYPE}, $const->{VALUE}];
67 }
68
69 sub FromTypeToPythonFunction($$)
70 {
71         my ($self, $type) = @_;
72
73         #FIXME
74 }
75
76 sub FromPythonToTypeFunction($$)
77 {
78         my ($self, $type) = @_;
79
80         #FIXME
81 }
82
83 sub TypeConstructor($$)
84 {
85         my ($self, $type) = @_;
86
87         $self->pidl("staticforward PyTypeObject $type->{NAME}_ObjectType;");
88         $self->pidl("typedef struct {");
89         $self->indent;
90         $self->pidl("PyObject_HEAD");
91         $self->pidl("void *object;"); # FIXME: Use real type rather than void
92         $self->deindent;
93         $self->pidl("} $type->{NAME}_Object;");
94
95         $self->pidl("");
96
97         $self->pidl("static PyObject *py_$type->{NAME}_getattr(PyTypeObject *obj, char *name)");
98         $self->pidl("{");
99         $self->indent;
100         $self->pidl("return Py_None;");
101         $self->deindent;
102         $self->pidl("}");
103         $self->pidl("");
104
105         $self->pidl("static void py_$type->{NAME}_dealloc(PyObject* self)");
106         $self->pidl("{");
107         $self->indent;
108         $self->pidl("$type->{NAME}_Object *obj = ($type->{NAME}_Object *)self;");
109         $self->pidl("talloc_free(obj->object);");
110         $self->pidl("PyObject_Del(self);");
111         $self->deindent;
112         $self->pidl("}");
113         $self->pidl("");
114
115         $self->pidl("static PyObject *py_$type->{NAME}_setattr(PyTypeObject *obj, char *name, PyObject *value)");
116         $self->pidl("{");
117         $self->indent;
118         $self->pidl("return Py_None;");
119         $self->deindent;
120         $self->pidl("}");
121         $self->pidl("");
122
123         $self->pidl("static PyTypeObject $type->{NAME}_ObjectType = {");
124         $self->indent;
125         $self->pidl("PyObject_HEAD_INIT(NULL) 0,");
126         $self->pidl(".tp_name = (char *)\"$type->{NAME}\",");
127         $self->pidl(".tp_basicsize = sizeof($type->{NAME}_Object),");
128         $self->pidl(".tp_dealloc = py_$type->{NAME}_dealloc,");
129         $self->pidl(".tp_getattr = py_$type->{NAME}_getattr,");
130         $self->pidl(".tp_setattr = py_$type->{NAME}_setattr,");
131         $self->deindent;
132         $self->pidl("};");
133
134         $self->pidl("");
135
136         $self->pidl("static PyObject *py_$type->{NAME}(PyObject *self, PyObject *args)");
137         $self->pidl("{");
138         $self->indent;
139         $self->pidl("$type->{NAME}\_Object *ret;");
140         $self->pidl("ret = PyObject_New($type->{NAME}_Object, &$type->{NAME}_ObjectType);");
141         $self->pidl("return (PyObject *) ret;");
142         $self->deindent;
143         $self->pidl("}");
144         $self->pidl("");
145 }
146
147 sub PythonFunction($$$)
148 {
149         my ($self, $fn, $iface) = @_;
150
151         $self->pidl("static PyObject *py_$fn->{NAME}(PyObject *self, PyObject *args)");
152         $self->pidl("{");
153         $self->indent;
154         $self->pidl("$iface\_InterfaceObject *iface = ($iface\_InterfaceObject *)self;");
155         $self->pidl("NTSTATUS status;");
156         $self->pidl("");
157         # FIXME
158         $self->handle_ntstatus("status", "NULL");
159         $self->pidl("return Py_None;");
160         $self->deindent;
161         $self->pidl("}");
162         $self->pidl("");
163 }
164
165 sub handle_ntstatus($$$)
166 {
167         my ($self, $var, $retval) = @_;
168
169         $self->pidl("if (NT_STATUS_IS_ERR($var)) {");
170         $self->indent;
171         $self->pidl("PyErr_SetString(PyExc_RuntimeError, nt_errstr($var));");
172         $self->pidl("return $retval;");
173         $self->deindent;
174         $self->pidl("}");
175         $self->pidl("");
176 }
177
178 sub Interface($$)
179 {
180         my($self,$interface) = @_;
181
182         $self->pidl_hdr("#ifndef _HEADER_PYTHON_$interface->{NAME}\n");
183         $self->pidl_hdr("#define _HEADER_PYTHON_$interface->{NAME}\n\n");
184
185         $self->pidl_hdr("\n");
186
187         $self->Const($_) foreach (@{$interface->{CONSTS}});
188
189         foreach (@{$interface->{TYPES}}) {
190                 $self->FromTypeToPythonFunction($_);    
191                 $self->FromPythonToTypeFunction($_);    
192                 $self->TypeConstructor($_);
193         }
194
195         $self->pidl("staticforward PyTypeObject $interface->{NAME}_InterfaceType;");
196         $self->pidl("typedef struct {");
197         $self->indent;
198         $self->pidl("PyObject_HEAD");
199         $self->pidl("struct dcerpc_pipe *pipe;");
200         $self->deindent;
201         $self->pidl("} $interface->{NAME}_InterfaceObject;");
202
203         $self->pidl("");
204
205         foreach my $d (@{$interface->{FUNCTIONS}}) {
206                 next if not defined($d->{OPNUM});
207                 next if has_property($d, "nopython");
208
209                 $self->PythonFunction($d, $interface->{NAME});
210         }
211
212         $self->pidl("static PyMethodDef interface_$interface->{NAME}\_methods[] = {");
213         $self->indent;
214         foreach my $d (@{$interface->{FUNCTIONS}}) {
215                 next if not defined($d->{OPNUM});
216                 next if has_property($d, "nopython");
217
218                 my $fn_name = $d->{NAME};
219
220                 $fn_name =~ s/^$interface->{NAME}_//;
221
222                 $self->pidl("{ (char *)\"$fn_name\", (PyCFunction)py_$d->{NAME}, METH_VARARGS|METH_KEYWORDS, NULL },");
223         }
224         $self->pidl("{ NULL, NULL, 0, NULL }");
225         $self->deindent;
226         $self->pidl("};");
227         $self->pidl("");
228
229         $self->pidl("static void interface_$interface->{NAME}_dealloc(PyObject* self)");
230         $self->pidl("{");
231         $self->indent;
232         $self->pidl("$interface->{NAME}_InterfaceObject *interface = ($interface->{NAME}_InterfaceObject *)self;");
233         $self->pidl("talloc_free(interface->pipe);");
234         $self->pidl("PyObject_Del(self);");
235         $self->deindent;
236         $self->pidl("}");
237         $self->pidl("");
238
239         $self->pidl("static PyObject *interface_$interface->{NAME}_getattr(PyTypeObject *obj, char *name)");
240         $self->pidl("{");
241         $self->indent;
242         $self->pidl("return Py_FindMethod(interface_$interface->{NAME}\_methods, (PyObject *)obj, name);");
243         $self->deindent;
244         $self->pidl("}");
245
246         $self->pidl("");
247
248         $self->pidl("static PyTypeObject $interface->{NAME}_InterfaceType = {");
249         $self->indent;
250         $self->pidl("PyObject_HEAD_INIT(NULL) 0,");
251         $self->pidl(".tp_name = (char *)\"$interface->{NAME}\",");
252         $self->pidl(".tp_basicsize = sizeof($interface->{NAME}_InterfaceObject),");
253         $self->pidl(".tp_dealloc = interface_$interface->{NAME}_dealloc,");
254         $self->pidl(".tp_getattr = interface_$interface->{NAME}_getattr,");
255         $self->deindent;
256         $self->pidl("};");
257
258         $self->pidl("");
259
260         $self->pidl("static PyObject *interface_$interface->{NAME}(PyObject *self, PyObject *args)");
261         $self->pidl("{");
262         $self->indent;
263         $self->pidl("$interface->{NAME}_InterfaceObject *ret;");
264         $self->pidl("const char *binding_string;");
265         $self->pidl("struct cli_credentials *credentials;");
266         $self->pidl("struct loadparm_context *lp_ctx;");
267         $self->pidl("NTSTATUS status;");
268         $self->pidl("");
269
270         # FIXME: Arguments: binding string, credentials, loadparm context
271         $self->pidl("ret = PyObject_New($interface->{NAME}_InterfaceObject, &$interface->{NAME}_InterfaceType);");
272         $self->pidl("");
273
274         $self->pidl("status = dcerpc_pipe_connect(NULL, &ret->pipe, binding_string, ");
275         $self->pidl("             &ndr_table_$interface->{NAME}, credentials, NULL, lp_ctx);");
276         $self->handle_ntstatus("status", "NULL");
277
278         $self->pidl("return (PyObject *)ret;");
279         $self->deindent;
280         $self->pidl("}");
281         
282         $self->pidl("");
283
284         $self->pidl_hdr("\n");
285         $self->pidl_hdr("#endif /* _HEADER_NDR_$interface->{NAME} */\n");
286 }
287
288 sub Parse($$$$)
289 {
290     my($self,$basename,$ndr,$hdr) = @_;
291     
292     my $py_hdr = $hdr;
293     $py_hdr =~ s/ndr_([^\/]+)$/py_$1/g;
294
295     $self->pidl_hdr("/* header auto-generated by pidl */\n\n");
296         
297     $self->pidl("
298 /* Python wrapper functions auto-generated by pidl */
299 #include \"includes.h\"
300 #include <Python.h>
301 #include \"librpc/rpc/dcerpc.h\"
302 #include \"$hdr\"
303 #include \"$py_hdr\"
304
305 ");
306
307         foreach my $x (@$ndr) {
308             ($x->{TYPE} eq "INTERFACE") && $self->Interface($x);
309                 ($x->{TYPE} eq "IMPORT") && $self->Import(@{$x->{PATHS}});
310         }
311         
312         $self->pidl("static PyMethodDef $basename\_methods[] = {");
313         $self->indent;
314         foreach my $x (@$ndr) {
315             next if ($x->{TYPE} ne "INTERFACE");
316                 $self->pidl("{ (char *)\"$x->{NAME}\", (PyCFunction)interface_$x->{NAME}, METH_VARARGS|METH_KEYWORDS, NULL },");
317
318                 foreach my $d (@{$x->{TYPES}}) {
319                         next if has_property($d, "nopython");
320                         next if ($d->{TYPE} eq "ENUM" or $d->{TYPE} eq "BITMAP");
321
322                         my $fn_name = $d->{NAME};
323
324                         $fn_name =~ s/^$x->{NAME}_//;
325                         $fn_name =~ s/^$basename\_//;
326
327                         $self->pidl("{ (char *)\"$fn_name\", (PyCFunction)py_$d->{NAME}, METH_VARARGS|METH_KEYWORDS, NULL },");
328                 }
329         }
330         
331         $self->pidl("{ NULL, NULL, 0, NULL }");
332         $self->deindent;
333         $self->pidl("};");
334
335         $self->pidl("");
336
337         $self->pidl("void init$basename(void)");
338         $self->pidl("{");
339         $self->indent;
340         $self->pidl("PyObject *m;");
341         $self->pidl("m = Py_InitModule((char *)\"$basename\", $basename\_methods);");
342         foreach (keys %{$self->{constants}}) {
343                 # FIXME: Handle non-string constants
344                 $self->pidl("PyModule_AddObject(m, \"$_\", PyString_FromString(" . $self->{constants}->{$_}->[1] . "));");
345         }
346         $self->deindent;
347         $self->pidl("}");
348     return ($self->{res_hdr}, $self->{res});
349 }
350
351 1;