r26660: pidl/python: Generate stub functions for DCE/RPC client functions, constructo...
[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         #FIXME
88 }
89
90 sub PythonFunction($$)
91 {
92         my ($self, $fn) = @_;
93
94         $self->pidl("static PyObject *py_$fn->{NAME}(PyObject *self, PyObject *args)");
95         $self->pidl("{");
96         $self->indent;
97         # FIXME
98         $self->pidl("return Py_None;");
99         $self->deindent;
100         $self->pidl("}");
101         $self->pidl("");
102 }
103
104 sub Interface($$)
105 {
106         my($self,$interface) = @_;
107
108         $self->pidl_hdr("#ifndef _HEADER_PYTHON_$interface->{NAME}\n");
109         $self->pidl_hdr("#define _HEADER_PYTHON_$interface->{NAME}\n\n");
110
111         $self->pidl_hdr("\n");
112
113         $self->Const($_) foreach (@{$interface->{CONSTS}});
114
115         foreach (@{$interface->{TYPES}}) {
116                 $self->FromTypeToPythonFunction($_);    
117                 $self->FromPythonToTypeFunction($_);    
118                 $self->TypeConstructor($_);
119         }
120
121         $self->pidl("staticforward PyTypeObject $interface->{NAME}_InterfaceType;");
122         $self->pidl("typedef struct {");
123         $self->indent;
124         $self->pidl("PyObject_HEAD");
125         $self->pidl("struct dcerpc_pipe *pipe;");
126         $self->deindent;
127         $self->pidl("} $interface->{NAME}_InterfaceObject;");
128
129         $self->pidl("");
130
131         foreach my $d (@{$interface->{FUNCTIONS}}) {
132                 next if not defined($d->{OPNUM});
133                 next if has_property($d, "nopython");
134
135                 $self->PythonFunction($d, $interface->{NAME});
136         }
137
138         $self->pidl("static PyMethodDef interface_$interface->{NAME}\_methods[] = {");
139         $self->indent;
140         foreach my $d (@{$interface->{FUNCTIONS}}) {
141                 next if not defined($d->{OPNUM});
142                 next if has_property($d, "nopython");
143
144                 my $fn_name = $d->{NAME};
145
146                 $fn_name =~ s/^$interface->{NAME}_//;
147
148                 $self->pidl("{ (char *)\"$fn_name\", (PyCFunction)py_$d->{NAME}, METH_VARARGS|METH_KEYWORDS, NULL },");
149         }
150         $self->pidl("{ NULL, NULL, 0, NULL }");
151         $self->deindent;
152         $self->pidl("};");
153         $self->pidl("");
154
155         $self->pidl("static void interface_$interface->{NAME}_dealloc(PyObject* self)");
156         $self->pidl("{");
157         $self->indent;
158         $self->pidl("$interface->{NAME}_InterfaceObject *interface;");
159         $self->pidl("talloc_free(interface->pipe);");
160         $self->pidl("PyObject_Del(self);");
161         $self->deindent;
162         $self->pidl("}");
163         $self->pidl("");
164
165         $self->pidl("static PyObject *interface_$interface->{NAME}_getattr(PyTypeObject *obj, char *name)");
166         $self->pidl("{");
167         $self->indent;
168         $self->pidl("return Py_FindMethod(interface_$interface->{NAME}\_methods, (PyObject *)obj, name);");
169         $self->deindent;
170         $self->pidl("}");
171
172         $self->pidl("");
173
174         $self->pidl("static PyTypeObject $interface->{NAME}_InterfaceType = {");
175         $self->indent;
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,");
181         $self->deindent;
182         $self->pidl("};");
183
184         $self->pidl("");
185
186         $self->pidl("static PyObject *interface_$interface->{NAME}(PyObject *self, PyObject *args)");
187         $self->pidl("{");
188         $self->indent;
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;");
194         $self->pidl("");
195
196         # FIXME: Arguments: binding string, credentials, loadparm context
197         $self->pidl("ret = PyObject_New($interface->{NAME}_InterfaceObject, &$interface->{NAME}_InterfaceType);");
198         $self->pidl("");
199
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)) {");
203         $self->indent;
204         $self->pidl("PyErr_SetString(PyExc_RuntimeError, nt_errstr(status));");
205         $self->pidl("return NULL;");
206         $self->deindent;
207         $self->pidl("}");
208         $self->pidl("");
209
210         $self->pidl("return (PyObject *)ret;");
211         $self->deindent;
212         $self->pidl("}");
213         
214         $self->pidl("");
215
216         $self->pidl_hdr("\n");
217         $self->pidl_hdr("#endif /* _HEADER_NDR_$interface->{NAME} */\n");
218 }
219
220 sub Parse($$$$)
221 {
222     my($self,$basename,$ndr,$hdr) = @_;
223     
224     my $py_hdr = $hdr;
225     $py_hdr =~ s/ndr_([^\/]+)$/py_$1/g;
226
227     $self->pidl_hdr("/* header auto-generated by pidl */\n\n");
228         
229     $self->pidl("
230 /* Python wrapper functions auto-generated by pidl */
231 #include \"includes.h\"
232 #include <Python.h>
233 #include \"librpc/rpc/dcerpc.h\"
234 #include \"$hdr\"
235 #include \"$py_hdr\"
236
237 ");
238
239         foreach my $x (@$ndr) {
240             ($x->{TYPE} eq "INTERFACE") && $self->Interface($x);
241                 ($x->{TYPE} eq "IMPORT") && $self->Import(@{$x->{PATHS}});
242         }
243         
244         $self->pidl("static PyMethodDef $basename\_methods[] = {");
245         $self->indent;
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 },");
249         }
250         
251         $self->pidl("{ NULL, NULL, 0, NULL }");
252         $self->deindent;
253         $self->pidl("};");
254
255         $self->pidl("");
256
257         $self->pidl("void init$basename(void)");
258         $self->pidl("{");
259         $self->indent;
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] . "));");
265         }
266         $self->deindent;
267         $self->pidl("}");
268     return ($self->{res_hdr}, $self->{res});
269 }
270
271 1;