Debian Codegraph @

cpython27/Lib/encodings/uu_codec.py

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