38
38
39
39
import struct
40
40
import logging
41
+ import sys
41
42
42
43
log = logging .getLogger (__name__ )
43
44
logging .basicConfig (level = logging .INFO )
@@ -71,6 +72,15 @@ def __str__(self):
71
72
72
73
@classmethod
73
74
def parse (cls , fp ):
75
+ """
76
+ >>> from io import BytesIO
77
+ >>> fp = BytesIO(b'\\ x04test\\ x86')
78
+ >>> sar = ShortAttributeRecord.parse(fp)
79
+ >>> str(sar.name)
80
+ 'test'
81
+ >>> sar.value
82
+ <TrueTextRecord(type=0x86)>
83
+ """
74
84
name = Utf8String .parse (fp ).value
75
85
type = struct .unpack (b'<B' , fp .read (1 ))[0 ]
76
86
value = Record .records [type ].parse (fp )
@@ -100,13 +110,24 @@ def to_bytes(self):
100
110
101
111
def __str__ (self ):
102
112
return '%s:%s="%s"' % (self .prefix , self .name , str (self .value ))
103
-
113
+
104
114
@classmethod
105
115
def parse (cls , fp ):
116
+ """
117
+ >>> from io import BytesIO
118
+ >>> fp = BytesIO(b'\\ x01x\\ x04test\\ x86')
119
+ >>> ar = AttributeRecord.parse(fp)
120
+ >>> str(ar.prefix)
121
+ 'x'
122
+ >>> str(ar.name)
123
+ 'test'
124
+ >>> ar.value
125
+ <TrueTextRecord(type=0x86)>
126
+ """
106
127
prefix = Utf8String .parse (fp ).value
107
- name = Utf8String .parse (fp ).value
108
- type = struct .unpack (b'<B' , fp .read (1 ))[0 ]
109
- value = Record .records [type ].parse (fp )
128
+ name = Utf8String .parse (fp ).value
129
+ type = struct .unpack (b'<B' , fp .read (1 ))[0 ]
130
+ value = Record .records [type ].parse (fp )
110
131
111
132
return cls (prefix , name , value )
112
133
@@ -131,12 +152,23 @@ def to_bytes(self):
131
152
132
153
def __str__ (self ):
133
154
return '%s="%s"' % (dictionary [self .index ], str (self .value ))
134
-
155
+
135
156
@classmethod
136
157
def parse (cls , fp ):
158
+ """
159
+ >>> from io import BytesIO
160
+ >>> fp = BytesIO(b'\\ x0c\\ x86')
161
+ >>> sdar = ShortDictionaryAttributeRecord.parse(fp)
162
+ >>> sdar.index
163
+ 12
164
+ >>> sdar.value
165
+ <TrueTextRecord(type=0x86)>
166
+ >>> str(sdar)
167
+ 'To="true"'
168
+ """
137
169
index = MultiByteInt31 .parse (fp ).value
138
- type = struct .unpack (b'<B' , fp .read (1 ))[0 ]
139
- value = Record .records [type ].parse (fp )
170
+ type = struct .unpack (b'<B' , fp .read (1 ))[0 ]
171
+ value = Record .records [type ].parse (fp )
140
172
141
173
return cls (index , value )
142
174
@@ -162,15 +194,26 @@ def to_bytes(self):
162
194
return bytes (bt )
163
195
164
196
def __str__ (self ):
165
- return '%s:%s="%s"' % (self .prefix , dictionary [self .index ],
197
+ return '%s:%s="%s"' % (self .prefix , dictionary [self .index ],
166
198
str (self .value ))
167
-
199
+
168
200
@classmethod
169
201
def parse (cls , fp ):
202
+ """
203
+ >>> from io import BytesIO
204
+ >>> fp = BytesIO(b'\\ x01x\\ x02\\ x86')
205
+ >>> dar = DictionaryAttributeRecord.parse(fp)
206
+ >>> str(dar.prefix)
207
+ 'x'
208
+ >>> dar.index
209
+ 2
210
+ >>> str(dar.value)
211
+ 'true'
212
+ """
170
213
prefix = Utf8String .parse (fp ).value
171
214
index = MultiByteInt31 .parse (fp ).value
172
- type = struct .unpack (b'<B' , fp .read (1 ))[0 ]
173
- value = Record .records [type ].parse (fp )
215
+ type = struct .unpack (b'<B' , fp .read (1 ))[0 ]
216
+ value = Record .records [type ].parse (fp )
174
217
175
218
return cls (prefix , index , value )
176
219
@@ -196,6 +239,15 @@ def to_bytes(self):
196
239
197
240
@classmethod
198
241
def parse (cls , fp ):
242
+ """
243
+ >>> from io import BytesIO
244
+ >>> fp = BytesIO(b'\\ x06')
245
+ >>> sdxar = ShortDictionaryXmlnsAttributeRecord.parse(fp)
246
+ >>> sdxar.index
247
+ 6
248
+ >>> str(sdxar)
249
+ 'xmlns="http://www.w3.org/2005/08/addressing"'
250
+ """
199
251
index = MultiByteInt31 .parse (fp ).value
200
252
return cls (index )
201
253
@@ -223,6 +275,17 @@ def to_bytes(self):
223
275
224
276
@classmethod
225
277
def parse (cls , fp ):
278
+ """
279
+ >>> from io import BytesIO
280
+ >>> fp = BytesIO(b'\\ x01a\\ x06')
281
+ >>> dxar = DictionaryXmlnsAttributeRecord.parse(fp)
282
+ >>> str(dxar.prefix)
283
+ 'a'
284
+ >>> dxar.index
285
+ 6
286
+ >>> str(dxar)
287
+ 'xmlns:a="http://www.w3.org/2005/08/addressing"'
288
+ """
226
289
prefix = Utf8String .parse (fp ).value
227
290
index = MultiByteInt31 .parse (fp ).value
228
291
return cls (prefix , index )
@@ -236,15 +299,26 @@ def __init__(self, value, *args, **kwargs):
236
299
self .value = value
237
300
238
301
def to_bytes (self ):
302
+ """
303
+ >>> ShortXmlnsAttributeRecord('test').to_bytes()
304
+ b'\\ x08\\ x04test'
305
+ """
239
306
bt = struct .pack (b'<B' , self .type )
240
307
bt += Utf8String (self .value ).to_bytes ()
241
308
return bytes (bt )
242
309
243
310
def __str__ (self ):
244
311
return 'xmlns="%s"' % (self .value ,)
245
-
312
+
246
313
@classmethod
247
314
def parse (cls , fp ):
315
+ """
316
+ >>> from io import BytesIO
317
+ >>> fp = BytesIO(b'\\ x04test')
318
+ >>> sxar = ShortXmlnsAttributeRecord.parse(fp)
319
+ >>> str(sxar)
320
+ 'xmlns="test"'
321
+ """
248
322
value = Utf8String .parse (fp ).value
249
323
return cls (value )
250
324
@@ -258,16 +332,26 @@ def __init__(self, name, value, *args, **kwargs):
258
332
self .value = value
259
333
260
334
def to_bytes (self ):
335
+ """
336
+ >>> XmlnsAttributeRecord('name', 'value').to_bytes()
337
+ b'\\ t\\ x04name\\ x05value'
338
+ """
261
339
bt = struct .pack (b'<B' , self .type )
262
340
bt += Utf8String (self .name ).to_bytes ()
263
341
bt += Utf8String (self .value ).to_bytes ()
264
342
return bytes (bt )
265
343
266
344
def __str__ (self ):
267
345
return 'xmlns:%s="%s"' % (self .name , self .value )
268
-
346
+
269
347
@classmethod
270
348
def parse (cls , fp ):
349
+ r"""
350
+ >>> from io import BytesIO
351
+ >>> fp = BytesIO(b'\x04name\x05value')
352
+ >>> str(XmlnsAttributeRecord.parse(fp))
353
+ 'xmlns:name="value"'
354
+ """
271
355
name = Utf8String .parse (fp ).value
272
356
value = Utf8String .parse (fp ).value
273
357
return cls (name , value )
@@ -278,12 +362,23 @@ def __init__(self, name, value):
278
362
super (PrefixAttributeRecord , self ).__init__ (self .char , name , value )
279
363
280
364
def to_bytes (self ):
365
+ r"""
366
+ >>> PrefixAttributeARecord('name', TrueTextRecord()).to_bytes()
367
+ b'&\x04name\x86'
368
+ """
281
369
string = Utf8String (self .name )
282
370
return bytes (struct .pack (b'<B' , self .type ) + string .to_bytes () +
283
371
self .value .to_bytes ())
284
372
285
373
@classmethod
286
374
def parse (cls , fp ):
375
+ r"""
376
+ >>> from io import BytesIO
377
+ >>> fp = BytesIO(b'\x04name\x86')
378
+ >>> paar = PrefixAttributeARecord.parse(fp)
379
+ >>> str(paar)
380
+ 'a:name="true"'
381
+ """
287
382
name = Utf8String .parse (fp ).value
288
383
type = struct .unpack (b'<B' , fp .read (1 ))[0 ]
289
384
value = Record .records [type ].parse (fp )
@@ -296,12 +391,23 @@ def __init__(self, index, value):
296
391
index , value )
297
392
298
393
def to_bytes (self ):
394
+ r"""
395
+ >>> PrefixDictionaryAttributeBRecord(2, TrueTextRecord()).to_bytes()
396
+ b'\r\x02\x86'
397
+ """
299
398
idx = MultiByteInt31 (self .index )
300
399
return bytes (struct .pack (b'<B' , self .type ) + idx .to_bytes () +
301
400
self .value .to_bytes ())
302
401
303
402
@classmethod
304
403
def parse (cls , fp ):
404
+ r"""
405
+ >>> from io import BytesIO
406
+ >>> fp = BytesIO(b'\02\x86')
407
+ >>> pdabr = PrefixDictionaryAttributeBRecord.parse(fp)
408
+ >>> str(pdabr)
409
+ 'b:Envelope="true"'
410
+ """
305
411
index = MultiByteInt31 .parse (fp ).value
306
412
type = struct .unpack (b'<B' , fp .read (1 ))[0 ]
307
413
value = Record .records [type ].parse (fp )
@@ -320,8 +426,8 @@ def parse(cls, fp):
320
426
))
321
427
322
428
429
+ __module__ = sys .modules [__name__ ]
323
430
__records__ = []
324
-
325
431
for c in range (0x0C , 0x25 + 1 ):
326
432
char = chr (c - 0x0C + ord ('a' ))
327
433
clsname = 'PrefixDictionaryAttribute' + char .upper () + 'Record'
@@ -335,6 +441,7 @@ def parse(cls, fp):
335
441
char = char ,
336
442
)
337
443
)
444
+ setattr (__module__ , clsname , cls )
338
445
__records__ .append (cls )
339
446
340
447
for c in range (0x26 , 0x3F + 1 ):
@@ -350,6 +457,7 @@ def parse(cls, fp):
350
457
char = char ,
351
458
)
352
459
)
460
+ setattr (__module__ , clsname , cls )
353
461
__records__ .append (cls )
354
462
355
463
Record .add_records (__records__ )
0 commit comments