home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_262 / quopri.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-09-09  |  5.1 KB  |  249 lines

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