]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.10/Lib/encodings/uu_codec.py
AppPkg/Applications/Python/Python-2.7.10: Initial Checkin part 4/5.
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.10 / Lib / encodings / uu_codec.py
CommitLineData
3257aa99
DM
1""" Python 'uu_codec' Codec - UU content transfer encoding\r
2\r
3 Unlike most of the other codecs which target Unicode, this codec\r
4 will return Python string objects for both encode and decode.\r
5\r
6 Written by Marc-Andre Lemburg (mal@lemburg.com). Some details were\r
7 adapted from uu.py which was written by Lance Ellinghouse and\r
8 modified by Jack Jansen and Fredrik Lundh.\r
9\r
10"""\r
11import codecs, binascii\r
12\r
13### Codec APIs\r
14\r
15def uu_encode(input,errors='strict',filename='<data>',mode=0666):\r
16\r
17 """ Encodes the object input and returns a tuple (output\r
18 object, length consumed).\r
19\r
20 errors defines the error handling to apply. It defaults to\r
21 'strict' handling which is the only currently supported\r
22 error handling for this codec.\r
23\r
24 """\r
25 assert errors == 'strict'\r
26 from cStringIO import StringIO\r
27 from binascii import b2a_uu\r
28 # using str() because of cStringIO's Unicode undesired Unicode behavior.\r
29 infile = StringIO(str(input))\r
30 outfile = StringIO()\r
31 read = infile.read\r
32 write = outfile.write\r
33\r
34 # Encode\r
35 write('begin %o %s\n' % (mode & 0777, filename))\r
36 chunk = read(45)\r
37 while chunk:\r
38 write(b2a_uu(chunk))\r
39 chunk = read(45)\r
40 write(' \nend\n')\r
41\r
42 return (outfile.getvalue(), len(input))\r
43\r
44def uu_decode(input,errors='strict'):\r
45\r
46 """ Decodes the object input and returns a tuple (output\r
47 object, length consumed).\r
48\r
49 input must be an object which provides the bf_getreadbuf\r
50 buffer slot. Python strings, buffer objects and memory\r
51 mapped files are examples of objects providing this slot.\r
52\r
53 errors defines the error handling to apply. It defaults to\r
54 'strict' handling which is the only currently supported\r
55 error handling for this codec.\r
56\r
57 Note: filename and file mode information in the input data is\r
58 ignored.\r
59\r
60 """\r
61 assert errors == 'strict'\r
62 from cStringIO import StringIO\r
63 from binascii import a2b_uu\r
64 infile = StringIO(str(input))\r
65 outfile = StringIO()\r
66 readline = infile.readline\r
67 write = outfile.write\r
68\r
69 # Find start of encoded data\r
70 while 1:\r
71 s = readline()\r
72 if not s:\r
73 raise ValueError, 'Missing "begin" line in input data'\r
74 if s[:5] == 'begin':\r
75 break\r
76\r
77 # Decode\r
78 while 1:\r
79 s = readline()\r
80 if not s or \\r
81 s == 'end\n':\r
82 break\r
83 try:\r
84 data = a2b_uu(s)\r
85 except binascii.Error, v:\r
86 # Workaround for broken uuencoders by /Fredrik Lundh\r
87 nbytes = (((ord(s[0])-32) & 63) * 4 + 5) // 3\r
88 data = a2b_uu(s[:nbytes])\r
89 #sys.stderr.write("Warning: %s\n" % str(v))\r
90 write(data)\r
91 if not s:\r
92 raise ValueError, 'Truncated input data'\r
93\r
94 return (outfile.getvalue(), len(input))\r
95\r
96class Codec(codecs.Codec):\r
97\r
98 def encode(self,input,errors='strict'):\r
99 return uu_encode(input,errors)\r
100\r
101 def decode(self,input,errors='strict'):\r
102 return uu_decode(input,errors)\r
103\r
104class IncrementalEncoder(codecs.IncrementalEncoder):\r
105 def encode(self, input, final=False):\r
106 return uu_encode(input, self.errors)[0]\r
107\r
108class IncrementalDecoder(codecs.IncrementalDecoder):\r
109 def decode(self, input, final=False):\r
110 return uu_decode(input, self.errors)[0]\r
111\r
112class StreamWriter(Codec,codecs.StreamWriter):\r
113 pass\r
114\r
115class StreamReader(Codec,codecs.StreamReader):\r
116 pass\r
117\r
118### encodings module API\r
119\r
120def getregentry():\r
121 return codecs.CodecInfo(\r
122 name='uu',\r
123 encode=uu_encode,\r
124 decode=uu_decode,\r
125 incrementalencoder=IncrementalEncoder,\r
126 incrementaldecoder=IncrementalDecoder,\r
127 streamreader=StreamReader,\r
128 streamwriter=StreamWriter,\r
129 )\r