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

Public Member Functions

def __init__
 
def guess_type
 
def guess_extension
 
def read
 
def readfp
 

Data Fields

 encodings_map
 
 suffix_map
 
 types_map
 
 common_types
 

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 41 of file mimetypes.py.

Constructor & Destructor Documentation

def __init__ (   self,
  filenames = () 
)

Definition at line 49 of file mimetypes.py.

References mimetypes.init().

49 
50  def __init__(self, filenames=()):
51  if not inited:
52  init()
53  self.encodings_map = encodings_map.copy()
54  self.suffix_map = suffix_map.copy()
55  self.types_map = types_map.copy()
56  self.common_types = common_types.copy()
57  for name in filenames:
58  self.read(name)

Member Function Documentation

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

122  def guess_extension(self, type, strict=1):
123  """Guess the extension for a file based on its MIME type.
124 
125  Return value is a string giving a filename extension,
126  including the leading dot ('.'). The extension is not
127  guaranteed to have been associated with any particular data
128  stream, but would be mapped to the MIME type `type' by
129  guess_type(). If no extension can be guessed for `type', None
130  is returned.
131 
132  Optional `strict' argument when false adds a bunch of commonly found,
133  but non-standard types.
134  """
135  type = type.lower()
136  for ext, stype in self.types_map.items():
137  if type == stype:
138  return ext
139  if not strict:
140  for ext, stype in common_types.items():
141  if type == stype:
142  return ext
143  return None
def guess_type (   self,
  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 59 of file mimetypes.py.

References MimeTypes.common_types, MimeTypes.encodings_map, posixpath.splitext(), urllib.splittype(), MimeTypes.suffix_map, and MimeTypes.types_map.

59 
60  def guess_type(self, url, strict=1):
61  """Guess the type of a file based on its URL.
62 
63  Return value is a tuple (type, encoding) where type is None if
64  the type can't be guessed (no or unknown suffix) or a string
65  of the form type/subtype, usable for a MIME Content-type
66  header; and encoding is None for no encoding or the name of
67  the program used to encode (e.g. compress or gzip). The
68  mappings are table driven. Encoding suffixes are case
69  sensitive; type suffixes are first tried case sensitive, then
70  case insensitive.
71 
72  The suffixes .tgz, .taz and .tz (case sensitive!) are all
73  mapped to '.tar.gz'. (This is table-driven too, using the
74  dictionary suffix_map.)
75 
76  Optional `strict' argument when false adds a bunch of commonly found,
77  but non-standard types.
78  """
79  scheme, url = urllib.splittype(url)
80  if scheme == 'data':
81  # syntax of data URLs:
82  # dataurl := "data:" [ mediatype ] [ ";base64" ] "," data
83  # mediatype := [ type "/" subtype ] *( ";" parameter )
84  # data := *urlchar
85  # parameter := attribute "=" value
86  # type/subtype defaults to "text/plain"
87  comma = url.find(',')
88  if comma < 0:
89  # bad data URL
90  return None, None
91  semi = url.find(';', 0, comma)
92  if semi >= 0:
93  type = url[:semi]
94  else:
95  type = url[:comma]
96  if '=' in type or '/' not in type:
97  type = 'text/plain'
98  return type, None # never compressed, so encoding is None
99  base, ext = posixpath.splitext(url)
100  while self.suffix_map.has_key(ext):
101  base, ext = posixpath.splitext(base + self.suffix_map[ext])
102  if self.encodings_map.has_key(ext):
103  encoding = self.encodings_map[ext]
104  base, ext = posixpath.splitext(base)
105  else:
106  encoding = None
107  types_map = self.types_map
108  common_types = self.common_types
109  if types_map.has_key(ext):
110  return types_map[ext], encoding
111  elif types_map.has_key(ext.lower()):
112  return types_map[ext.lower()], encoding
113  elif strict:
114  return None, encoding
115  elif common_types.has_key(ext):
116  return common_types[ext], encoding
117  elif common_types.has_key(ext.lower()):
118  return common_types[ext.lower()], encoding
119  else:
120  return None, encoding
def read (   self,
  filename 
)
Read a single mime.types-format file, specified by pathname.

Definition at line 144 of file mimetypes.py.

References aifc.open(), MimeTypes.readfp(), and ConfigParser.readfp().

145  def read(self, filename):
146  """Read a single mime.types-format file, specified by pathname."""
147  fp = open(filename)
148  self.readfp(fp)
149  fp.close()
def readfp (   self,
  fp 
)
Read a single mime.types-format file.

Definition at line 150 of file mimetypes.py.

References MimeTypes.types_map.

151  def readfp(self, fp):
152  """Read a single mime.types-format file."""
153  map = self.types_map
154  while 1:
155  line = fp.readline()
156  if not line:
157  break
158  words = line.split()
159  for i in range(len(words)):
160  if words[i][0] == '#':
161  del words[i:]
162  break
163  if not words:
164  continue
165  type, suffixes = words[0], words[1:]
166  for suff in suffixes:
167  map['.' + suff] = type
168 

Field Documentation

common_types

Definition at line 55 of file mimetypes.py.

encodings_map

Definition at line 52 of file mimetypes.py.

suffix_map

Definition at line 53 of file mimetypes.py.

types_map

Definition at line 54 of file mimetypes.py.


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