Back to index

python3.2  3.2.2
Classes | Functions | Variables
mimetypes Namespace Reference

Classes

class  MimeTypes

Functions

def guess_type
def guess_all_extensions
def guess_extension
def add_type
def init
def read_mime_types
def _default_mime_types
def usage

Variables

 _winreg = None
list __all__
list knownfiles
 inited = False
 _db = None
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=True) -- guess the MIME type and encoding of a URL.

guess_extension(type, strict=True) -- 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 (on Windows, the
  default values are taken from the registry)
read_mime_types(file) -- parse one file, return a dictionary or None

Function Documentation

def mimetypes._default_mime_types ( ) [private]

Definition at line 370 of file mimetypes.py.

00370 
00371 def _default_mime_types():
00372     global suffix_map
00373     global encodings_map
00374     global types_map
00375     global common_types
00376 
00377     suffix_map = {
00378         '.svgz': '.svg.gz',
00379         '.tgz': '.tar.gz',
00380         '.taz': '.tar.gz',
00381         '.tz': '.tar.gz',
00382         '.tbz2': '.tar.bz2',
00383         }
00384 
00385     encodings_map = {
00386         '.gz': 'gzip',
00387         '.Z': 'compress',
00388         '.bz2': 'bzip2',
00389         }
00390 
00391     # Before adding new types, make sure they are either registered with IANA,
00392     # at http://www.iana.org/assignments/media-types
00393     # or extensions, i.e. using the x- prefix
00394 
00395     # If you add to these, please keep them sorted!
00396     types_map = {
00397         '.a'      : 'application/octet-stream',
00398         '.ai'     : 'application/postscript',
00399         '.aif'    : 'audio/x-aiff',
00400         '.aifc'   : 'audio/x-aiff',
00401         '.aiff'   : 'audio/x-aiff',
00402         '.au'     : 'audio/basic',
00403         '.avi'    : 'video/x-msvideo',
00404         '.bat'    : 'text/plain',
00405         '.bcpio'  : 'application/x-bcpio',
00406         '.bin'    : 'application/octet-stream',
00407         '.bmp'    : 'image/x-ms-bmp',
00408         '.c'      : 'text/plain',
00409         # Duplicates :(
00410         '.cdf'    : 'application/x-cdf',
00411         '.cdf'    : 'application/x-netcdf',
00412         '.cpio'   : 'application/x-cpio',
00413         '.csh'    : 'application/x-csh',
00414         '.css'    : 'text/css',
00415         '.dll'    : 'application/octet-stream',
00416         '.doc'    : 'application/msword',
00417         '.dot'    : 'application/msword',
00418         '.dvi'    : 'application/x-dvi',
00419         '.eml'    : 'message/rfc822',
00420         '.eps'    : 'application/postscript',
00421         '.etx'    : 'text/x-setext',
00422         '.exe'    : 'application/octet-stream',
00423         '.gif'    : 'image/gif',
00424         '.gtar'   : 'application/x-gtar',
00425         '.h'      : 'text/plain',
00426         '.hdf'    : 'application/x-hdf',
00427         '.htm'    : 'text/html',
00428         '.html'   : 'text/html',
00429         '.ico'    : 'image/vnd.microsoft.icon',
00430         '.ief'    : 'image/ief',
00431         '.jpe'    : 'image/jpeg',
00432         '.jpeg'   : 'image/jpeg',
00433         '.jpg'    : 'image/jpeg',
00434         '.js'     : 'application/x-javascript',
00435         '.ksh'    : 'text/plain',
00436         '.latex'  : 'application/x-latex',
00437         '.m1v'    : 'video/mpeg',
00438         '.man'    : 'application/x-troff-man',
00439         '.me'     : 'application/x-troff-me',
00440         '.mht'    : 'message/rfc822',
00441         '.mhtml'  : 'message/rfc822',
00442         '.mif'    : 'application/x-mif',
00443         '.mov'    : 'video/quicktime',
00444         '.movie'  : 'video/x-sgi-movie',
00445         '.mp2'    : 'audio/mpeg',
00446         '.mp3'    : 'audio/mpeg',
00447         '.mp4'    : 'video/mp4',
00448         '.mpa'    : 'video/mpeg',
00449         '.mpe'    : 'video/mpeg',
00450         '.mpeg'   : 'video/mpeg',
00451         '.mpg'    : 'video/mpeg',
00452         '.ms'     : 'application/x-troff-ms',
00453         '.nc'     : 'application/x-netcdf',
00454         '.nws'    : 'message/rfc822',
00455         '.o'      : 'application/octet-stream',
00456         '.obj'    : 'application/octet-stream',
00457         '.oda'    : 'application/oda',
00458         '.p12'    : 'application/x-pkcs12',
00459         '.p7c'    : 'application/pkcs7-mime',
00460         '.pbm'    : 'image/x-portable-bitmap',
00461         '.pdf'    : 'application/pdf',
00462         '.pfx'    : 'application/x-pkcs12',
00463         '.pgm'    : 'image/x-portable-graymap',
00464         '.pl'     : 'text/plain',
00465         '.png'    : 'image/png',
00466         '.pnm'    : 'image/x-portable-anymap',
00467         '.pot'    : 'application/vnd.ms-powerpoint',
00468         '.ppa'    : 'application/vnd.ms-powerpoint',
00469         '.ppm'    : 'image/x-portable-pixmap',
00470         '.pps'    : 'application/vnd.ms-powerpoint',
00471         '.ppt'    : 'application/vnd.ms-powerpoint',
00472         '.ps'     : 'application/postscript',
00473         '.pwz'    : 'application/vnd.ms-powerpoint',
00474         '.py'     : 'text/x-python',
00475         '.pyc'    : 'application/x-python-code',
00476         '.pyo'    : 'application/x-python-code',
00477         '.qt'     : 'video/quicktime',
00478         '.ra'     : 'audio/x-pn-realaudio',
00479         '.ram'    : 'application/x-pn-realaudio',
00480         '.ras'    : 'image/x-cmu-raster',
00481         '.rdf'    : 'application/xml',
00482         '.rgb'    : 'image/x-rgb',
00483         '.roff'   : 'application/x-troff',
00484         '.rtx'    : 'text/richtext',
00485         '.sgm'    : 'text/x-sgml',
00486         '.sgml'   : 'text/x-sgml',
00487         '.sh'     : 'application/x-sh',
00488         '.shar'   : 'application/x-shar',
00489         '.snd'    : 'audio/basic',
00490         '.so'     : 'application/octet-stream',
00491         '.src'    : 'application/x-wais-source',
00492         '.sv4cpio': 'application/x-sv4cpio',
00493         '.sv4crc' : 'application/x-sv4crc',
00494         '.svg'    : 'image/svg+xml',
00495         '.swf'    : 'application/x-shockwave-flash',
00496         '.t'      : 'application/x-troff',
00497         '.tar'    : 'application/x-tar',
00498         '.tcl'    : 'application/x-tcl',
00499         '.tex'    : 'application/x-tex',
00500         '.texi'   : 'application/x-texinfo',
00501         '.texinfo': 'application/x-texinfo',
00502         '.tif'    : 'image/tiff',
00503         '.tiff'   : 'image/tiff',
00504         '.tr'     : 'application/x-troff',
00505         '.tsv'    : 'text/tab-separated-values',
00506         '.txt'    : 'text/plain',
00507         '.ustar'  : 'application/x-ustar',
00508         '.vcf'    : 'text/x-vcard',
00509         '.wav'    : 'audio/x-wav',
00510         '.wiz'    : 'application/msword',
00511         '.wsdl'   : 'application/xml',
00512         '.xbm'    : 'image/x-xbitmap',
00513         '.xlb'    : 'application/vnd.ms-excel',
00514         # Duplicates :(
00515         '.xls'    : 'application/excel',
00516         '.xls'    : 'application/vnd.ms-excel',
00517         '.xml'    : 'text/xml',
00518         '.xpdl'   : 'application/xml',
00519         '.xpm'    : 'image/x-xpixmap',
00520         '.xsl'    : 'application/xml',
00521         '.xwd'    : 'image/x-xwindowdump',
00522         '.zip'    : 'application/zip',
00523         }
00524 
00525     # These are non-standard types, commonly found in the wild.  They will
00526     # only match if strict=0 flag is given to the API methods.
00527 
00528     # Please sort these too
00529     common_types = {
00530         '.jpg' : 'image/jpg',
00531         '.mid' : 'audio/midi',
00532         '.midi': 'audio/midi',
00533         '.pct' : 'image/pict',
00534         '.pic' : 'image/pict',
00535         '.pict': 'image/pict',
00536         '.rtf' : 'application/rtf',
00537         '.xul' : 'text/xul'
00538         }
00539 
00540 
00541 _default_mime_types()
00542 

Here is the caller graph for this function:

def mimetypes.add_type (   type,
  ext,
  strict = True 
)
Add a mapping between a type and an extension.

When the extension is already known, the new
type will replace the old one. When the type
is already known the extension will be added
to the list of known extensions.

If strict is true, information will be added to
list of standard types, else to the list of non-standard
types.

Definition at line 323 of file mimetypes.py.

00323 
00324 def add_type(type, ext, strict=True):
00325     """Add a mapping between a type and an extension.
00326 
00327     When the extension is already known, the new
00328     type will replace the old one. When the type
00329     is already known the extension will be added
00330     to the list of known extensions.
00331 
00332     If strict is true, information will be added to
00333     list of standard types, else to the list of non-standard
00334     types.
00335     """
00336     if _db is None:
00337         init()
00338     return _db.add_type(type, ext, strict)
00339 

Here is the call graph for this function:

def mimetypes.guess_all_extensions (   type,
  strict = True 
)
Guess the extensions for a file based on its MIME type.

Return value is a list of strings giving the possible filename
extensions, 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 290 of file mimetypes.py.

00290 
00291 def guess_all_extensions(type, strict=True):
00292     """Guess the extensions for a file based on its MIME type.
00293 
00294     Return value is a list of strings giving the possible filename
00295     extensions, including the leading dot ('.').  The extension is not
00296     guaranteed to have been associated with any particular data
00297     stream, but would be mapped to the MIME type `type' by
00298     guess_type().  If no extension can be guessed for `type', None
00299     is returned.
00300 
00301     Optional `strict' argument when false adds a bunch of commonly found,
00302     but non-standard types.
00303     """
00304     if _db is None:
00305         init()
00306     return _db.guess_all_extensions(type, strict)

Here is the call graph for this function:

def mimetypes.guess_extension (   type,
  strict = True 
)
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 307 of file mimetypes.py.

00307 
00308 def guess_extension(type, strict=True):
00309     """Guess the extension for a file based on its MIME type.
00310 
00311     Return value is a string giving a filename extension, including the
00312     leading dot ('.').  The extension is not guaranteed to have been
00313     associated with any particular data stream, but would be mapped to the
00314     MIME type `type' by guess_type().  If no extension can be guessed for
00315     `type', None is returned.
00316 
00317     Optional `strict' argument when false adds a bunch of commonly found,
00318     but non-standard types.
00319     """
00320     if _db is None:
00321         init()
00322     return _db.guess_extension(type, strict)

Here is the call graph for this function:

Here is the caller graph for this function:

def mimetypes.guess_type (   url,
  strict = True 
)
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 267 of file mimetypes.py.

00267 
00268 def guess_type(url, strict=True):
00269     """Guess the type of a file based on its URL.
00270 
00271     Return value is a tuple (type, encoding) where type is None if the
00272     type can't be guessed (no or unknown suffix) or a string of the
00273     form type/subtype, usable for a MIME Content-type header; and
00274     encoding is None for no encoding or the name of the program used
00275     to encode (e.g. compress or gzip).  The mappings are table
00276     driven.  Encoding suffixes are case sensitive; type suffixes are
00277     first tried case sensitive, then case insensitive.
00278 
00279     The suffixes .tgz, .taz and .tz (case sensitive!) are all mapped
00280     to ".tar.gz".  (This is table-driven too, using the dictionary
00281     suffix_map).
00282 
00283     Optional `strict' argument when false adds a bunch of commonly found, but
00284     non-standard types.
00285     """
00286     if _db is None:
00287         init()
00288     return _db.guess_type(url, strict)
00289 

Here is the call graph for this function:

Here is the caller graph for this function:

def mimetypes.init (   files = None)

Definition at line 340 of file mimetypes.py.

00340 
00341 def init(files=None):
00342     global suffix_map, types_map, encodings_map, common_types
00343     global inited, _db
00344     inited = True    # so that MimeTypes.__init__() doesn't call us again
00345     db = MimeTypes()
00346     if files is None:
00347         if _winreg:
00348             db.read_windows_registry()
00349         files = knownfiles
00350     for file in files:
00351         if os.path.isfile(file):
00352             db.read(file)
00353     encodings_map = db.encodings_map
00354     suffix_map = db.suffix_map
00355     types_map = db.types_map[True]
00356     common_types = db.types_map[False]
00357     # Make the DB a global variable now that it is fully initialized
00358     _db = db
00359 

Here is the caller graph for this function:

def mimetypes.read_mime_types (   file)

Definition at line 360 of file mimetypes.py.

00360 
00361 def read_mime_types(file):
00362     try:
00363         f = open(file)
00364     except IOError:
00365         return None
00366     db = MimeTypes()
00367     db.readfp(f, True)
00368     return db.types_map[True]
00369 

def mimetypes.usage (   code,
  msg = '' 
)

Definition at line 558 of file mimetypes.py.

00558 
00559     def usage(code, msg=''):
00560         print(USAGE)
00561         if msg: print(msg)
00562         sys.exit(code)

Here is the call graph for this function:


Variable Documentation

Initial value:
00001 [
00002     "guess_type","guess_extension","guess_all_extensions",
00003     "add_type","read_mime_types","init"
00004 ]

Definition at line 35 of file mimetypes.py.

mimetypes._db = None

Definition at line 53 of file mimetypes.py.

Definition at line 33 of file mimetypes.py.

Definition at line 570 of file mimetypes.py.

Definition at line 580 of file mimetypes.py.

Definition at line 52 of file mimetypes.py.

Initial value:
00001 [
00002     "/etc/mime.types",
00003     "/etc/httpd/mime.types",                    # Mac OS X
00004     "/etc/httpd/conf/mime.types",               # Apache
00005     "/etc/apache/mime.types",                   # Apache 1
00006     "/etc/apache2/mime.types",                  # Apache 2
00007     "/usr/local/etc/httpd/conf/mime.types",
00008     "/usr/local/lib/netscape/mime.types",
00009     "/usr/local/etc/httpd/conf/mime.types",     # Apache 1.2
00010     "/usr/local/etc/mime.types",                # Apache 1.3
00011     ]

Definition at line 40 of file mimetypes.py.

Definition at line 569 of file mimetypes.py.

Initial value:
00001 """\
00002 Usage: mimetypes.py [options] type
00003 
00004 Options:
00005     --help / -h       -- print this message and exit
00006     --lenient / -l    -- additionally search of some common, but non-standard
00007                          types.
00008     --extension / -e  -- guess extension instead of type
00009 
00010 More than one type argument may be given.
00011 """

Definition at line 546 of file mimetypes.py.