summaryrefslogtreecommitdiffstats
path: root/bindings/python/lang.py
blob: b8488617579869a72efb9d747a88e9a323849023 (plain)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
# Lasso - A free implementation of the Liberty Alliance specifications.
#
# Copyright (C) 2004-2007 Entr'ouvert
# http://lasso.entrouvert.org
#
# Authors: See AUTHORS file in top-level directory.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.

import os
import sys
import re
import textwrap
from utils import *


def remove_bad_optional(args):
    args.reverse()
    non_opt = False
    new_args = []
    for x in args:
        if not '=' in x:
            non_opt = True
        elif non_opt:
            print('W: changed', x, file=sys.stderr)
            x = re.sub(' *=.*', '', x)
            print('to', x, file=sys.stderr)
        new_args.append(x)
    new_args.reverse()
    return new_args

def defval_to_python_value(defval):
    if defval is None:
        return 'None'
    if defval.startswith('b:'):
        if defval[2:].lower() == 'true':
            return 'True'
        if defval[2:].lower() == 'false':
            return 'False'
    if defval.startswith('c:'):
        try:
            return str(int(defval[2:]))
        except:
            return defval[8:]
    raise Exception('Could not convert %s to python value' % defval)

def get_python_arg_decl(arg):
    if is_optional(arg):
        return '%s = %s' % (arg_name(arg), defval_to_python_value(arg_default(arg)))
    else:
        return arg_name(arg)

class Binding:
    def __init__(self, binding_data):
        self.binding_data = binding_data
        self.src_dir = os.path.dirname(__file__)

    def free_value(self, fd, type, name = None):
        if not name:
            name = arg_name(type)
        if not name:
            raise Exception('Cannot free, missing a name')
        if is_cstring(type):
            print('    lasso_release_string(%s);' % name, file=fd)
        elif is_int(type, self.binding_data) or is_boolean(type):
            pass
        elif is_xml_node(type):
            print('    lasso_release_xml_node(%s);' % name, file=fd)
        elif is_glist(type):
            etype = element_type(type)
            if is_cstring(etype):
                print('    lasso_release_list_of_strings(%s);' % name, file=fd)
            elif is_object(etype):
                print('    lasso_release_list_of_gobjects(%s);' % name, file=fd)
            else:
                raise Exception('Unsupported caller owned return type %s' % ((repr(type), name),))
        elif is_hashtable(type):
            raise Exception('Unsupported caller owned return type %s' % ((repr(type), name),))
        elif is_object(type):
            print('    if (return_value) g_object_unref(%s);' % name, file=fd)
        else:
            raise Exception('Unsupported caller owned return type %s' % ((repr(type), name),))


    def generate(self):
        fd = open('lasso.py', 'w')
        self.generate_header(fd)
        self.generate_exceptions(fd)
        self.generate_constants(fd)
        for clss in self.binding_data.structs:
            self.generate_class(clss, fd)
        self.generate_functions(fd)
        self.generate_footer(fd)
        fd.close()

        fd = open('_lasso.c', 'w')
        self.generate_wrapper(fd)
        fd.close()

    def generate_header(self, fd):
        print('''\
# this file has been generated automatically; do not edit

import _lasso

def cptrToPy(cptr):
    if cptr is None:
        return None
    klass = getattr(lasso, cptr.typename)
    o = klass.__new__(klass)
    o._cptr = cptr
    return o

def str2lasso(s):
    if isinstance(s, unicode):
        return s.encode('utf-8')
    return s

class frozendict(dict):
    \'\'\'Immutable dict\'\'\'
    # from Python Cookbook:
    #   http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/414283
    def _blocked_attribute(obj):
        raise AttributeError('A frozendict cannot be modified.')
    _blocked_attribute = property(_blocked_attribute)

    __delitem__ = __setitem__ = clear = _blocked_attribute
    pop = popitem = setdefault = update = _blocked_attribute

    def __new__(cls, *args):
        new = dict.__new__(cls)
        dict.__init__(new, *args)
        return new

    def __init__(self, *args):
        pass

    def __hash__(self):
        try:
            return self._cached_hash
        except AttributeError:
            h = self._cached_hash = hash(tuple(sorted(self.items())))
            return h

    def __repr__(self):
        return 'frozendict(%s)' % dict.__repr__(self)
''', file=fd)

    def generate_exceptions(self, fd):
        done_cats = []
        print('''\
class Error(Exception):
    code = None

    @staticmethod
    def raise_on_rc(rc):
        global exceptions_dict
        if rc != 0:
            exception = exceptions_dict.get(rc, Error())
            exception.code = rc
            raise exception

    def __str__(self):
        if self.code:
            return '<lasso.%s(%s): %s>' % (self.__class__.__name__, self.code, _lasso.strError(self.code))
        else:
            return '<lasso.%s: %s>' % (self.__class__.__name__, self.message)

    def __getitem__(self, i):
        # compatibility with SWIG bindings
        if i == 0:
            return self.code
        elif i == 1:
            return _lasso.strError(self.code)
        else:
            raise IndexError()
''', file=fd)
        for exc_cat in self.binding_data.overrides.findall('exception/category'):
            cat = exc_cat.attrib.get('name')
            done_cats.append(cat)
            parent_cat = exc_cat.attrib.get('parent', '')
            print('''\
class %sError(%sError):
    pass
''' % (cat, parent_cat), file=fd)

        exceptions_dict = {}

        for c in self.binding_data.constants:
            m = re.match(r'LASSO_(\w+)_ERROR_(.*)', c[1])
            if not m:
                continue
            cat, detail = m.groups()
            cat = cat.title().replace('_', '')
            detail = (cat + '_' + detail).title().replace('_', '')
            if not cat in done_cats:
                done_cats.append(cat)
                for exc_cat in self.binding_data.overrides.findall('exception/category'):
                    if exc_cat.attrib.get('name') == cat:
                        parent_cat = exc_cat.attrib.get('parent')
                        break
                else:
                    parent_cat = ''

                print('''\
class %sError(%sError):
    pass
''' % (cat, parent_cat), file=fd)

            exceptions_dict[detail] = c[1][6:]

            if (detail, cat) == ('UnsupportedProfile', 'Logout'):
                # skip Logout/UnsupportedProfile exception as its name would
                # be the same as Profile/UnsupportedProfile; it is not a
                # problem skipping it as they both inherit from ProfileError
                # and the exception code will correctly be set by raise_on_rc
                # afterwards.  (actually it is even totally unnecessary to skip
                # it here as Profile/UnsupportedProfile is handled after
                # Logout/UnsupportedProfile, this is just done in the case the
                # ordering would change)
                continue

            print('''\
class %sError(%sError):
    pass
''' % (detail, cat), file=fd)

        print('exceptions_dict = {', file=fd)
        for k, v in exceptions_dict.items():
            print('    _lasso.%s: %sError,' % (v, k), file=fd)
        print('}', file=fd)
        print('', file=fd)

    def generate_footer(self, fd):
        print('''

import lasso

# backward compatibility with the SWIG binding

WSF_SUPPORT = WSF_ENABLED

Profile.isIdentityDirty = property(Profile.hasDirtyIdentity)
Profile.isSessionDirty = property(Profile.hasDirtySession)

def identity_get_provider_ids(self):
    return self.federations.keys()
Identity.providerIds = property(identity_get_provider_ids)

def server_get_provider_ids(self):
    return self.providers.keys()
Server.providerIds = property(server_get_provider_ids)

def session_get_provider_ids(self):
    return self.assertions.keys()
Session.providerIds = property(session_get_provider_ids)

def LassoNode__getstate__(self):
    return { '__dump__': self.dump() }

def LassoNode__setstate__(self, d):
    self._cptr = _lasso.node_new_from_dump(d.pop('__dump__'))

Node.__getstate__ = LassoNode__getstate__
Node.__setstate__ = LassoNode__setstate__

Samlp2AuthnRequest.nameIDPolicy = Samlp2AuthnRequest.nameIdPolicy
LibAuthnRequest.nameIDPolicy = LibAuthnRequest.nameIdPolicy
Saml2Subject.nameID = Saml2Subject.nameId
MiscTextNode.text_child = MiscTextNode.textChild
NodeList = list
StringList = list
StringDict = dict
registerIdWsf2DstService = registerIdwsf2DstService

if WSF_SUPPORT:
    DiscoDescription_newWithBriefSoapHttpDescription = DiscoDescription.newWithBriefSoapHttpDescription
    Discovery.buildRequestMsg = Discovery.buildSoapRequestMsg
    InteractionProfileService.buildRequestMsg = InteractionProfileService.buildSoapRequestMsg
    InteractionProfileService.buildResponseMsg = InteractionProfileService.buildSoapResponseMsg
    DataService.buildRequestMsg = DataService.buildSoapRequestMsg
    DiscoModifyResponse.newEntryIds = DiscoModifyResponse.newEntryIDs
''', file=fd)

    def generate_constants(self, fd):
        print('### Constants (both enums and defines)', file=fd)
        for c in self.binding_data.constants:
            print('%s = _lasso.%s' % (c[1][6:], c[1][6:]), file=fd)
        for c in self.binding_data.overrides.findall('constant'):
            name = c.attrib.get('name')
            if c.attrib.get('value'):
                name = name[6:] # dropping LASSO_
                value = c.attrib.get('value')
                if value == 'True':
                    print('%s = True' % name, file=fd)
                else:
                    print('E: unknown value for constant: %r' % value, file=sys.stderr)
        print('', file=fd)

    def generate_class(self, clss, fd):
        klassname = clss.name[5:] # remove Lasso from class name
        if clss.parent == 'GObject':
            parentname = 'object'
        else:
            parentname = clss.parent[5:]

        print('''class %(klassname)s(%(parentname)s):''' % locals(), file=fd)

        methods = clss.methods[:]
        # constructor(s)
        method_prefix = 'lasso_' + format_as_underscored(klassname) + '_'
        empty = True
        for m in self.binding_data.functions:
            if m.name == method_prefix + 'new':
                empty = False
                c_args = []
                py_args = []
                for arg in m.args:
                    py_args.append(get_python_arg_decl(arg))
                    if not is_int(arg, self.binding_data) and is_object(arg):
                        c_args.append('%(name)s and %(name)s._cptr' % { 'name' : arg_name(arg) })
                    elif is_cstring(arg):
                        c_args.append('str2lasso(%s)' % arg_name(arg))
                    else:
                        c_args.append(arg_name(arg))
                py_args = remove_bad_optional(py_args)

                c_args = ', '.join(c_args)
                py_args = ', ' + ', '.join(py_args)
                print('    def __init__(self%s):' % py_args, file=fd)
                # XXX: could check self._cptr.typename to see if it got the
                # right class type
                print('        self._cptr = _lasso.%s(%s)' % (
                        m.name[6:], c_args), file=fd)
                print('        if self._cptr is None:', file=fd)
                print('            raise Error(\'failed to create object\')', file=fd)
                print('', file=fd)

        for m in self.binding_data.functions:
            if m.name.startswith(method_prefix + 'new_'):
                empty = False
                constructor_name = format_as_camelcase(m.name[len(method_prefix):])
                c_args = []
                py_args = []
                for arg in m.args:
                    aname = arg_name(arg)
                    py_args.append(get_python_arg_decl(arg))

                    if not is_int(arg, self.binding_data) and is_object(arg):
                        c_args.append('%s and %s._cptr' % (aname, aname))
                    elif is_cstring(arg):
                        c_args.append('str2lasso(%s)' % arg_name(arg))
                    else:
                        c_args.append(aname)
                opt = False
                py_args = remove_bad_optional(py_args)
                for x in py_args:
                    if '=' in x:
                        opt = True
                    elif opt:
                        print('W: non-optional follows optional,', m, file=sys.stderr)
                c_args = ', '.join(c_args)
                py_args = ', ' + ', '.join(py_args)
                print('    @classmethod', file=fd)
                print('    def %s(cls%s):' % (constructor_name, py_args), file=fd)
                print('         return cptrToPy(_lasso.%s(%s))' % (m.name[6:], c_args), file=fd)
                print('', file=fd)

        # create properties for members
        for m in clss.members:
            empty = False
            mname = format_as_camelcase(m[1])
            options = m[2]
            # getter
            print('    def get_%s(self):' % mname, file=fd)
            print('        t = _lasso.%s_%s_get(self._cptr)' % (
                    klassname, mname), file=fd)
            if is_int(m, self.binding_data) or is_xml_node(m) or is_cstring(m) or is_boolean(m):
                pass
            elif is_object(m):
                print('        t = cptrToPy(t)', file=fd)
            elif is_glist(m):
                el_type = element_type(m)
                if is_cstring(el_type):
                    pass
                elif is_xml_node(el_type):
                    pass
                elif is_object(el_type):
                    print('        if not t: return t', file=fd)
                    print('        t = tuple([cptrToPy(x) for x in t])', file=fd)
                else:
                    raise Exception('Unsupported python getter %s.%s' % (clss, m))
            elif is_hashtable(m):
                el_type = element_type(m)
                print('        if not t: return t', file=fd)
                if is_object(el_type):
                    print('        d2 = {}', file=fd)
                    print('        for k, v in t.items():', file=fd)
                    print('            d2[k] = cptrToPy(v)', file=fd)
                    print('        t = frozendict(d2)', file=fd)
                else:
                    print('        t = frozendict(t)', file=fd)
            elif is_boolean(m) or is_int(m, self.binding_data) or is_xml_node(m) or is_cstring(m):
                pass
            else:
                raise Exception('Unsupported python getter %s.%s' % (clss, m))
            print('        return t;', file=fd)
            # setter
            print('    def set_%s(self, value):' % mname, file=fd)
            if is_int(m, self.binding_data) or is_xml_node(m) or is_boolean(m):
                pass
            elif is_cstring(m):
                print('        value = str2lasso(value)', file=fd)
            elif is_object(m):
                print('        if value is not None:', file=fd)
                print('            value = value and value._cptr', file=fd)
            elif is_glist(m):
                el_type = element_type(m)
                if is_cstring(el_type) or is_xml_node(el_type):
                    pass
                elif is_object(el_type):
                    print('        if value is not None:', file=fd)
                    print('            value = tuple([x._cptr for x in value])', file=fd)
                else:
                    raise Exception('Unsupported python setter %s.%s' % (clss, m))
            elif is_hashtable(m):
                print('W: unsupported setter for hashtable %s' % (m,), file=sys.stderr)
            else:
                print('W: unsupported setter for %s' % (m,), file=sys.stderr)
            print('        _lasso.%s_%s_set(self._cptr, value)' % (
                    klassname, mname), file=fd)
            print('    %s = property(get_%s, set_%s)' % (mname, mname, mname), file=fd)
            old_mname = old_format_as_camelcase(m[1])
            if mname != old_mname:
                print('    %s = %s' % (old_mname, mname), file=fd)
            print('', file=fd)

        # first pass on methods, getting accessors
        # second pass on methods, real methods
        for m in methods:
            empty = False
            if m.name.endswith('_new') or m.name.endswith('_new_from_dump') or \
                    m.name.endswith('_new_full'):
                continue
            if not m.name.startswith(method_prefix):
                print('W:', m.name, 'vs', method_prefix, file=sys.stderr)
                continue

            if m.rename:
                mname = m.rename[len(method_prefix):]
                function_name = m.rename[6:]
            else:
                mname = m.name[len(method_prefix):]
                function_name = m.name[6:]
            py_args = []
            c_args = []
            outarg = None
            for arg in m.args[1:]:
                if is_out(arg):
                    assert not outarg
                    outarg = arg
                    outvar = '_%s_out' % arg_name(arg)
                else:
                    py_args.append(get_python_arg_decl(arg))

                if is_out(arg):
                    c_args.append(outvar)
                elif is_cstring(arg):
                    c_args.append('str2lasso(%s)' % arg_name(arg))
                elif is_xml_node(arg) or is_boolean(arg) or is_cstring(arg) or is_int(arg, self.binding_data) or is_glist(arg) or is_hashtable(arg) or is_time_t_pointer(arg):
                    c_args.append(arg_name(arg))
                elif is_object(arg):
                    c_args.append('%(name)s and %(name)s._cptr' % { 'name': arg_name(arg) })
                else:
                    raise Exception('Does not handle argument of type: %s' % ((m, arg),))
            # check py_args
            py_args = remove_bad_optional(py_args)
            opt = False
            for x in py_args:
                if '=' in x:
                    opt = True
                elif opt:
                    print('W: non-optional follow optional,', m, file=sys.stderr)

            if py_args:
                py_args = ', ' + ', '.join(py_args)
            else:
                py_args = ''
            if c_args:
                c_args = ', ' + ', '.join(c_args)
            else:
                c_args = ''

            print('    def %s(self%s):' % (
                    format_underscore_as_camelcase(mname), py_args), file=fd)
            if m.docstring:
                print("        '''", file=fd)
                print(self.format_docstring(m, mname, 8), file=fd)
                print("        '''", file=fd)

            if outarg:
                print("        %s = list((None,))" % outvar, file=fd)
            return_type = m.return_type
            return_type_qualifier = m.return_type_qualifier
            assert is_int(make_arg(return_type),self.binding_data) or not outarg
            if return_type in (None, 'void'):
                print('        _lasso.%s(self._cptr%s)' % (
                        function_name, c_args), file=fd)
            elif is_rc(m.return_arg):
                print('        rc = _lasso.%s(self._cptr%s)' % (
                        function_name, c_args), file=fd)
                print('        Error.raise_on_rc(rc)', file=fd)
            elif is_int(m.return_arg, self.binding_data) or is_xml_node(m.return_arg) or is_cstring(m.return_arg) or is_boolean(m.return_arg):
                print('        return _lasso.%s(self._cptr%s)' % (
                        function_name, c_args), file=fd)
            elif is_glist(m.return_arg):
                el_type = element_type(m.return_arg)
                if is_object(el_type):
                    print('        value = _lasso.%s(self._cptr%s)' % (
                            function_name, c_args), file=fd)
                    print('        if value is not None:', file=fd)
                    print('            value = tuple([cptrToPy(x) for x in value])', file=fd)
                    print('        return value', file=fd)
                elif is_cstring(el_type):
                    print('        return _lasso.%s(self._cptr%s)' % (
                            function_name, c_args), file=fd)
                else:
                    raise Exception('Return Type GList<%s> is not supported' % el_type)
            elif is_hashtable(m.return_arg):
                raise Exception('Return type GHashTable unsupported')
            elif is_object(m.return_arg):
                print('        return cptrToPy(_lasso.%s(self._cptr%s))' % (
                        function_name, c_args), file=fd)
            else:
                raise Exception('Return type %s is unsupported' % (m.return_arg,))
            if outarg:
                print('        return %s[0]' % outvar, file=fd)
            print('', file=fd)
        # transform methods to properties
        for m in methods:
            if len(m.args) > 1:
                continue
            name = m.rename or m.name
            suffix = name[len(method_prefix)+len('get_'):]
            if clss.getMember(suffix):
                print('W: method %s and member %s clashes' % (m.name, arg_name(clss.getMember(suffix))), file=sys.stderr)
                continue
            if not name.startswith(method_prefix) or not name[len(method_prefix):].startswith('get_'):
                continue
            setter_suffix = 'set_' + suffix
            setter = None
            for n in methods:
                if n.name.endswith(setter_suffix) and len(n.args) == 2:
                    setter = n
            pname = format_as_camelcase(name[len(method_prefix)+len('get_'):])
            fname = format_as_camelcase(name[len(method_prefix):])
            if not setter:
                print('    %s = property(%s)' % (pname, fname), file=fd)
            else:
                f2name = format_as_camelcase(setter.name[len(method_prefix):])
                print('    %s = property(%s, %s)' % (pname, fname, f2name), file=fd)
        if empty:
            print('    pass', file=fd)
        print('', file=fd)

    def format_docstring(self, func, method_name, indent):
        if func.args:
            first_arg_name = func.args[0][1]
        else:
            first_arg_name = None

        def format_inlines_sub(s):
            type = s.group(1)[0]
            var = s.group(1)[1:]
            if type == '#': # struct
                if var.startswith('Lasso'):
                    return 'L{%s}' % var[5:]
            elif type == '%': # %TRUE, %FALSE
                if var == 'TRUE':
                    return 'True'
                if var == 'FALSE':
                    return 'False'
                print('W: unknown docstring thingie: %s' % s.group(1), file=sys.stderr)
            elif type == '@':
                if var == first_arg_name:
                    var = 'self'
                return 'C{%s}' % var
            return s.group(1)

        regex = re.compile(r'([\#%@]\w+)', re.DOTALL)

        def format_inline(s):
            s = regex.sub(format_inlines_sub, s)
            return s.replace('NULL', 'None')

        docstring = func.docstring
        s = []

        if docstring.description:
            for paragraph in docstring.description.split('\n\n'):
                if '<itemizedlist>' in paragraph:
                    before, after = paragraph.split('<itemizedlist>' ,1)
                    if before:
                        s.append('\n'.join(textwrap.wrap(
                                        format_inline(before), 70)))

                    # remove tags
                    after = after.replace('<itemizedlist>', '')
                    after = after.replace('</itemizedlist>', '')

                    for listitem in after.split('<listitem><para>'):
                        listitem = listitem.replace('</para></listitem>', '').strip()
                        s.append('\n'.join(textwrap.wrap(
                                        format_inline(listitem), 70,
                                        initial_indent = ' - ',
                                        subsequent_indent = '   ')))
                        s.append('\n\n')

                else:
                    s.append('\n'.join(textwrap.wrap(
                                    format_inline(paragraph), 70)))
                    s.append('\n\n')

        for param in docstring.parameters:
            s.append('\n'.join(textwrap.wrap(
                            format_inline(param[1]), 70,
                            initial_indent = '@param %s: ' % param[0],
                            subsequent_indent = 4*' ')))
            s.append('\n')
        if docstring.return_value:
            rv = docstring.return_value
            exceptions_instead = ['0 on success; or a negative value otherwise.',
                    '0 on success; a negative value if an error occured.',
                    '0 on success; another value if an error occured.']
            if not rv in exceptions_instead:
                owner_info = ['This xmlnode must be freed by caller.',
                        'The string must be freed by the caller.',
                        'It must be freed by the caller.',
                        'This string must be freed by the caller.']
                for o_i in owner_info:
                    rv = rv.replace(o_i, '')
                s.append('\n'.join(textwrap.wrap(
                                format_inline(rv), 70,
                                initial_indent = '@return: ',
                                subsequent_indent = 4*' ')))
                s.append('\n')


        if s:
            s[-1] = s[-1].rstrip() # remove trailing newline from last line

        return '\n'.join([(indent*' ')+x for x in ''.join(s).splitlines()])


    def generate_functions(self, fd):
        for m in self.binding_data.functions:
            if m.name.endswith('_new') or '_new_' in m.name:
                continue
            if m.rename:
                pname = m.rename
                name = m.rename
                if name.startswith('lasso_'):
                    name = name[6:]
                    pname = format_as_camelcase(name)
            else:
                name = m.name[6:]
                pname = format_as_camelcase(name)
            print('%s = _lasso.%s' % (pname, name), file=fd)


    def generate_wrapper(self, fd):
        print(open(os.path.join(self.src_dir,'wrapper_top.c')).read(), file=fd)
        for h in self.binding_data.headers:
            print('#include <%s>' % h, file=fd)
        print('', file=fd)

        self.generate_constants_wrapper(fd)

        self.wrapper_list = []
        for m in self.binding_data.functions:
            self.generate_function_wrapper(m, fd)
        for c in self.binding_data.structs:
            self.generate_member_wrapper(c, fd)
            for m in c.methods:
                self.generate_function_wrapper(m, fd)
        self.generate_wrapper_list(fd)
        print(open(os.path.join(self.src_dir,'wrapper_bottom.c')).read(), file=fd)

    def generate_constants_wrapper(self, fd):
        print('''static void
register_constants(PyObject *d)
{
    PyObject *obj;
''', file=fd)
        for c in self.binding_data.constants:
            if c[0] == 'i':
                print('    obj = PyInt_FromLong(%s);' % c[1], file=fd)
            elif c[0] == 's':
                print('    obj = PyString_FromString((char*)%s);' % c[1], file=fd)
            elif c[0] == 'b':
                print('''\
#ifdef %s
    obj = Py_True;
#else
    obj = Py_False;
#endif''' % c[1], file=fd)
            else:
                print('E: unknown constant type: %r' % c[0], file=sys.stderr)
            print('    PyDict_SetItemString(d, "%s", obj);' % c[1][6:], file=fd)
            print('    Py_DECREF(obj);', file=fd)
        print('}', file=fd)
        print('', file=fd)


    def generate_member_wrapper(self, c, fd):
        klassname = c.name
        for m in c.members:
            name = arg_name(m)
            mname = format_as_camelcase(arg_name(m))
            # getter
            print('''static PyObject*
%s_%s_get(G_GNUC_UNUSED PyObject *self, PyObject *args)
{''' % (klassname[5:], mname), file=fd)
            self.wrapper_list.append('%s_%s_get' % (klassname[5:], mname))

            ftype = arg_type(m)
            if is_cstring(m):
                ftype = 'char*'
            print('    %s return_value;' % ftype, file=fd)
            print('    PyObject* return_pyvalue;', file=fd)
            print('    PyGObjectPtr* cvt_this;', file=fd)
            print('    %s* this;' % klassname, file=fd)
            print('', file=fd)
            print('    if (! PyArg_ParseTuple(args, "O", &cvt_this)) return NULL;', file=fd)
            print('    this = (%s*)cvt_this->obj;' % klassname, file=fd)
            print('    return_value = this->%s;' % arg_name(m), file=fd)
            try:
                self.return_value(fd, m)
            except:
                print('W: cannot make an assignment for', c, m, file=sys.stderr)
                raise
            print('    return return_pyvalue;', file=fd)
            print('}', file=fd)
            print('', file=fd)

            # setter
            print('''static PyObject*
%s_%s_set(G_GNUC_UNUSED PyObject *self, PyObject *args)
{''' % (klassname[5:], mname), file=fd)
            self.wrapper_list.append('%s_%s_set' % (klassname[5:], mname))

            print('    PyGObjectPtr* cvt_this;', file=fd)
            print('    %s* this;' % klassname, file=fd)
            type = m[0]
            # Determine type class
            if is_cstring(m):
                type = type.replace('const ', '')
                parse_format = 'z'
                parse_arg = '&value'
                print('    %s value;' % type, file=fd)
            elif is_int(m, self.binding_data):
                parse_format = 'l'
                parse_arg = '&value'
                print('    long value;', file=fd)
            elif is_glist(m) or is_hashtable(m) or is_xml_node(m) or is_boolean(m):
                parse_format = 'O'
                print('    PyObject *cvt_value;', file=fd)
                parse_arg = '&cvt_value'
            elif is_object(m):
                parse_format = 'O'
                print('    PyGObjectPtr *cvt_value;', file=fd)
                parse_arg = '&cvt_value'
            else:
                raise Exception('Unsupported field: %s' % (m,))
            # Get GObject
            print('    if (! PyArg_ParseTuple(args, "O%s", &cvt_this, %s)) return NULL;' % (
                    parse_format, parse_arg), file=fd)
            print('    this = (%s*)cvt_this->obj;' % klassname, file=fd)
            # Change value
            if is_int(m, self.binding_data):
                print('    this->%s = value;' % name, file=fd)
            elif is_boolean(m):
                print('    this->%s = PyInt_AS_LONG(cvt_value) ? TRUE : FALSE;' % name, file=fd)
            elif is_cstring(m):
                print('    lasso_assign_string(this->%s, value);' % name, file=fd)
            elif is_xml_node(m):
                print('    if (this->%s) xmlFreeNode(this->%s);' % (name, name), file=fd)
                print('    this->%s = get_xml_node_from_pystring(cvt_value);' % name, file=fd)
            elif is_glist(m):
                el_type = element_type(m)
                if is_cstring(el_type):
                    print('    set_list_of_strings(&this->%s, cvt_value);' % name, file=fd)
                elif is_xml_node(el_type):
                    print('    set_list_of_xml_nodes(&this->%s, cvt_value);' % name, file=fd)
                elif is_object(el_type):
                    print('    set_list_of_pygobject(&this->%s, cvt_value);' % name, file=fd)
                else:
                    raise Exception('Unsupported setter for %s' % (m,))
            elif is_hashtable(m):
                el_type = element_type(m)
                if is_object(el_type):
                    print('    set_hashtable_of_pygobject(this->%s, cvt_value);' % name, file=fd)
                else:
                    print('    set_hashtable_of_strings(this->%s, cvt_value);' % name, file=fd)
            elif is_object(m):
                print('    set_object_field((GObject**)&this->%s, cvt_value);' % name, file=fd)
            else:
                raise Exception('Unsupported member %s.%s' % (klassname, m))
            print('    return noneRef();', file=fd)
            print('}', file=fd)
            print('', file=fd)


    def return_value(self, fd, arg, return_var_name = 'return_value', return_pyvar_name = 'return_pyvalue'):
        if is_boolean(arg):
            print('    if (%s) {' % return_var_name, file=fd)
            print('        Py_INCREF(Py_True);', file=fd)
            print('        %s = Py_True;' % return_pyvar_name, file=fd)
            print('    } else {', file=fd)
            print('        Py_INCREF(Py_False);', file=fd)
            print('        %s = Py_False;' % return_pyvar_name, file=fd)
            print('    }', file=fd)
        elif is_int(arg, self.binding_data):
            print('    %s = PyInt_FromLong(%s);' % (return_pyvar_name, return_var_name), file=fd)
        elif is_cstring(arg) and is_transfer_full(arg):
            print('    if (%s) {' % return_var_name, file=fd)
            print('        %s = PyString_FromString(%s);' % (return_pyvar_name, return_var_name), file=fd)
            print('    } else {', file=fd)
            print('        %s = noneRef();' % return_pyvar_name, file=fd)
            print('    }', file=fd)
        elif is_cstring(arg):
            print('    if (%s) {' % return_var_name, file=fd)
            print('        %s = PyString_FromString(%s);' % (return_pyvar_name, return_var_name), file=fd)
            print('    } else {', file=fd)
            print('        %s = noneRef();' % return_pyvar_name, file=fd)
            print('    }', file=fd)
        elif is_glist(arg):
            el_type = element_type(arg)
            if is_object(el_type):
                print('    %s = get_list_of_pygobject(%s);' % (return_pyvar_name, return_var_name), file=fd)
            elif is_cstring(el_type):
                print('    %s = get_list_of_strings(%s);' % (return_pyvar_name, return_var_name), file=fd)
            elif is_xml_node(el_type):
                print('    %s = get_list_of_xml_nodes(%s);' % (return_pyvar_name, return_var_name), file=fd)
            else:
                raise Exception('failed to make an assignment for %s' % (arg,))
        elif is_hashtable(arg):
            el_type = element_type(arg)
            if is_object(el_type):
                print('    %s = get_dict_from_hashtable_of_objects(%s);' % (return_pyvar_name, return_var_name), file=fd)
            else:
                print('    %s = get_dict_from_hashtable_of_strings(%s);' % (return_pyvar_name, return_var_name), file=fd)
        elif is_xml_node(arg):
            # convert xmlNode* to strings
            print('    if (%s) {' % return_var_name, file=fd)
            print('        %s = get_pystring_from_xml_node(%s);' % (return_pyvar_name, return_var_name), file=fd)
            print('    } else {', file=fd)
            print('        %s = noneRef();' % return_pyvar_name, file=fd)
            print('    }', file=fd)
        elif is_object(arg):
            # return a PyGObjectPtr (wrapper around GObject)
            print('''\
    if (%s) {
        %s = PyGObjectPtr_New(G_OBJECT(%s));
    } else {
        %s = noneRef();
    }
''' % (return_var_name, return_pyvar_name, return_var_name, return_pyvar_name), file=fd)
        else:
            raise Exception('failed to make an assignment for %s' % (arg,))

    def generate_function_wrapper(self, m, fd):
        if m.rename:
            name = m.rename
            if name.startswith('lasso_'):
                name = name[6:]
        else:
            name = m.name[6:]
        self.wrapper_list.append(name)
        print('''static PyObject*
%s(G_GNUC_UNUSED PyObject *self, PyObject *args)
{''' % name, file=fd)
        parse_tuple_format = []
        parse_tuple_args = []
        for arg in m.args:
            atype = arg_type(arg)
            aname = arg_name(arg)
            arg_def = None
            python_cvt_def = None
            defval = None
            if is_optional(arg):
                if not '|' in parse_tuple_format:
                    parse_tuple_format.append('|')
            if is_cstring(arg):
                atype = unconstify(atype) 
                if is_optional(arg):
                    parse_tuple_format.append('z')
                else:
                    parse_tuple_format.append('s')
                parse_tuple_args.append('&%s' % aname)
                arg_def = '    %s %s = NULL;' % (arg[0], arg[1])
            elif is_int(arg, self.binding_data) or is_boolean(arg):
                parse_tuple_format.append('i')
                parse_tuple_args.append('&%s' % aname)
                if arg_default(arg):
                    defval = arg_default(arg)
                    if defval.startswith('b:'):
                        defval = defval[2:].upper()
                    else:
                        defval = defval[2:]
                    arg_def = '    %s %s = %s;' % (arg[0], arg[1], defval)
                else:
                    arg_def = '    %s %s;' % (arg[0], arg[1])
            elif is_xml_node(arg) or is_list(arg) or is_time_t_pointer(arg):
                parse_tuple_format.append('O')
                parse_tuple_args.append('&cvt_%s' % aname)
                arg_def = '    %s %s = NULL;' % (arg[0], arg[1])
                python_cvt_def = '    PyObject *cvt_%s = NULL;' % aname
            else:
                parse_tuple_format.append('O')
                parse_tuple_args.append('&cvt_%s' % aname)
                arg_def = '    %s %s = NULL;' % (arg[0], arg[1])
                python_cvt_def = '    PyGObjectPtr *cvt_%s = NULL;' % aname
            if is_out(arg):
                arg_def = '    %s %s = NULL;' % (var_type(arg), arg[1])
                parse_tuple_format.pop()
                parse_tuple_format.append('O')
                parse_tuple_args.pop()
                parse_tuple_args.append('&cvt_%s_out' % aname)
                python_cvt_def = '    PyObject *cvt_%s_out = NULL;' % aname
                print('    PyObject *out_pyvalue = NULL;', file=fd)
            print(arg_def, file=fd)
            if python_cvt_def:
                print(python_cvt_def, file=fd)

        if m.return_type:
            print('    %s return_value;' % m.return_type, file=fd)
            print('    PyObject* return_pyvalue = NULL;', file=fd)
        print('', file=fd)

        parse_tuple_args = ', '.join(parse_tuple_args)
        if parse_tuple_args:
            parse_tuple_args = ', ' + parse_tuple_args

        print('    if (! PyArg_ParseTuple(args, "%s"%s)) return NULL;' % (
                ''.join(parse_tuple_format), parse_tuple_args), file=fd)

        for f, arg in zip([ x for x in parse_tuple_format if x != '|'], m.args):
            if is_out(arg):
                continue
            if is_list(arg):
                qualifier = element_type(arg)
                if is_cstring(qualifier):
                    print('    set_list_of_strings(&%s, cvt_%s);' % (arg[1], arg[1]), file=fd)
                elif qualifier == 'xmlNode*':
                    print('    set_list_of_xml_nodes(&%s, cvt_%s);' % (arg[1], arg[1]), file=fd)
                elif isinstance(qualifier, str) and qualifier.startswith('Lasso'):
                    print('    set_list_of_pygobject(&%s, cvt_%s);' % (arg[1], arg[1]), file=fd)
                else:
                    print('E: unqualified GList argument in', name, qualifier, arg, file=sys.stderr)
            elif is_xml_node(arg):
                print('    %s = get_xml_node_from_pystring(cvt_%s);' % (arg[1], arg[1]), file=fd)
            elif is_time_t_pointer(arg):
                print('    %s = get_time_t(cvt_%s);' % (arg[1], arg[1]), file=fd)
            elif f == 'O':
                if is_optional(arg):
                    print('    if (PyObject_TypeCheck((PyObject*)cvt_%s, &PyGObjectPtrType)) {' % arg[1], file=fd)
                    print('        %s = (%s)cvt_%s->obj;' % (arg[1], arg[0], arg[1]), file=fd)
                    print('    } else {', file=fd)
                    print('        %s = NULL;' % arg[1], file=fd)
                    print('    }', file=fd)
                else:
                    print('    if (PyObject_TypeCheck((PyObject*)cvt_%s, &PyGObjectPtrType)) {' % arg[1], file=fd)
                    print('        %s = (%s)cvt_%s->obj;' % (arg[1], arg[0], arg[1]), file=fd)
                    print('    } else {', file=fd)
                    print('        PyErr_SetString(PyExc_TypeError, "value should be a PyGObject");', file=fd)
                    print('        return NULL;', file=fd)
                    print('    }', file=fd)


        if m.return_type:
            print('    return_value =', file=fd)
            if 'new' in m.name:
                print('(%s)' % m.return_type, file=fd)
        else:
            print('   ', file=fd)
        print('%s(%s);' % (m.name, ', '.join([ref_name(x) for x in m.args])), file=fd)

        if m.return_type:
            # Constructor so decrease refcount (it was incremented by PyGObjectPtr_New called
            # in self.return_value
            try:
                self.return_value(fd, m.return_arg)
            except:
                print('W: cannot assign return value of', m, file=sys.stderr)
                raise

            if is_transfer_full(m.return_arg, default=True):
                self.free_value(fd, m.return_arg, name = 'return_value')
        for f, arg in zip(parse_tuple_format, m.args):
            if is_out(arg):
                self.return_value(fd, arg, return_var_name = arg[1], return_pyvar_name = 'out_pyvalue')
                print('    PyList_SetItem(cvt_%s_out, 0, out_pyvalue);' % arg[1], file=fd)
            elif arg[0] == 'GList*':
                qualifier = arg[2].get('element-type')
                if qualifier == 'char*':
                    print('    free_list(&%s, (GFunc)g_free);' % arg[1], file=fd)
                elif qualifier == 'xmlNode*':
                    print('    free_list(&%s, (GFunc)xmlFreeNode);' % arg[1], file=fd)
                elif qualifier == 'LassoNode':
                    print('    free_list(&%s, (GFunc)g_object_unref);' % arg[1], file=fd)
            elif is_time_t_pointer(arg):
                print('    if (%s) free(%s);' % (arg[1], arg[1]), file=fd)
            elif not is_transfer_full(arg) and is_xml_node(arg):
                self.free_value(fd, arg)

        if not m.return_type:
            print('    return noneRef();', file=fd)
        else:
            print('    return return_pyvalue;', file=fd)
        print('}', file=fd)
        print('', file=fd)

    def generate_wrapper_list(self, fd):
        print('''
static PyMethodDef lasso_methods[] = {''', file=fd)
        for m in self.wrapper_list:
            print('    {"%s", %s, METH_VARARGS, NULL},' % (m, m), file=fd)
        print('    {NULL, NULL, 0, NULL}', file=fd)
        print('};', file=fd)
        print('', file=fd)