[Checkins] SVN: z3c.mimetype/trunk/ Add __cmp__ to IMIMEType objects, so they could be compared to strings and other mime type objects.

Dan Korostelev nadako at gmail.com
Mon Aug 31 10:52:04 EDT 2009


Log message for revision 103405:
  Add __cmp__ to IMIMEType objects, so they could be compared to strings and other mime type objects.
  Add a test that checks that IMIMEType objects are singletons in a scope of one mime type.

Changed:
  _U  z3c.mimetype/trunk/
  U   z3c.mimetype/trunk/src/z3c/mimetype/README.txt
  U   z3c.mimetype/trunk/src/z3c/mimetype/interfaces.py
  U   z3c.mimetype/trunk/src/z3c/mimetype/mimetype.py

-=-

Property changes on: z3c.mimetype/trunk
___________________________________________________________________
Modified: svn:ignore
   - bin
coverage
docs
parts
.installed.cfg

   + bin
coverage
docs
parts
.installed.cfg
develop-eggs


Modified: z3c.mimetype/trunk/src/z3c/mimetype/README.txt
===================================================================
--- z3c.mimetype/trunk/src/z3c/mimetype/README.txt	2009-08-31 14:45:37 UTC (rev 103404)
+++ z3c.mimetype/trunk/src/z3c/mimetype/README.txt	2009-08-31 14:52:03 UTC (rev 103405)
@@ -97,8 +97,8 @@
 ~~~~~~~~~~~~~~~~~~~~~
 
 Another useful method is ``getTypeByContents``. It's first argument should
-be an opened file. Also, it can take ``min_priority`` and ``max_priority`` arguments,
-but it's only useful if you know the shared-mime-info specification.
+be file-like object. Also, it can take ``min_priority`` and ``max_priority``
+arguments, but it's only useful if you know the shared-mime-info specification.
 
 We have some sample files that should be detected by contents::
 
@@ -226,6 +226,13 @@
   >>> z3c.mimetype.lookup('image', 'png')
   <_MIMEType image/png>
 
+Note, that the IMIMETypes objects are singletons (one object per mime type), so
+if you youse lookup function with same mime type once again, you'll get the same
+object::
+
+  >>> z3c.mimetype.lookup('text/plain') is mt
+  True
+
 The ``getType`` function is just a wrapper for the ``getType`` function of
 global mimetype utility, so it works like described above::
 
@@ -246,3 +253,37 @@
   text/plain
 
   >>> del fpng, funknownbinary
+
+MIME type object comparsion
+---------------------------
+
+It's own useful to compare a mime type to some string if we need to check if
+some file is of specific type, the IMIMEType objects should be comparable to
+strings::
+
+  >>> mt = z3c.mimetype.lookup('image/png')
+  >>> mt == 'image/png'
+  True
+  >>> mt == u'image/png'
+  True
+  >>> 'image/png' == mt 
+  True
+  >>> u'image/png' == mt 
+  True
+
+Of course, it can also be compared to other IMIMEType objects::
+
+  >>> mt2 = z3c.mimetype.lookup('image/jpeg')
+  >>> mt == mt2
+  False
+
+Let's use some internal API to create a second instance of image/png IMIMEType
+object and try to compare it with the first one::
+
+  >>> from z3c.mimetype.mimetype import _MIMEType, MIME_TYPES
+  >>> del MIME_TYPES[('image', 'png')]
+  >>> mt3 = _MIMEType('image', 'png')
+  >>> mt is not mt3
+  True
+  >>> mt == mt3
+  True

Modified: z3c.mimetype/trunk/src/z3c/mimetype/interfaces.py
===================================================================
--- z3c.mimetype/trunk/src/z3c/mimetype/interfaces.py	2009-08-31 14:45:37 UTC (rev 103404)
+++ z3c.mimetype/trunk/src/z3c/mimetype/interfaces.py	2009-08-31 14:52:03 UTC (rev 103405)
@@ -59,7 +59,10 @@
     def __str__():
         """Return a ``media/subtype`` presentation of mime type"""
 
+    def __cmp__(string_or_mimetype):
+        """Mime type objects should be comparable to strings and other types"""
 
+
 class IConvenienceAPI(Interface):
     """Convenience API to be provided by the __init__ module."""
     

Modified: z3c.mimetype/trunk/src/z3c/mimetype/mimetype.py
===================================================================
--- z3c.mimetype/trunk/src/z3c/mimetype/mimetype.py	2009-08-31 14:45:37 UTC (rev 103404)
+++ z3c.mimetype/trunk/src/z3c/mimetype/mimetype.py	2009-08-31 14:52:03 UTC (rev 103405)
@@ -86,3 +86,8 @@
 
     def __repr__(self):
         return '<%s %s/%s>' % (self.__class__.__name__, self.media, self.subtype)
+
+    def __cmp__(self, other):
+        if IMIMEType.providedBy(other):
+            other = str(other)
+        return cmp(str(self), other)



More information about the checkins mailing list