+++ /dev/null
-""" Python 'uu_codec' Codec - UU content transfer encoding\r
-\r
- Unlike most of the other codecs which target Unicode, this codec\r
- will return Python string objects for both encode and decode.\r
-\r
- Written by Marc-Andre Lemburg (mal@lemburg.com). Some details were\r
- adapted from uu.py which was written by Lance Ellinghouse and\r
- modified by Jack Jansen and Fredrik Lundh.\r
-\r
-"""\r
-import codecs, binascii\r
-\r
-### Codec APIs\r
-\r
-def uu_encode(input,errors='strict',filename='<data>',mode=0666):\r
-\r
- """ Encodes the object input and returns a tuple (output\r
- object, length consumed).\r
-\r
- errors defines the error handling to apply. It defaults to\r
- 'strict' handling which is the only currently supported\r
- error handling for this codec.\r
-\r
- """\r
- assert errors == 'strict'\r
- from cStringIO import StringIO\r
- from binascii import b2a_uu\r
- # using str() because of cStringIO's Unicode undesired Unicode behavior.\r
- infile = StringIO(str(input))\r
- outfile = StringIO()\r
- read = infile.read\r
- write = outfile.write\r
-\r
- # Encode\r
- write('begin %o %s\n' % (mode & 0777, filename))\r
- chunk = read(45)\r
- while chunk:\r
- write(b2a_uu(chunk))\r
- chunk = read(45)\r
- write(' \nend\n')\r
-\r
- return (outfile.getvalue(), len(input))\r
-\r
-def uu_decode(input,errors='strict'):\r
-\r
- """ Decodes the object input and returns a tuple (output\r
- object, length consumed).\r
-\r
- input must be an object which provides the bf_getreadbuf\r
- buffer slot. Python strings, buffer objects and memory\r
- mapped files are examples of objects providing this slot.\r
-\r
- errors defines the error handling to apply. It defaults to\r
- 'strict' handling which is the only currently supported\r
- error handling for this codec.\r
-\r
- Note: filename and file mode information in the input data is\r
- ignored.\r
-\r
- """\r
- assert errors == 'strict'\r
- from cStringIO import StringIO\r
- from binascii import a2b_uu\r
- infile = StringIO(str(input))\r
- outfile = StringIO()\r
- readline = infile.readline\r
- write = outfile.write\r
-\r
- # Find start of encoded data\r
- while 1:\r
- s = readline()\r
- if not s:\r
- raise ValueError, 'Missing "begin" line in input data'\r
- if s[:5] == 'begin':\r
- break\r
-\r
- # Decode\r
- while 1:\r
- s = readline()\r
- if not s or \\r
- s == 'end\n':\r
- break\r
- try:\r
- data = a2b_uu(s)\r
- except binascii.Error, v:\r
- # Workaround for broken uuencoders by /Fredrik Lundh\r
- nbytes = (((ord(s[0])-32) & 63) * 4 + 5) // 3\r
- data = a2b_uu(s[:nbytes])\r
- #sys.stderr.write("Warning: %s\n" % str(v))\r
- write(data)\r
- if not s:\r
- raise ValueError, 'Truncated input data'\r
-\r
- return (outfile.getvalue(), len(input))\r
-\r
-class Codec(codecs.Codec):\r
-\r
- def encode(self,input,errors='strict'):\r
- return uu_encode(input,errors)\r
-\r
- def decode(self,input,errors='strict'):\r
- return uu_decode(input,errors)\r
-\r
-class IncrementalEncoder(codecs.IncrementalEncoder):\r
- def encode(self, input, final=False):\r
- return uu_encode(input, self.errors)[0]\r
-\r
-class IncrementalDecoder(codecs.IncrementalDecoder):\r
- def decode(self, input, final=False):\r
- return uu_decode(input, self.errors)[0]\r
-\r
-class StreamWriter(Codec,codecs.StreamWriter):\r
- pass\r
-\r
-class StreamReader(Codec,codecs.StreamReader):\r
- pass\r
-\r
-### encodings module API\r
-\r
-def getregentry():\r
- return codecs.CodecInfo(\r
- name='uu',\r
- encode=uu_encode,\r
- decode=uu_decode,\r
- incrementalencoder=IncrementalEncoder,\r
- incrementaldecoder=IncrementalDecoder,\r
- streamreader=StreamReader,\r
- streamwriter=StreamWriter,\r
- )\r