home *** CD-ROM | disk | FTP | other *** search
/ com!online 2005 April / com_0405_1.iso / opensource / BTpp-0.5.4-bin.exe / $INSTDIR / BT++.exe / mimetools.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-04-19  |  7.4 KB  |  231 lines

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