uu_codec.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 ([email protected]). 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. infile = StringIO(input)
  21. outfile = StringIO()
  22. read = infile.read
  23. write = outfile.write
  24. # Encode
  25. write('begin %o %s\n' % (mode & 0777, filename))
  26. chunk = read(45)
  27. while chunk:
  28. write(b2a_uu(chunk))
  29. chunk = read(45)
  30. write(' \nend\n')
  31. return (outfile.getvalue(), len(input))
  32. def uu_decode(input,errors='strict'):
  33. """ Decodes the object input and returns a tuple (output
  34. object, length consumed).
  35. input must be an object which provides the bf_getreadbuf
  36. buffer slot. Python strings, buffer objects and memory
  37. mapped files are examples of objects providing this slot.
  38. errors defines the error handling to apply. It defaults to
  39. 'strict' handling which is the only currently supported
  40. error handling for this codec.
  41. Note: filename and file mode information in the input data is
  42. ignored.
  43. """
  44. assert errors == 'strict'
  45. from cStringIO import StringIO
  46. from binascii import a2b_uu
  47. infile = StringIO(input)
  48. outfile = StringIO()
  49. readline = infile.readline
  50. write = outfile.write
  51. # Find start of encoded data
  52. while 1:
  53. s = readline()
  54. if not s:
  55. raise ValueError, 'Missing "begin" line in input data'
  56. if s[:5] == 'begin':
  57. break
  58. # Decode
  59. while 1:
  60. s = readline()
  61. if not s or \
  62. s == 'end\n':
  63. break
  64. try:
  65. data = a2b_uu(s)
  66. except binascii.Error, v:
  67. # Workaround for broken uuencoders by /Fredrik Lundh
  68. nbytes = (((ord(s[0])-32) & 63) * 4 + 5) / 3
  69. data = a2b_uu(s[:nbytes])
  70. #sys.stderr.write("Warning: %s\n" % str(v))
  71. write(data)
  72. if not s:
  73. raise ValueError, 'Truncated input data'
  74. return (outfile.getvalue(), len(input))
  75. class Codec(codecs.Codec):
  76. def encode(self,input,errors='strict'):
  77. return uu_encode(input,errors)
  78. def decode(self,input,errors='strict'):
  79. return uu_decode(input,errors)
  80. class StreamWriter(Codec,codecs.StreamWriter):
  81. pass
  82. class StreamReader(Codec,codecs.StreamReader):
  83. pass
  84. ### encodings module API
  85. def getregentry():
  86. return (uu_encode,uu_decode,StreamReader,StreamWriter)