File: prototypes.py

package info (click to toggle)
bitpim 0.9.08.dfsg.1-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 24,228 kB
  • ctags: 45,773
  • sloc: python: 191,894; cpp: 2,074; perl: 600; ansic: 343; sh: 185; makefile: 76; sed: 1
file content (1608 lines) | stat: -rw-r--r-- 61,926 bytes parent folder | download
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
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
### BITPIM
###
### Copyright (C) 2003-2005 Roger Binns <rogerb@rogerbinns.com>
###
### This program is free software; you can redistribute it and/or modify
### it under the terms of the BitPim license as detailed in the LICENSE file.
###
### $Id: prototypes.py 3490 2006-07-19 04:17:00Z djpham $

"""The various types used in protocol descriptions

To implement a type used for protocol descriptions,
examine the code for UINTlsb in this file.  Of note:

  - Inherit from BaseProtogenClass
  - Call superclass constructors using super()
  - Do not process any of the args or kwargs in the constructor unless
    you need them to alter how you are constructed
  - At the end of the constructor, call _update if you are the most
    derived class
  - In _update, call super()._update, and then delete keyword arguments
    as you process them (consider using L{BaseProtogenClass._consumekw} function)
  - If you are the most derived class, complain about
    unused keyword arguments (consider using
    L{BaseProtogenClass._complainaboutunusedargs} function)
  - set _bufferstartoffset and _bufferendoffset whenever
    you are read or written from a buffer
  - (optionally) define a getvalue() method that returns
    a better type.  For example if your class is integer
    like then this would return a real int.  If string like,
    then this will return a real string.
  - If you are a container, override iscontainer.  You will
    also need to provide a containerelements() method which
    lets you iterate over the entries.

containerelements method:

  - You should return tuples of (fieldname, fieldvalue, descriptionstring or None)
  - fieldvalue should be the actual object, not a pretty version (eg a USTRING not str)
  
    
"""
import sys
import calendar
import cStringIO
import re
import time

import common

class ProtogenException(Exception):
    """Base class for exceptions encountered with data marshalling"""
    def __init__(self, *args, **kwargs):
        Exception.__init__(self, *args, **kwargs)

class SizeNotKnownException(ProtogenException):
    "Unable to marshal since size isn't known"
    def __init__(self):
        ProtogenException.__init__(self, "The size of this item is not known and hence cannot be en/decoded")

class ValueNotSetException(ProtogenException):
    "Value not been set"
    def __init__(self):
        ProtogenException.__init__(self, "The value for this object has not been set.")

class ValueException(ProtogenException):
    "Some sort of problem with the value"
    def __init__(self, str):
        ProtogenException.__init__(self,str)

class NotTerminatedException(ProtogenException):
    "The value should have been terminated and wasn't"
    def __init__(self):
        ProtogenException.__init__(self,"The value should have been terminated and wasn't")

class ValueLengthException(ProtogenException):
    "The value is the wrong size (too big or too small)"
    def __init__(self, sz, space):
        ProtogenException.__init__(self, "The value (length %d) is the wrong size for space %d" % (sz,space))
    
class MissingQuotesException(ProtogenException):
    "The value does not have quotes around it"
    def __init__(self):
        ProtogenException.__init__(self, "The value does not have the required quote characters around it")
    

class BaseProtogenClass(object):
    """All types are derived from this"""
    def packetsize(self):
        "Returns size in bytes that we occupy"
        # This is implemented by writing to a buffer and seeing how big the result is.
        # The code generator used to make one per class but it was so rarely used (twice)
        # that this implementation is available instead
        b=buffer()
        self.writetobuffer(b)
        return len(b.getvalue())
        
    def writetobuffer(self, buf):
        "Scribble ourselves to the buf"
        raise NotImplementedError("writetobuffer()")
    def readfrombuffer(self, buf):
        "Get our value from the buffer"
        raise NotImplementedError("readfrombuffer()")
    def getvalue(self):
        "Returns our underlying value if sensible (eg an integer, string or list) else returns self"
        return self
    def packetspan(self):
        """Returns tuple of begining,end offsets from last packet we were read or written from.

        Note that in normal Python style, end is one beyond the last byte we
        actually own"""
        return self._bufferstartoffset, self._bufferendoffset
    def _consumekw(self, dict, consumelist):
        """A helper function for easily setting internal values from the dict

        For each name in consumelist, we look for it in the dict and
        set self._name to the value from dict.  The key is then deleted
        from the dict."""
        for name in consumelist:
            if dict.has_key(name):
                setattr(self, "_"+name, dict[name])
                del dict[name]
    def _complainaboutunusedargs(self, klass, dict):
        """A helper function that will raise an exception if there are unused keyword arguments.

        @Note: that we only complain if in the most derived class, so it is safe
        to always call this helper as the last line of your constructor.

        @param klass:  This should be the class you are calling this function from
        @param dict:   The keyword arguments still in play
        """
        if len(dict) and self.__class__.__mro__[0]==klass:
            raise TypeError('Unexpected keyword args supplied: '+`dict`)

    def _ismostderived(self, klass):
        
        return self.__class__.__mro__[0]==klass

    def _update(self, args, kwargs):
        return

    def iscontainer(self):
        """Do we contain fields?"""
        return False

    def update(self, *args, **kwargs):
        self._update(args, kwargs)

    # help with logging
    def autologwrite(self, buf, logtitle="<written data>"):
        f=sys._getframe() # my frame
        f=f.f_back # my caller
        if f:
            f=f.f_back # their caller
            if f:
                caller=f.f_locals.get("self", None)
                if caller:
                    try:
                        caller.logdata(logtitle, buf.getvalue(), self)
                    except:
                        pass

    def autologread(self, buf, logtitle="<read data>"):
        f=sys._getframe() # my frame
        f=f.f_back # my caller
        if f:
            f=f.f_back # their caller
            if f:
                caller=f.f_locals.get("self", None)
                if caller:
                    try:
                        caller.logdata(logtitle, buf.getdata(), self)
                    except:
                        pass

class UINTlsb(BaseProtogenClass):
    "An integer in Least Significant Byte first order"
    def __init__(self, *args, **kwargs):
        """
        An integer value can be specified in the constructor, or as the value keyword arg.

        @keyword constant:  (Optional) A constant value.  All reads must have this value
        @keyword constantexception: (Optional) Type of exception raised when data doesn't match constant.
        @keyword sizeinbytes: (Mandatory for writing, else Optional) How big we are in bytes
        @keyword default:  (Optional) Our default value
        @keyword value: (Optional) The value
        """
        super(UINTlsb, self).__init__(*args, **kwargs)
        self._constant=None
        self._constantexception=ValueError
        self._sizeinbytes=None
        self._value=None
        self._default=None

        if self._ismostderived(UINTlsb):
            self._update(args,kwargs)


    def _update(self, args, kwargs):
        super(UINTlsb,self)._update(args, kwargs)
        
        self._consumekw(kwargs, ("constant", "constantexception", "sizeinbytes", "default", "value"))
        self._complainaboutunusedargs(UINTlsb,kwargs)

        # Set our value if one was specified
        if len(args)==0:
            pass
        elif len(args)==1:
            self._value=int(args[0])
        else:
            raise TypeError("Unexpected arguments "+`args`)

        if self._value is None and self._default is not None:
            self._value=self._default

        if self._value is None and self._constant is not None:
            self._value=self._constant

        if self._constant is not None and self._constant!=self._value:
            raise self._constantexception("This field is a constant of %d.  You tried setting it to %d" % (self._constant, self._value))


    def readfrombuffer(self, buf):
        if self._sizeinbytes is None:
            raise SizeNotKnownException()
        self._bufferstartoffset=buf.getcurrentoffset()
        # lsb read
        res=0
        shift=0
        for dummy in range(self._sizeinbytes):
            res|=buf.getnextbyte()<<shift
            shift+=8
        self._value=res
        self._bufferendoffset=buf.getcurrentoffset()
        if self._constant is not None and self._value!=self._constant:
            raise self._constantexception("The value read should be a constant of %d, but was %d instead" % (self._constant, self._value))
         
    def writetobuffer(self, buf):
        if self._sizeinbytes is None:
            raise SizeNotKnownException()
        if self._value is None:
            raise ValueNotSetException()
        
        self._bufferstartoffset=buf.getcurrentoffset()
        # lsb write
        res=self._value
        for dummy in range(self._sizeinbytes):
            buf.appendbyte(res&0xff)
            res>>=8
        self._bufferendoffset=buf.getcurrentoffset()

    def packetsize(self):
        if self._sizeinbytes is None:
            raise SizeNotKnownException()
        return self._sizeinbytes
        
    def getvalue(self):
        """Returns the integer we are"""
        if self._value is None:
            raise ValueNotSetException()
        return self._value

class BOOLlsb(UINTlsb):
    "An Boolean in Least Significant Byte first order"
    def __init__(self, *args, **kwargs):
        """
        A boolean value can be specified in the constructor.

        Keyword arguments are the same a UINTlsb
        """
        super(BOOLlsb, self).__init__(*args, **kwargs)

        if self._ismostderived(BOOLlsb):
            self._update(args,kwargs)

    def _update(self, args, kwargs):
        super(BOOLlsb,self)._update(args,kwargs)
        self._complainaboutunusedargs(BOOLlsb,kwargs)
        self._boolme()

    def _boolme(self):
        if self._value is not None:
            self._value=bool(self._value)

    def readfrombuffer(self, buf):
        UINTlsb.readfrombuffer(self,buf)
        self._boolme()
    
class STRING(BaseProtogenClass):
    "A text string DEPRECATED USE USTRING "
    def __init__(self, *args, **kwargs):
        """
        A string value can be specified to this constructor, or in the value keyword arg.

        @keyword constant: (Optional) A constant value.  All reads must have this value
        @keyword terminator: (Default=0) The string terminator (or None).  If set there will
             always be a terminator when writing.  The terminator is not returned when getting
             the value.
        @keyword pad: (Default=0) The padding byte if fixed length when writing, or stripped off
                       when reading
        @keyword sizeinbytes: (Optional) Set if fixed length.
             If not set, then the terminator will be used to find the end of strings on reading.
             If not set and the terminator is None, then reads will be entire rest of buffer.
        @keyword maxsizeinbytes: (Optional) Max string length.  Used together
             with terminator to limit the max length of the value.
             TODO: Need to add this to USTRING also.
        @keyword default: (Optional) Our default value
        @keyword raiseonunterminatedread: (Default True) raise L{NotTerminatedException} if there is
             no terminator on the value being read in.  terminator must also be set.
        @keyword raiseontruncate: (Default True) raise L{ValueLengthException} if the supplied
             value is too large to fit within sizeinbytes.
        @keyword value: (Optional) Value
        @keyword pascal: (Default False) The string is preceded with one byte giving the length
                         of the string (including terminator if there is one)
        """
        super(STRING, self).__init__(*args, **kwargs)
        
        self._constant=None
        self._terminator=0
        self._pad=0
        self._sizeinbytes=None
        self._default=None
        self._raiseonunterminatedread=True
        self._raiseontruncate=True
        self._value=None
        self._pascal=False
        self._maxsizeinbytes=None

        if self._ismostderived(STRING):
            self._update(args,kwargs)

    def _update(self, args, kwargs):
        super(STRING,self)._update(args, kwargs)

        self._consumekw(kwargs, ("constant", "terminator", "pad", "pascal",
        "sizeinbytes", "default", "raiseonunterminatedread", "value",
                                 "raiseontruncate", "maxsizeinbytes"))
        self._complainaboutunusedargs(STRING,kwargs)

        # Set our value if one was specified
        if len(args)==0:
            pass
        elif len(args)==1:
            self._value=common.forceascii(args[0])
            if self._constant is not None and self._constant!=self._value:
                raise ValueException("This field is a constant of '%s'.  You tried setting it to '%s'" % (self._constant, self._value))
        else:
            raise TypeError("Unexpected arguments "+`args`)
        if self._value is None and self._default is not None:
            self._value=self._default

        if self._value is not None:
            self._value=str(self._value) # no unicode here!
            l=len(self._value)
            if self._sizeinbytes is not None:
                if self._terminator is not None:
                    l+=1
                if l>self._sizeinbytes:
                    if self._raiseontruncate:
                        raise ValueLengthException(l, self._sizeinbytes)
                    
                    self._value=self._value[:self._sizeinbytes]
                    if len(self._value) and self._terminator is not None:
                        self._value=self._value[:-1]
            elif self._maxsizeinbytes is not None:
                if l>self._maxsizeinbytes:
                    if self._raiseontruncate:
                        raise ValueLengthException(l, self._maxsizeinbytes)
                    self._value=self._value[:self._maxsizeinbytes]

    def readfrombuffer(self, buf):
        self._bufferstartoffset=buf.getcurrentoffset()
        
        flush=0
        if self._pascal: 
            if self._sizeinbytes is None:
                self._sizeinbytes=buf.getnextbyte()
            # allow for fixed size pascal strings
            else:
                temp=self._sizeinbytes-1
                self._sizeinbytes=buf.getnextbyte()
                flush=temp-self._sizeinbytes
                if(temp < 0):
                    raise ValueLengthException()

        if self._sizeinbytes is not None:
            # fixed size
            self._value=buf.getnextbytes(self._sizeinbytes)
            if self._terminator is not None:
                # using a terminator
                pos=self._value.find(chr(self._terminator))
                if pos>=0:
                    self._value=self._value[:pos]
                elif self._raiseonunterminatedread:
                    raise NotTerminatedException()
            elif self._pad is not None:
                # else remove any padding
                while len(self._value) and self._value[-1]==chr(self._pad):
                    self._value=self._value[:-1]
        else:
            if self._terminator is None:
                # read in entire rest of packet
                self._value=buf.getremainingbytes()
            else:
                # read up to terminator
                self._value=""
                while buf.hasmore():
                    self._value+=chr(buf.getnextbyte())
                    if self._value[-1]==chr(self._terminator):
                        break
                if self._value[-1]!=chr(self._terminator):
                    if self._raiseonunterminatedread:
                        raise NotTerminatedException()
                else:
                    self._value=self._value[:-1]

        if self._maxsizeinbytes is not None:
            self._value=self._value[:self._maxsizeinbytes]

        if self._constant is not None and self._value!=self._constant:
            raise ValueException("The value read was not the constant")

        # for fixed size pascal strings flush the remainder of the buffer
        if(flush):
            buf.getnextbytes(flush)

        self._bufferendoffset=buf.getcurrentoffset()

    def writetobuffer(self, buf):
        if self._value is None:
            raise ValueNotSetException()

        self._bufferstartoffset=buf.getcurrentoffset()
        # calculate length
        l=len(self._value)
        if self._terminator is not None:
            l+=1
        if self._pascal:
            buf.appendbyte(l)
            l+=1
        buf.appendbytes(self._value)
        if self._terminator is not None:
            buf.appendbyte(self._terminator)
        if self._sizeinbytes is not None:
            if l<self._sizeinbytes:
                buf.appendbytes(chr(self._pad)*(self._sizeinbytes-l))

        self._bufferendoffset=buf.getcurrentoffset()

    def packetsize(self):
        if self._sizeinbytes is not None:
            return self._sizeinbytes

        if self._value is None:
            raise ValueNotSetException()

        l=len(self._value)
        if self._terminator is not None:
            l+=1
        if self._pascal:
            l+=1
        return l

    def getvalue(self):
        """Returns the string we are"""
        if self._value is None:
            raise ValueNotSetException()
        return self._value


class USTRING(BaseProtogenClass):
    "A text string that supports configurable encodings"
    def __init__(self, *args, **kwargs):
        """
        A string value can be specified to this constructor, or in the value keyword arg.

        @keyword constant: (Optional) A constant value.  All reads must have this value
        @keyword terminator: (Default=0) The string terminator (or None).  If set there will
             always be a terminator when writing.  The terminator is not returned when getting
             the value.
        @keyword terminator_length: (Default=1) (min:1, max:4)The length of the string terminator. 
             This keyword is not used if the terminator is None. Multi-byte terminators are treated 
             as LSB when read from the phone.
        @keyword pad: (Default=0) The padding byte if fixed length when writing, or stripped off
                       when reading
        @keyword sizeinbytes: (Optional) Set if fixed length.
             If not set, then the terminator will be used to find the end of strings on reading.
             If not set and the terminator is None, then reads will be entire rest of buffer.
        @keyword maxsizeinbytes: (Optional) Max string length.  Used together
             with terminator to limit the max length of the value.
        @keyword default: (Optional) Our default value
        @keyword raiseonunterminatedread: (Default True) raise L{NotTerminatedException} if there is
             no terminator on the value being read in. Terminator must also be set.
        @keyword raiseontruncate: (Default True) raise L{ValueLengthException} if the supplied
             value is too large to fit within sizeinbytes.
        @keyword value: (Optional) Value
        @keyword pascal: (Default False) The string is preceded with one byte giving the length
                         of the string (including terminator if there is one)
        @keyword encoding: (Default 'ascii') The charset to use when reading/writing to a buffer
        @keyword read_encoding: (Default keyword:encoding) The charset to use when reading from a buffer
        @keyword write_encoding: (Default keyword:encoding) The charset to use when writing to a buffer
        """
        super(USTRING, self).__init__(*args, **kwargs)
        
        self._constant=None
        self._terminator=0
        self._pad=0
        self._sizeinbytes=None
        self._default=None
        self._raiseonunterminatedread=True
        self._raiseontruncate=True
        self._value=None
        self._pascal=False
        self._maxsizeinbytes=None
        self._encoding='ascii'
        self._read_encoding=None
        self._write_encoding=None
        self._terminator_length=1

        if self._ismostderived(USTRING):
            self._update(args,kwargs)

    def _update(self, args, kwargs):
        super(USTRING,self)._update(args, kwargs)

        self._consumekw(kwargs, ("constant", "terminator", "pad", "pascal",
        "sizeinbytes", "default", "raiseonunterminatedread", "value", "raiseontruncate",
        "encoding", "read_encoding", "write_encoding", "maxsizeinbytes"))
        self._complainaboutunusedargs(USTRING,kwargs)
        if self._read_encoding==None:
            self._read_encoding=self._encoding
        if self._write_encoding==None:
            self._write_encoding=self._encoding
        if self._terminator_length < 1 or self._terminator_length > 4:
            raise ValueException("Terminator length outside allowed range of 1-4.  You tried setting it to %d" % self._terminator_length)
        
        # Set our value if one was specified
        if len(args)==0:
            pass
        elif len(args)==1:
            self._value=args[0]
        else:
            raise TypeError("Unexpected arguments "+`args`)
        if self._value is None and self._default is not None:
            self._value=self._default

        if self._value is not None:
            # convert _value to unicode, non-string objects convert to string representation
            # allows strings to be initialised with integers etc.
            if not isinstance(self._value, (str, unicode)):
                # convert numbers into strings
                temp=str(self._value)
                self._value=unicode(temp, 'ascii', 'replace')
            # we should only receive unicode strings from bitpim, but...
            elif not isinstance(self._value, unicode):
                # there is a bug, the docs say this should work on unicode strings but it does not
                self._value=unicode(self._value, 'ascii', 'replace')

        if self._constant is not None and self._constant!=self._value:
            raise ValueException("This field is a constant of '%s'.  You tried setting it to '%s'" % (self._constant, self._value))
        # test that we can convert the string, also needed for "sizeinbytes" 
        try:
            test=self.convert_for_write()
        except UnicodeEncodeError:
            raise common.PhoneStringEncodeException(self._value, uni_string_codec) 

        if self._value is not None:
            max_size=None
            if self._sizeinbytes is not None:
                max_size=self._sizeinbytes
            elif self._maxsizeinbytes is not None:
                max_size=self._maxsizeinbytes
            if max_size is not None:
                l=len(test)
                if self._terminator is not None:
                    l+=self._terminator_length
                if l>max_size:
                    if self._raiseontruncate:
                        raise ValueLengthException(l, self._sizeinbytes)
                    # truncate, but the number of bytes might be larger than the number of
                    # unicode characters depending on conversion
                    self._value=self._value[:max_size]
                    term_len=0
                    if self._terminator!=None:
                        term_len=self._terminator_length
                    # adjust for terminator and multibyte characters
                    while (len(self.convert_for_write())+term_len)>max_size:
                        self._value=self._value[:-1]

    def readfrombuffer(self, buf):
        self._bufferstartoffset=buf.getcurrentoffset()
        
        flush=0
        _value=''
        if self._pascal: 
            if self._sizeinbytes is None:
                self._sizeinbytes=buf.getnextbyte()
            # allow for fixed size pascal strings
            else:
                temp=self._sizeinbytes-1
                self._sizeinbytes=buf.getnextbyte()
                flush=temp-self._sizeinbytes
                if(temp < 0):
                    raise ValueLengthException()

        if self._sizeinbytes is not None:
            # fixed size
            _value=buf.getnextbytes(self._sizeinbytes)
            if self._terminator is not None:
                # using a terminator
                pos=-1
                for i in range(0, self._sizeinbytes, self._terminator_length):
                    term=0
                    for j in range(self._terminator_length):
                        term+=ord(_value[i+j])<<(j*8)
                    if term==self._terminator:
                        pos=i
                        break
                if pos>=0:
                    _value=_value[:pos]
                elif self._raiseonunterminatedread:
                    raise NotTerminatedException()
            elif self._pad is not None:
                # else remove any padding
                while len(_value) and _value[-1]==chr(self._pad):
                    _value=_value[:-1]
        else:
            if self._terminator is None:
                # read in entire rest of packet
                _value=buf.getremainingbytes()
            else:
                # read up to terminator
                _value=""
                count=0
                term=0
                while buf.hasmore():
                    _value+=chr(buf.getnextbyte())
                    count+=1
                    if (count % self._terminator_length)==0:
                        term=0
                        # see if we have a terminator
                        for j in range(self._terminator_length):
                            term=(term<<8)+ord(_value[count-1-j])
                        if term==self._terminator:
                            break
                if term!=self._terminator and self._raiseonunterminatedread:
                    raise NotTerminatedException()
                else:
                    _value=_value[:-1]

        if self._maxsizeinbytes is not None:
            self._value=self._value[:self._maxsizeinbytes]

        if self._constant is not None and _value!=self._constant:
            raise ValueException("The value read was not the constant")

        # for fixed size pascal strings flush the remainder of the buffer
        if(flush):
            buf.getnextbytes(flush)

        self._bufferendoffset=buf.getcurrentoffset()

        # we will convert to unicode when the value is retrieved.
        # this prevents garbage that will never be used from causing an
        # exception. e.g. A deleted calendar record on some LG phones may contain
        # all f's, this causes an exception if we try to convert to unicode.
        self._value=_value

    def writetobuffer(self, buf):
        if self._value is None:
            raise ValueNotSetException()
        self._bufferstartoffset=buf.getcurrentoffset()
        # calculate length
        temp_str=self.convert_for_write()
        l=len(temp_str)
        if self._terminator is not None:
            l+=1
        if self._pascal:
            buf.appendbyte(l)
            l+=1
        buf.appendbytes(temp_str)
        term=self._terminator
        if self._terminator is not None:
            for j in range(self._terminator_length):
                buf.appendbyte((term & 0xFF))
                term=term>>8
        if self._sizeinbytes is not None:
            if l<self._sizeinbytes:
                buf.appendbytes(chr(self._pad)*(self._sizeinbytes-l))
        self._bufferendoffset=buf.getcurrentoffset()

    def convert_for_write(self):
        # if we are not in unicode this means that we contain data read from the phone
        # in this case just return this back, there is no need to convert twice.
        if not isinstance(self._value, unicode):
            return self._value
        try:
            temp_str=common.encode_with_degrade(self._value, self._write_encoding)
        except UnicodeError:
            raise common.PhoneStringEncodeException(self._value, self._write_encoding) 
        return temp_str

    def packetsize(self):
        if self._sizeinbytes is not None:
            return self._sizeinbytes

        if self._value is None:
            raise ValueNotSetException()

        l=len(self.convert_for_write())
        if self._terminator is not None:
            l+=1

        return l

    def getvalue(self):
        """Returns the string we are"""
        if self._value is None:
            raise ValueNotSetException()
        # convert to unicode if we are not already
        if not isinstance(self._value, unicode):
            try:
                self._value=self._value.decode(self._read_encoding)
            except UnicodeDecodeError:
                # this means the codec is set wrong for this phone !!
                raise common.PhoneStringDecodeException(self._value, self._read_encoding) 
        return self._value

class SEVENBITSTRING(BaseProtogenClass):
    """A text string where ASCII characters are stored as packed 7 bit characters.  This is
    typically used in SMS messages."""
    def __init__(self, *args, **kwargs):
        """
        @keyword terminator: (Default=\x00) The termination character
        @keyword sizeinbytes: Amount of space the string sits in
        """
        super(SEVENBITSTRING, self).__init__(*args, **kwargs)
        self._value=None
        self._terminator='\x00'
        self._sizeinbytes=None
        if self._ismostderived(SEVENBITSTRING):
            self._update(args,kwargs)

    def _update(self, args, kwargs):
        super(SEVENBITSTRING,self)._update(args, kwargs)

        self._consumekw(kwargs, ("terminator", "value", "sizeinbytes"))
        self._complainaboutunusedargs(SEVENBITSTRING, kwargs)
        if len(args):
            raise TypeError("Unexpected arguments "+`args`)
        if self._sizeinbytes is None:
            raise ValueException("You must specify a size in bytes")

    def readfrombuffer(self, buf):
        self._bufferstartoffset=buf.getcurrentoffset()
        bytes=buf.getnextbytes(self._sizeinbytes)
        self._value=common.decodecharacterbits(bytes, bitsperchar=7, charconv=chr, terminator=self._terminator)
        self._bufferendoffset=buf.getcurrentoffset()

    def getvalue(self):
        """Returns the string we are"""
        if self._value is None:
            raise ValueNotSetException()
        return self._value        
        
class SMSDATE(BaseProtogenClass):
    """A date as used in SMS messages.  It is six bytes long with the
    bytes being year month day hour minute second.  From stuff on the
    web, it appears GSM phones swap each nybble."""
    def __init__(self, *args, **kwargs):
        """@keyword sizeinbytes: (optional) Must be six"""
        super(SMSDATE, self).__init__(*args, **kwargs)
        self._values=None
        self._sizeinbytes=6
        if self._ismostderived(SMSDATE):
            self._update(args, kwargs)

    def _update(self, args, kwargs):
        super(SMSDATE, self)._update(args, kwargs)
        self._consumekw(kwargs, ("sizeinbytes",))
        self._complainaboutunusedargs(SMSDATE, kwargs)
        if len(args):
            raise TypeError("Unexpected arguments "+`args`)
        if self._sizeinbytes != 6:
            raise ValueNotSetException("You can only specify 6 as the size in bytes")

    def readfrombuffer(self, buf):
        self._bufferstartoffset=buf.getcurrentoffset()
        year=buf.getnextbyte()
        if year<0:
            year+=1900
        else:
            year+=2000
        month=buf.getnextbyte()
        day=buf.getnextbyte()
        hour=buf.getnextbyte()
        minute=buf.getnextbyte()
        second=buf.getnextbyte()
        self._value=year,month,day, hour,minute,second
        self._bufferendoffset=buf.getcurrentoffset()

    def getvalue(self):
         """Returns the  ISO date time string we are"""
         if self._value is None:
             raise ValueNotSetException()
         return "%d%02d%02dT%02d%02d%02d" % self._value


class CSVSTRING(BaseProtogenClass):
    """A text string enclosed in quotes, with a way to escape quotes that a supposed
    to be part of the string.  Typical of Samsung phones."""
    def __init__(self, *args, **kwargs):
        """
        A string value can be specified to this constructor, or in the value keyword arg.

        @keyword constant: (Optional) A constant value.  All reads must have this value
        @keyword terminator: (Default=,) The string terminator (or None).  If set there will
             always be a terminator when writing.  The terminator is not returned when getting
             the value.
        @keyword quotechar: (Default=Double Quote) Quote character that surrounds string
        @keyword readescape: (Default=True) Interpret PPP escape char (0x7d)
        @keywors writeescape: (Default=False) Escape quotechar.  If false, drop quotechar in string.
        @keyword maxsizeinbytes: (Optional) On writing, truncate strings longer than this (length is before
                       any escaping and quoting
        @keyword default: (Optional) Our default value
        @keyword raiseonunterminatedread: (Default True) raise L{NotTerminatedException} if there is
             no terminator on the value being read in.  terminator must also be set.
        @keyword raiseontruncate: (Default True) raise L{ValueLengthException} if the supplied
             value is too large to fit within sizeinbytes.
        @keyword raiseonmissingquotes: (Default True) raise L{MissingQuotesException} if the string does
             not have quote characters around it
        @keyword value: (Optional) Value
        @keyword invalidchars: (Default=quotechar) A string containing invalid
             characters which would be removed before writing to buffer.
        @keyword encoding: (Default=None) If specified Unicode charset.
        @keyword raiseonunicodeerror: (Default=True) raise exception if fail
        to encode/decode Unicode.
        """
        super(CSVSTRING, self).__init__(*args, **kwargs)
        
        self._constant=None
        self._terminator=ord(',')
        self._quotechar=ord('"')
        self._readescape=True
        self._writeescape=False
        self._maxsizeinbytes=None
        self._default=None
        self._raiseonunterminatedread=True
        self._raiseontruncate=True
        self._raiseonmissingquotes=True
        self._invalidchars=chr(self._quotechar)
        self._value=None
        self._encoding=None
        self._raiseonunicodeerror=True

        if self._ismostderived(CSVSTRING):
            self._update(args,kwargs)

    def _update(self, args, kwargs):
        super(CSVSTRING,self)._update(args, kwargs)

        self._consumekw(kwargs, ("constant", "terminator", "quotechar", "readescape",
                                 "writeescape", "maxsizeinbytes", "default",
                                 "raiseonunterminatedread", "value",
                                 "raiseontruncate", "raiseonmissingquotes",
                                 "invalidchars"))
        self._complainaboutunusedargs(CSVSTRING,kwargs)

        # Set our value if one was specified
        if len(args)==0:
            pass
        elif len(args)==1:
            self._value=common.forceascii(args[0])
            if self._constant is not None and self._constant!=self._value:
                raise ValueException("This field is a constant of '%s'.  You tried setting it to '%s'" % (self._constant, self._value))
        else:
            raise TypeError("Unexpected arguments "+`args`)
        if self._value is None and self._default is not None:
            self._value=self._default

        if self._value is not None:
            self._value=str(self._value) # no unicode here!
            if self._invalidchars:
                self._value=re.sub(r'[%s]'%self._invalidchars, r'', self._value)
            if self._maxsizeinbytes is not None:
                l=len(self._value)
                if l>self._maxsizeinbytes:
                    if self._raiseontruncate:
                        raise ValueLengthException(l, self._maxsizeinbytes)
                    
                    self._value=self._value[:self._maxsizeinbytes]

    def readfrombuffer(self, buf):
        self._bufferstartoffset=buf.getcurrentoffset()

        # First character better be a terminator
        # Raise on, first char not quote, trailing quote never found, character after last quote
        # not , or EOL.  Will need an option to not raise if initial quote not found

        if self._terminator is None:
            # read in entire rest of packet
            # Ignore maxsizeinbytes
            # Check for leading and trailing quotes later
            self._value=buf.getremainingbytes()
        else:
            # Possibly quoted string.  If first character is not a quote, read until
            # terminator or end of line.  Exception will be thrown after reading if
            # the string was not quoted and it was supposed to be.
            self._value=chr(buf.getnextbyte())
            if self._value == ',':
                self._value = ''
            else:
                inquotes=False
                if self._quotechar is not None:
                    if self._value[0]==chr(self._quotechar):
                        inquotes=True
                while buf.hasmore():
                    self._value+=chr(buf.getnextbyte())
                    if inquotes:
                        if self._value[-1]==chr(self._quotechar):
                            inquotes=False
                    else:
                        if self._value[-1]==chr(self._terminator):
                            break
                if self._value[-1]==self._terminator:
                    if self._raiseonunterminatedread:
                        raise NotTerminatedException()
                else:
                    self._value=self._value[:-1]

        if self._quotechar is not None and self._value:
            if self._value[0]==chr(self._quotechar) and self._value[-1]==chr(self._quotechar):
                self._value=self._value[1:-1]
            else:
                raise MissingQuotesException()

        if self._readescape:
            self._value=common.pppunescape(self._value)
            
        if self._constant is not None and self._value!=self._constant:
            raise ValueException("The value read was not the constant")

        self._bufferendoffset=buf.getcurrentoffset()

    def writetobuffer(self, buf):
        # Need to raise exception if string exceeds maxsizeinbytes
        if self._value is None:
            raise ValueNotSetException()

        if self._encoding and isinstance(self._value, unicode):
            try:
                _value=common.encode_with_degrade(self._value,
                                                  self._encoding)
            except UnicodeError:
                if self._raiseonunicodeerror:
                    raise common.PhoneStringEncodeException(self._value,
                                                            self._encoding)
                else:
                    # failed to encode, clear it out
                    _value=''
        else:
            _value=self._value

        self._bufferstartoffset=buf.getcurrentoffset()

        if self._quotechar is not None:
            buf.appendbyte(self._quotechar)
        buf.appendbytes(_value)
        if self._quotechar is not None:
            buf.appendbyte(self._quotechar)
        if self._terminator is not None:
            buf.appendbyte(self._terminator)

        self._bufferendoffset=buf.getcurrentoffset()

    def packetsize(self):
        if self._sizeinbytes is not None:
            return self._sizeinbytes

        if self._value is None:
            raise ValueNotSetException()

        l=len(self._value)
        if self._terminator is not None:
            l+=1

        return l

    def getvalue(self):
        """Returns the string we are"""
        if self._value is None:
            raise ValueNotSetException()
        # convert to unicode if we are not already
        if self._encoding and not isinstance(self._value, unicode):
            try:
                if self._raiseonunicodeerror:
                    self._value=self._value.decode(self._encoding)
                else:
                    self._value=self._value.decode(self._encoding, 'ignore')
            except UnicodeDecodeError:
                # this means the codec is set wrong for this phone !!
                raise common.PhoneStringDecodeException(self._value, self._encoding) 
        return self._value

class CSVINT(CSVSTRING):
    """Integers in CSV lines"""
    def __init__(self, *args, **kwargs):
        super(CSVINT,self).__init__(*args, **kwargs)

        self._quotechar=None

        if self._ismostderived(CSVINT):
            self._update(args,kwargs)

    def _update(self, args, kwargs):
        for k in 'constant', 'default', 'value':
            if kwargs.has_key(k):
                kwargs[k]=str(kwargs[k])
        if len(args)==0:
            pass
        elif len(args)==1:
            args=(str(args[0]),)
        else:
            raise TypeError("expected integer as arg")
            
        super(CSVINT,self)._update(args,kwargs)
        self._complainaboutunusedargs(CSVINT,kwargs)
        
    def getvalue(self):
        """Convert the string into an integer

        @rtype: integer
        """

        # Will probably want an flag to allow null strings, converting
        # them to a default value
        
        val=super(CSVINT,self).getvalue()
        try:
            ival=int(val)
        except:
            try:
                ival=int(self._default)
            except:
                raise ValueException("The field '%s' is not an integer" % (val))
        return ival

class CSVDATE(CSVSTRING):
    """Dates in CSV lines"""
    def __init__(self, *args, **kwargs):
        super(CSVDATE,self).__init__(*args, **kwargs)

        self._valuedate=(0,0,0) # Year month day

        self._quotechar=None
        
        if self._ismostderived(CSVDATE):
            self._update(args,kwargs)

    def _update(self, args, kwargs):
        for k in 'constant', 'default', 'value':
            if kwargs.has_key(k):
                kwargs[k]=self._converttostring(kwargs[k])
        if len(args)==0:
            pass
        elif len(args)==1:
            args=(self._converttostring(args[0]),)
        else:
            raise TypeError("expected (year,month,day) as arg")

        super(CSVDATE,self)._update(args, kwargs) # we want the args
        self._complainaboutunusedargs(CSVDATE,kwargs)

    def getvalue(self):
        """Unpack the string into the date

        @rtype: tuple
        @return: (year, month, day)
        """

        s=super(CSVDATE,self).getvalue()
        val=s.split("/") # List of of Month, day, year
        if len(val)<2:
            year = 0
            month = 0
            day = 0
        else:
            year=int(val[2])
            month=int(val[0])
            day=int(val[1])
        return (year, month, day)
        
    def _converttostring(self, date):
        if len(date)>=3:
            year,month,day=date[:3]
            if month>0 or day>0 or year>0:
                s='%2.2d/%2.2d/%4.4d'%(month, day, year)
            else:
                s=""
        else:
            s=""
        return s
        

class CSVTIME(CSVSTRING):
    """Timestamp in CSV lines"""
    def __init__(self, *args, **kwargs):
        super(CSVTIME,self).__init__(*args, **kwargs)

        self._valuetime=(0,0,0,0,0,0) # Year month day, hour, minute, second

        self._quotechar=None
        
        if self._ismostderived(CSVTIME):
            self._update(args,kwargs)

    def _update(self, args, kwargs):
        for k in 'constant', 'default', 'value':
            if kwargs.has_key(k):
                kwargs[k]=self._converttostring(kwargs[k])
        if len(args)==0:
            pass
        elif len(args)==1:
            args=(self._converttostring(args[0]),)
        else:
            raise TypeError("expected (year,month,day) as arg")

        super(CSVTIME,self)._update(args, kwargs) # we want the args
        self._complainaboutunusedargs(CSVTIME,kwargs)

    def getvalue(self):
        """Unpack the string into the date

        @rtype: tuple
        @return: (year, month, day)
        """

        s=super(CSVTIME,self).getvalue()
        year=int(s[0:4])
        month=int(s[4:6])
        day=int(s[6:8])
        hour=int(s[9:11])
        minute=int(s[11:13])
        second=int(s[13:15])
        return (year, month, day, hour, minute, second)
        
    def _converttostring(self, time):
        if len(time)>=6:
            year,month,day,hour,minute,second=time[:6]
            s='%4.4d%2.2d%2.2dT%2.2d%2.2d%2.2d'%(year, month, day, hour, minute, second)
        else:
            s=""
        return s
        

class COUNTEDBUFFEREDSTRING(BaseProtogenClass):
    """A string as used on Audiovox.  There is a one byte header saying how long the string
    is, followed by the string in a fixed sized buffer"""
    def __init__(self, *args, **kwargs):
        """
        A string value can be specified to this constructor, or in the value keyword arg.

        @keyword constant: (Optional) A constant value.  All reads must have this value
        @keyword pad: (Default=32 - space) When writing, what to pad the rest of the buffer with
        @keyword default: (Optional) Our default value
        @keyword raiseontruncate: (Default True) raise L{ValueLengthException} if the supplied
             value is too large to fit within the buffer.
        @keyword value: (Optional) Value
        @keyword sizeinbytes: (Mandatory) Size of the buffer, including the count byte
        """
        super(COUNTEDBUFFEREDSTRING,self).__init__(*args, **kwargs)

        self._constant=None
        self._pad=32
        self._sizeinbytes=None
        self._default=None
        self._raiseontruncate=True
        self._value=None

        if self._ismostderived(COUNTEDBUFFEREDSTRING):
            self._update(args, kwargs)

    def _update(self, args, kwargs):
        super(COUNTEDBUFFEREDSTRING,self)._update(args, kwargs)

        self._consumekw(kwargs, ("constant", "pad", "sizeinbytes", "default", "raiseontruncate", "value"))
        self._complainaboutunusedargs(COUNTEDBUFFEREDSTRING,kwargs)
        # Set our value if one was specified
        if len(args)==0:
            pass
        elif len(args)==1:
            self._value=str(args[0])
            if self._constant is not None and self._constant!=self._value:
                raise ValueException("This field is a constant of '%s'.  You tried setting it to '%s'" % (self._constant, self._value))
        else:
            raise TypeError("Unexpected arguments "+`args`)
        if self._value is None and self._default is not None:
            self._value=self._default

        if self._sizeinbytes is None:
            raise ValueException("sizeinbytes must be specified for COUNTEDBUFFEREDSTRING")

        if self._value is not None:
            l=len(self._value)
            if l>self._sizeinbytes-1:
                if self._raiseontruncate:
                    raise ValueLengthException(l, self._sizeinbytes-1)
                    
                self._value=self._value[:self._sizeinbytes-1]

    def readfrombuffer(self, buf):
        assert self._sizeinbytes is not None
        self._bufferstartoffset=buf.getcurrentoffset()

        strlen=buf.getnextbyte()
        if strlen>self._sizeinbytes-1:
            raise ValueException("counter specifies size of %d which is greater than remaining stringbuffer size of %d!" % (strlen, self._sizeinbytes-1))
        self._value=buf.getnextbytes(self._sizeinbytes-1) # -1 due to counter byte
        self._value=self._value[:strlen]
        if self._constant is not None and self._value!=self._constant:
            raise ValueException("The value read was not the constant")

        self._bufferendoffset=buf.getcurrentoffset()

    def writetobuffer(self, buf):
        assert self._sizeinbytes is not None
        if self._value is None:
            raise ValueNotSetException()

        self._bufferstartoffset=buf.getcurrentoffset()
        buf.appendbyte(len(self._value))
        buf.appendbytes(self._value)
        if len(self._value)+1<self._sizeinbytes:
            buf.appendbytes(chr(self._pad)*(self._sizeinbytes-1-len(self._value)))

        self._bufferendoffset=buf.getcurrentoffset()

    def packetsize(self):
        assert self._sizeinbytes is not None
        return self._sizeinbytes

    def getvalue(self):
        """Returns the string we are"""
        if self._value is None:
            raise ValueNotSetException()
        return self._value
            
class DATA(BaseProtogenClass):
    "A block of bytes"
    def __init__(self, *args, **kwargs):
        """
        A data value can be specified to this constructor or in the value keyword arg

        @keyword constant: (Optional) A constant value.  All reads must have this value
        @keyword pad: (Default=0) The padding byte if fixed length when writing and the
             value isn't long enough
        @keyword sizeinbytes: (Optional) Set if fixed length.
             If not set, then the rest of the packet will be consumed on reads.
        @keyword default: (Optional) Our default value
        @keyword raiseonwrongsize: (Default True) raise L{ValueLengthException} if the supplied
             value is too large to fit within sizeinbytes.
        """
        super(DATA, self).__init__(*args, **kwargs)
        
        self._constant=None
        self._pad=0
        self._sizeinbytes=None
        self._default=None
        self._raiseonwrongsize=True
        self._value=None

        if self._ismostderived(DATA):
            self._update(args,kwargs)

    def _update(self, args, kwargs):
        super(DATA,self)._update(args, kwargs)

        self._consumekw(kwargs, ("constant", "pad", "sizeinbytes", "default", "raiseonwrongsize", "value"))
        self._complainaboutunusedargs(DATA,kwargs)

        # Set our value if one was specified
        if len(args)==0:
            pass
        elif len(args)==1:
            self._value=args[0]
            if self._constant is not None and self._constant!=self._value:
                raise ValueException("This field is a constant and you set it to a different value")
        else:
            raise TypeError("Unexpected arguments "+`args`)
        if self._value is None and self._default is not None:
            self._value=self._default

        if self._value is not None:
            if self._sizeinbytes is not None:
                l=len(self._value)
                if l<self._sizeinbytes:
                    if self._pad is not None:
                        self._value+=chr(self._pad)*(self._sizeinbytes-l)

                l=len(self._value)

                if l!=self._sizeinbytes:
                    if self._raiseonwrongsize:
                        raise ValueLengthException(l, self._sizeinbytes)
                    else:
                        self._value=self._value[:self._sizeinbytes]


    def readfrombuffer(self, buf):
        self._bufferstartoffset=buf.getcurrentoffset()

        if self._sizeinbytes is not None:
            # fixed size
            self._value=buf.getnextbytes(self._sizeinbytes)
        else:
            # read in entire rest of packet
            self._value=buf.getremainingbytes()

        if self._constant is not None and self._value!=self._constant:
            raise ValueException("The value read was not the constant")
        self._bufferendoffset=buf.getcurrentoffset()

    def writetobuffer(self, buf):
        if self._value is None:
            raise ValueNotSetException()
                
        self._bufferstartoffset=buf.getcurrentoffset()
        buf.appendbytes(self._value)
        self._bufferendoffset=buf.getcurrentoffset()

    def packetsize(self):
        if self._sizeinbytes is not None:
            return self._sizeinbytes

        if self._value is None:
            raise ValueNotSetException()

        l=len(self._value)

        return l

    def getvalue(self):
        """Returns the bytes we are"""
        if self._value is None:
            raise ValueNotSetException()
        return self._value


class UNKNOWN(DATA):
    "A block of bytes whose purpose we don't know"

    def __init__(self, *args, **kwargs):
        """
        Same arguments as L{DATA.__init__}.  We default to a block
        of pad chars (usually \x00)
        """
        dict={'pad':0 , 'default': ""}
        dict.update(kwargs)
        super(UNKNOWN,self).__init__(*args, **dict)

        if self._ismostderived(UNKNOWN):
            self._update(args,dict)

    def _update(self, args, kwargs):
        super(UNKNOWN,self)._update(args, kwargs)
        self._complainaboutunusedargs(UNKNOWN,kwargs)

        # Was a value specified?
        if len(args)==1:
            self._value=args[0]
        elif len(args)>1:
            raise TypeError("Unexpected arguments "+`args`)

class LIST(BaseProtogenClass):
    """A list of items

    You can generally treat this class as though it is a list.  Note that some
    list like methods haven't been implemented (there are so darn many!)  If you
    are missing one you want to use, please add it to this class.
    """

    def __init__(self, *args, **kwargs):
        """
        You can pass objects to start the list with, or to the value keyword arg

        @keyword createdefault:  (Default False) Creates default members of the list if enough
            were not supplied before writing.
        @keyword length:  (Optional) How many items there are in the list
        @keyword raiseonbadlength: (Default True) raises L{ValueLengthException} if there are
            the wrong number of items in the list.  Note that this checking is only done
            when writing or reading from a buffer.  length must be set for this to have any
            effect.  If you have createdefault set then having less than length elements will
            not cause the exception.
        @keyword elementclass: (Mandatory) The class of each element
        @keyword elementinitkwargs: (Optional) KWargs for the constructor of each element
        @keyword value: (Optional) Value
        """
        self._thelist=[]
        super(LIST, self).__init__(*args, **kwargs)
        self._createdefault=False
        self._length=None
        self._raiseonbadlength=True
        self._elementclass=None
        self._elementinitkwargs={}

        if self._ismostderived(LIST):
            self._update(args,kwargs)

    def _update(self, args, kwargs):
        super(LIST,self)._update(args, kwargs)
        self._consumekw(kwargs, ("createdefault","length","raiseonbadlength","elementclass","elementinitkwargs"))
        if kwargs.has_key("value"):
            self._thelist=list(kwargs['value'])
            del kwargs['value']
            
        self._complainaboutunusedargs(LIST,kwargs)

        if self._elementclass is None:
            raise TypeError("elementclass argument was not supplied")
        
        if len(args):
            self.extend(args)

    def readfrombuffer(self,buf):
        self._bufferstartoffset=buf.getcurrentoffset()
        # delete all existing items
        self._thelist=[]
            
        if self._length is None:
            # read rest of buffer
            while buf.hasmore():
                x=self._makeitem()
                x.readfrombuffer(buf)
                self._thelist.append(x)
        else:
            for dummy in range(self._length):
                # read specified number of items
                x=self._makeitem()
                x.readfrombuffer(buf)
                self._thelist.append(x)

        self._bufferendoffset=buf.getcurrentoffset()

    def writetobuffer(self, buf):
        self._bufferstartoffset=buf.getcurrentoffset()

        self._ensurelength()
        for i in self:
            i.writetobuffer(buf)

        self._bufferendoffset=buf.getcurrentoffset()

    def packetsize(self):
        self._ensurelength()
        sz=0
        for item in self:
            sz+=item.packetsize()
        return sz

    def iscontainer(self):
        return True

    def containerelements(self):
        self._ensurelength()
        for i,v in enumerate(self._thelist):
            yield "["+`i`+"]",v,None
        

    # Provide various list methods.  I initially tried just subclassing list,
    # but it was impossible to tell which methods needed to be reimplemented.
    # I was concerned about double conversions.
    # For example if my append turned the item into elementclass
    # and then the builtin list append called setitem to do the append, which I
    # also override so it then converts the elementclass into another
    # elementclass.
    def append(self, item):
        self._thelist.append(self._makeitem(item))

    def extend(self, items):
        self._thelist.extend(map(self._makeitem, items))

    def insert(self, index, item):
        self._thelist.insert(index, self._makeitem(item))

    def __getitem__(self, index):
        return self._thelist[index]

    def __iter__(self):
        try:
            return self._thelist.__iter__()
        except:
            return self.__fallbackiter()

    def __fallbackiter(self):
        # used for Python 2.2 which doesn't have list.__iter__
        for item in self._thelist:
            yield item

    def __len__(self):
        return self._thelist.__len__()

    def __setitem__(self, index, value):
        self._thelist.__setitem__(index, self._makeitem(value))

    def __delitem__(self, index):
        self._thelist.__delitem__(index)
        

    def _makeitem(self, *args, **kwargs):
        "Creates a child element"
        # if already of the type, then return it
        if len(args)==1 and isinstance(args[0], self._elementclass):
            return args[0]
        d={}
        d.update(self._elementinitkwargs)
        d.update(kwargs)
        return self._elementclass(*args, **d)

    def _ensurelength(self):
        "Ensures we are the correct length"
        if self._createdefault and self._length is not None and len(self._thelist)<self._length:
            while len(self._thelist)<self._length:
                x=self._makeitem()
                self._thelist.append(x)
            return
        if self._length is not None and self._raiseonbadlength and len(self._thelist)!=self._length:
            raise ValueLengthException(len(self), self._length)

class buffer:
    "This is used for reading and writing byte data"
    def __init__(self, data=None):
        "Call with data to read from it, or with None to write to it"
        if data is not None:
            self._data=data
        else:
            self._buffer=cStringIO.StringIO()

        self._offset=0

    def getcurrentoffset(self):
        "Returns distance into data we are"
        return self._offset

    def peeknextbyte(self, howmuch=0):
        "Returns value of next byte, but doesn't advance position"
        if self._offset+howmuch>=len(self._data):
            return None
        return ord(self._data[self._offset+howmuch]) 

    def getnextbyte(self):
        "Returns next byte"
        if self._offset>=len(self._data):
            raise IndexError("trying to read one byte beyond end of "+`len(self._data)`+" byte buffer")
        res=ord(self._data[self._offset])
        self._offset+=1
        return res

    def getnextbytes(self, howmany):
        "Returns howmany bytes"
        assert howmany>=0
        if self._offset+howmany>len(self._data):
            raise IndexError("Trying to read "+`howmany`+" bytes starting at "+`self._offset`+" which will go beyond end of "+`len(self._data)`+" byte buffer")
        res=self._data[self._offset:self._offset+howmany]
        self._offset+=howmany
        return res

    def peeknextbytes(self, howmany):
        if self._offset+howmany>len(self._data):
            return None
        return self._data[self._offset:self._offset+howmany]

    def getremainingbytes(self):
        "Returns rest of buffer"
        sz=len(self._data)-self._offset
        return self.getnextbytes(sz)

    def hasmore(self):
        "Is there any data left?"
        return self._offset<len(self._data)

    def howmuchmore(self):
        "Returns how many bytes left"
        return len(self._data)-self._offset

    def appendbyte(self, val):
        """Appends byte to data.
        @param val: a number 0 <= val <=255
        """
        assert val>=0 and val<=255
        self._buffer.write(chr(val))
        self._offset+=1
        assert self._offset==len(self._buffer.getvalue())

    def appendbytes(self, bytes):
        "Adds bytes to end"
        self._buffer.write(bytes)
        self._offset+=len(bytes)
        assert self._offset==len(self._buffer.getvalue())

    def getvalue(self):
        "Returns the buffer being built"
        return self._buffer.getvalue()

    def getdata(self):
        "Returns the data passed in"
        return self._data