~marienz/twisted-x11/trunk

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
#!/usr/bin/env python

from __future__ import absolute_import, with_statement

import sys
import os.path as osp
import os
import contextlib
from keyword import iskeyword
from warnings import warn
from collections import defaultdict
from operator import itemgetter

from tpfkax import types, state

import protoextras

# The way asserts are used in this file is a bit dodgy.
# Instead of specifying conditions that should always be true
# they indicate generating invalid code is likely if the assertion fails.
# That can be because I do not fully understand xcbgen or because
# a case in the input (xml) is unhandled. So when an assertion fails
# modifying or removing it is often sane, and running with assertions off
# on new input is a bad idea.


_cardinal_types = dict(((k,), v) for k, v in {
        'CARD8':  'B',
        'CARD16': 'H',
        'CARD32': 'I',
        'INT8':   'b',
        'INT16':  'h',
        'INT32':  'i',
        'BYTE': 'B',
        'BOOL': '?',
        'char': 'b',
        'void': 'B',
        'float': 'f',
        'double' : 'd',
        }.iteritems())


def _format_to_identifier(format):
    """Convert a struct format string to a (partial) python identifier."""
    # This hacks around the problem that "?" (for boolean) is not valid
    # in a python identifier. Modify this if "O" (our current replacement)
    # becomes valid in a struct format.
    return format.replace('?', 'O')


class Writer(object):

    def __init__(self, levels):
        self.lines = [[] for i in xrange(levels)]
        self.level = 0

    def __call__(self, fmt, *args):
        self._current.append(fmt % args)

    def setlevel(self, idx):
        self._current = self.lines[idx]


(
    LEVEL_IMPORTS, LEVEL_MAIN, LEVEL_EXT,
    LEVEL_EVENTS, LEVEL_ERRORS, LEVEL_TAIL, LEVELS) = xrange(7)


def _t(module, typetuple):
    """Does Python-name conversion on a type tuple of strings."""
    if not module.namespace.is_ext:
        assert len(typetuple) == 2, 'xproto referring to extensions?'
        assert typetuple[0] == 'xcb'
        return typetuple[-1]

    assert typetuple[0] == 'xcb', typetuple
    if len(typetuple) == 2:
        modname = 'xproto'
    else:
        assert len(typetuple) == 3, typetuple
        if typetuple[1] == module.namespace.ext_name:
            return typetuple[-1]
        modname = module.imports_by_ext[typetuple[1]].header
    module.dotimports.add(modname)
    return '%s.%s' % (modname, typetuple[-1])

def _n(name):
    """Does Python-name conversion on a single string fragment.

    Handles some number-only names and reserved words.
    """
    # If this was Python 3 we could use str.isidentifier here...
    if name[0].isdigit():
        return '_' + name
    # None is a keyword in Python 3 but not in 2.6 or below.
    if iskeyword(name) or name == 'None':
        return name + '_'
    return name

def _decap(name):
    if not name[0].isupper() or (len(name) > 1 and name[1].isupper()):
        print 'might need specialcasing for %r' % (name,)
    return name[0].lower() + name[1:]


class _Formatter(object):

    # The style of the format string is (unsurprisingly) irrelevant:
    # '4I' and 'IIII' result in the same underlying C structure.
    # We currently always get the latter form.
    # We currently do not collapse consecutive padding (we get 'x2x',
    # which we could convert to '3x'). This does not matter much,
    # although there may be a few cases where we end up with two
    # instances of the same pattern because of this. Kept because
    # the result is slightly more readable.

    def __init__(self):
        self._reset()

    def _reset(self):
        self._fmt = ''
        self._size = 0
        self._list = []

    def push_format(self, field, name):
        self._fmt += field.type.py_format_str
        self._size += field.type.size
        self._list.append(name)

    def push_pad(self, size):
        self._fmt += '%sx' % (size if size != 1 else '')
        self._size += size

    def flush(self):
        result = (self._fmt, self._size, ', '.join(self._list))
        self._reset()
        return result


@contextlib.contextmanager
def Formatter():
    formatter = _Formatter()
    yield formatter
    if formatter._fmt:
        warn('formatter not flushed!')


class PySimple(types.SimpleType):

    def __init__(self, name, size):
        types.SimpleType.__init__(self, name, size)
        self.py_format_str = _cardinal_types[self.name]

    def out(self, module, name):
        """Simple types get no python output."""


class PyEnum(types.Enum):
    def out(self, module, name):
        """Exported function that handles enum declarations."""
        _py = module.writer
        _py.setlevel(LEVEL_MAIN)
        _py('')
        _py('class %s(base.Enum):', name[-1])

        for count, (enam, value) in enumerate(self.values):
            _py('    %s = %s', _n(enam), value if value != '' else count)


class PyList(types.ListType):
    """Nothing special for lists so far."""
    def resolve(self, module):
        types.ListType.resolve(self, module)
        if self.size is None:
            self.py_format_str = None
        else:
            self.py_format_str = (str(self.expr.value) +
                                  _cardinal_types[self.member.name])
        self.py_alignsize = self.member.size


class PyExpr(types.ExprType):
    """Nothing special for structs so far."""
    def resolve(self, module):
        types.ExprType.resolve(self, module)
        self.py_format_str = _cardinal_types[self.name]


class PyPadding(types.SizedPadType):
    def __init__(self, size):
        types.SizedPadType.__init__(self, size)
        self.py_format_str = ('' if size == 1 else str(size)) + 'x'


def _py_get_expr(expr, lenfieldnames):
    """Figures out what code is needed to get the length of a list field.

    Recurses for math operations.
    Returns bitcount for value-mask fields.
    Otherwise, uses the value of the length field.
    """
    if expr.op is not None:
        return '(%s %s %s)' % (_py_get_expr(expr.lhs, lenfieldnames), expr.op,
                               _py_get_expr(expr.rhs, lenfieldnames))

    if expr.lenfield_name is not None:
        if expr.lenfield_name in lenfieldnames:
            lenexp = expr.lenfield_name
        else:
            lenexp = 'self.' + expr.lenfield_name
    else:
        lenexp = str(expr.value)

    if expr.bitfield:
        return 'base.popcount(%s)' % (lenexp,)
    else:
        return lenexp


def _py_get_expr_request(expr, lenfieldnames):
    """Converts an expression-type field in a request to python code.

    This currently has only one user (xproto's QueryTextExtents).
    """
    if expr.bitfield:
        raise NotImplementedError()

    if expr.op is not None:
        return '(%s %s %s)' % (_py_get_expr_request(expr.lhs, lenfieldnames),
                               expr.op,
                               _py_get_expr_request(expr.rhs, lenfieldnames))

    assert expr.lhs is expr.rhs is None

    if expr.lenfield_name is None:
        assert expr.value is not None
        return str(expr.value)

    assert expr.value is None
    lenof = lenfieldnames.get(expr.lenfield_name, None)
    if lenof is not None:
        return 'len(%s)' % (lenof[0].field_name,)
    else:
        return expr.lenfield_name


def _type_pad(t, i):
    return '-%s & %s' % (i, (3 if t > 4 else t - 1))

def _get_lenfield_names(fields, is_request):
    """Build a map from length field names to the fields they are a length of.

    It turns out to be legal for one len field to refer to more than
    one list (see render AddGlyphs for example), presumably indicating
    the lengths must match.
    """
    lenfieldnames = defaultdict(list)
    for field in fields:
        if isinstance(field.type, types.ListType):
            expr = field.type.expr
            if expr.lenfield_name is not None:
                if not field.type.expr.bitfield and (is_request or
                                                     expr.op is None):
                    # Simple length. Drop it.
                    lenfieldnames[expr.lenfield_name].append(field)
                # else: TODO figure out what to do...

    return lenfieldnames


def _py_complex_resolve(self, module):
    """Extension to the resolve method for all Complex subclasses."""
    self.py_format_str = ''

    for field in self.fields:
        if field.type.py_format_str is None:
            self.py_format_str = None
        elif self.py_format_str is not None:
            self.py_format_str += field.type.py_format_str


def _py_complex_out(self, module, name):
    _py = module.writer
    need_alignment = False
    # HACK: this is either the actual offset if it is fixed or the string
    # "offset" if it is dynamic (and we've emitted code for a variable
    # with that name).
    offset = 0
    # This is set to False every time we do something that would raise
    # if we ran out of buffer, to True every time we do something that
    # would silently cause incorrect output. If we reach the end of
    # the struct with this True we emit an explicit check.
    need_size_check = False

    with Formatter() as formatter:
        lenfieldnames = _get_lenfield_names(self.fields, False)
        # XXX HACK
        if isinstance(self, types.Reply):
            lenfieldnames['length'] = None

        for field in self.fields:
            field_name = _n(field.field_name)
            # XXX this is not sane
            if (self.name, field.field_name) \
                    in protoextras.REPLY_FORMAT_TO_ARRAY:
                lenfieldnames[field_name] = None
            if field.field_name not in lenfieldnames:
                field_name = 'self.' + field_name

            if field.auto:
                formatter.push_pad(field.type.size)
                continue
            if isinstance(field.type, types.SimpleType):
                formatter.push_format(field, field_name)
                continue
            if isinstance(field.type, types.SizedPadType):
                formatter.push_pad(field.type.size)
                continue

            format, size, attrlist = formatter.flush()
            if len(attrlist) > 0:
                module.struct_from.add(format)
                _py("        (%s,) = _struct_%s_from(buf, %s)",
                    attrlist, _format_to_identifier(format), offset)
                need_size_check = False
            if size > 0:
                if offset == 'offset':
                    _py('        offset += %d', size)
                else:
                    offset += size

            if need_alignment:
                alignment = getattr(field.type, 'py_alignsize',
                                    field.type.size)
                if alignment is None:
                    alignment = 4
                if offset == 'offset':
                    _py('        offset += %s',
                        _type_pad(alignment, 'offset'))
                else:
                    offset += -offset & (3 if alignment > 4
                                         else alignment - 1)
            need_alignment = True

            if isinstance(field.type, types.ListType) \
                    and isinstance(field.type.member, types.SimpleType):
                module.arrayimport = True
                format = protoextras.REPLY_ARRAY_TO_FORMAT.get(
                    (self.name, field.field_name))
                count = _py_get_expr(field.type.expr, lenfieldnames)
                if format is None:
                    # Regular fixed-size array.
                    elemsize = field.type.member.size
                    format_str = repr(field.type.member.py_format_str)
                else:
                    # Dynamically-sized array.
                    elemsize = 1
                    # This is triggered for getting unset properties
                    _py('        if not %s:', format)
                    _py('            if %s:', count)
                    _py('                raise etx11.ProtocolException(')
                    _py("                    'no format?')")
                    _py('            format = 8')
                    # TODO: what about signedness?
                    format_str = 'base.FORMAT_TO_TYPECODE[%s]' % (format,)
                _py('        ar = array(%s)', format_str)
                _py('        ar.fromstring(buffer(buf, %s, %s * %d))',
                    offset, count, elemsize)
                _py('        self.%s = ar', _n(field.field_name))
                need_size_check = True
                if offset == 'offset':
                    _py('        offset += %s * %d', count, elemsize)
                else:
                    _py('        offset = %s + %s * %d',
                        offset, count, elemsize)
                    offset = 'offset'
            elif isinstance(field.type, types.ListType):
                _py('        self.%s, seqlen = base.parseList(buffer(buf, %s), %s, %s)',
                    _n(field.field_name),
                    offset,
                    _py_get_expr(field.type.expr, lenfieldnames),
                    _t(module, field.type.member.name))
                # XXX Actually this only acts as a size check if there
                # is at least one item in the list! Do we care?
                need_size_check = False
                if offset == 'offset':
                    _py('        offset += seqlen')
                else:
                    _py('        offset = %s + seqlen', offset)
                    offset = 'offset'
            elif isinstance(field.type, types.ComplexType) \
                    and field.type.size is not None:
                _py('        self.%s = %s(buffer(buf, %s))',
                    _n(field.field_name), _t(module, field.field_type),
                    offset)
                need_size_check = False
                if offset == 'offset':
                    _py('        offset += %s', field.type.size)
                else:
                    offset += field.type.size
            else:
                # Code removed because untested and probably bitrotted.
                raise AssertionError('unreachable?')

        format, size, attrlist = formatter.flush()
        if len(attrlist) > 0:
            if need_alignment:
                if offset == 'offset':
                    _py('        offset += %s', _type_pad(4, 'offset'))
                else:
                    offset += -offset & 3
            module.struct_from.add(format)
            _py('        (%s,) = _struct_%s_from(buf, %s)',
                attrlist, _format_to_identifier(format), offset)
            need_size_check = False
        if size:
            if offset == 'offset':
                _py('        offset += %d', size)
            else:
                offset += size

        if need_size_check:
            module.dotdotimports.add('etx11')
            _py('        if %s > len(buf):', offset)
            _py("            raise etx11.ProtocolException('buffer too short')")

        if offset == 'offset':
            _py('        self._offset = offset')
        else:
            _py('    _offset = %s', offset)


class PyStruct(types.Struct):

    def resolve(self, module):
        types.Struct.resolve(self, module)
        _py_complex_resolve(self, module)

    def out(self, module, name):
        """Exported function that handles structure declarations."""
        _py = module.writer

        _py.setlevel(LEVEL_MAIN)
        _py('')
        _py('class %s(base.Struct):', name[-1])
        # fixed size used to be handled specially. Reintroduce?
        _py('    def __init__(self, buf):')
        # This is tidy and has the side effect of making empty structs work.
        _py('        base.Struct.__init__(self)')

        _py_complex_out(self, module, name)


class PyUnion(types.Union):

    def resolve(self, module):
        types.Union.resolve(self, module)
        _py_complex_resolve(self, module)

    def out(self, module, name):
        """Exported function that handles union declarations."""
        _py = module.writer

        if self.size is None:
            raise NotImplementedError('variable-size union')

        # XXX nothing currently uses this _offset, perhaps stop setting it?
        _py.setlevel(LEVEL_MAIN)
        _py('')
        _py('class %s(base.Union):', name[-1])
        _py('')
        _py('    _offset = %s', self.size)
        _py('')
        _py('    def __init__(self, buf):')

        # This only deals with the two unions currently actually used
        # in xcb-proto. Supporting more here is easy but having
        # untested code here is bad, so just punt on field types not
        # actually used in xcb-proto.

        for field in self.fields:
            if isinstance(field.type, types.ListType) \
                    and isinstance(field.type.member, types.SimpleType):
                module.arrayimport = True
                elemsize = field.type.member.size
                count = _py_get_expr(field.type.expr, {})
                _py('        ar = array(%r)', field.type.member.py_format_str)
                _py('        ar.fromstring(buffer(buf, 0, %s * %d))',
                    count, elemsize)
                _py('        self.%s = ar', _n(field.field_name))
            elif isinstance(field.type, types.ComplexType) \
                    and field.type.size is not None:
                _py('        self.%s = %s(buf)',
                    _n(field.field_name), _t(module, field.field_type))
            else:
                raise NotImplementedError('unsupported union field type')


class PyRequest(types.Request):

    def resolve(self, module):
        types.Request.resolve(self, module)
        _py_complex_resolve(self, module)

    def out(self, module, name):
        """Exported function that handles request declarations."""
        _py = module.writer

        _py.setlevel(LEVEL_MAIN)

        if self.reply:
            py_reply_name = name[-1] + 'Reply'

            # Reply class definition

            _py.setlevel(LEVEL_MAIN)
            _py('')
            _py('class %s(base.Reply):', py_reply_name)
            _py('    def __init__(self, buf, length):')
            _py('        base.Reply.__init__(self)')

            _py_complex_out(self.reply, module, name)
            # HACK: we use this as part of a vararg call, see below.
            py_reply_name = ', ' + py_reply_name
        else:
            py_reply_name = ''

        param_fields = []
        wire_fields = []
        kwargs = []
        mask_length_names = {}
        mask_to_list = {}

        lenfieldnames = _get_lenfield_names(self.fields, True)

        for field in self.fields:
            mask_enum = protoextras.VALUE_MASK_TO_ENUM.get((name,
                                                            field.field_name))
            if mask_enum is not None:
                # Careful: the bit values are strings currently, those
                # do not sort right (0, 1, 10, 11, 12, ... , 2, ...)
                mask_bits = module.get_type(mask_enum).item.bits
                mask_kwargs = [(_decap(bitname), int(bit))
                               for bitname, bit in mask_bits]
                mask_kwargs.sort(key=itemgetter(1))
                mask_length_names[field.field_name] = mask_kwargs
                # I will rely on python or pyflakes to catch duplicates.
                kwargs.extend(kwarg for kwarg, bit in mask_kwargs)
            elif isinstance(field.type, types.ListType) and \
                    field.type.expr.lenfield_name in mask_length_names:
                mask_to_list[field.type.expr.lenfield_name] = field.field_name
            elif field.visible:
                # Having lengths passed in explicitly feels really weird.
                # We can just do len(blah) instead...
                if field.field_name not in lenfieldnames:
                    # The field should appear as a call parameter
                    param_fields.append(field)
            if field.wire:
                # We need to set the field up in the structure
                wire_fields.append(field)

        methodName = _decap(name[-1])

        if kwargs:
            kwargstring = ', ' + ', '.join('%s=None' % (k,) for k in kwargs)
        else:
            kwargstring = ''

        _py.setlevel(LEVEL_EXT)
        _py('')
        _py('    def %s(%s%s):',
            methodName,
            ', '.join(['self'] + [_n(x.field_name) for x in param_fields]),
            kwargstring)

        # The "minor opcode" field passed to sendRequest.
        # For extensions this will turn out to be the actual minor opcode.
        # For the core protocol it can be any expression or padding (0).
        minor_opcode = None
        # Whether we ended up using a StringIO buffer.
        has_buf = False
        with Formatter() as formatter:
            for field in wire_fields:
                if field.auto:
                    if field.field_name == 'major_opcode':
                        pass
                    elif field.field_name == 'minor_opcode':
                        assert module.namespace.is_ext
                        minor_opcode = str(self.opcode)
                    elif field.field_name == 'length':
                        assert minor_opcode is not None
                    else:
                        raise AssertionError('unknown auto field %s' %
                                             (field.field_name,))
                elif isinstance(field.type, types.SimpleType):
                    lenof = lenfieldnames.get(field.field_name)
                    mask_kwargs = mask_length_names.get(field.field_name)
                    if mask_kwargs is not None:
                        mask_list_name = mask_to_list[field.field_name]
                        _py('        %s = 0', field.field_name)
                        _py('        %s = []', mask_list_name)
                        for kwarg, bit in mask_kwargs:
                            _py('        if %s is not None:', kwarg)
                            _py('            %s |= 1 << %s',
                                field.field_name, bit)
                            _py('            %s.append(%s)',
                                mask_list_name, kwarg)
                        expr = _n(field.field_name)
                    elif lenof is None:
                        expr = _n(field.field_name)
                    else:
                        if len(lenof) > 1:
                            # Same length used for multiple lists.
                            # Sanity check:
                            _py('        assert %s', ' == '.join(
                                    'len(%s)' % (lfield.field_name,)
                                    for lfield in lenof))
                        expr = 'len(%s)' % (lenof[0].field_name,)
                    if minor_opcode is None:
                        assert field.type.size == 1
                        minor_opcode = expr
                    else:
                        formatter.push_format(field, expr)
                elif isinstance(field.type, types.SizedPadType):
                    if minor_opcode is None:
                        assert field.type.size == 1
                        minor_opcode = '0'
                    else:
                        formatter.push_pad(field.type.size)
                elif isinstance(field.type, types.ExprType):
                    expr = _py_get_expr_request(field.type.expr,
                                                lenfieldnames)
                    if minor_opcode is None:
                        assert field.type.size == 1
                        minor_opcode = expr
                    else:
                        formatter.push_format(field, expr)
                else:
                    # We hit a non-struct field. Flush what we have so far:
                    assert minor_opcode is not None
                    if not has_buf:
                        has_buf = True
                        module.stringioimport = True
                        _py('        buf = StringIO()')

                    (format, size, attrlist) = formatter.flush()
                    if size > 0:
                        module.struct_pack.add(format)
                        _py('        buf.write(_struct_%s_pack(%s))',
                            _format_to_identifier(format), attrlist)

                    if isinstance(field.type, types.ComplexType) and \
                            not isinstance(field.type, types.Union) and \
                            field.type.size is not None:
                        module.struct_pack.add(field.type.py_format_str)
                        # TODO: see if it makes more sense to unpack this.
                        # ("fieldname_foo, fieldname_blah = fieldname",
                        # and push that onto the formatter above).
                        # Means we merge more struct pack calls.
                        # Might also mean better error messages.
                        _py('        buf.write(_struct_%s_pack(*%s))',
                            _format_to_identifier(field.type.py_format_str),
                            _n(field.field_name))
                    elif isinstance(field.type, types.ListType) and \
                            isinstance(field.type.member, types.SimpleType):
                        module.arrayimport = True
                        # XXX this should not force an extra copy. I
                        # want this: buf.write(buffer(array(...))) But
                        # that fails with "char buffer type not
                        # available". This was
                        # buf.write(str(buffer(array(...)))) but I
                        # think buf.write(array(...).tostring()) is
                        # clearer.
                        _py('        buf.write(array(\'%s\', %s).tostring())',
                            field.type.member.py_format_str,
                            _n(field.field_name))
                    elif isinstance(field.type, types.ListType) \
                            and isinstance(field.type.member, types.Struct):
                        module.struct_pack.add(
                            field.type.member.py_format_str)
                        _py('        for elt in %s:', _n(field.field_name))
                        _py('            buf.write(_struct_%s_pack(*elt))',
                            _format_to_identifier(
                                field.type.member.py_format_str))
                    else:
                        raise AssertionError('unreachable?')

            # Make sure we always output a buffer with a length multiple of 4.
            # If our size is fixed add any padding as part of the format.
            if self.size is not None and self.size % 4:
                formatter.push_pad(-self.size & 3)

            (format, size, attrlist) = formatter.flush()
            if size > 0:
                module.struct_pack.add(format)
                if has_buf:
                    _py('        buf.write(_struct_%s_pack(%s))',
                        _format_to_identifier(format), attrlist)

        # If our size is not fixed we need to calculate any padding at
        # run time.
        if self.size is None:
            assert has_buf
            _py("        buf.write('\\0' * (-buf.tell() & 3))")

        if module.namespace.is_ext:
            major_opcode = 'self.major_opcode'
        else:
            major_opcode = str(self.opcode)

        assert minor_opcode is not None

        # The three forms of sendRequest differ in the third arg and
        # whitespace (just for more readable generated source).
        if has_buf:
            value = 'buf.getvalue()'
            _py('        return self.connection.sendRequest(')
            _py('            %s, %s, buf.getvalue()%s)',
                major_opcode, minor_opcode, py_reply_name)
        elif size > 0:
            _py('        return self.connection.sendRequest(')
            _py('            %s, %s,', major_opcode, minor_opcode)
            _py('            _struct_%s_pack(%s)%s)',
                _format_to_identifier(format), attrlist, py_reply_name)

        else:
            _py("        return self.connection.sendRequest(%s, %s, ''%s)",
                major_opcode, minor_opcode, py_reply_name)


class PyEvent(types.Event):

    def resolve(self, module):
        types.Event.resolve(self, module)
        _py_complex_resolve(self, module)

    def out(self, module, name):
        """Exported function that handles event declarations."""
        _py = module.writer

        # The exactly one case of no has_seq is hardcoded in the core.
        # Complain if this changes.
        if not self.has_seq and \
                self.opcodes != {('xcb', 'KeymapNotify'): 11}:
            raise AssertionError('no-sequence-number change breaks core!')

        py_event_name = name[-1] + 'Event'

        # Structure definition
        _py.setlevel(LEVEL_MAIN)
        _py('')
        _py('class %s(base.Event):', py_event_name)
        _py('    def __init__(self, buf):')
        _py('        base.Event.__init__(self)')

        _py_complex_out(self, module, name)

        # Opcode define.
        # (this check is currently always true).
        if self.opcodes[name] >= 0:
            _py.setlevel(LEVEL_EVENTS)
            _py('        %s: %s,', self.opcodes[name], py_event_name)


class PyError(types.Error):

    def resolve(self, module):
        types.Error.resolve(self, module)
        _py_complex_resolve(self, module)

    def out(self, module, name):
        """Exported function that handles error declarations."""
        _py = module.writer

        # The naming convention used in the XML is inconsistent:
        # xproto, render and sync use names like "Value",
        # others use names like "BadWindow".
        # Prefixing at least the xproto ones with "Bad" seems like a good idea
        # because it matches the diagnostics in the xlib documentation.
        # It seems to make sense for most others too
        # (BadUnsupportedPrivateRequest being the dodgiest one).
        # But "BadBadWindow" is a bit redundant, so leave those alone:
        py_except_name = name[-1]
        if not py_except_name.startswith('Bad'):
            py_except_name = 'Bad' + py_except_name

        # Structure definition

        module.dotdotimports.add('etx11')

        _py.setlevel(LEVEL_MAIN)
        _py('')
        _py('class %s(etx11.RemoteError):', py_except_name)
        _py('    def __init__(self, buf):')
        _py('        etx11.RemoteError.__init__(self)')

        _py_complex_out(self, module, name)

        # Opcode define.
        # (this is false for glx.BadGeneric).
        if self.opcodes[name] >= 0:
            _py.setlevel(LEVEL_ERRORS)
            _py('        %s: %s,', self.opcodes[name], py_except_name)


TYPEMAP = {'enum': PyEnum,
           'struct': PyStruct,
           'union': PyUnion,
           'request': PyRequest,
           'event': PyEvent,
           'error': PyError,
           'simple': PySimple,
           'list': PyList,
           'expr': PyExpr,
           'pad': PyPadding,
           }


class PyModule(state.Module):

    def __init__(self, filename, dest, cache):
        state.Module.__init__(self, filename, TYPEMAP, cache)
        self.py_dest = dest

    def open(self):
        """Store the auto-generated comment, imports and other boilerplate."""
        ns = self.namespace

        self.dotdotimports = set(['base'])
        self.dotimports = set([])
        self.arrayimport = False
        self.stringioimport = False
        self.struct_from = set()
        self.struct_pack = set()

        _py = self.writer = Writer(LEVELS)

        _py.setlevel(LEVEL_IMPORTS)
        _py('#')
        _py('# This file generated automatically from %s by py_client.py.',
            ns.file)
        _py('# Edit at your peril.')
        _py('#')
        _py('')

        _py('from __future__ import absolute_import')
        _py('')
        _py('from struct import Struct')

        _py.setlevel(LEVEL_MAIN)
        _py('')

        if ns.is_ext:
            _py('')
            _py('MAJOR_VERSION = %s', ns.major_version)
            _py('MINOR_VERSION = %s', ns.minor_version)
            _py('')
            _py("NAME = '%s'", ns.ext_xname)
            _py('')

        _py.setlevel(LEVEL_EXT)
        _py('')
        if ns.is_ext:
            _py('class %sExtension(base.Extension):', ns.ext_name)
            _py('')
            _py('    _name = NAME')
            _py("    _pyname = '%s'", ns.header)
        else:
            _py('class Core(base.Core):')

        _py.setlevel(LEVEL_EVENTS)
        _py('')
        _py('    _events = {')

        _py.setlevel(LEVEL_ERRORS)
        _py('    }')
        _py('')
        _py('    _errors = {')

        _py.setlevel(LEVEL_TAIL)
        _py('    }')

    def close(self):
        """Write out all the stored content lines, then closes the file."""
        _py = self.writer
        del self.writer

        _py.setlevel(LEVEL_IMPORTS)
        if self.stringioimport:
            _py('from cStringIO import StringIO')
        if self.arrayimport:
            _py('from array import array')
        if self.dotdotimports:
            _py('from .. import %s', ', '.join(self.dotdotimports))
        if self.dotimports:
            _py('from . import %s', ', '.join(self.dotimports))

        _py('')
        _py('')
        # Precompute our Struct objects at import time.
        # This is slightly faster than using the simple 100 entry cache
        # provided by the struct module.
        common = self.struct_from & self.struct_pack
        for pattern in common:
            _py("_struct = Struct('%s')", pattern)
            pattern = _format_to_identifier(pattern)
            _py('_struct_%s_from = _struct.unpack_from', pattern)
            _py('_struct_%s_pack = _struct.pack', pattern)
        if common:
            _py('del _struct')
            _py('')
        for pattern in self.struct_from - self.struct_pack:
            _py("_struct_%s_from = Struct('%s').unpack_from",
                _format_to_identifier(pattern), pattern)
        _py('')
        for pattern in self.struct_pack - self.struct_from:
            _py("_struct_%s_pack = Struct('%s').pack",
                _format_to_identifier(pattern), pattern)

        filename = osp.join(self.py_dest, '%s.py' % (self.namespace.header,))
        with open(filename, 'w') as pyfile:
            for lines in _py.lines:
                for line in lines:
                    pyfile.write(line)
                    pyfile.write('\n')


def main():
    if not __debug__:
        print 'Warning: running in "optimized" mode (python -O)'
        print 'Only do this with xml input known to be supported!'
        print 'I will silently produce invalid output where I normally crash.'
    if len(sys.argv) != 3:
        print 'Usage: py_client.py sourcedir destdir'
        sys.exit(1)

    sourcedir, destdir = sys.argv[1:]

    cache = state.XMLCache()

    for fn in os.listdir(sourcedir):
        if osp.splitext(fn)[1] == '.xml':
            PyModule(osp.join(sourcedir, fn), destdir, cache).run()


if __name__ == '__main__':
    main()