uu_codec.py 3.8 KB

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