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

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  3.  
  4. import os
  5. import sys
  6. import tempfile
  7. from warnings import filterwarnings, catch_warnings
  8. with catch_warnings():
  9.     if sys.py3kwarning:
  10.         filterwarnings('ignore', '.*rfc822 has been removed', DeprecationWarning)
  11.     import rfc822
  12. from warnings import warnpy3k
  13. warnpy3k('in 3.x, mimetools has been removed in favor of the email package', stacklevel = 2)
  14. __all__ = [
  15.     'Message',
  16.     'choose_boundary',
  17.     'encode',
  18.     'decode',
  19.     'copyliteral',
  20.     'copybinary']
  21.  
  22. class Message(rfc822.Message):
  23.     
  24.     def __init__(self, fp, seekable = 1):
  25.         rfc822.Message.__init__(self, fp, seekable)
  26.         self.encodingheader = self.getheader('content-transfer-encoding')
  27.         self.typeheader = self.getheader('content-type')
  28.         self.parsetype()
  29.         self.parseplist()
  30.  
  31.     
  32.     def parsetype(self):
  33.         str = self.typeheader
  34.         if str is None:
  35.             str = 'text/plain'
  36.         if ';' in str:
  37.             i = str.index(';')
  38.             self.plisttext = str[i:]
  39.             str = str[:i]
  40.         else:
  41.             self.plisttext = ''
  42.         fields = str.split('/')
  43.         for i in range(len(fields)):
  44.             fields[i] = fields[i].strip().lower()
  45.         
  46.         self.type = '/'.join(fields)
  47.         self.maintype = fields[0]
  48.         self.subtype = '/'.join(fields[1:])
  49.  
  50.     
  51.     def parseplist(self):
  52.         str = self.plisttext
  53.         self.plist = []
  54.         while str[:1] == ';':
  55.             str = str[1:]
  56.             if ';' in str:
  57.                 end = str.index(';')
  58.             else:
  59.                 end = len(str)
  60.             f = str[:end]
  61.             if '=' in f:
  62.                 i = f.index('=')
  63.                 f = f[:i].strip().lower() + '=' + f[i + 1:].strip()
  64.             self.plist.append(f.strip())
  65.             str = str[end:]
  66.  
  67.     
  68.     def getplist(self):
  69.         return self.plist
  70.  
  71.     
  72.     def getparam(self, name):
  73.         name = name.lower() + '='
  74.         n = len(name)
  75.         for p in self.plist:
  76.             if p[:n] == name:
  77.                 return rfc822.unquote(p[n:])
  78.         
  79.  
  80.     
  81.     def getparamnames(self):
  82.         result = []
  83.         for p in self.plist:
  84.             i = p.find('=')
  85.             if i >= 0:
  86.                 result.append(p[:i].lower())
  87.                 continue
  88.         return result
  89.  
  90.     
  91.     def getencoding(self):
  92.         if self.encodingheader is None:
  93.             return '7bit'
  94.         return None.encodingheader.lower()
  95.  
  96.     
  97.     def gettype(self):
  98.         return self.type
  99.  
  100.     
  101.     def getmaintype(self):
  102.         return self.maintype
  103.  
  104.     
  105.     def getsubtype(self):
  106.         return self.subtype
  107.  
  108.  
  109.  
  110. try:
  111.     import thread
  112. except ImportError:
  113.     import dummy_thread as thread
  114.  
  115. _counter_lock = thread.allocate_lock()
  116. del thread
  117. _counter = 0
  118.  
  119. def _get_next_counter():
  120.     global _counter
  121.     _counter_lock.acquire()
  122.     _counter += 1
  123.     result = _counter
  124.     _counter_lock.release()
  125.     return result
  126.  
  127. _prefix = None
  128.  
  129. def choose_boundary():
  130.     global _prefix
  131.     import time
  132.     if _prefix is None:
  133.         import socket
  134.         
  135.         try:
  136.             hostid = socket.gethostbyname(socket.gethostname())
  137.         except socket.gaierror:
  138.             hostid = '127.0.0.1'
  139.  
  140.         
  141.         try:
  142.             uid = repr(os.getuid())
  143.         except AttributeError:
  144.             uid = '1'
  145.  
  146.         
  147.         try:
  148.             pid = repr(os.getpid())
  149.         except AttributeError:
  150.             pid = '1'
  151.  
  152.         _prefix = hostid + '.' + uid + '.' + pid
  153.     return '%s.%.3f.%d' % (_prefix, time.time(), _get_next_counter())
  154.  
  155.  
  156. def decode(input, output, encoding):
  157.     if encoding == 'base64':
  158.         import base64
  159.         return base64.decode(input, output)
  160.     if None == 'quoted-printable':
  161.         import quopri
  162.         return quopri.decode(input, output)
  163.     if None in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
  164.         import uu
  165.         return uu.decode(input, output)
  166.     if None in ('7bit', '8bit'):
  167.         return output.write(input.read())
  168.     if None in decodetab:
  169.         pipethrough(input, decodetab[encoding], output)
  170.     else:
  171.         raise ValueError, 'unknown Content-Transfer-Encoding: %s' % encoding
  172.  
  173.  
  174. def encode(input, output, encoding):
  175.     if encoding == 'base64':
  176.         import base64
  177.         return base64.encode(input, output)
  178.     if None == 'quoted-printable':
  179.         import quopri
  180.         return quopri.encode(input, output, 0)
  181.     if None in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
  182.         import uu
  183.         return uu.encode(input, output)
  184.     if None in ('7bit', '8bit'):
  185.         return output.write(input.read())
  186.     if None in encodetab:
  187.         pipethrough(input, encodetab[encoding], output)
  188.     else:
  189.         raise ValueError, 'unknown Content-Transfer-Encoding: %s' % encoding
  190.  
  191. uudecode_pipe = '(\nTEMP=/tmp/@uu.$$\nsed "s%^begin [0-7][0-7]* .*%begin 600 $TEMP%" | uudecode\ncat $TEMP\nrm $TEMP\n)'
  192. decodetab = {
  193.     'uuencode': uudecode_pipe,
  194.     'x-uuencode': uudecode_pipe,
  195.     'uue': uudecode_pipe,
  196.     'x-uue': uudecode_pipe,
  197.     'quoted-printable': 'mmencode -u -q',
  198.     'base64': 'mmencode -u -b' }
  199. encodetab = {
  200.     'x-uuencode': 'uuencode tempfile',
  201.     'uuencode': 'uuencode tempfile',
  202.     'x-uue': 'uuencode tempfile',
  203.     'uue': 'uuencode tempfile',
  204.     'quoted-printable': 'mmencode -q',
  205.     'base64': 'mmencode -b' }
  206.  
  207. def pipeto(input, command):
  208.     pipe = os.popen(command, 'w')
  209.     copyliteral(input, pipe)
  210.     pipe.close()
  211.  
  212.  
  213. def pipethrough(input, command, output):
  214.     (fd, tempname) = tempfile.mkstemp()
  215.     temp = os.fdopen(fd, 'w')
  216.     copyliteral(input, temp)
  217.     temp.close()
  218.     pipe = os.popen(command + ' <' + tempname, 'r')
  219.     copybinary(pipe, output)
  220.     pipe.close()
  221.     os.unlink(tempname)
  222.  
  223.  
  224. def copyliteral(input, output):
  225.     while None:
  226.         line = input.readline()
  227.         if not line:
  228.             break
  229.         continue
  230.         return None
  231.  
  232.  
  233. def copybinary(input, output):
  234.     BUFSIZE = 8192
  235.     while None:
  236.         line = input.read(BUFSIZE)
  237.         if not line:
  238.             break
  239.         continue
  240.         return None
  241.  
  242.