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

Data Structures

class  MimeTypes
 

Functions

def guess_type
 
def guess_extension
 
def init
 
def read_mime_types
 
def usage
 

Variables

list __all__ = ["guess_type","guess_extension","read_mime_types","init"]
 
list knownfiles
 
int inited = 0
 
dictionary suffix_map
 
dictionary encodings_map
 
dictionary types_map
 
dictionary common_types
 
string USAGE
 
int strict = 1
 
int extension = 0
 
tuple guess = guess_extension(gtype, strict)
 

Detailed Description

Guess the MIME type of a file.

This module defines two useful functions:

guess_type(url, strict=1) -- guess the MIME type and encoding of a URL.

guess_extension(type, strict=1) -- guess the extension for a given MIME type.

It also contains the following, for tuning the behavior:

Data:

knownfiles -- list of files to parse
inited -- flag set when init() has been called
suffix_map -- dictionary mapping suffixes to suffixes
encodings_map -- dictionary mapping suffixes to encodings
types_map -- dictionary mapping suffixes to types

Functions:

init([files]) -- parse a list of files, default knownfiles
read_mime_types(file) -- parse one file, return a dictionary or None

Function Documentation

def mimetypes.guess_extension (   type,
  strict = 1 
)
Guess the extension for a file based on its MIME type.

Return value is a string giving a filename extension, including the
leading dot ('.').  The extension is not guaranteed to have been
associated with any particular data stream, but would be mapped to the
MIME type `type' by guess_type().  If no extension can be guessed for
`type', None is returned.

Optional `strict' argument when false adds a bunch of commonly found,
but non-standard types.

Definition at line 191 of file mimetypes.py.

References init().

192 def guess_extension(type, strict=1):
193  """Guess the extension for a file based on its MIME type.
194 
195  Return value is a string giving a filename extension, including the
196  leading dot ('.'). The extension is not guaranteed to have been
197  associated with any particular data stream, but would be mapped to the
198  MIME type `type' by guess_type(). If no extension can be guessed for
199  `type', None is returned.
200 
201  Optional `strict' argument when false adds a bunch of commonly found,
202  but non-standard types.
203  """
204  init()
205  return guess_extension(type, strict)
206 
def mimetypes.guess_type (   url,
  strict = 1 
)
Guess the type of a file based on its URL.

Return value is a tuple (type, encoding) where type is None if the
type can't be guessed (no or unknown suffix) or a string of the
form type/subtype, usable for a MIME Content-type header; and
encoding is None for no encoding or the name of the program used
to encode (e.g. compress or gzip).  The mappings are table
driven.  Encoding suffixes are case sensitive; type suffixes are
first tried case sensitive, then case insensitive.

The suffixes .tgz, .taz and .tz (case sensitive!) are all mapped
to ".tar.gz".  (This is table-driven too, using the dictionary
suffix_map).

Optional `strict' argument when false adds a bunch of commonly found, but
non-standard types.

Definition at line 169 of file mimetypes.py.

References init().

170 def guess_type(url, strict=1):
171  """Guess the type of a file based on its URL.
172 
173  Return value is a tuple (type, encoding) where type is None if the
174  type can't be guessed (no or unknown suffix) or a string of the
175  form type/subtype, usable for a MIME Content-type header; and
176  encoding is None for no encoding or the name of the program used
177  to encode (e.g. compress or gzip). The mappings are table
178  driven. Encoding suffixes are case sensitive; type suffixes are
179  first tried case sensitive, then case insensitive.
180 
181  The suffixes .tgz, .taz and .tz (case sensitive!) are all mapped
182  to ".tar.gz". (This is table-driven too, using the dictionary
183  suffix_map).
184 
185  Optional `strict' argument when false adds a bunch of commonly found, but
186  non-standard types.
187  """
188  init()
189  return guess_type(url, strict)
190 
def mimetypes.init (   files = None)

Definition at line 207 of file mimetypes.py.

References aifc.open().

208 def init(files=None):
209  global guess_extension, guess_type
210  global suffix_map, types_map, encodings_map, common_types
211  global inited
212  inited = 1
213  db = MimeTypes()
214  if files is None:
215  files = knownfiles
216  for file in files:
217  if os.path.isfile(file):
218  db.readfp(open(file))
219  encodings_map = db.encodings_map
220  suffix_map = db.suffix_map
221  types_map = db.types_map
222  guess_extension = db.guess_extension
223  guess_type = db.guess_type
224  common_types = db.common_types
225 
def mimetypes.read_mime_types (   file)

Definition at line 226 of file mimetypes.py.

References aifc.open().

227 def read_mime_types(file):
228  try:
229  f = open(file)
230  except IOError:
231  return None
232  db = MimeTypes()
233  db.readfp(f)
234  return db.types_map
235 
def mimetypes.usage (   code,
  msg = '' 
)

Definition at line 407 of file mimetypes.py.

References getopt.getopt().

408  def usage(code, msg=''):
409  print USAGE
410  if msg: print msg
411  sys.exit(code)

Variable Documentation

list __all__ = ["guess_type","guess_extension","read_mime_types","init"]

Definition at line 29 of file mimetypes.py.

dictionary common_types
Initial value:
1 = {
2  '.jpg' : 'image/jpg',
3  '.mid' : 'audio/midi',
4  '.midi': 'audio/midi',
5  '.pct' : 'image/pict',
6  '.pic' : 'image/pict',
7  '.pict': 'image/pict',
8  '.rtf' : 'application/rtf',
9  '.xul' : 'text/xul'
10  }

Definition at line 379 of file mimetypes.py.

dictionary encodings_map
Initial value:
1 = {
2  '.gz': 'gzip',
3  '.Z': 'compress',
4  }

Definition at line 242 of file mimetypes.py.

int extension = 0

Definition at line 419 of file mimetypes.py.

tuple guess = guess_extension(gtype, strict)

Definition at line 429 of file mimetypes.py.

int inited = 0

Definition at line 38 of file mimetypes.py.

list knownfiles
Initial value:
1 = [
2  "/usr/local/etc/httpd/conf/mime.types",
3  "/usr/local/lib/netscape/mime.types",
4  "/usr/local/etc/httpd/conf/mime.types", # Apache 1.2
5  "/usr/local/etc/mime.types", # Apache 1.3
6  ]

Definition at line 31 of file mimetypes.py.

int strict = 1

Definition at line 418 of file mimetypes.py.

dictionary suffix_map
Initial value:
1 = {
2  '.tgz': '.tar.gz',
3  '.taz': '.tar.gz',
4  '.tz': '.tar.gz',
5  }

Definition at line 236 of file mimetypes.py.

dictionary types_map

Definition at line 252 of file mimetypes.py.

string USAGE
Initial value:
1 = """\
2 Usage: mimetypes.py [options] type
3 
4 Options:
5  --help / -h -- print this message and exit
6  --lenient / -l -- additionally search of some common, but non-standard
7  types.
8  --extension / -e -- guess extension instead of type
9 
10 More than one type argument may be given.
11 """

Definition at line 395 of file mimetypes.py.