uu_codec.py 3.9 KB

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