1 """ Python 'uu_codec' Codec - UU content transfer encoding
3 Unlike most of the other codecs which target Unicode, this codec
4 will return Python string objects for both encode and decode.
6 Written by Marc-Andre Lemburg (mal@lemburg.com). Some details were
7 adapted from uu.py which was written by Lance Ellinghouse and
8 modified by Jack Jansen and Fredrik Lundh.
11 import codecs
, binascii
15 def uu_encode(input,errors
='strict',filename
='<data>',mode
=0666):
17 """ Encodes the object input and returns a tuple (output
18 object, length consumed).
20 errors defines the error handling to apply. It defaults to
21 'strict' handling which is the only currently supported
22 error handling for this codec.
25 assert errors
== 'strict'
26 from cStringIO
import StringIO
27 from binascii
import b2a_uu
28 # using str() because of cStringIO's Unicode undesired Unicode behavior.
29 infile
= StringIO(str(input))
35 write('begin %o %s\n' % (mode
& 0777, filename
))
42 return (outfile
.getvalue(), len(input))
44 def uu_decode(input,errors
='strict'):
46 """ Decodes the object input and returns a tuple (output
47 object, length consumed).
49 input must be an object which provides the bf_getreadbuf
50 buffer slot. Python strings, buffer objects and memory
51 mapped files are examples of objects providing this slot.
53 errors defines the error handling to apply. It defaults to
54 'strict' handling which is the only currently supported
55 error handling for this codec.
57 Note: filename and file mode information in the input data is
61 assert errors
== 'strict'
62 from cStringIO
import StringIO
63 from binascii
import a2b_uu
64 infile
= StringIO(str(input))
66 readline
= infile
.readline
69 # Find start of encoded data
73 raise ValueError, 'Missing "begin" line in input data'
85 except binascii
.Error
, v
:
86 # Workaround for broken uuencoders by /Fredrik Lundh
87 nbytes
= (((ord(s
[0])-32) & 63) * 4 + 5) / 3
88 data
= a2b_uu(s
[:nbytes
])
89 #sys.stderr.write("Warning: %s\n" % str(v))
92 raise ValueError, 'Truncated input data'
94 return (outfile
.getvalue(), len(input))
96 class Codec(codecs
.Codec
):
98 def encode(self
,input,errors
='strict'):
99 return uu_encode(input,errors
)
101 def decode(self
,input,errors
='strict'):
102 return uu_decode(input,errors
)
104 class IncrementalEncoder(codecs
.IncrementalEncoder
):
105 def encode(self
, input, final
=False):
106 return uu_encode(input, self
.errors
)[0]
108 class IncrementalDecoder(codecs
.IncrementalDecoder
):
109 def decode(self
, input, final
=False):
110 return uu_decode(input, self
.errors
)[0]
112 class StreamWriter(Codec
,codecs
.StreamWriter
):
115 class StreamReader(Codec
,codecs
.StreamReader
):
118 ### encodings module API
121 return codecs
.CodecInfo(
125 incrementalencoder
=IncrementalEncoder
,
126 incrementaldecoder
=IncrementalDecoder
,
127 streamreader
=StreamReader
,
128 streamwriter
=StreamWriter
,