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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __all__ = [
  5.     'Generator',
  6.     'DecodedGenerator']
  7. import re
  8. import sys
  9. import time
  10. import random
  11. import warnings
  12. from cStringIO import StringIO
  13. from email.header import Header
  14. UNDERSCORE = '_'
  15. NL = '\n'
  16. fcre = re.compile('^From ', re.MULTILINE)
  17.  
  18. def _is8bitstring(s):
  19.     if isinstance(s, str):
  20.         
  21.         try:
  22.             unicode(s, 'us-ascii')
  23.         except UnicodeError:
  24.             return True
  25.         
  26.  
  27.     None<EXCEPTION MATCH>UnicodeError
  28.     return False
  29.  
  30.  
  31. class Generator:
  32.     
  33.     def __init__(self, outfp, mangle_from_ = True, maxheaderlen = 78):
  34.         self._fp = outfp
  35.         self._mangle_from_ = mangle_from_
  36.         self._maxheaderlen = maxheaderlen
  37.  
  38.     
  39.     def write(self, s):
  40.         self._fp.write(s)
  41.  
  42.     
  43.     def flatten(self, msg, unixfrom = False):
  44.         if unixfrom:
  45.             ufrom = msg.get_unixfrom()
  46.             if not ufrom:
  47.                 ufrom = 'From nobody ' + time.ctime(time.time())
  48.             
  49.             print >>self._fp, ufrom
  50.         
  51.         self._write(msg)
  52.  
  53.     
  54.     def clone(self, fp):
  55.         return self.__class__(fp, self._mangle_from_, self._maxheaderlen)
  56.  
  57.     
  58.     def _write(self, msg):
  59.         oldfp = self._fp
  60.         
  61.         try:
  62.             self._fp = sfp = StringIO()
  63.             self._dispatch(msg)
  64.         finally:
  65.             self._fp = oldfp
  66.  
  67.         meth = getattr(msg, '_write_headers', None)
  68.         if meth is None:
  69.             self._write_headers(msg)
  70.         else:
  71.             meth(self)
  72.         self._fp.write(sfp.getvalue())
  73.  
  74.     
  75.     def _dispatch(self, msg):
  76.         main = msg.get_content_maintype()
  77.         sub = msg.get_content_subtype()
  78.         specific = UNDERSCORE.join((main, sub)).replace('-', '_')
  79.         meth = getattr(self, '_handle_' + specific, None)
  80.         if meth is None:
  81.             generic = main.replace('-', '_')
  82.             meth = getattr(self, '_handle_' + generic, None)
  83.             if meth is None:
  84.                 meth = self._writeBody
  85.             
  86.         
  87.         meth(msg)
  88.  
  89.     
  90.     def _write_headers(self, msg):
  91.         for h, v in msg.items():
  92.             print >>self._fp, '%s:' % h,
  93.             if self._maxheaderlen == 0:
  94.                 print >>self._fp, v
  95.                 continue
  96.             if isinstance(v, Header):
  97.                 print >>self._fp, v.encode()
  98.                 continue
  99.             if _is8bitstring(v):
  100.                 print >>self._fp, v
  101.                 continue
  102.             print >>self._fp, Header(v, maxlinelen = self._maxheaderlen, header_name = h, continuation_ws = '\t').encode()
  103.         
  104.         print >>self._fp
  105.  
  106.     
  107.     def _handle_text(self, msg):
  108.         payload = msg.get_payload()
  109.         if payload is None:
  110.             return None
  111.         if not isinstance(payload, basestring):
  112.             raise TypeError('string payload expected: %s' % type(payload))
  113.         isinstance(payload, basestring)
  114.         if self._mangle_from_:
  115.             payload = fcre.sub('>From ', payload)
  116.         
  117.         self._fp.write(payload)
  118.  
  119.     _writeBody = _handle_text
  120.     
  121.     def _handle_multipart(self, msg):
  122.         msgtexts = []
  123.         subparts = msg.get_payload()
  124.         if subparts is None:
  125.             subparts = []
  126.         elif isinstance(subparts, basestring):
  127.             self._fp.write(subparts)
  128.             return None
  129.         if not isinstance(subparts, list):
  130.             subparts = [
  131.                 subparts]
  132.         
  133.         for part in subparts:
  134.             s = StringIO()
  135.             g = self.clone(s)
  136.             g.flatten(part, unixfrom = False)
  137.             msgtexts.append(s.getvalue())
  138.         
  139.         alltext = NL.join(msgtexts)
  140.         boundary = msg.get_boundary(failobj = _make_boundary(alltext))
  141.         if msg.get_boundary() != boundary:
  142.             msg.set_boundary(boundary)
  143.         
  144.         if msg.preamble is not None:
  145.             print >>self._fp, msg.preamble
  146.         
  147.         print >>self._fp, '--' + boundary
  148.         if msgtexts:
  149.             self._fp.write(msgtexts.pop(0))
  150.         
  151.         for body_part in msgtexts:
  152.             print >>self._fp, '\n--' + boundary
  153.             self._fp.write(body_part)
  154.         
  155.         self._fp.write('\n--' + boundary + '--')
  156.         if msg.epilogue is not None:
  157.             print >>self._fp
  158.             self._fp.write(msg.epilogue)
  159.         
  160.  
  161.     
  162.     def _handle_message_delivery_status(self, msg):
  163.         blocks = []
  164.         for part in msg.get_payload():
  165.             s = StringIO()
  166.             g = self.clone(s)
  167.             g.flatten(part, unixfrom = False)
  168.             text = s.getvalue()
  169.             lines = text.split('\n')
  170.             if lines and lines[-1] == '':
  171.                 blocks.append(NL.join(lines[:-1]))
  172.                 continue
  173.             blocks.append(text)
  174.         
  175.         self._fp.write(NL.join(blocks))
  176.  
  177.     
  178.     def _handle_message(self, msg):
  179.         s = StringIO()
  180.         g = self.clone(s)
  181.         g.flatten(msg.get_payload(0), unixfrom = False)
  182.         self._fp.write(s.getvalue())
  183.  
  184.  
  185. _FMT = '[Non-text (%(type)s) part of message omitted, filename %(filename)s]'
  186.  
  187. class DecodedGenerator(Generator):
  188.     
  189.     def __init__(self, outfp, mangle_from_ = True, maxheaderlen = 78, fmt = None):
  190.         Generator.__init__(self, outfp, mangle_from_, maxheaderlen)
  191.         if fmt is None:
  192.             self._fmt = _FMT
  193.         else:
  194.             self._fmt = fmt
  195.  
  196.     
  197.     def _dispatch(self, msg):
  198.         for part in msg.walk():
  199.             maintype = part.get_content_maintype()
  200.             if maintype == 'text':
  201.                 print >>self, part.get_payload(decode = True)
  202.                 continue
  203.             if maintype == 'multipart':
  204.                 continue
  205.             print >>self, self._fmt % {
  206.                 'type': part.get_content_type(),
  207.                 'maintype': part.get_content_maintype(),
  208.                 'subtype': part.get_content_subtype(),
  209.                 'filename': part.get_filename('[no filename]'),
  210.                 'description': part.get('Content-Description', '[no description]'),
  211.                 'encoding': part.get('Content-Transfer-Encoding', '[no encoding]') }
  212.         
  213.  
  214.  
  215. _width = len(repr(sys.maxint - 1))
  216. _fmt = '%%0%dd' % _width
  217.  
  218. def _make_boundary(text = None):
  219.     token = random.randrange(sys.maxint)
  220.     boundary = '===============' + _fmt % token + '=='
  221.     if text is None:
  222.         return boundary
  223.     b = boundary
  224.     counter = 0
  225.     while True:
  226.         cre = re.compile('^--' + re.escape(b) + '(--)?$', re.MULTILINE)
  227.         if not cre.search(text):
  228.             break
  229.         
  230.         b = boundary + '.' + str(counter)
  231.         counter += 1
  232.     return b
  233.  
  234.