0001 """ Python 'uu_codec' Codec - UU content transfer encoding 0002 0003 Unlike most of the other codecs which target Unicode, this codec 0004 will return Python string objects for both encode and decode. 0005 0006 Written by Marc-Andre Lemburg (mal@lemburg.com). Some details were 0007 adapted from uu.py which was written by Lance Ellinghouse and 0008 modified by Jack Jansen and Fredrik Lundh. 0009 0010 """ 0011 import codecs, binascii 0012 0013 ### Codec APIs 0014 0015 def uu_encode(input,errors='strict',filename='<data>',mode=0666): 0016 0017 """ Encodes the object input and returns a tuple (output 0018 object, length consumed). 0019 0020 errors defines the error handling to apply. It defaults to 0021 'strict' handling which is the only currently supported 0022 error handling for this codec. 0023 0024 """ 0025 assert errors == 'strict' 0026 from cStringIO import StringIO 0027 from binascii import b2a_uu 0028 infile = StringIO(input) 0029 outfile = StringIO() 0030 read = infile.read 0031 write = outfile.write 0032 0033 # Encode 0034 write('begin %o %s\n' % (mode & 0777, filename)) 0035 chunk = read(45) 0036 while chunk: 0037 write(b2a_uu(chunk)) 0038 chunk = read(45) 0039 write(' \nend\n') 0040 0041 return (outfile.getvalue(), len(input)) 0042 0043 def uu_decode(input,errors='strict'): 0044 0045 """ Decodes the object input and returns a tuple (output 0046 object, length consumed). 0047 0048 input must be an object which provides the bf_getreadbuf 0049 buffer slot. Python strings, buffer objects and memory 0050 mapped files are examples of objects providing this slot. 0051 0052 errors defines the error handling to apply. It defaults to 0053 'strict' handling which is the only currently supported 0054 error handling for this codec. 0055 0056 Note: filename and file mode information in the input data is 0057 ignored. 0058 0059 """ 0060 assert errors == 'strict' 0061 from cStringIO import StringIO 0062 from binascii import a2b_uu 0063 infile = StringIO(input) 0064 outfile = StringIO() 0065 readline = infile.readline 0066 write = outfile.write 0067 0068 # Find start of encoded data 0069 while 1: 0070 s = readline() 0071 if not s: 0072 raise ValueError, 'Missing "begin" line in input data' 0073 if s[:5] == 'begin': 0074 break 0075 0076 # Decode 0077 while 1: 0078 s = readline() 0079 if not s or \ 0080 s == 'end\n': 0081 break 0082 try: 0083 data = a2b_uu(s) 0084 except binascii.Error, v: 0085 # Workaround for broken uuencoders by /Fredrik Lundh 0086 nbytes = (((ord(s[0])-32) & 63) * 4 + 5) / 3 0087 data = a2b_uu(s[:nbytes]) 0088 #sys.stderr.write("Warning: %s\n" % str(v)) 0089 write(data) 0090 if not s: 0091 raise ValueError, 'Truncated input data' 0092 0093 return (outfile.getvalue(), len(input)) 0094 0095 class Codec(codecs.Codec): 0096 0097 def encode(self,input,errors='strict'): 0098 return uu_encode(input,errors) 0099 def decode(self,input,errors='strict'): 0100 return uu_decode(input,errors) 0101 0102 class StreamWriter(Codec,codecs.StreamWriter): 0103 pass 0104 0105 class StreamReader(Codec,codecs.StreamReader): 0106 pass 0107 0108 ### encodings module API 0109 0110 def getregentry(): 0111 0112 return (uu_encode,uu_decode,StreamReader,StreamWriter) 0113
Generated by PyXR 0.9.4