home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / lib / python2.5 / quopri.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2008-10-29  |  6.5 KB  |  287 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. '''Conversions to/from quoted-printable transport encoding as per RFC 1521.'''
  5. __all__ = [
  6.     'encode',
  7.     'decode',
  8.     'encodestring',
  9.     'decodestring']
  10. ESCAPE = '='
  11. MAXLINESIZE = 76
  12. HEX = '0123456789ABCDEF'
  13. EMPTYSTRING = ''
  14.  
  15. try:
  16.     from binascii import a2b_qp, b2a_qp
  17. except ImportError:
  18.     a2b_qp = None
  19.     b2a_qp = None
  20.  
  21.  
  22. def needsquoting(c, quotetabs, header):
  23.     """Decide whether a particular character needs to be quoted.
  24.  
  25.     The 'quotetabs' flag indicates whether embedded tabs and spaces should be
  26.     quoted.  Note that line-ending tabs and spaces are always encoded, as per
  27.     RFC 1521.
  28.     """
  29.     if c in ' \t':
  30.         return quotetabs
  31.     
  32.     if c == '_':
  33.         return header
  34.     
  35.     if not c == ESCAPE:
  36.         pass
  37.     return not None if c <= c else c <= '~'
  38.  
  39.  
  40. def quote(c):
  41.     '''Quote a single character.'''
  42.     i = ord(c)
  43.     return ESCAPE + HEX[i // 16] + HEX[i % 16]
  44.  
  45.  
  46. def encode(input, output, quotetabs, header = 0):
  47.     """Read 'input', apply quoted-printable encoding, and write to 'output'.
  48.  
  49.     'input' and 'output' are files with readline() and write() methods.
  50.     The 'quotetabs' flag indicates whether embedded tabs and spaces should be
  51.     quoted.  Note that line-ending tabs and spaces are always encoded, as per
  52.     RFC 1521.
  53.     The 'header' flag indicates whether we are encoding spaces as _ as per
  54.     RFC 1522.
  55.     """
  56.     if b2a_qp is not None:
  57.         data = input.read()
  58.         odata = b2a_qp(data, quotetabs = quotetabs, header = header)
  59.         output.write(odata)
  60.         return None
  61.     
  62.     
  63.     def write(s, output = output, lineEnd = '\n'):
  64.         if s and s[-1:] in ' \t':
  65.             output.write(s[:-1] + quote(s[-1]) + lineEnd)
  66.         elif s == '.':
  67.             output.write(quote(s) + lineEnd)
  68.         else:
  69.             output.write(s + lineEnd)
  70.  
  71.     prevline = None
  72.     while None:
  73.         line = input.readline()
  74.         if not line:
  75.             break
  76.         
  77.         outline = []
  78.         stripped = ''
  79.         if line[-1:] == '\n':
  80.             line = line[:-1]
  81.             stripped = '\n'
  82.         
  83.         for c in line:
  84.             if needsquoting(c, quotetabs, header):
  85.                 c = quote(c)
  86.             
  87.             if header and c == ' ':
  88.                 outline.append('_')
  89.                 continue
  90.             outline.append(c)
  91.         
  92.         if prevline is not None:
  93.             write(prevline)
  94.         
  95.         thisline = EMPTYSTRING.join(outline)
  96.         while len(thisline) > MAXLINESIZE:
  97.             write(thisline[:MAXLINESIZE - 1], lineEnd = '=\n')
  98.             thisline = thisline[MAXLINESIZE - 1:]
  99.         prevline = thisline
  100.         continue
  101.         if prevline is not None:
  102.             write(prevline, lineEnd = stripped)
  103.         
  104.  
  105.  
  106. def encodestring(s, quotetabs = 0, header = 0):
  107.     if b2a_qp is not None:
  108.         return b2a_qp(s, quotetabs = quotetabs, header = header)
  109.     
  110.     StringIO = StringIO
  111.     import cStringIO
  112.     infp = StringIO(s)
  113.     outfp = StringIO()
  114.     encode(infp, outfp, quotetabs, header)
  115.     return outfp.getvalue()
  116.  
  117.  
  118. def decode(input, output, header = 0):
  119.     """Read 'input', apply quoted-printable decoding, and write to 'output'.
  120.     'input' and 'output' are files with readline() and write() methods.
  121.     If 'header' is true, decode underscore as space (per RFC 1522)."""
  122.     if a2b_qp is not None:
  123.         data = input.read()
  124.         odata = a2b_qp(data, header = header)
  125.         output.write(odata)
  126.         return None
  127.     
  128.     new = ''
  129.     while None:
  130.         line = input.readline()
  131.         if not line:
  132.             break
  133.         
  134.         i = 0
  135.         n = len(line)
  136.         if n > 0 and line[n - 1] == '\n':
  137.             partial = 0
  138.             n = n - 1
  139.             while n > 0 and line[n - 1] in ' \t\r':
  140.                 n = n - 1
  141.         else:
  142.             partial = 1
  143.         while i < n:
  144.             c = line[i]
  145.             if c == '_' and header:
  146.                 new = new + ' '
  147.                 i = i + 1
  148.                 continue
  149.             if c != ESCAPE:
  150.                 new = new + c
  151.                 i = i + 1
  152.                 continue
  153.             if i + 1 == n and not partial:
  154.                 partial = 1
  155.                 break
  156.                 continue
  157.             if i + 1 < n and line[i + 1] == ESCAPE:
  158.                 new = new + ESCAPE
  159.                 i = i + 2
  160.                 continue
  161.             if i + 2 < n and ishex(line[i + 1]) and ishex(line[i + 2]):
  162.                 new = new + chr(unhex(line[i + 1:i + 3]))
  163.                 i = i + 3
  164.                 continue
  165.             new = new + c
  166.             i = i + 1
  167.         if not partial:
  168.             output.write(new + '\n')
  169.             new = ''
  170.             continue
  171.         continue
  172.         if new:
  173.             output.write(new)
  174.         
  175.  
  176.  
  177. def decodestring(s, header = 0):
  178.     if a2b_qp is not None:
  179.         return a2b_qp(s, header = header)
  180.     
  181.     StringIO = StringIO
  182.     import cStringIO
  183.     infp = StringIO(s)
  184.     outfp = StringIO()
  185.     decode(infp, outfp, header = header)
  186.     return outfp.getvalue()
  187.  
  188.  
  189. def ishex(c):
  190.     """Return true if the character 'c' is a hexadecimal digit."""
  191.     if c <= c:
  192.         pass
  193.     elif not c <= '9':
  194.         if c <= c:
  195.             pass
  196.         elif not c <= 'f':
  197.             pass
  198.     return None if c <= c else c <= 'F'
  199.  
  200.  
  201. def unhex(s):
  202.     '''Get the integer value of a hexadecimal number.'''
  203.     bits = 0
  204.     for c in s:
  205.         if c <= c:
  206.             pass
  207.         elif c <= '9':
  208.             i = ord('0')
  209.         elif c <= c:
  210.             pass
  211.         elif c <= 'f':
  212.             i = ord('a') - 10
  213.         elif c <= c:
  214.             pass
  215.         elif c <= 'F':
  216.             i = ord('A') - 10
  217.         else:
  218.             break
  219.         bits = bits * 16 + (ord(c) - i)
  220.     
  221.     return bits
  222.  
  223.  
  224. def main():
  225.     import sys
  226.     import getopt
  227.     
  228.     try:
  229.         (opts, args) = getopt.getopt(sys.argv[1:], 'td')
  230.     except getopt.error:
  231.         msg = None
  232.         sys.stdout = sys.stderr
  233.         print msg
  234.         print 'usage: quopri [-t | -d] [file] ...'
  235.         print '-t: quote tabs'
  236.         print '-d: decode; default encode'
  237.         sys.exit(2)
  238.  
  239.     deco = 0
  240.     tabs = 0
  241.     for o, a in opts:
  242.         if o == '-t':
  243.             tabs = 1
  244.         
  245.         if o == '-d':
  246.             deco = 1
  247.             continue
  248.     
  249.     if tabs and deco:
  250.         sys.stdout = sys.stderr
  251.         print '-t and -d are mutually exclusive'
  252.         sys.exit(2)
  253.     
  254.     if not args:
  255.         args = [
  256.             '-']
  257.     
  258.     sts = 0
  259.     for file in args:
  260.         if file == '-':
  261.             fp = sys.stdin
  262.         else:
  263.             
  264.             try:
  265.                 fp = open(file)
  266.             except IOError:
  267.                 msg = None
  268.                 sys.stderr.write("%s: can't open (%s)\n" % (file, msg))
  269.                 sts = 1
  270.                 continue
  271.  
  272.         if deco:
  273.             decode(fp, sys.stdout)
  274.         else:
  275.             encode(fp, sys.stdout, tabs)
  276.         if fp is not sys.stdin:
  277.             fp.close()
  278.             continue
  279.     
  280.     if sts:
  281.         sys.exit(sts)
  282.     
  283.  
  284. if __name__ == '__main__':
  285.     main()
  286.  
  287.