Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
mimetypes.MimeTypes Class Reference

List of all members.

Public Member Functions

def __init__
def add_type
def guess_type
def guess_all_extensions
def guess_extension
def read
def readfp
def read_windows_registry

Public Attributes

 encodings_map
 suffix_map
 types_map
 types_map_inv

Detailed Description

MIME-types datastore.

This datastore can handle information from mime.types-style files
and supports basic determination of MIME type from a filename or
URL, and can guess a reasonable extension given a MIME type.

Definition at line 56 of file mimetypes.py.


Constructor & Destructor Documentation

def mimetypes.MimeTypes.__init__ (   self,
  filenames = (),
  strict = True 
)

Definition at line 64 of file mimetypes.py.

00064 
00065     def __init__(self, filenames=(), strict=True):
00066         if not inited:
00067             init()
00068         self.encodings_map = encodings_map.copy()
00069         self.suffix_map = suffix_map.copy()
00070         self.types_map = ({}, {}) # dict for (non-strict, strict)
00071         self.types_map_inv = ({}, {})
00072         for (ext, type) in types_map.items():
00073             self.add_type(type, ext, True)
00074         for (ext, type) in common_types.items():
00075             self.add_type(type, ext, False)
00076         for name in filenames:
00077             self.read(name, strict)

Here is the call graph for this function:

Here is the caller graph for this function:


Member Function Documentation

def mimetypes.MimeTypes.add_type (   self,
  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 78 of file mimetypes.py.

00078 
00079     def add_type(self, type, ext, strict=True):
00080         """Add a mapping between a type and an extension.
00081 
00082         When the extension is already known, the new
00083         type will replace the old one. When the type
00084         is already known the extension will be added
00085         to the list of known extensions.
00086 
00087         If strict is true, information will be added to
00088         list of standard types, else to the list of non-standard
00089         types.
00090         """
00091         self.types_map[strict][ext] = type
00092         exts = self.types_map_inv[strict].setdefault(type, [])
00093         if ext not in exts:
00094             exts.append(ext)

Here is the caller graph for this function:

def mimetypes.MimeTypes.guess_all_extensions (   self,
  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().

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

Definition at line 157 of file mimetypes.py.

00157 
00158     def guess_all_extensions(self, type, strict=True):
00159         """Guess the extensions for a file based on its MIME type.
00160 
00161         Return value is a list of strings giving the possible filename
00162         extensions, including the leading dot ('.').  The extension is not
00163         guaranteed to have been associated with any particular data stream,
00164         but would be mapped to the MIME type `type' by guess_type().
00165 
00166         Optional `strict' argument when false adds a bunch of commonly found,
00167         but non-standard types.
00168         """
00169         type = type.lower()
00170         extensions = self.types_map_inv[True].get(type, [])
00171         if not strict:
00172             for ext in self.types_map_inv[False].get(type, []):
00173                 if ext not in extensions:
00174                     extensions.append(ext)
00175         return extensions

Here is the call graph for this function:

Here is the caller graph for this function:

def mimetypes.MimeTypes.guess_extension (   self,
  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 176 of file mimetypes.py.

00176 
00177     def guess_extension(self, type, strict=True):
00178         """Guess the extension for a file based on its MIME type.
00179 
00180         Return value is a string giving a filename extension,
00181         including the leading dot ('.').  The extension is not
00182         guaranteed to have been associated with any particular data
00183         stream, but would be mapped to the MIME type `type' by
00184         guess_type().  If no extension can be guessed for `type', None
00185         is returned.
00186 
00187         Optional `strict' argument when false adds a bunch of commonly found,
00188         but non-standard types.
00189         """
00190         extensions = self.guess_all_extensions(type, strict)
00191         if not extensions:
00192             return None
00193         return extensions[0]

Here is the call graph for this function:

def mimetypes.MimeTypes.guess_type (   self,
  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 95 of file mimetypes.py.

00095 
00096     def guess_type(self, url, strict=True):
00097         """Guess the type of a file based on its URL.
00098 
00099         Return value is a tuple (type, encoding) where type is None if
00100         the type can't be guessed (no or unknown suffix) or a string
00101         of the form type/subtype, usable for a MIME Content-type
00102         header; and encoding is None for no encoding or the name of
00103         the program used to encode (e.g. compress or gzip).  The
00104         mappings are table driven.  Encoding suffixes are case
00105         sensitive; type suffixes are first tried case sensitive, then
00106         case insensitive.
00107 
00108         The suffixes .tgz, .taz and .tz (case sensitive!) are all
00109         mapped to '.tar.gz'.  (This is table-driven too, using the
00110         dictionary suffix_map.)
00111 
00112         Optional `strict' argument when False adds a bunch of commonly found,
00113         but non-standard types.
00114         """
00115         scheme, url = urllib.parse.splittype(url)
00116         if scheme == 'data':
00117             # syntax of data URLs:
00118             # dataurl   := "data:" [ mediatype ] [ ";base64" ] "," data
00119             # mediatype := [ type "/" subtype ] *( ";" parameter )
00120             # data      := *urlchar
00121             # parameter := attribute "=" value
00122             # type/subtype defaults to "text/plain"
00123             comma = url.find(',')
00124             if comma < 0:
00125                 # bad data URL
00126                 return None, None
00127             semi = url.find(';', 0, comma)
00128             if semi >= 0:
00129                 type = url[:semi]
00130             else:
00131                 type = url[:comma]
00132             if '=' in type or '/' not in type:
00133                 type = 'text/plain'
00134             return type, None           # never compressed, so encoding is None
00135         base, ext = posixpath.splitext(url)
00136         while ext in self.suffix_map:
00137             base, ext = posixpath.splitext(base + self.suffix_map[ext])
00138         if ext in self.encodings_map:
00139             encoding = self.encodings_map[ext]
00140             base, ext = posixpath.splitext(base)
00141         else:
00142             encoding = None
00143         types_map = self.types_map[True]
00144         if ext in types_map:
00145             return types_map[ext], encoding
00146         elif ext.lower() in types_map:
00147             return types_map[ext.lower()], encoding
00148         elif strict:
00149             return None, encoding
00150         types_map = self.types_map[False]
00151         if ext in types_map:
00152             return types_map[ext], encoding
00153         elif ext.lower() in types_map:
00154             return types_map[ext.lower()], encoding
00155         else:
00156             return None, encoding

Here is the call graph for this function:

def mimetypes.MimeTypes.read (   self,
  filename,
  strict = True 
)
Read a single mime.types-format file, specified by pathname.

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

Definition at line 194 of file mimetypes.py.

00194 
00195     def read(self, filename, strict=True):
00196         """
00197         Read a single mime.types-format file, specified by pathname.
00198 
00199         If strict is true, information will be added to
00200         list of standard types, else to the list of non-standard
00201         types.
00202         """
00203         with open(filename) as fp:
00204             self.readfp(fp, strict)

Here is the call graph for this function:

Here is the caller graph for this function:

def mimetypes.MimeTypes.read_windows_registry (   self,
  strict = True 
)
Load the MIME types database from Windows registry.

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

Definition at line 228 of file mimetypes.py.

00228 
00229     def read_windows_registry(self, strict=True):
00230         """
00231         Load the MIME types database from Windows registry.
00232 
00233         If strict is true, information will be added to
00234         list of standard types, else to the list of non-standard
00235         types.
00236         """
00237 
00238         # Windows only
00239         if not _winreg:
00240             return
00241 
00242         def enum_types(mimedb):
00243             i = 0
00244             while True:
00245                 try:
00246                     ctype = _winreg.EnumKey(mimedb, i)
00247                 except EnvironmentError:
00248                     break
00249                 else:
00250                     yield ctype
00251                 i += 1
00252 
00253         default_encoding = sys.getdefaultencoding()
00254         with _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT,
00255                              r'MIME\Database\Content Type') as mimedb:
00256             for ctype in enum_types(mimedb):
00257                 try:
00258                     with _winreg.OpenKey(mimedb, ctype) as key:
00259                         suffix, datatype = _winreg.QueryValueEx(key,
00260                                                                 'Extension')
00261                 except EnvironmentError:
00262                     continue
00263                 if datatype != _winreg.REG_SZ:
00264                     continue
00265                 self.add_type(ctype, suffix, strict)
00266 

Here is the call graph for this function:

def mimetypes.MimeTypes.readfp (   self,
  fp,
  strict = True 
)
Read a single mime.types-format file.

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

Definition at line 205 of file mimetypes.py.

00205 
00206     def readfp(self, fp, strict=True):
00207         """
00208         Read a single mime.types-format file.
00209 
00210         If strict is true, information will be added to
00211         list of standard types, else to the list of non-standard
00212         types.
00213         """
00214         while 1:
00215             line = fp.readline()
00216             if not line:
00217                 break
00218             words = line.split()
00219             for i in range(len(words)):
00220                 if words[i][0] == '#':
00221                     del words[i:]
00222                     break
00223             if not words:
00224                 continue
00225             type, suffixes = words[0], words[1:]
00226             for suff in suffixes:
00227                 self.add_type(type, '.' + suff, strict)

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 67 of file mimetypes.py.

Definition at line 68 of file mimetypes.py.

Definition at line 69 of file mimetypes.py.

Definition at line 70 of file mimetypes.py.


The documentation for this class was generated from the following file: