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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
#! /usr/bin/env python
import os
import re
import sys
import elementtree.ElementTree as ET
import lang_python
class BindingData:
def __init__(self):
self.headers = []
self.constants = []
self.structs = []
self.struct_dict = {}
self.functions = []
self.enums = []
self.overrides = ET.parse('overrides.xml')
def display_structs(self):
for struct in self.structs:
struct.display()
def display_funcs(self):
for func in self.functions:
print func.return_type, func.name
for a in func.args:
print ' ', a
def order_class_hierarchy(self):
new_order = []
while self.structs:
for c in self.structs:
if c.parent == 'GObject' or c.parent in [x.name for x in new_order]:
self.structs.remove(c)
new_order.append(c)
break
self.structs = new_order
def create_struct_dict(self):
for c in self.structs:
self.struct_dict[c.name] = c
def attach_methods(self):
self.create_struct_dict()
for f in self.functions[:]:
if len(f.args) == 0:
continue
if f.name.endswith('_new'):
# constructor for another class
continue
arg_type = f.args[0][0]
if arg_type[-1] == '*':
arg_type = arg_type[:-1]
c = self.struct_dict.get(arg_type)
if not c:
continue
c.methods.append(f)
self.functions.remove(f)
def look_for_docstrings(self):
regex = re.compile(r'\/\*\*\s(.*?)\*\*\/', re.DOTALL)
for base, dirnames, filenames in os.walk('../lasso/'):
if base.endswith('/.svn'):
# ignore svn directories
continue
if not 'Makefile.am' in filenames:
# not a source dir
continue
makefile_am = open(os.path.join(base, 'Makefile.am')).read()
filenames = [x for x in filenames if x.endswith('.c') if x in makefile_am]
for filename in filenames:
s = open(os.path.join(base, filename)).read()
docstrings = regex.findall(s)
for d in docstrings:
docstring = '\n'.join([x[3:] for x in d.splitlines()])
function_name = docstring.splitlines(1)[0].strip().strip(':')
func = [f for f in self.functions if f.name == function_name]
if not func:
continue
func = func[0]
func.docstring = docstring
class Struct:
def __init__(self, name):
self.name = name[1:] # skip leading _
self.parent = None
self.members = []
self.methods = []
def __repr__(self):
return '<Struct name:%s, childof:%s>' % (self.name, self.parent)
def display(self):
print self.__repr__()
for m in self.members:
print ' ', m
for m in self.methods:
print ' ', m
class Function:
return_type = None
name = None
args = None
docstring = None
def __repr__(self):
return '%s %s %r' % (self.return_type, self.name, self.args)
def apply_overrides(self):
for func in binding.overrides.findall('func'):
if func.attrib.get('name') != self.name:
continue
for param in func.findall('param'):
try:
arg = [x for x in self.args if x[1] == param.attrib.get('name')][0]
except IndexError:
print >> sys.stderr, 'W: no such param (%s) in function (%s)' % (
param.attrib.get('name'), self.name)
continue
if param.attrib.get('optional') == 'true':
arg[2]['optional'] = True
if param.attrib.get('default'):
arg[2]['default'] = param.attrib.get('default')
def normalise_var(type, name):
if name[0] == '*':
type += '*'
name = name[1:]
return type, name
def parse_header(header_file):
global binding
struct_names = {}
in_comment = False
in_enum = False
in_struct = None
in_struct_private = False
in_ifdef_zero = False
lines = file(header_file).readlines()
i = 0
while i < len(lines):
line = lines[i]
while line.endswith('\\\n'):
i += 1
line = line[:-2] + ' ' + lines[i].lstrip()
if in_comment:
if '*/' in line:
in_comment = False
elif '/*' in line and not '*/' in line:
in_comment = True
elif in_ifdef_zero:
# minimal support for code sections commented with #if 0
if line.startswith('#endif'):
in_ifdef_zero = False
elif line.startswith('#if 0'):
in_ifdef_zero = True
elif in_enum:
if line.startswith('}'):
in_enum = False
enum_name = line[2:].strip().strip(';')
binding.enums.append(enum_name)
else:
m = re.match('\s*([a-zA-Z0-9_]+)', line)
if m:
binding.constants.append(('i', m.group(1)))
elif line.startswith('#define'):
m = re.match(r'#define\s+([a-zA-Z0-9_]+)\s+[-\w"]', line)
if m:
constant = m.group(1)
if constant[0] != '_':
# ignore private constants
if '"' in line:
constant_type = 's'
else:
constant_type = 'i'
binding.constants.append((constant_type, constant))
elif line.startswith('typedef enum {'):
in_enum = True
elif line.startswith('typedef struct'):
m = re.match('typedef struct ([a-zA-Z0-9_]+)', line)
if m:
struct_name = m.group(1)
if not (struct_name.endswith('Class') or struct_name.endswith('Private')):
struct_names[struct_name] = True
elif line.startswith('struct _'):
m = re.match('struct ([a-zA-Z0-9_]+)', line)
struct_name = m.group(1)
if struct_name in struct_names:
in_struct = Struct(struct_name)
in_struct_private = False
elif in_struct:
if line.startswith('}'):
binding.structs.append(in_struct)
in_struct = None
elif '/*< public >*/' in line:
in_struct_private = False
elif '/*< private >*/' in line:
in_struct_private = True
elif in_struct_private:
pass
else:
member_match = re.match('\s+(\w+)\s+(\*?\w+)', line)
if member_match:
member_type = member_match.group(1)
member_name = member_match.group(2)
if member_name == 'parent':
in_struct.parent = member_type
else:
in_struct.members.append(
list(normalise_var(member_type, member_name)) + [{}])
if member_type == 'GList':
options = in_struct.members[-1][-1]
if '/* of' in line:
of_type = line[line.index('/* of')+6:].split()[0]
if of_type == 'strings':
of_type = 'char*'
options['elem_type'] = of_type
elif line.startswith('LASSO_EXPORT '):
while not line.strip().endswith(';'):
i += 1
line = line[:-1] + lines[i].lstrip()
m = re.match(r'LASSO_EXPORT\s+((?:const |)[\w]+\*?)\s+(\*?\w+)\s*\((.*?)\)', line)
if m and not m.group(2).endswith('_get_type'):
f = Function()
binding.functions.append(f)
return_type, function_name, args = m.groups()
if function_name[0] == '*':
return_type += '*'
function_name = function_name[1:]
if return_type != 'void':
f.return_type = return_type
f.name = function_name
f.args = []
for arg in [x.strip() for x in args.split(',')]:
if arg == 'void' or arg == '':
continue
m = re.match(r'((const\s+)?\w+\*?)\s+(\*?\w+)', arg)
if m:
f.args.append(list(normalise_var(m.group(1), m.group(3))) + [{}])
else:
print 'failed to process:', arg, 'in line:', line
f.apply_overrides()
i += 1
def parse_headers():
for base, dirnames, filenames in os.walk('../lasso/'):
if base.endswith('/.svn'):
# ignore svn directories
continue
if not 'Makefile.am' in filenames:
# not a source dir
continue
makefile_am = open(os.path.join(base, 'Makefile.am')).read()
filenames = [x for x in filenames if x.endswith('.h') if x in makefile_am]
for filename in filenames:
if filename == 'lasso_config.h' or 'private' in filename:
continue
binding.headers.append(os.path.join(base, filename)[3:])
parse_header(os.path.join(base, filename))
binding.headers.insert(0, 'lasso/xml/saml-2.0/saml2_assertion.h')
binding = BindingData()
parse_headers()
binding.order_class_hierarchy()
binding.look_for_docstrings()
binding.attach_methods()
python_binding = lang_python.PythonBinding(binding)
python_binding.generate()
import pprint
#binding.display_structs()
#binding.display_funcs()
|