LabComm.py 28.4 KB
Newer Older
Anders Nilsson's avatar
Anders Nilsson committed
1
2
#!/usr/bin/python
#
3
# LabComm2014 packet has the following layout
Anders Nilsson's avatar
Anders Nilsson committed
4
5
#
#   +----+----+----+----+
Anders Blomdell's avatar
Anders Blomdell committed
6
7
8
#   | id                    (packed32)
#   +----+----+----+----+
#   | length                (packed32)
Anders Nilsson's avatar
Anders Nilsson committed
9
10
11
12
13
#   +----+----+----+----+
#   | data
#   | ...
#   +----+--
#
14
# LabComm2014 SAMPLE_DEF:
Anders Nilsson's avatar
Anders Nilsson committed
15
16
#
#   +----+----+----+----+
Anders Blomdell's avatar
Anders Blomdell committed
17
#   | id = 0x02             (packed32)
Anders Nilsson's avatar
Anders Nilsson committed
18
#   +----+----+----+----+
Anders Blomdell's avatar
Anders Blomdell committed
19
20
21
#   | length                (packed32)
#   +----+----+----+----+
#   | type number           (packed32)
Anders Nilsson's avatar
Anders Nilsson committed
22
23
24
25
#   +----+----+----+----+
#   | type name (UTF8)
#   | ...
#   +----+----+----+----+
Anders Blomdell's avatar
Anders Blomdell committed
26
27
28
#   | signature length      (packed32)
#   +----+----+----+----+
#   | type signature
Anders Nilsson's avatar
Anders Nilsson committed
29
30
31
#   | ...
#   +----+--
#
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# LabComm2014 SAMPLE_REF:
#
#   +----+----+----+----+
#   | id = 0x03             (packed32)
#   +----+----+----+----+
#   | length                (packed32)
#   +----+----+----+----+
#   | type number           (packed32)
#   +----+----+----+----+
#   | type name (UTF8)
#   | ...
#   +----+----+----+----+
#   | signature length      (packed32)
#   +----+----+----+----+
#   | type signature
#   | ...
#   +----+--
#
50
51
52
53
# LabComm2014 TYPE_DEF: (as SAMPLE_DEF, but signatures are hierarchical,
#                         i.e., may contain references to other types
#
#   +----+----+----+----+
54
#   | id = 0x04             (packed32)
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#   +----+----+----+----+
#   | length                (packed32)
#   +----+----+----+----+
#   | type number           (packed32)
#   +----+----+----+----+
#   | type name (UTF8)
#   | ...
#   +----+----+----+----+
#   | signature length      (packed32)
#   +----+----+----+----+
#   | type signature
#   | ...
#   +----+--
#
# LabComm2014 TYPE_BINDING
#
#   +----+----+----+----+
72
#   | id = 0x05             (packed32)
73
74
75
76
77
78
79
80
#   +----+----+----+----+
#   | length                (packed32)
#   +----+----+----+----+
#   | sample number         (packed32)
#   +----+----+----+----+
#   | type number           (packed32)
#   +----+----+----+----+
#
81
# LabComm2014 User data:
Anders Nilsson's avatar
Anders Nilsson committed
82
#
Anders Blomdell's avatar
Anders Blomdell committed
83
84
85
86
87
88
#   +----+----+----+----+
#   | id >= 0x00000040      (packed32)
#   +----+----+----+----+
#   | length                (packed32)
#   +----+----+----+----+
#   | user data
Anders Nilsson's avatar
Anders Nilsson committed
89
90
#   | ...
#   +----+--
91
#
Anders Blomdell's avatar
Anders Blomdell committed
92
93
#
# LabComm2006 packets has the following layout
Anders Nilsson's avatar
Anders Nilsson committed
94
#
Anders Blomdell's avatar
Anders Blomdell committed
95
96
97
98
99
100
#   +----+----+----+----+
#   | id                |
#   +----+----+----+----+
#   | data
#   | ...
#   +----+--
Anders Nilsson's avatar
Anders Nilsson committed
101
#
Anders Blomdell's avatar
Anders Blomdell committed
102
# LabComm2006 SAMPLE:
Anders Nilsson's avatar
Anders Nilsson committed
103
104
105
106
107
108
109
110
111
#
#   +----+----+----+----+
#   | id = 0x00000002   |
#   +----+----+----+----+
#   | type number       |
#   +----+----+----+----+
#   | type name (UTF8)
#   | ...
#   +----+----+----+----+
Anders Blomdell's avatar
Anders Blomdell committed
112
#   | type signature
Anders Nilsson's avatar
Anders Nilsson committed
113
114
115
116
#   | ...
#   +----+--
#
#
Anders Blomdell's avatar
Anders Blomdell committed
117
# LabComm2006 User data:
Anders Nilsson's avatar
Anders Nilsson committed
118
119
#
#   +----+----+----+----+
Anders Blomdell's avatar
Anders Blomdell committed
120
#   | id >= 0x00000040  |
Anders Nilsson's avatar
Anders Nilsson committed
121
122
123
124
#   +----+----+----+----+
#   | user data
#   | ...
#   +----+--
125
#
Anders Nilsson's avatar
Anders Nilsson committed
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

#??  | field1 name
#??  | (length + UTF8)...
#??  +----+----+----+----+
#??  | indices           |
#??  +----+----+----+----+
#??  | index1            | (0 -> variable size)
#??  +----+----+----+----+
#??  | ...
#??  +----+----+----+----+
#??  | indexN            |
#??  +----+----+----+----+
#??  | ...
#??  +----+----+----+----+
#??  | fieldN type       |
#??  +----+----+----+----+
#??  | ...
#??  +----+----+----+----+
#??  | END_TYPE          |
#??  +----+----+----+----+
#??
#??type = <user defined type>
#??
#??  +----+----+----+----+
#??  | type number       |
#??  +----+----+----+----+
#??  | field1 varindices |
#??  | ...               |
#??  +----+----+----+----+
#??  | field1 data       |
#??  | ...               |
#??  +----+----+----+----+
#??  | ...
#??  +----+----+----+----+
#??  | fieldN data       |
#??  | ...               |
#??  +----+----+----+----+
163
164
165
166
#
#
# type numbers and lengths do not have a fixed lenght, but are packed into
# sequences of 7 bit chunks, represented in bytes with the high bit meaning
167
# that more data is to come.
168

169
import types
Anders Nilsson's avatar
Anders Nilsson committed
170
171
import struct as packer

172
DEFAULT_VERSION = "LabComm2014"
173

174
# Allowed packet tags
175
176
i_VERSION     = 0x01
i_SAMPLE_DEF  = 0x02
177
i_SAMPLE_REF  = 0x03
178
179
i_TYPE_DEF    = 0x04
i_TYPE_BINDING= 0x05
180
181
i_PRAGMA      = 0x3f
i_USER        = 0x40 # ..0xffffffff
Anders Nilsson's avatar
Anders Nilsson committed
182

183
# Predefined types
Anders Nilsson's avatar
Anders Nilsson committed
184
185
186
i_ARRAY   = 0x10
i_STRUCT  = 0x11

187
i_BOOLEAN = 0x20
Anders Nilsson's avatar
Anders Nilsson committed
188
189
190
191
192
193
194
i_BYTE    = 0x21
i_SHORT   = 0x22
i_INTEGER = 0x23
i_LONG    = 0x24
i_FLOAT   = 0x25
i_DOUBLE  = 0x26
i_STRING  = 0x27
195
i_SAMPLE  = 0x28
Anders Nilsson's avatar
Anders Nilsson committed
196

197
198

# Version testing
199
def usePacketLength(version):
200
    return version in [ None, "LabComm2014" ]
201
202
203
204

class length_encoder:
    def __init__(self, encoder):
        self.encoder = encoder
205
        self.version = encoder.version
206
207
208
        self.data = ""

    def write(self, data):
209
        self.data += data
210
211

    def __enter__(self):
212
        return Encoder(writer=self, version=None, codec=self.encoder)
213
214

    def __exit__(self, type, value, traceback):
215
216
217
        if usePacketLength(self.version):
             self.encoder.encode_packed32(len(self.data))
        self.encoder.pack("%ds" % len(self.data), self.data)
Anders Nilsson's avatar
Anders Nilsson committed
218
219
220
221

def indent(i, s):
    return ("\n%s" % (" " * i)).join(s.split("\n"))

222
223
224
225
226
227
#
# Base type for all decl's
#
class type_decl(object):
    pass

Anders Nilsson's avatar
Anders Nilsson committed
228
229
230
#
# Primitive types
#
231
class primitive(type_decl):
Anders Nilsson's avatar
Anders Nilsson committed
232
233
234
    def decode_decl(self, decoder):
        return self

235
236
237
238
239
    def __eq__(self, other):
        return self.__class__ == other.__class__

    def __ne__(self, other):
        return not self.__eq__(other)
240

241
242
243
    def __hash__(self):
        return hash(self.__class__)

Anders Nilsson's avatar
Anders Nilsson committed
244
245
246
247
248
249
class BOOLEAN(primitive):
    def encode_decl(self, encoder):
        return encoder.encode_type(i_BOOLEAN)

    def encode(self, encoder, value):
        return encoder.encode_boolean(value)
250

Anders Nilsson's avatar
Anders Nilsson committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
    def decode(self, decoder, obj=None):
        return decoder.decode_boolean()

    def new_instance(self):
        return False

    def __repr__(self):
        return "labcomm.BOOLEAN()"

class BYTE(primitive):
    def encode_decl(self, encoder):
        return encoder.encode_type(i_BYTE)

    def encode(self, encoder, value):
        return encoder.encode_byte(value)
266

Anders Nilsson's avatar
Anders Nilsson committed
267
268
    def decode(self, decoder, obj=None):
        return decoder.decode_byte()
269

Anders Nilsson's avatar
Anders Nilsson committed
270
271
272
273
274
275
276
277
278
279
280
281
    def new_instance(self):
        return 0

    def __repr__(self):
        return "labcomm.BYTE()"

class SHORT(primitive):
    def encode_decl(self, encoder):
        return encoder.encode_type(i_SHORT)

    def encode(self, encoder, value):
        return encoder.encode_short(value)
282

Anders Nilsson's avatar
Anders Nilsson committed
283
284
    def decode(self, decoder, obj=None):
        return decoder.decode_short()
285

Anders Nilsson's avatar
Anders Nilsson committed
286
287
288
289
290
291
292
293
294
295
296
297
    def new_instance(self):
        return 0

    def __repr__(self):
        return "labcomm.SHORT()"

class INTEGER(primitive):
    def encode_decl(self, encoder):
        return encoder.encode_type(i_INTEGER)

    def encode(self, encoder, value):
        return encoder.encode_int(value)
298

Anders Nilsson's avatar
Anders Nilsson committed
299
300
    def decode(self, decoder, obj=None):
        return decoder.decode_int()
301

Anders Nilsson's avatar
Anders Nilsson committed
302
303
304
305
306
307
308
309
310
311
312
313
    def new_instance(self):
        return 0

    def __repr__(self):
        return "labcomm.INTEGER()"

class LONG(primitive):
    def encode_decl(self, encoder):
        return encoder.encode_type(i_LONG)

    def encode(self, encoder, value):
        return encoder.encode_long(value)
314

Anders Nilsson's avatar
Anders Nilsson committed
315
316
    def decode(self, decoder, obj=None):
        return decoder.decode_long()
317

Anders Nilsson's avatar
Anders Nilsson committed
318
319
320
321
322
323
324
325
326
327
328
329
    def new_instance(self):
        return long(0)

    def __repr__(self):
        return "labcomm.LONG()"

class FLOAT(primitive):
    def encode_decl(self, encoder):
        return encoder.encode_type(i_FLOAT)

    def encode(self, encoder, value):
        return encoder.encode_float(value)
330

Anders Nilsson's avatar
Anders Nilsson committed
331
332
    def decode(self, decoder, obj=None):
        return decoder.decode_float()
333

Anders Nilsson's avatar
Anders Nilsson committed
334
335
336
337
338
339
340
341
342
343
344
345
    def new_instance(self):
        return 0.0

    def __repr__(self):
        return "labcomm.FLOAT()"

class DOUBLE(primitive):
    def encode_decl(self, encoder):
        return encoder.encode_type(i_DOUBLE)

    def encode(self, encoder, value):
        return encoder.encode_double(value)
346

Anders Nilsson's avatar
Anders Nilsson committed
347
348
    def decode(self, decoder, obj=None):
        return decoder.decode_double()
349

Anders Nilsson's avatar
Anders Nilsson committed
350
351
352
353
354
355
356
357
358
359
360
361
    def new_instance(self):
        return 0.0

    def __repr__(self):
        return "labcomm.DOUBLE()"

class STRING(primitive):
    def encode_decl(self, encoder):
        return encoder.encode_type(i_STRING)

    def encode(self, encoder, value):
        return encoder.encode_string(value)
362

Anders Nilsson's avatar
Anders Nilsson committed
363
364
    def decode(self, decoder, obj=None):
        return decoder.decode_string()
365

Anders Nilsson's avatar
Anders Nilsson committed
366
367
368
369
370
371
    def new_instance(self):
        return ""

    def __repr__(self):
        return "labcomm.STRING()"

372
373
374
375
376
377
class SAMPLE(primitive):

    def encode_decl(self, encoder):
        return encoder.encode_type(i_SAMPLE)

    def encode(self, encoder, value):
378
379
380
        if not isinstance(value, type_decl):
            # Probably trying to encode a sample class, grab signature
            value = value.signature
381
        return encoder.encode_int(encoder.ref_to_index.get(value, 0))
382

383
384
385
386
387
388
389
390
391
    def decode(self, decoder, obj=None):
        return decoder.decode_ref()

    def new_instance(self):
        return ""

    def __repr__(self):
        return "labcomm.SAMPLE()"

Anders Nilsson's avatar
Anders Nilsson committed
392
393
394
#
# Aggregate types
#
Anders Blomdell's avatar
Anders Blomdell committed
395
class sampledef_or_sampleref_or_typedef(type_decl):
396
    def __init__(self, name=None, decl=None):
Anders Nilsson's avatar
Anders Nilsson committed
397
398
399
        self.name = name
        self.decl = decl

400
    def encode_decl(self, encoder):
401
        encoder.encode_type(self.type_index)
402
        with length_encoder(encoder) as e1:
403
            e1.encode_type(self.get_index(encoder))
404
405
            # XXX: temporary hack for intentions
            e1.encode_empty_intentions(self.name)
406
407
            with length_encoder(e1) as e2:
                self.decl.encode_decl(e2)
Anders Nilsson's avatar
Anders Nilsson committed
408

Anders Blomdell's avatar
Anders Blomdell committed
409
410
    def encode(self, encoder, value):
        self.decl.encode(encoder, value)
Anders Nilsson's avatar
Anders Nilsson committed
411
412
413

    def decode_decl(self, decoder):
        index = decoder.decode_type_number()
414
415
416
         # XXX: temporary hack for intentions
         #      assume the name is the only intention
        name = decoder.decode_intentions()
417
418
        if usePacketLength(decoder.version):
            length = decoder.decode_packed32()
Anders Nilsson's avatar
Anders Nilsson committed
419
420
        decl = decoder.decode_decl()
        result = self.__class__.__new__(self.__class__)
421
422
        result.__init__(name=name, decl=decl)
        self.add_index(decoder, index, result)
Anders Nilsson's avatar
Anders Nilsson committed
423
424
425
426
427
428
429
430
431
432
        return result

    def decode(self, decoder, obj=None):
        obj = decoder.create_object(self.name)
        obj = self.decl.decode(decoder, obj)
        return obj

    def new_instance(self):
        return self.decl.new_instance()

433
    def __eq__(self, other):
434
        return (self.__class__ == other.__class__ and
435
436
                self.name == other.name and
                self.decl == other.decl)
437

438
    def __ne__(self, other):
439
        return not self.__eq__(other)
440

441
442
    def __hash__(self):
        return hash(self.__class__) ^ hash(self.name) ^ hash(self.decl)
443

Anders Nilsson's avatar
Anders Nilsson committed
444
    def __repr__(self):
445
446
        return "%s('%s', %s)" % (self.type_name, self.name, self.decl)

Anders Blomdell's avatar
Anders Blomdell committed
447
class sample_def(sampledef_or_sampleref_or_typedef):
448
449
450
451
452
453
454
455
    type_index = i_SAMPLE_DEF
    type_name = 'sample'

    def get_index(self, encoder):
        return encoder.decl_to_index[self]

    def add_index(self, decoder, index, decl):
        decoder.add_decl(decl, index)
456
457
458

    def rename(self, name):
        return sample_def(name=name, decl=self.decl)
459

Anders Blomdell's avatar
Anders Blomdell committed
460
class sample_ref(sampledef_or_sampleref_or_typedef):
461
462
    type_index = i_SAMPLE_REF
    type_name = 'sample_ref'
463

464
465
466
467
468
469
470
471
472
473
474
475
476
    def __init__(self, name=None, decl=None, sample=None):
        self.name = name
        self.decl = decl
        if sample == None and name != None and decl != None:
            self.sample = sample_def(name, decl)
        else:
            self.sample = sample

    def get_index(self, encoder):
        return encoder.ref_to_index[self.sample]

    def add_index(self, decoder, index, decl):
        decoder.add_ref(decl, index)
Anders Nilsson's avatar
Anders Nilsson committed
477

Anders Blomdell's avatar
Anders Blomdell committed
478
479
480
481
482
483
484
485
486
487
class typedef(sampledef_or_sampleref_or_typedef):
    type_index = i_TYPE_DEF
    type_name = 'typedef'

    def encode_decl(self, encoder):
        self.decl.encode_decl(encoder)

    def encode(self, encoder, value):
        self.decl.encode(encoder, value)

488
class array(type_decl):
Anders Nilsson's avatar
Anders Nilsson committed
489
    def __init__(self, indices, decl):
490
        self.indices = tuple(indices)
Anders Nilsson's avatar
Anders Nilsson committed
491
        self.decl = decl
492

493
    def __eq__(self, other):
494
        return (self.__class__ == other.__class__ and
495
496
                self.indices == other.indices and
                self.decl == other.decl)
497

498
    def __ne__(self, other):
499
        return not self.__eq__(other)
500

501
502
    def __hash__(self):
        return hash(self.__class__) ^ hash(self.indices) ^ hash(self.decl)
503

Anders Nilsson's avatar
Anders Nilsson committed
504
505
    def encode_decl(self, encoder):
        encoder.encode_type(i_ARRAY)
506
        encoder.encode_packed32(len(self.indices))
Anders Nilsson's avatar
Anders Nilsson committed
507
        for i in self.indices:
508
            encoder.encode_packed32(i)
Anders Nilsson's avatar
Anders Nilsson committed
509
510
        encoder.encode_type_number(self.decl)

511
512
513
514
    def min_max_shape(self, l, depth, shape):
        if isinstance(l, types.StringTypes):
            return shape
        try:
Anders Nilsson's avatar
Anders Nilsson committed
515
516
517
            length = len(l)
            if len(shape) <= depth:
                shape.append((length, length))
518
                pass
Anders Nilsson's avatar
Anders Nilsson committed
519
520
521
522
523
            else:
                (low, high) = shape[depth]
                low = min(low, length)
                high = max(high, length)
                shape[depth] = (low, high)
524
                pass
Anders Nilsson's avatar
Anders Nilsson committed
525
526
            for e in l:
                shape = self.min_max_shape(e, depth + 1, shape)
527
528
529
530
                pass
            pass
        except TypeError:
            pass
531
        return shape
Anders Nilsson's avatar
Anders Nilsson committed
532
533
534
535
536
537
538
539
540
541

    def shape(self, l):
        shape = self.min_max_shape(l, 0, [])
        result = []
        for (low, high) in shape:
            if low != high:
                raise Exception("Non rectangular shape %s" % shape)
            result.append(low)
        return result

542

Anders Nilsson's avatar
Anders Nilsson committed
543
544
545
546
547
548
549
550
    def encode_indices(self, encoder, value):
        depth = len(self.indices)
        shape = self.shape(value)
        if len(shape) < len(self.indices):
            raise Exception("Actual dimension %s differs from declared %s" %
                            (shape, self.indices))
        for i in range(len(self.indices)):
            if self.indices[i] == 0:
551
                encoder.encode_packed32(shape[i])
Anders Nilsson's avatar
Anders Nilsson committed
552
553
554
555
556
557
            elif self.indices[i] != shape[i]:
                raise Exception("Actual dimension %d in %s differs from %s" %
                                (i, shape, self.indices))
        return depth

    def encode_value(self, encoder, value, depth):
558
        if depth:
Anders Nilsson's avatar
Anders Nilsson committed
559
560
561
562
            for e in value:
                self.encode_value(encoder, e, depth - 1)
        else:
            self.decl.encode(encoder, value)
563

Anders Nilsson's avatar
Anders Nilsson committed
564
565
566
567
568
    def encode(self, encoder, value):
        depth = self.encode_indices(encoder, value)
        self.encode_value(encoder, value, depth)

    def decode_decl(self, decoder):
569
        n_indices = decoder.decode_packed32()
Anders Nilsson's avatar
Anders Nilsson committed
570
571
        indices = []
        for i in range(n_indices):
572
            index = decoder.decode_packed32()
Anders Nilsson's avatar
Anders Nilsson committed
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
            indices.append(index)
        elem_decl = decoder.decode_decl()
        return array(indices, elem_decl)

    def decode_value(self, decoder, indices):
        if not len(indices):
            result = self.decl.decode(decoder)
        else:
            result = []
            for i in range(indices[0]):
                result.append(self.decode_value(decoder, indices[1:]))
        return result

    def decode(self, decoder, obj=None):
        indices = []
        for i in self.indices:
            if i == 0:
590
                i = decoder.decode_packed32()
Anders Nilsson's avatar
Anders Nilsson committed
591
592
593
594
595
596
597
598
599
600
601
            indices.append(i)
        return self.decode_value(decoder, indices)

    def new_instance_value(self, indices):
        if not len(indices):
            result = self.decl.new_instance()
        else:
            result = []
            for i in range(indices[0]):
                result.append(self.new_instance_value(indices[1:]))
        return result
602

Anders Nilsson's avatar
Anders Nilsson committed
603
604
605
606
    def new_instance(self):
        indices = []
        for i in self.indices:
            if i == 0:
607
                i = decoder.decode_packed32()
Anders Nilsson's avatar
Anders Nilsson committed
608
609
            indices.append(i)
        return self.new_instance_value(indices)
610

Anders Nilsson's avatar
Anders Nilsson committed
611
612
613
    def __repr__(self):
        return "labcomm.array(%s,\n    %s)" % (
            self.indices, indent(4, self.decl.__repr__()))
614

615
class struct(type_decl):
Anders Nilsson's avatar
Anders Nilsson committed
616
    def __init__(self, field):
617
618
619
        self.field = tuple(field)

    def __eq__(self, other):
620
        return (self.__class__ == other.__class__ and
621
                self.field == other.field)
622

623
    def __ne__(self, other):
624
        return not self.__eq__(other)
625
626

    def __hash__(self):
627
        return hash(self.__class__) ^ hash(self.field)
Anders Nilsson's avatar
Anders Nilsson committed
628
629
630

    def encode_decl(self, encoder):
        encoder.encode_type(i_STRUCT)
631
        encoder.encode_packed32(len(self.field))
Anders Nilsson's avatar
Anders Nilsson committed
632
        for (name, decl) in self.field:
633
            encoder.encode_empty_intentions(name)
Anders Nilsson's avatar
Anders Nilsson committed
634
635
636
637
638
639
640
641
            encoder.encode_type_number(decl)

    def encode(self, encoder, obj):
        if isinstance(obj, dict):
            for (name, decl) in self.field:
                decl.encode(encoder, obj[name])
        else:
            for (name, decl) in self.field:
Anders Blomdell's avatar
Anders Blomdell committed
642
                decl.encode(encoder, getattr(obj, name))
Anders Nilsson's avatar
Anders Nilsson committed
643
644

    def decode_decl(self, decoder):
645
        n_field = decoder.decode_packed32()
Anders Nilsson's avatar
Anders Nilsson committed
646
647
        field = []
        for i in range(n_field):
648
            name = decoder.decode_intentions()
Anders Nilsson's avatar
Anders Nilsson committed
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
            decl = decoder.decode_decl()
            field.append((name, decl))
        return struct(field)

    def decode(self, decoder, obj=None):
        if obj == None:
            obj = decoder.create_object()
        for (name, decl) in self.field:
            obj.__setattr__(name, decl.decode(decoder))
        return obj

    def new_instance(self):
        result = anonymous_object()
        for (name, decl) in self.field:
            result.__setattr__(name, decl.new_instance())
        return result
665

Anders Nilsson's avatar
Anders Nilsson committed
666
667
668
669
670
671
672
673
674
    def __repr__(self):
        delim = ""
        result = "labcomm.struct(["
        for (name, decl) in self.field:
            result += "%s\n  ('%s', %s)" % (delim, name, decl)
            delim = ","
        result += "\n])"
        return result

675
676
SAMPLE_DEF = sample_def()
SAMPLE_REF = sample_ref()
Anders Nilsson's avatar
Anders Nilsson committed
677

678
ARRAY = array([], None)
679
STRUCT = struct([])
Anders Nilsson's avatar
Anders Nilsson committed
680

681
class anonymous_object(dict):
Anders Nilsson's avatar
Anders Nilsson committed
682
683
684
685
    def __setattr__(self, name, value):
        if name.startswith("_"):
            super(anonymous_object, self).__setattr__(name, value)
        else:
686
            self[name] = value
Anders Nilsson's avatar
Anders Nilsson committed
687
688
689
690
691

    def __getattr__(self, name):
        if name.startswith("_"):
            return super(anonymous_object, self).__getattr__(name)
        else:
692
            return self[name]
Anders Nilsson's avatar
Anders Nilsson committed
693
694

class Codec(object):
695
696
697
698
699
700
701
702
703
704
705
706
707
    def __init__(self, codec=None):
        self.type_to_name = codec and codec.type_to_name or {}
        self.name_to_type = codec and codec.name_to_type or {}
        self.index_to_decl = codec and codec.index_to_decl or {}
        self.decl_to_index = codec and codec.decl_to_index  or {}
        self.name_to_decl = codec and codec.name_to_decl  or {}
        self.index_to_ref = codec and codec.index_to_ref or {}
        self.ref_to_index = codec and codec.ref_to_index or {}
        self.name_to_ref = codec and codec.name_to_ref or {}
        self.decl_index = codec and codec.decl_index or i_USER
        self.ref_index = codec and codec.ref_index or i_USER
        if not codec:
            self.predefined_types()
Anders Nilsson's avatar
Anders Nilsson committed
708
709

    def predefined_types(self):
710
        self.add_decl(SAMPLE_DEF, i_SAMPLE_DEF)
711
        self.add_decl(SAMPLE_REF, i_SAMPLE_REF)
Anders Nilsson's avatar
Anders Nilsson committed
712
713
714
715
716
717
718
719
720
721
722
723

        self.add_decl(ARRAY, i_ARRAY)
        self.add_decl(STRUCT, i_STRUCT)

        self.add_decl(BOOLEAN(), i_BOOLEAN)
        self.add_decl(BYTE(), i_BYTE)
        self.add_decl(SHORT(), i_SHORT)
        self.add_decl(INTEGER(), i_INTEGER)
        self.add_decl(LONG(), i_LONG)
        self.add_decl(FLOAT(), i_FLOAT)
        self.add_decl(DOUBLE(), i_DOUBLE)
        self.add_decl(STRING(), i_STRING)
724
        self.add_decl(SAMPLE(), i_SAMPLE)
725

Anders Nilsson's avatar
Anders Nilsson committed
726
727
    def add_decl(self, decl, index=0):
        if index == 0:
728
729
            if decl in self.decl_to_index:
                return False
Anders Nilsson's avatar
Anders Nilsson committed
730
731
732
733
734
735
736
737
            index = self.decl_index
            self.decl_index += 1
        self.index_to_decl[index] = decl
        self.decl_to_index[decl] = index
        try:
            self.name_to_decl[decl.name] = decl
        except:
            pass
738
        return True
739

740
    def add_ref(self, ref, index=0):
741
742
743
        if not isinstance(ref, type_decl):
            # Probably trying to register a sample class, grab signature
            ref = ref.signature
744
        if index == 0:
745
746
            if ref.sample in self.ref_to_index:
                return False
747
748
749
750
751
752
753
754
            index = self.ref_index
            self.ref_index += 1
        self.index_to_ref[index] = ref.sample
        self.ref_to_index[ref.sample] = index
        try:
            self.name_to_ref[ref.sample.name] = ref.sample
        except:
            pass
755
        return True
756

Anders Nilsson's avatar
Anders Nilsson committed
757
758
759
760
761
762
763
764
765
766
    def add_binding(self, name, decl):
        self.type_to_name[decl] = name
        self.name_to_type[name] = decl

    def samples(self):
        result = []
        index = self.index_to_decl.keys()
        index.sort()
        for i in index:
            e = self.index_to_decl[i]
767
            if i >= i_USER and isinstance(e, sample):
Anders Nilsson's avatar
Anders Nilsson committed
768
769
                result.append(e)
        return result
770

Anders Nilsson's avatar
Anders Nilsson committed
771
772

class Encoder(Codec):
773
774
    def __init__(self, writer, version=DEFAULT_VERSION, codec=None):
        super(Encoder, self).__init__(codec)
Anders Nilsson's avatar
Anders Nilsson committed
775
        self.writer = writer
776
        self.version = version
777
        if self.version in [ "LabComm2014" ]:
778
779
780
781
782
783
            self.encode_type(i_VERSION)
            with length_encoder(self) as e:
                e.encode_string(version)
        elif self.version in [ None,  "LabComm2006" ]:
            pass
        else:
784
            raise Exception("Unsupported labcomm version %s" % self.version)
Anders Nilsson's avatar
Anders Nilsson committed
785
786
787
788
789

    def pack(self, format, *args):
        self.writer.write(packer.pack(format, *args))

    def add_decl(self, decl, index=0):
790
791
        if not isinstance(decl, type_decl):
            decl = decl.signature
792
793
794
795
796
        if index == 0:
            self.writer.mark_begin(decl, None)
            if super(Encoder, self).add_decl(decl, index):
                decl.encode_decl(self)
            self.writer.mark_end(decl, None)
797

798
799
800
801
802
    def add_ref(self, ref, index=0):
        if not isinstance(ref, type_decl):
            # Trying to register a sample class
            ref = ref.signature
        decl = sample_ref(name=ref.name, decl=ref.decl, sample=ref)
803
        if index == 0:
804
805
806
807
            self.writer.mark_begin(ref, None)
            if super(Encoder, self).add_ref(decl, index):
                decl.encode_decl(self)
            self.writer.mark_end(ref, None)
808

Anders Nilsson's avatar
Anders Nilsson committed
809
810
811
812
    def encode(self, object, decl=None):
        if decl == None:
            name = self.type_to_name[object.__class__]
            decl = self.name_to_decl[name]
813
814
        if not isinstance(decl, type_decl):
            decl = decl.signature
815
        self.writer.mark_begin(decl, object)
Anders Nilsson's avatar
Anders Nilsson committed
816
        self.encode_type_number(decl)
817
818
        with length_encoder(self) as e:
            decl.encode(e, object)
819
        self.writer.mark_end(decl, object)
Anders Nilsson's avatar
Anders Nilsson committed
820
821
822
823
824
825

    def encode_type_number(self, decl):
        try:
            self.encode_type(self.decl_to_index[decl])
        except KeyError:
            decl.encode_decl(self)
826

827
    def encode_packed32(self, v):
828
        if self.version in [ None, "LabComm2014" ]:
829
830
            v = v & 0xffffffff
            tmp = [ v & 0x7f ]
831
            v = v >> 7
832
833
834
835
            while v:
                tmp.append(v & 0x7f | 0x80)
                v = v >> 7
            for c in reversed(tmp):
836
                self.encode_byte(c)
837
838
839
840
841
        elif self.version == "LabComm2006" :
            v = v & 0xffffffff
            self.encode_int(v)
        else :
            raise Exception("Unsupported labcomm version %s" % self.version)
842

Anders Nilsson's avatar
Anders Nilsson committed
843
    def encode_type(self, index):
Sven Robertz's avatar
Sven Robertz committed
844
        self.encode_packed32(index)
845

Anders Nilsson's avatar
Anders Nilsson committed
846
847
848
849
850
851
852
    def encode_boolean(self, v):
        if v:
            self.pack("!b", 1)
        else:
            self.pack("!b", 0)

    def encode_byte(self, v):
853
        self.pack("!B", v)
Anders Nilsson's avatar
Anders Nilsson committed
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871

    def encode_short(self, v):
        self.pack("!h", v)

    def encode_int(self, v):
        self.pack("!i", v)

    def encode_long(self, v):
        self.pack("!q", v)

    def encode_float(self, v):
        self.pack("!f", v)

    def encode_double(self, v):
        self.pack("!d", v)

    def encode_string(self, v):
        s = v.encode("utf8")
Sven Robertz's avatar
Sven Robertz committed
872
873
	self.encode_packed32(len(s));
	self.pack("%ds" % len(s),s)
Anders Nilsson's avatar
Anders Nilsson committed
874

875
    def encode_empty_intentions(self, name):
876
#        pass
877
        self.encode_packed32(1)
878
        self.encode_string("")
879
        self.encode_string(name)
880

Anders Nilsson's avatar
Anders Nilsson committed
881
class Decoder(Codec):
882
    def __init__(self, reader, version=DEFAULT_VERSION):
Anders Nilsson's avatar
Anders Nilsson committed
883
884
        super(Decoder, self).__init__()
        self.reader = reader
885
        self.version = version
886

Anders Nilsson's avatar
Anders Nilsson committed
887
888
889
890
891
892
893
894
895
896
897
898
899
    def unpack(self, format):
        size = packer.calcsize(format)
        data = ""
        while len(data) < size:
            data += self.reader.read(size - len(data))
        result = packer.unpack(format, data)
        return result[0]

    def decode_decl(self):
        index = self.decode_type_number()
        result = self.index_to_decl[index]
        if index < i_USER:
            result = result.decode_decl(self)
900
901
        else:
            raise Exception('Should not be used')
Anders Nilsson's avatar
Anders Nilsson committed
902
903
        return result

904
905
906
907
    def skip(self, length):
        for _ in xrange(length):
            self.decode_byte()

908
909
910
911
    # kludge, should really check if the index exists in self.version
    def skip_or_raise(self, length, index):
        if usePacketLength(self.version):
            self.skip(length)
912
        else:
913
914
            raise Exception("Invalid type index %d" % index)

Anders Nilsson's avatar
Anders Nilsson committed
915
    def decode(self):
916
917
918
919
920
921
922
        while True:
            index = self.decode_type_number()
            if usePacketLength(self.version):
                length = self.decode_packed32()
            if index != i_VERSION:
                break
            else:
923
                other_version = self.decode_string()
924
925
926
                if self.version != other_version:
                    raise Exception("LabComm version mismatch %s != %s" %
                                    (version, other_version))
927
        if index == i_SAMPLE_DEF:
928
929
            decl = self.index_to_decl[index].decode_decl(self)
            value = None
930
931
932
        elif index == i_SAMPLE_REF:
            decl = self.index_to_decl[index].decode_decl(self)
            value = None
933
        elif index == i_TYPE_DEF:
934
            self.skip_or_raise(length, index)
935
936
937
            decl = None
            value = None
        elif index == i_TYPE_BINDING:
938
            self.skip_or_raise(length, index)
939
940
941
            decl = None
            value = None
        elif index == i_PRAGMA:
942
            self.skip_or_raise(length, index)
943
944
            decl = None
            value = None
945
        elif index < i_USER:
946
            raise Exception("Invalid type index %d" % index)
Anders Nilsson's avatar
Anders Nilsson committed
947
        else:
948
            decl = self.index_to_decl[index]
Anders Nilsson's avatar
Anders Nilsson committed
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
            value = decl.decode(self)
        self.reader.mark(value, decl)
        return (value, decl)

    def create_object(self, name=None):
        if isinstance(self, primitive):
            result = None
        else:
            try:
                decl = self.name_to_type[name]
                result = decl.__new__(decl)
                result.__init__()
            except KeyError:
                result = anonymous_object()

        return result
965

966
    def decode_packed32(self):
967
        if self.version in [ "LabComm2013", "LabComm2014" ] :
968
969
970
971
972
973
974
975
976
977
978
            result = 0
            while True:
                tmp = self.decode_byte()
                result = (result << 7) | (tmp & 0x7f)
                if (tmp & 0x80) == 0:
                    break
            return result
        elif self.version == "LabComm2006" :
            return self.decode_int()
        else :
            raise Exception("Unsupported labcomm version %s" % self.version)
979

Anders Nilsson's avatar
Anders Nilsson committed
980
    def decode_type_number(self):
981
        return self.decode_packed32()
982

Anders Nilsson's avatar
Anders Nilsson committed
983
984
    def decode_boolean(self):
        return self.unpack("!b") != 0
985

Anders Nilsson's avatar
Anders Nilsson committed
986
    def decode_byte(self):
987
        return self.unpack("!B")
988

Anders Nilsson's avatar
Anders Nilsson committed
989
990
    def decode_short(self):
        return self.unpack("!h")
991

Anders Nilsson's avatar
Anders Nilsson committed
992
993
    def decode_int(self):
        return self.unpack("!i")
994

Anders Nilsson's avatar
Anders Nilsson committed
995
996
    def decode_long(self):
        return self.unpack("!q")
997

Anders Nilsson's avatar
Anders Nilsson committed
998
999
    def decode_float(self):
        return self.unpack("!f")
1000

For faster browsing, not all history is shown. View entire blame