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 infile
= StringIO(input)
34 write('begin %o %s\n' % (mode
& 0777, filename
))
41 return (outfile
.getvalue(), len(input))
43 def uu_decode(input,errors
='strict'):
45 """ Decodes the object input and returns a tuple (output
46 object, length consumed).
48 input must be an object which provides the bf_getreadbuf
49 buffer slot. Python strings, buffer objects and memory
50 mapped files are examples of objects providing this slot.
52 errors defines the error handling to apply. It defaults to
53 'strict' handling which is the only currently supported
54 error handling for this codec.
56 Note: filename and file mode information in the input data is
60 assert errors
== 'strict'
61 from cStringIO
import StringIO
62 from binascii
import a2b_uu
63 infile
= StringIO(input)
65 readline
= infile
.readline
68 # Find start of encoded data
72 raise ValueError, 'Missing "begin" line in input data'
84 except binascii
.Error
, v
:
85 # Workaround for broken uuencoders by /Fredrik Lundh
86 nbytes
= (((ord(s
[0])-32) & 63) * 4 + 5) / 3
87 data
= a2b_uu(s
[:nbytes
])
88 #sys.stderr.write("Warning: %s\n" % str(v))
91 raise ValueError, 'Truncated input data'
93 return (outfile
.getvalue(), len(input))
95 class Codec(codecs
.Codec
):
97 def encode(self
,input,errors
='strict'):
98 return uu_encode(input,errors
)
99 def decode(self
,input,errors
='strict'):
100 return uu_decode(input,errors
)
102 class StreamWriter(Codec
,codecs
.StreamWriter
):
105 class StreamReader(Codec
,codecs
.StreamReader
):
108 ### encodings module API
112 return (uu_encode
,uu_decode
,StreamReader
,StreamWriter
)