1 ###################################################
2 # Python function wrapper generator
3 # Copyright jelmer@samba.org 2007
4 # released under the GNU GPL
6 package Parse::Pidl::Samba4::Python;
12 use Parse::Pidl::Typelist;
13 use Parse::Pidl::Util qw(has_property ParseExpr);
15 use vars qw($VERSION);
20 my $self = { res => "", res_hdr => "", tabs => "", constants => {}};
27 $self->{res_hdr} .= shift;
34 $self->{res} .= $self->{tabs};
43 $self->{tabs} .= "\t";
49 $self->{tabs} = substr($self->{tabs}, 0, -1);
59 $self->pidl_hdr("#include \"librpc/gen_ndr/py_$_\.h\"\n");
65 my ($self, $const) = @_;
66 $self->{constants}->{$const->{NAME}} = [$const->{DATA}->{TYPE}, $const->{VALUE}];
69 sub FromTypeToPythonFunction($$)
71 my ($self, $type) = @_;
76 sub FromPythonToTypeFunction($$)
78 my ($self, $type) = @_;
83 sub TypeConstructor($$)
85 my ($self, $type) = @_;
90 sub PythonFunction($$)
94 $self->pidl("static PyObject *py_$fn->{NAME}(PyObject *self, PyObject *args)");
98 $self->pidl("return Py_None;");
106 my($self,$interface) = @_;
108 $self->pidl_hdr("#ifndef _HEADER_PYTHON_$interface->{NAME}\n");
109 $self->pidl_hdr("#define _HEADER_PYTHON_$interface->{NAME}\n\n");
111 $self->pidl_hdr("\n");
113 $self->Const($_) foreach (@{$interface->{CONSTS}});
115 foreach (@{$interface->{TYPES}}) {
116 $self->FromTypeToPythonFunction($_);
117 $self->FromPythonToTypeFunction($_);
118 $self->TypeConstructor($_);
121 $self->pidl("staticforward PyTypeObject $interface->{NAME}_InterfaceType;");
122 $self->pidl("typedef struct {");
124 $self->pidl("PyObject_HEAD");
125 $self->pidl("struct dcerpc_pipe *pipe;");
127 $self->pidl("} $interface->{NAME}_InterfaceObject;");
131 foreach my $d (@{$interface->{FUNCTIONS}}) {
132 next if not defined($d->{OPNUM});
133 next if has_property($d, "nopython");
135 $self->PythonFunction($d, $interface->{NAME});
138 $self->pidl("static PyMethodDef interface_$interface->{NAME}\_methods[] = {");
140 foreach my $d (@{$interface->{FUNCTIONS}}) {
141 next if not defined($d->{OPNUM});
142 next if has_property($d, "nopython");
144 my $fn_name = $d->{NAME};
146 $fn_name =~ s/^$interface->{NAME}_//;
148 $self->pidl("{ (char *)\"$fn_name\", (PyCFunction)py_$d->{NAME}, METH_VARARGS|METH_KEYWORDS, NULL },");
150 $self->pidl("{ NULL, NULL, 0, NULL }");
155 $self->pidl("static void interface_$interface->{NAME}_dealloc(PyObject* self)");
158 $self->pidl("$interface->{NAME}_InterfaceObject *interface;");
159 $self->pidl("talloc_free(interface->pipe);");
160 $self->pidl("PyObject_Del(self);");
165 $self->pidl("static PyObject *interface_$interface->{NAME}_getattr(PyTypeObject *obj, char *name)");
168 $self->pidl("return Py_FindMethod(interface_$interface->{NAME}\_methods, (PyObject *)obj, name);");
174 $self->pidl("static PyTypeObject $interface->{NAME}_InterfaceType = {");
176 $self->pidl("PyObject_HEAD_INIT(NULL) 0,");
177 $self->pidl(".tp_name = \"$interface->{NAME}\",");
178 $self->pidl(".tp_basicsize = sizeof($interface->{NAME}_InterfaceObject),");
179 $self->pidl(".tp_dealloc = interface_$interface->{NAME}_dealloc,");
180 $self->pidl(".tp_getattr = interface_$interface->{NAME}_getattr,");
186 $self->pidl("static PyObject *interface_$interface->{NAME}(PyObject *self, PyObject *args)");
189 $self->pidl("$interface->{NAME}_InterfaceObject *ret;");
190 $self->pidl("const char *binding_string;");
191 $self->pidl("struct cli_credentials *credentials;");
192 $self->pidl("struct loadparm_context *lp_ctx;");
193 $self->pidl("NTSTATUS status;");
196 # FIXME: Arguments: binding string, credentials, loadparm context
197 $self->pidl("ret = PyObject_New($interface->{NAME}_InterfaceObject, &$interface->{NAME}_InterfaceType);");
200 $self->pidl("status = dcerpc_pipe_connect(NULL, &ret->pipe, binding_string, ");
201 $self->pidl(" &ndr_table_$interface->{NAME}, credentials, NULL, lp_ctx);");
202 $self->pidl("if (NT_STATUS_IS_ERR(status)) {");
204 $self->pidl("PyErr_SetString(PyExc_RuntimeError, nt_errstr(status));");
205 $self->pidl("return NULL;");
210 $self->pidl("return (PyObject *)ret;");
216 $self->pidl_hdr("\n");
217 $self->pidl_hdr("#endif /* _HEADER_NDR_$interface->{NAME} */\n");
222 my($self,$basename,$ndr,$hdr) = @_;
225 $py_hdr =~ s/ndr_([^\/]+)$/py_$1/g;
227 $self->pidl_hdr("/* header auto-generated by pidl */\n\n");
230 /* Python wrapper functions auto-generated by pidl */
231 #include \"includes.h\"
233 #include \"librpc/rpc/dcerpc.h\"
239 foreach my $x (@$ndr) {
240 ($x->{TYPE} eq "INTERFACE") && $self->Interface($x);
241 ($x->{TYPE} eq "IMPORT") && $self->Import(@{$x->{PATHS}});
244 $self->pidl("static PyMethodDef $basename\_methods[] = {");
246 foreach my $x (@$ndr) {
247 next if ($x->{TYPE} ne "INTERFACE");
248 $self->pidl("{ (char *)\"$x->{NAME}\", (PyCFunction)interface_$x->{NAME}, METH_VARARGS|METH_KEYWORDS, NULL },");
251 $self->pidl("{ NULL, NULL, 0, NULL }");
257 $self->pidl("void init$basename(void)");
260 $self->pidl("PyObject *m;");
261 $self->pidl("m = Py_InitModule((char *)\"$basename\", $basename\_methods);");
262 foreach (keys %{$self->{constants}}) {
263 # FIXME: Handle non-string constants
264 $self->pidl("PyModule_AddObject(m, \"$_\", PyString_FromString(" . $self->{constants}->{$_}->[1] . "));");
268 return ($self->{res_hdr}, $self->{res});