home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Lib / mimetools.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-06-23  |  7.9 KB  |  220 lines

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