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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __all__ = [
  5.     'body_decode',
  6.     'body_encode',
  7.     'body_quopri_check',
  8.     'body_quopri_len',
  9.     'decode',
  10.     'decodestring',
  11.     'encode',
  12.     'encodestring',
  13.     'header_decode',
  14.     'header_encode',
  15.     'header_quopri_check',
  16.     'header_quopri_len',
  17.     'quote',
  18.     'unquote']
  19. import re
  20. from string import hexdigits
  21. from email.utils import fix_eols
  22. CRLF = '\r\n'
  23. NL = '\n'
  24. MISC_LEN = 7
  25. hqre = re.compile('[^-a-zA-Z0-9!*+/ ]')
  26. bqre = re.compile('[^ !-<>-~\\t]')
  27.  
  28. def header_quopri_check(c):
  29.     return bool(hqre.match(c))
  30.  
  31.  
  32. def body_quopri_check(c):
  33.     return bool(bqre.match(c))
  34.  
  35.  
  36. def header_quopri_len(s):
  37.     count = 0
  38.     for c in s:
  39.         if hqre.match(c):
  40.             count += 3
  41.             continue
  42.         count += 1
  43.     
  44.     return count
  45.  
  46.  
  47. def body_quopri_len(str):
  48.     count = 0
  49.     for c in str:
  50.         if bqre.match(c):
  51.             count += 3
  52.             continue
  53.         count += 1
  54.     
  55.     return count
  56.  
  57.  
  58. def _max_append(L, s, maxlen, extra = ''):
  59.     if not L:
  60.         L.append(s.lstrip())
  61.     elif len(L[-1]) + len(s) <= maxlen:
  62.         L[-1] += extra + s
  63.     else:
  64.         L.append(s.lstrip())
  65.  
  66.  
  67. def unquote(s):
  68.     return chr(int(s[1:3], 16))
  69.  
  70.  
  71. def quote(c):
  72.     return '=%02X' % ord(c)
  73.  
  74.  
  75. def header_encode(header, charset = 'iso-8859-1', keep_eols = False, maxlinelen = 76, eol = NL):
  76.     if not header:
  77.         return header
  78.     if not keep_eols:
  79.         header = fix_eols(header)
  80.     
  81.     quoted = []
  82.     if maxlinelen is None:
  83.         max_encoded = 100000
  84.     else:
  85.         max_encoded = maxlinelen - len(charset) - MISC_LEN - 1
  86.     for c in header:
  87.         if c == ' ':
  88.             _max_append(quoted, '_', max_encoded)
  89.             continue
  90.         if not hqre.match(c):
  91.             _max_append(quoted, c, max_encoded)
  92.             continue
  93.         _max_append(quoted, '=%02X' % ord(c), max_encoded)
  94.     
  95.     joiner = eol + ' '
  96.     return []([ '=?%s?q?%s?=' % (charset, line) for line in quoted ])
  97.  
  98.  
  99. def encode(body, binary = False, maxlinelen = 76, eol = NL):
  100.     if not body:
  101.         return body
  102.     if not binary:
  103.         body = fix_eols(body)
  104.     
  105.     encoded_body = ''
  106.     lineno = -1
  107.     lines = body.splitlines(1)
  108.     for line in lines:
  109.         if line.endswith(CRLF):
  110.             line = line[:-2]
  111.         elif line[-1] in CRLF:
  112.             line = line[:-1]
  113.         
  114.         lineno += 1
  115.         encoded_line = ''
  116.         prev = None
  117.         linelen = len(line)
  118.         for j in range(linelen):
  119.             c = line[j]
  120.             prev = c
  121.             if bqre.match(c):
  122.                 c = quote(c)
  123.             elif j + 1 == linelen:
  124.                 if c not in ' \t':
  125.                     encoded_line += c
  126.                 
  127.                 prev = c
  128.                 continue
  129.             
  130.             if len(encoded_line) + len(c) >= maxlinelen:
  131.                 encoded_body += encoded_line + '=' + eol
  132.                 encoded_line = ''
  133.             
  134.             encoded_line += c
  135.         
  136.         if prev and prev in ' \t':
  137.             if lineno + 1 == len(lines):
  138.                 prev = quote(prev)
  139.                 if len(encoded_line) + len(prev) > maxlinelen:
  140.                     encoded_body += encoded_line + '=' + eol + prev
  141.                 else:
  142.                     encoded_body += encoded_line + prev
  143.             else:
  144.                 encoded_body += encoded_line + prev + '=' + eol
  145.             encoded_line = ''
  146.         
  147.         if lines[lineno].endswith(CRLF) or lines[lineno][-1] in CRLF:
  148.             encoded_body += encoded_line + eol
  149.         else:
  150.             encoded_body += encoded_line
  151.         encoded_line = ''
  152.     
  153.     return encoded_body
  154.  
  155. body_encode = encode
  156. encodestring = encode
  157.  
  158. def decode(encoded, eol = NL):
  159.     if not encoded:
  160.         return encoded
  161.     decoded = ''
  162.     for line in encoded.splitlines():
  163.         line = line.rstrip()
  164.         if not line:
  165.             decoded += eol
  166.             continue
  167.         
  168.         i = 0
  169.         n = len(line)
  170.         while i < n:
  171.             c = line[i]
  172.             if c != '=':
  173.                 decoded += c
  174.                 i += 1
  175.             elif i + 1 == n:
  176.                 i += 1
  177.                 continue
  178.             elif i + 2 < n and line[i + 1] in hexdigits and line[i + 2] in hexdigits:
  179.                 decoded += unquote(line[i:i + 3])
  180.                 i += 3
  181.             else:
  182.                 decoded += c
  183.                 i += 1
  184.             if i == n:
  185.                 decoded += eol
  186.                 continue
  187.     
  188.     if not encoded.endswith(eol) and decoded.endswith(eol):
  189.         decoded = decoded[:-1]
  190.     
  191.     return decoded
  192.  
  193. body_decode = decode
  194. decodestring = decode
  195.  
  196. def _unquote_match(match):
  197.     s = match.group(0)
  198.     return unquote(s)
  199.  
  200.  
  201. def header_decode(s):
  202.     s = s.replace('_', ' ')
  203.     return re.sub('=\\w{2}', _unquote_match, s)
  204.  
  205.