1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
###################################################
# Python function wrapper generator
# Copyright jelmer@samba.org 2007
# released under the GNU GPL
package Parse::Pidl::Samba4::Python;
use Exporter;
@ISA = qw(Exporter);
use strict;
use Parse::Pidl::Typelist;
use Parse::Pidl::Util qw(has_property ParseExpr);
use vars qw($VERSION);
$VERSION = '0.01';
sub new($) {
my ($class) = @_;
my $self = { res => "", res_hdr => "", tabs => "", constants => {}};
bless($self, $class);
}
sub pidl_hdr ($$)
{
my $self = shift;
$self->{res_hdr} .= shift;
}
sub pidl($$)
{
my ($self, $d) = @_;
if ($d) {
$self->{res} .= $self->{tabs};
$self->{res} .= $d;
}
$self->{res} .= "\n";
}
sub indent($)
{
my ($self) = @_;
$self->{tabs} .= "\t";
}
sub deindent($)
{
my ($self) = @_;
$self->{tabs} = substr($self->{tabs}, 0, -1);
}
sub Import
{
my $self = shift;
my @imports = @_;
foreach (@imports) {
s/\.idl\"$//;
s/^\"//;
$self->pidl_hdr("#include \"librpc/gen_ndr/py_$_\.h\"\n");
}
}
sub Const($$)
{
my ($self, $const) = @_;
$self->{constants}->{$const->{NAME}} = [$const->{DATA}->{TYPE}, $const->{VALUE}];
}
sub Interface($$)
{
my($self,$interface) = @_;
$self->pidl_hdr("#ifndef _HEADER_PYTHON_$interface->{NAME}\n");
$self->pidl_hdr("#define _HEADER_PYTHON_$interface->{NAME}\n\n");
$self->pidl_hdr("\n");
$self->Const($_) foreach (@{$interface->{CONSTS}});
$self->pidl_hdr("\n");
$self->pidl_hdr("#endif /* _HEADER_NDR_$interface->{NAME} */\n");
}
sub Parse($$$$)
{
my($self,$basename,$ndr,$hdr) = @_;
my $py_hdr = $hdr;
$py_hdr =~ s/ndr_([^\/]+)$/py_$1/g;
$self->pidl_hdr("/* header auto-generated by pidl */\n\n");
$self->pidl("
/* Python wrapper functions auto-generated by pidl */
#include \"includes.h\"
#include <Python.h>
#include \"$hdr\"
#include \"$py_hdr\"
");
foreach my $x (@$ndr) {
($x->{TYPE} eq "INTERFACE") && $self->Interface($x);
($x->{TYPE} eq "IMPORT") && $self->Import(@{$x->{PATHS}});
}
$self->pidl("static PyMethodDef $basename\_methods[] = {");
$self->indent;
$self->pidl("{ NULL, NULL, 0, NULL }");
$self->deindent;
$self->pidl("};");
$self->pidl("");
$self->pidl("void init$basename(void)");
$self->pidl("{");
$self->indent;
$self->pidl("PyObject *m, *d;");
$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->deindent;
$self->pidl("}");
return ($self->{res_hdr}, $self->{res});
}
1;
|