Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
mimetools Namespace Reference

Data Structures

class  Message
 

Functions

def choose_boundary
 
def decode
 
def encode
 
def pipeto
 
def pipethrough
 
def copyliteral
 
def copybinary
 

Variables

list __all__
 
 _prefix = None
 
string uudecode_pipe
 
dictionary decodetab
 
dictionary encodetab
 

Detailed Description

Various tools used by MIME-reading or MIME-writing programs.

Function Documentation

def mimetools.choose_boundary ( )
Return a random string usable as a multipart boundary.
The method used is so that it is *very* unlikely that the same
string of characters will every occur again in the Universe,
so the caller needn't check the data it is packing for the
occurrence of the boundary.

The boundary contains dots so you have to quote it in the header.

Definition at line 101 of file mimetools.py.

References random.randint().

102 def choose_boundary():
103  """Return a random string usable as a multipart boundary.
104  The method used is so that it is *very* unlikely that the same
105  string of characters will every occur again in the Universe,
106  so the caller needn't check the data it is packing for the
107  occurrence of the boundary.
108 
109  The boundary contains dots so you have to quote it in the header."""
110 
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  try:
119  uid = `os.getuid()`
120  except:
121  uid = '1'
122  try:
123  pid = `os.getpid()`
124  except:
125  pid = '1'
126  _prefix = hostid + '.' + uid + '.' + pid
127  timestamp = '%.3f' % time.time()
128  seed = `random.randint(0, 32767)`
129  return _prefix + '.' + timestamp + '.' + seed
130 
131 
132 # Subroutines for decoding some common content-transfer-types
def mimetools.copybinary (   input,
  output 
)

Definition at line 221 of file mimetools.py.

222 def copybinary(input, output):
223  BUFSIZE = 8192
224  while 1:
225  line = input.read(BUFSIZE)
226  if not line: break
227  output.write(line)
def mimetools.copyliteral (   input,
  output 
)

Definition at line 215 of file mimetools.py.

216 def copyliteral(input, output):
217  while 1:
218  line = input.readline()
219  if not line: break
220  output.write(line)
def mimetools.decode (   input,
  output,
  encoding 
)
Decode common content-transfer-encodings (base64, quopri, uuencode).

Definition at line 133 of file mimetools.py.

References base64.decode(), uu.decode(), quopri.decode(), and pipethrough().

134 def decode(input, output, encoding):
135  """Decode common content-transfer-encodings (base64, quopri, uuencode)."""
136  if encoding == 'base64':
137  import base64
138  return base64.decode(input, output)
139  if encoding == 'quoted-printable':
140  import quopri
141  return quopri.decode(input, output)
142  if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
143  import uu
144  return uu.decode(input, output)
145  if encoding in ('7bit', '8bit'):
146  return output.write(input.read())
147  if decodetab.has_key(encoding):
148  pipethrough(input, decodetab[encoding], output)
149  else:
150  raise ValueError, \
151  'unknown Content-Transfer-Encoding: %s' % encoding
def mimetools.encode (   input,
  output,
  encoding 
)
Encode common content-transfer-encodings (base64, quopri, uuencode).

Definition at line 152 of file mimetools.py.

References base64.encode(), quopri.encode(), uu.encode(), and pipethrough().

153 def encode(input, output, encoding):
154  """Encode common content-transfer-encodings (base64, quopri, uuencode)."""
155  if encoding == 'base64':
156  import base64
157  return base64.encode(input, output)
158  if encoding == 'quoted-printable':
159  import quopri
160  return quopri.encode(input, output, 0)
161  if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
162  import uu
163  return uu.encode(input, output)
164  if encoding in ('7bit', '8bit'):
165  return output.write(input.read())
166  if encodetab.has_key(encoding):
167  pipethrough(input, encodetab[encoding], output)
168  else:
169  raise ValueError, \
170  'unknown Content-Transfer-Encoding: %s' % encoding
171 
172 # The following is no longer used for standard encodings
173 
174 # XXX This requires that uudecode and mmencode are in $PATH
def mimetools.pipethrough (   input,
  command,
  output 
)

Definition at line 205 of file mimetools.py.

References copybinary(), copyliteral(), tempfile.mktemp(), and aifc.open().

206 def pipethrough(input, command, output):
207  tempname = tempfile.mktemp()
208  temp = open(tempname, 'w')
209  copyliteral(input, temp)
210  temp.close()
211  pipe = os.popen(command + ' <' + tempname, 'r')
212  copybinary(pipe, output)
213  pipe.close()
214  os.unlink(tempname)
def mimetools.pipeto (   input,
  command 
)

Definition at line 200 of file mimetools.py.

References copyliteral().

201 def pipeto(input, command):
202  pipe = os.popen(command, 'w')
203  copyliteral(input, pipe)
204  pipe.close()

Variable Documentation

list __all__
Initial value:
1 = ["Message","choose_boundary","encode","decode","copyliteral",
2  "copybinary"]

Definition at line 8 of file mimetools.py.

_prefix = None

Definition at line 99 of file mimetools.py.

dictionary decodetab
Initial value:
1 = {
2  'uuencode': uudecode_pipe,
3  'x-uuencode': uudecode_pipe,
4  'uue': uudecode_pipe,
5  'x-uue': uudecode_pipe,
6  'quoted-printable': 'mmencode -u -q',
7  'base64': 'mmencode -u -b',
8 }

Definition at line 182 of file mimetools.py.

dictionary encodetab
Initial value:
1 = {
2  'x-uuencode': 'uuencode tempfile',
3  'uuencode': 'uuencode tempfile',
4  'x-uue': 'uuencode tempfile',
5  'uue': 'uuencode tempfile',
6  'quoted-printable': 'mmencode -q',
7  'base64': 'mmencode -b',
8 }

Definition at line 191 of file mimetools.py.

string uudecode_pipe
Initial value:
1 = '''(
2 TEMP=/tmp/@uu.$$
3 sed "s%^begin [0-7][0-7]* .*%begin 600 $TEMP%" | uudecode
4 cat $TEMP
5 rm $TEMP
6 )'''

Definition at line 175 of file mimetools.py.