home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_684 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  5.2 KB  |  261 lines

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