pidl/python: Generate stub functions for DCE/RPC client functions, constructor for...
authorjelmer <jelmer@0c0555d6-39d7-0310-84fc-f1cc0bd64818>
Thu, 3 Jan 2008 23:58:38 +0000 (23:58 +0000)
committerjelmer <jelmer@0c0555d6-39d7-0310-84fc-f1cc0bd64818>
Thu, 3 Jan 2008 23:58:38 +0000 (23:58 +0000)
git-svn-id: svn+ssh://svn.samba.org/data/svn/samba/branches/SAMBA_4_0@26660 0c0555d6-39d7-0310-84fc-f1cc0bd64818

source/pidl/lib/Parse/Pidl/Samba4/Python.pm

index 7c2c2c06c695e84fc9b5ec2818ab97f3837e44d6..0a2fb0b0f364b7f3b4997697bf3ebda665043a34 100644 (file)
@@ -66,6 +66,41 @@ sub Const($$)
     $self->{constants}->{$const->{NAME}} = [$const->{DATA}->{TYPE}, $const->{VALUE}];
 }
 
+sub FromTypeToPythonFunction($$)
+{
+       my ($self, $type) = @_;
+
+       #FIXME
+}
+
+sub FromPythonToTypeFunction($$)
+{
+       my ($self, $type) = @_;
+
+       #FIXME
+}
+
+sub TypeConstructor($$)
+{
+       my ($self, $type) = @_;
+
+       #FIXME
+}
+
+sub PythonFunction($$)
+{
+       my ($self, $fn) = @_;
+
+       $self->pidl("static PyObject *py_$fn->{NAME}(PyObject *self, PyObject *args)");
+       $self->pidl("{");
+       $self->indent;
+       # FIXME
+       $self->pidl("return Py_None;");
+       $self->deindent;
+       $self->pidl("}");
+       $self->pidl("");
+}
+
 sub Interface($$)
 {
        my($self,$interface) = @_;
@@ -77,6 +112,107 @@ sub Interface($$)
 
        $self->Const($_) foreach (@{$interface->{CONSTS}});
 
+       foreach (@{$interface->{TYPES}}) {
+               $self->FromTypeToPythonFunction($_);    
+               $self->FromPythonToTypeFunction($_);    
+               $self->TypeConstructor($_);
+       }
+
+       $self->pidl("staticforward PyTypeObject $interface->{NAME}_InterfaceType;");
+       $self->pidl("typedef struct {");
+       $self->indent;
+       $self->pidl("PyObject_HEAD");
+       $self->pidl("struct dcerpc_pipe *pipe;");
+       $self->deindent;
+       $self->pidl("} $interface->{NAME}_InterfaceObject;");
+
+       $self->pidl("");
+
+       foreach my $d (@{$interface->{FUNCTIONS}}) {
+               next if not defined($d->{OPNUM});
+               next if has_property($d, "nopython");
+
+               $self->PythonFunction($d, $interface->{NAME});
+       }
+
+       $self->pidl("static PyMethodDef interface_$interface->{NAME}\_methods[] = {");
+       $self->indent;
+       foreach my $d (@{$interface->{FUNCTIONS}}) {
+               next if not defined($d->{OPNUM});
+               next if has_property($d, "nopython");
+
+               my $fn_name = $d->{NAME};
+
+               $fn_name =~ s/^$interface->{NAME}_//;
+
+               $self->pidl("{ (char *)\"$fn_name\", (PyCFunction)py_$d->{NAME}, METH_VARARGS|METH_KEYWORDS, NULL },");
+       }
+       $self->pidl("{ NULL, NULL, 0, NULL }");
+       $self->deindent;
+       $self->pidl("};");
+       $self->pidl("");
+
+       $self->pidl("static void interface_$interface->{NAME}_dealloc(PyObject* self)");
+       $self->pidl("{");
+       $self->indent;
+       $self->pidl("$interface->{NAME}_InterfaceObject *interface;");
+       $self->pidl("talloc_free(interface->pipe);");
+       $self->pidl("PyObject_Del(self);");
+       $self->deindent;
+       $self->pidl("}");
+       $self->pidl("");
+
+       $self->pidl("static PyObject *interface_$interface->{NAME}_getattr(PyTypeObject *obj, char *name)");
+       $self->pidl("{");
+       $self->indent;
+       $self->pidl("return Py_FindMethod(interface_$interface->{NAME}\_methods, (PyObject *)obj, name);");
+       $self->deindent;
+       $self->pidl("}");
+
+       $self->pidl("");
+
+       $self->pidl("static PyTypeObject $interface->{NAME}_InterfaceType = {");
+       $self->indent;
+       $self->pidl("PyObject_HEAD_INIT(NULL) 0,");
+       $self->pidl(".tp_name = \"$interface->{NAME}\",");
+       $self->pidl(".tp_basicsize = sizeof($interface->{NAME}_InterfaceObject),");
+       $self->pidl(".tp_dealloc = interface_$interface->{NAME}_dealloc,");
+       $self->pidl(".tp_getattr = interface_$interface->{NAME}_getattr,");
+       $self->deindent;
+       $self->pidl("};");
+
+       $self->pidl("");
+
+       $self->pidl("static PyObject *interface_$interface->{NAME}(PyObject *self, PyObject *args)");
+       $self->pidl("{");
+       $self->indent;
+       $self->pidl("$interface->{NAME}_InterfaceObject *ret;");
+       $self->pidl("const char *binding_string;");
+       $self->pidl("struct cli_credentials *credentials;");
+       $self->pidl("struct loadparm_context *lp_ctx;");
+       $self->pidl("NTSTATUS status;");
+       $self->pidl("");
+
+       # FIXME: Arguments: binding string, credentials, loadparm context
+       $self->pidl("ret = PyObject_New($interface->{NAME}_InterfaceObject, &$interface->{NAME}_InterfaceType);");
+       $self->pidl("");
+
+       $self->pidl("status = dcerpc_pipe_connect(NULL, &ret->pipe, binding_string, ");
+       $self->pidl("             &ndr_table_$interface->{NAME}, credentials, NULL, lp_ctx);");
+       $self->pidl("if (NT_STATUS_IS_ERR(status)) {");
+       $self->indent;
+       $self->pidl("PyErr_SetString(PyExc_RuntimeError, nt_errstr(status));");
+       $self->pidl("return NULL;");
+       $self->deindent;
+       $self->pidl("}");
+       $self->pidl("");
+
+       $self->pidl("return (PyObject *)ret;");
+       $self->deindent;
+       $self->pidl("}");
+       
+       $self->pidl("");
+
        $self->pidl_hdr("\n");
        $self->pidl_hdr("#endif /* _HEADER_NDR_$interface->{NAME} */\n");
 }
@@ -94,6 +230,7 @@ sub Parse($$$$)
 /* Python wrapper functions auto-generated by pidl */
 #include \"includes.h\"
 #include <Python.h>
+#include \"librpc/rpc/dcerpc.h\"
 #include \"$hdr\"
 #include \"$py_hdr\"
 
@@ -106,6 +243,11 @@ sub Parse($$$$)
        
        $self->pidl("static PyMethodDef $basename\_methods[] = {");
        $self->indent;
+       foreach my $x (@$ndr) {
+           next if ($x->{TYPE} ne "INTERFACE");
+               $self->pidl("{ (char *)\"$x->{NAME}\", (PyCFunction)interface_$x->{NAME}, METH_VARARGS|METH_KEYWORDS, NULL },");
+       }
+       
        $self->pidl("{ NULL, NULL, 0, NULL }");
        $self->deindent;
        $self->pidl("};");
@@ -115,12 +257,11 @@ sub Parse($$$$)
        $self->pidl("void init$basename(void)");
        $self->pidl("{");
        $self->indent;
-       $self->pidl("PyObject *m, *d;");
+       $self->pidl("PyObject *m;");
        $self->pidl("m = Py_InitModule((char *)\"$basename\", $basename\_methods);");
-       $self->pidl("d = PyModule_GetDict(m);");
        foreach (keys %{$self->{constants}}) {
                # FIXME: Handle non-string constants
-               $self->pidl("PyDict_SetItemString(d, \"$_\", PyString_FromString(" . $self->{constants}->{$_}->[1] . "));");
+               $self->pidl("PyModule_AddObject(m, \"$_\", PyString_FromString(" . $self->{constants}->{$_}->[1] . "));");
        }
        $self->deindent;
        $self->pidl("}");