Back to index

plone3  3.1.7
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions | Private Attributes | Static Private Attributes
MimetypesRegistry.MimeTypesRegistry.MimeTypesRegistry Class Reference
Inheritance diagram for MimetypesRegistry.MimeTypesRegistry.MimeTypesRegistry:
Inheritance graph
[legend]
Collaboration diagram for MimetypesRegistry.MimeTypesRegistry.MimeTypesRegistry:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def register
def register_mimetype
def register_extension
def register_glob
def unregister
def mimetypes
def list_mimetypes
def lookup
def lookupExtension
def globFilename
def lookupGlob
def classify
def __call__
def guess_encoding
def manage_delObjects
def manage_addMimeType
def manage_editMimeType

Public Attributes

 encodings_map
 suffix_map
 extensions
 globs

Static Public Attributes

string id = 'mimetypes_registry'
string meta_type = 'MimeTypes Registry'
int isPrincipiaFolderish = 1
tuple meta_types
tuple manage_options
tuple manage_addMimeTypeForm = PageTemplateFile('addMimeType', _www)
tuple manage_main = PageTemplateFile('listMimeTypes', _www)
tuple manage_editMimeTypeForm = PageTemplateFile('editMimeType', _www)
tuple security = ClassSecurityInfo()

Private Member Functions

def _classifiers

Private Attributes

 _mimetypes
 _new_style_mtr

Static Private Attributes

tuple __implements__ = (IMimetypesRegistry, ISourceAdapter)
int __allow_access_to_unprotected_subobjects__ = 1

Detailed Description

Mimetype registry that deals with
a) registering types
b) wildcarding of rfc-2046 types
c) classifying data into a given type

Definition at line 45 of file MimeTypesRegistry.py.


Constructor & Destructor Documentation

Definition at line 79 of file MimeTypesRegistry.py.

00079 
00080     def __init__(self,):
00081         self.encodings_map = encodings_map.copy()
00082         self.suffix_map = suffix_map.copy()
00083         # Major key -> minor IMimetype objects
00084         self._mimetypes  = PersistentMapping()
00085         # ext -> IMimetype mapping
00086         self.extensions = PersistentMapping()
00087         # glob -> (regex, mimetype) mapping
00088         self.globs = OOBTree()
00089         self.manage_addProperty('defaultMimetype', 'text/plain', 'string')
00090         self.manage_addProperty('unicodePolicies', 'strict ignore replace',
00091                                 'tokens')
00092         self.manage_addProperty('unicodePolicy', 'unicodePolicies', 'selection')
00093         self.manage_addProperty('fallbackEncoding', 'latin1', 'string')
00094 
00095         # initialize mime types
00096         initialize(self)
00097         self._new_style_mtr = 1


Member Function Documentation

Return a triple (data, filename, mimetypeobject) given
some raw data and optional paramters

method from the isourceAdapter interface

Definition at line 340 of file MimeTypesRegistry.py.

00340 
00341     def __call__(self, data, **kwargs):
00342         """ Return a triple (data, filename, mimetypeobject) given
00343         some raw data and optional paramters
00344 
00345         method from the isourceAdapter interface
00346         """
00347         mimetype = kwargs.get('mimetype', None)
00348         filename = kwargs.get('filename', None)
00349         encoding = kwargs.get('encoding', None)
00350         mt = None
00351         if hasattr(data, 'filename'):
00352             filename = os.path.basename(data.filename)
00353         elif hasattr(data, 'name'):
00354             filename = os.path.basename(data.name)
00355 
00356         if hasattr(data, 'read'):
00357             _data = data.read()
00358             if hasattr(data, 'seek'):
00359                 data.seek(0)
00360             data = _data
00361 
00362         # We need to figure out if data is binary and skip encoding if
00363         # it is
00364         mt = self.classify(data, mimetype=mimetype, filename=filename)
00365 
00366         if not mt.binary and not type(data) is UnicodeType:
00367             # if no encoding specified, try to guess it from data
00368             if encoding is None:
00369                 encoding = self.guess_encoding(data)
00370 
00371             # ugly workaround for
00372             # https://sourceforge.net/tracker/?func=detail&aid=1068001&group_id=75272&atid=543430
00373             # covered by
00374             # https://sourceforge.net/tracker/?func=detail&atid=355470&aid=843590&group_id=5470
00375             # dont remove this code unless python is fixed.
00376             if encoding is "macintosh":
00377                 encoding = 'mac_roman'
00378 
00379             try:
00380                 try:
00381                     data = unicode(data, encoding, self.unicodePolicy)
00382                 except (ValueError, LookupError):
00383                     # wrong unicodePolicy
00384                     data = unicode(data, encoding)
00385             except:
00386                 data = unicode(data, self.fallbackEncoding)
00387 
00388         return (data, filename, aq_base(mt))

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 284 of file MimeTypesRegistry.py.

00284 
00285     def _classifiers(self):
00286         return [mt for mt in self.mimetypes() if IClassifier.isImplementedBy(mt)]

Here is the call graph for this function:

Here is the caller graph for this function:

def MimetypesRegistry.MimeTypesRegistry.MimeTypesRegistry.classify (   self,
  data,
  mimetype = None,
  filename = None 
)
Classify works as follows:
1) you tell me the rfc-2046 name and I give you an IMimetype
   object
2) the filename includes an extension from which we can guess
   the mimetype
3) we can optionally introspect the data
4) default to self.defaultMimetype if no data was provided
   else to application/octet-stream of no filename was provided,
   else to text/plain

Return an IMimetype object or None 

Definition at line 288 of file MimeTypesRegistry.py.

00288 
00289     def classify(self, data, mimetype=None, filename=None):
00290         """Classify works as follows:
00291         1) you tell me the rfc-2046 name and I give you an IMimetype
00292            object
00293         2) the filename includes an extension from which we can guess
00294            the mimetype
00295         3) we can optionally introspect the data
00296         4) default to self.defaultMimetype if no data was provided
00297            else to application/octet-stream of no filename was provided,
00298            else to text/plain
00299 
00300         Return an IMimetype object or None 
00301         """
00302         mt = None
00303         if mimetype:
00304             mt = self.lookup(mimetype)
00305             if mt:
00306                 mt = mt[0]
00307         elif filename:
00308             mt = self.lookupExtension(filename)
00309             if mt is None:
00310                 mt = self.globFilename(filename)
00311         if data and not mt:
00312             for c in self._classifiers():
00313                 if c.classify(data):
00314                     mt = c
00315                     break
00316             if not mt:
00317                 mstr = magic.guessMime(data)
00318                 if mstr:
00319                     mt = self.lookup(mstr)[0]
00320         if not mt:
00321             if not data:
00322                 mtlist = self.lookup(self.defaultMimetype)
00323             elif filename:
00324                 mtlist = self.lookup('application/octet-stream')
00325             else:
00326                 failed = 'text/x-unknown-content-type'
00327                 filename = filename or ''
00328                 data = data or ''
00329                 ct, enc = guess_content_type(filename, data, None)
00330                 if ct == failed:
00331                     ct = 'text/plain'
00332                 mtlist = self.lookup(ct)
00333             if len(mtlist)>0:
00334                 mt = mtlist[0]
00335             else:
00336                 return None
00337 
00338         # Remove acquisition wrappers
00339         return aq_base(mt)

Here is the call graph for this function:

Here is the caller graph for this function:

Lookup for IMimetypes object matching filename

Filename must be a complete filename with extension.

Return an IMimetype object associated with the glob's or None

Definition at line 261 of file MimeTypesRegistry.py.

00261 
00262     def globFilename(self, filename):
00263         """Lookup for IMimetypes object matching filename
00264 
00265         Filename must be a complete filename with extension.
00266 
00267         Return an IMimetype object associated with the glob's or None
00268         """
00269         globs = getattr(self, 'globs', None)
00270         if globs is None:
00271             return None
00272         for key in globs.keys():
00273             glob, mimetype = globs[key]
00274             if glob.match(filename):
00275                 return aq_base(mimetype)
00276         return None

Here is the caller graph for this function:

Try to guess encoding from a text value if no encoding
guessed, used the default charset from site properties (Zope)
with a fallback to UTF-8 (should never happen with correct
site_properties, but always raise Attribute error without
Zope)

Definition at line 390 of file MimeTypesRegistry.py.

00390 
00391     def guess_encoding(self, data):
00392         """ Try to guess encoding from a text value if no encoding
00393         guessed, used the default charset from site properties (Zope)
00394         with a fallback to UTF-8 (should never happen with correct
00395         site_properties, but always raise Attribute error without
00396         Zope)
00397         """
00398         if type(data) is type(u''):
00399             # data maybe unicode but with another encoding specified
00400             data = data.encode('UTF-8')
00401         encoding = guess_encoding(data)
00402         if encoding is None:
00403             try:
00404                 site_props = self.portal_properties.site_properties
00405                 encoding = site_props.getProperty('default_charset', 'UTF-8')
00406             except:
00407                 encoding = 'UTF-8'
00408         return encoding

Here is the caller graph for this function:

Return all defined mime types, as string

Definition at line 200 of file MimeTypesRegistry.py.

00200 
00201     def list_mimetypes(self):
00202         """Return all defined mime types, as string"""
00203         return [str(mt) for mt in self.mimetypes()]

Here is the call graph for this function:

Lookup for IMimetypes object matching mimetypestring

mimetypestring may have an empty minor part or containing a
wildcard (*) mimetypestring may and IMimetype object (in this
case it will be returned unchanged

Return a list of mimetypes objects associated with the
RFC-2046 name return an empty list if no one is known.

Definition at line 205 of file MimeTypesRegistry.py.

00205 
00206     def lookup(self, mimetypestring):
00207         """Lookup for IMimetypes object matching mimetypestring
00208 
00209         mimetypestring may have an empty minor part or containing a
00210         wildcard (*) mimetypestring may and IMimetype object (in this
00211         case it will be returned unchanged
00212 
00213         Return a list of mimetypes objects associated with the
00214         RFC-2046 name return an empty list if no one is known.
00215         """
00216         if IMimetype.isImplementedBy(mimetypestring):
00217             return (aq_base(mimetypestring), )
00218         __traceback_info__ = (repr(mimetypestring), str(mimetypestring))
00219         major, minor = split(str(mimetypestring))
00220         group = self._mimetypes.get(major, {})
00221         if not minor or minor == '*':
00222             res = group.values()
00223         else:
00224             res = group.get(minor)
00225             if res:
00226                 res = (res,)
00227             else:
00228                 return ()
00229         return tuple([aq_base(mtitem) for mtitem in res])

Here is the call graph for this function:

Here is the caller graph for this function:

Lookup for IMimetypes object matching filename

Filename maybe a file name like 'content.txt' or an extension
like 'rest'

Return an IMimetype object associated with the file's
extension or None

Definition at line 231 of file MimeTypesRegistry.py.

00231 
00232     def lookupExtension(self, filename):
00233         """Lookup for IMimetypes object matching filename
00234 
00235         Filename maybe a file name like 'content.txt' or an extension
00236         like 'rest'
00237 
00238         Return an IMimetype object associated with the file's
00239         extension or None
00240         """
00241         if filename.find('.') != -1:
00242             base, ext = os.path.splitext(filename)
00243             ext = ext[1:] # remove the dot
00244             while self.suffix_map.has_key(ext):
00245                 base, ext = os.path.splitext(base + self.suffix_map[ext])
00246                 ext = ext[1:] # remove the dot
00247         else:
00248             ext = filename
00249             base = None
00250 
00251         # XXX This code below make no sense and may break because base
00252         # isn't defined.
00253         if self.encodings_map.has_key(ext) and base:
00254             encoding = self.encodings_map[ext]
00255             base, ext = os.path.splitext(base)
00256             ext = ext[1:] # remove the dot
00257         else:
00258             encoding = None
00259         return aq_base(self.extensions.get(ext))

Here is the caller graph for this function:

Definition at line 278 of file MimeTypesRegistry.py.

00278 
00279     def lookupGlob(self, glob):
00280         globs = getattr(self, 'globs', None)
00281         if globs is None:
00282             return None
00283         return aq_base(globs.get(glob))

def MimetypesRegistry.MimeTypesRegistry.MimeTypesRegistry.manage_addMimeType (   self,
  id,
  mimetypes,
  extensions,
  icon_path,
  binary = 0,
  globs = None,
  REQUEST = None 
)
add a mime type to the tool

Definition at line 419 of file MimeTypesRegistry.py.

00419 
00420                            binary=0, globs=None, REQUEST=None):
00421         """add a mime type to the tool"""
00422         mt = MimeTypeItem(id, mimetypes, extensions=extensions,
00423                           binary=binary, icon_path=icon_path, globs=globs)
00424         self.register(mt)
00425         if REQUEST is not None:
00426             REQUEST['RESPONSE'].redirect(self.absolute_url()+'/manage_main')

Here is the call graph for this function:

Here is the caller graph for this function:

delete the selected mime types 

Definition at line 410 of file MimeTypesRegistry.py.

00410 
00411     def manage_delObjects(self, ids, REQUEST=None):
00412         """ delete the selected mime types """
00413         for id in ids:
00414             self.unregister(self.lookup(id)[0])
00415         if REQUEST is not None:
00416             REQUEST['RESPONSE'].redirect(self.absolute_url()+'/manage_main')

Here is the call graph for this function:

Here is the caller graph for this function:

def MimetypesRegistry.MimeTypesRegistry.MimeTypesRegistry.manage_editMimeType (   self,
  name,
  new_name,
  mimetypes,
  extensions,
  icon_path,
  binary = 0,
  globs = None,
  REQUEST = None 
)
Edit a mime type by name

Definition at line 429 of file MimeTypesRegistry.py.

00429 
00430                             icon_path, binary=0, globs=None, REQUEST=None):
00431         """Edit a mime type by name
00432         """
00433         mt = self.lookup(name)[0]
00434         self.unregister(mt)
00435         mt.edit(new_name, mimetypes, extensions, icon_path=icon_path,
00436                 binary=binary, globs=globs)
00437         self.register(mt)
00438         if REQUEST is not None:
00439             REQUEST['RESPONSE'].redirect(self.absolute_url()+'/manage_main')
00440 
00441 InitializeClass(MimeTypesRegistry)
00442 registerToolInterface('mimetypes_registry', IMimetypesRegistryTool)
00443 

Here is the call graph for this function:

Here is the caller graph for this function:

Return all defined mime types, each one implements at least
IMimetype

Definition at line 188 of file MimeTypesRegistry.py.

00188 
00189     def mimetypes(self):
00190         """Return all defined mime types, each one implements at least
00191         IMimetype
00192         """
00193         res = {}
00194         for g in self._mimetypes.values():
00195             for mt in g.values():
00196                 res[mt] =1
00197         return [aq_base(mtitem) for mtitem in res.keys()]
00198 

Here is the caller graph for this function:

Register a new mimetype

mimetype must implement IMimetype

Definition at line 99 of file MimeTypesRegistry.py.

00099 
00100     def register(self, mimetype):
00101         """ Register a new mimetype
00102 
00103         mimetype must implement IMimetype
00104         """
00105         mimetype = aq_base(mimetype)
00106         assert IMimetype.isImplementedBy(mimetype)
00107         for t in mimetype.mimetypes:
00108             self.register_mimetype(t, mimetype)
00109         for extension in mimetype.extensions:
00110             self.register_extension(extension, mimetype)
00111         for glob in mimetype.globs:
00112             self.register_glob(glob, mimetype)

Here is the call graph for this function:

Here is the caller graph for this function:

Associate a file's extension to a IMimetype

extension is a string representing a file extension (not
prefixed by a dot) mimetype must implement IMimetype

Definition at line 126 of file MimeTypesRegistry.py.

00126 
00127     def register_extension(self, extension, mimetype):
00128         """ Associate a file's extension to a IMimetype
00129 
00130         extension is a string representing a file extension (not
00131         prefixed by a dot) mimetype must implement IMimetype
00132         """
00133         mimetype = aq_base(mimetype)
00134         if self.extensions.has_key(extension):
00135             if self.extensions.get(extension) != mimetype:
00136                 log('Warning: redefining extension %s from %s to %s' % (
00137                     extension, self.extensions[extension], mimetype))
00138         # we don't validate fmt yet, but its ["txt", "html"]
00139         self.extensions[extension] = mimetype

Here is the caller graph for this function:

Associate a glob to a IMimetype

glob is a shell-like glob that will be translated to a regex
to match against whole filename.
mimetype must implement IMimetype.

Definition at line 141 of file MimeTypesRegistry.py.

00141 
00142     def register_glob(self, glob, mimetype):
00143         """ Associate a glob to a IMimetype
00144 
00145         glob is a shell-like glob that will be translated to a regex
00146         to match against whole filename.
00147         mimetype must implement IMimetype.
00148         """
00149         globs = getattr(self, 'globs', None)
00150         if globs is None:
00151             self.globs = globs = OOBTree()
00152         mimetype = aq_base(mimetype)
00153         existing = globs.get(glob)
00154         if existing is not None:
00155             regex, mt = existing
00156             if mt != mimetype:
00157                 log('Warning: redefining glob %s from %s to %s' % (
00158                     glob, mt, mimetype))
00159         # we don't validate fmt yet, but its ["txt", "html"]
00160         pattern = re.compile(fnmatch.translate(glob))
00161         globs[glob] = (pattern, mimetype)

Here is the caller graph for this function:

Definition at line 114 of file MimeTypesRegistry.py.

00114 
00115     def register_mimetype(self, mt, mimetype):
00116         major, minor = split(mt)
00117         if not major or not minor or minor == '*':
00118             raise MimeTypeException('Can\'t register mime type %s' % mt)
00119         group = self._mimetypes.setdefault(major, PersistentMapping())
00120         if group.has_key(minor):
00121             if group.get(minor) != mimetype:
00122                 log('Warning: redefining mime type %s (%s)' % (
00123                     mt, mimetype.__class__))
00124         group[minor] = mimetype

Here is the call graph for this function:

Here is the caller graph for this function:

Unregister a new mimetype

mimetype must implement IMimetype

Definition at line 163 of file MimeTypesRegistry.py.

00163 
00164     def unregister(self, mimetype):
00165         """ Unregister a new mimetype
00166 
00167         mimetype must implement IMimetype
00168         """
00169         assert IMimetype.isImplementedBy(mimetype)
00170         for t in mimetype.mimetypes:
00171             major, minor = split(t)
00172             group = self._mimetypes.get(major, {})
00173             if group.get(minor) == mimetype:
00174                 del group[minor]
00175         for e in mimetype.extensions:
00176             if self.extensions.get(e) == mimetype:
00177                 del self.extensions[e]
00178         globs = getattr(self, 'globs', None)
00179         if globs is not None:
00180             for glob in mimetype.globs:
00181                 existing = globs.get(glob)
00182                 if existing is None:
00183                     continue
00184                 regex, mt = existing
00185                 if mt == mimetype:
00186                     del globs[glob]

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 77 of file MimeTypesRegistry.py.

tuple MimetypesRegistry.MimeTypesRegistry.MimeTypesRegistry.__implements__ = (IMimetypesRegistry, ISourceAdapter) [static, private]

Definition at line 52 of file MimeTypesRegistry.py.

Definition at line 83 of file MimeTypesRegistry.py.

Definition at line 96 of file MimeTypesRegistry.py.

Definition at line 80 of file MimeTypesRegistry.py.

Definition at line 85 of file MimeTypesRegistry.py.

Definition at line 87 of file MimeTypesRegistry.py.

string MimetypesRegistry.MimeTypesRegistry.MimeTypesRegistry.id = 'mimetypes_registry' [static]

Definition at line 55 of file MimeTypesRegistry.py.

Definition at line 57 of file MimeTypesRegistry.py.

tuple MimetypesRegistry.MimeTypesRegistry.MimeTypesRegistry.manage_addMimeTypeForm = PageTemplateFile('addMimeType', _www) [static]

Definition at line 70 of file MimeTypesRegistry.py.

tuple MimetypesRegistry.MimeTypesRegistry.MimeTypesRegistry.manage_editMimeTypeForm = PageTemplateFile('editMimeType', _www) [static]

Definition at line 72 of file MimeTypesRegistry.py.

tuple MimetypesRegistry.MimeTypesRegistry.MimeTypesRegistry.manage_main = PageTemplateFile('listMimeTypes', _www) [static]

Definition at line 71 of file MimeTypesRegistry.py.

Initial value:
(
        ( { 'label'   : 'MimeTypes',
            'action' : 'manage_main'},) +
        Folder.manage_options[2:]
        )

Definition at line 64 of file MimeTypesRegistry.py.

Definition at line 56 of file MimeTypesRegistry.py.

Initial value:
all_meta_types(
        { 'name'   : 'MimeType',
          'action' : 'manage_addMimeTypeForm'},
        )

Definition at line 59 of file MimeTypesRegistry.py.

Definition at line 74 of file MimeTypesRegistry.py.

Definition at line 81 of file MimeTypesRegistry.py.


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