[Checkins] SVN: Sandbox/nadako/z3c.mimetype/src/z3c/mimetype/ Add convenience api.
Dan Korostelev
nadako at gmail.com
Tue Feb 24 07:55:11 EST 2009
Log message for revision 97205:
Add convenience api.
Changed:
U Sandbox/nadako/z3c.mimetype/src/z3c/mimetype/README.txt
U Sandbox/nadako/z3c.mimetype/src/z3c/mimetype/__init__.py
U Sandbox/nadako/z3c.mimetype/src/z3c/mimetype/configure.zcml
U Sandbox/nadako/z3c.mimetype/src/z3c/mimetype/interfaces.py
U Sandbox/nadako/z3c.mimetype/src/z3c/mimetype/mimetype.py
-=-
Modified: Sandbox/nadako/z3c.mimetype/src/z3c/mimetype/README.txt
===================================================================
--- Sandbox/nadako/z3c.mimetype/src/z3c/mimetype/README.txt 2009-02-24 12:46:39 UTC (rev 97204)
+++ Sandbox/nadako/z3c.mimetype/src/z3c/mimetype/README.txt 2009-02-24 12:55:10 UTC (rev 97205)
@@ -1,3 +1,4 @@
+==================
Mime type guessing
==================
@@ -170,3 +171,58 @@
>>> translate(mt.title, target_language='ru')
u'\u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 PNG'
+
+Convenience API
+---------------
+
+The root module, ``z3c.mimetype`` provides a convenience API for easy using
+MIME types detection and lookup. It provides a IConvenienceAPI interface::
+
+ >>> import z3c.mimetype
+ >>> from z3c.mimetype.interfaces import IConvenienceAPI
+ >>> IConvenienceAPI.providedBy(z3c.mimetype)
+ True
+
+This interface defines two functions::
+
+ >>> sorted(IConvenienceAPI)
+ ['getType', 'lookup']
+
+The lookup function is used for getting IMIMEType objects by their media and
+subtype names. You can specify media and subtype as two arguments or as one,
+first argument in the "media/subtype" form.
+
+Here's how to use it with single argument::
+
+ >>> mt = z3c.mimetype.lookup('text/plain')
+ >>> mt
+ <_MIMEType text/plain>
+
+ >>> IMIMEType.providedBy(mt)
+ True
+
+Here's how to use passing separate media and subtype arguments::
+
+ >>> z3c.mimetype.lookup('image', 'png')
+ <_MIMEType image/png>
+
+The ``getType`` function is just a wrapper for the ``getType`` function of
+global mimetype utility, so it works like described above::
+
+ >>> z3c.mimetype.getType()
+ Traceback (most recent call last):
+ ...
+ TypeError: Either filename or file should be provided or both of them
+
+ >>> print z3c.mimetype.getType(filename='wrong.doc')
+ application/msword
+
+ >>> fpng = openSample('png')
+ >>> print z3c.mimetype.getType(file=fpng)
+ image/png
+
+ >>> funknownbinary = openSample('binary')
+ >>> print util.getType(filename='somefile2.txt', file=funknownbinary)
+ text/plain
+
+ >>> del fpng, funknownbinary
Modified: Sandbox/nadako/z3c.mimetype/src/z3c/mimetype/__init__.py
===================================================================
--- Sandbox/nadako/z3c.mimetype/src/z3c/mimetype/__init__.py 2009-02-24 12:46:39 UTC (rev 97204)
+++ Sandbox/nadako/z3c.mimetype/src/z3c/mimetype/__init__.py 2009-02-24 12:55:10 UTC (rev 97205)
@@ -14,3 +14,13 @@
'''
$Id$
'''
+from zope.interface import moduleProvides
+from z3c.mimetype.interfaces import IConvenienceAPI
+from z3c.mimetype.utility import globalMIMETypesUtility
+from z3c.mimetype.mimetype import lookup
+
+def getType(filename=None, file=None):
+ return globalMIMETypesUtility.getType(filename, file)
+
+moduleProvides(IConvenienceAPI)
+__all__ = tuple(IConvenienceAPI)
Modified: Sandbox/nadako/z3c.mimetype/src/z3c/mimetype/configure.zcml
===================================================================
--- Sandbox/nadako/z3c.mimetype/src/z3c/mimetype/configure.zcml 2009-02-24 12:46:39 UTC (rev 97204)
+++ Sandbox/nadako/z3c.mimetype/src/z3c/mimetype/configure.zcml 2009-02-24 12:55:10 UTC (rev 97205)
@@ -1,10 +1,10 @@
<configure xmlns="http://namespaces.zope.org/zope">
- <utility component=".utility.globalMIMETypesUtility" />
+ <utility component=".utility.globalMIMETypesUtility" />
+
+ <utility
+ component=".mimetype.mimeTypesTranslationDomain"
+ name="shared-mime-info"
+ />
- <utility
- component=".mimetype.mimeTypesTranslationDomain"
- name="shared-mime-info"
- />
-
</configure>
Modified: Sandbox/nadako/z3c.mimetype/src/z3c/mimetype/interfaces.py
===================================================================
--- Sandbox/nadako/z3c.mimetype/src/z3c/mimetype/interfaces.py 2009-02-24 12:46:39 UTC (rev 97204)
+++ Sandbox/nadako/z3c.mimetype/src/z3c/mimetype/interfaces.py 2009-02-24 12:55:10 UTC (rev 97205)
@@ -28,7 +28,13 @@
'''Return type guessed by data. Accepts file-like object'''
def getType(filename=None, file=None):
- '''Try to guess content type either by file name or contents or both'''
+ '''Guess content type either by file name or contents or both.
+
+ At least one of these arguments should be provided.
+
+ This method always returns some useful mimetype,
+ ``application/octet-stream`` or ``text/plain``.
+ '''
class IMIMEType(Interface):
'''Single MIME type representation'''
@@ -50,3 +56,20 @@
def __str__():
'''Return a ``media/subtype`` presentation of mime type'''
+
+class IConvenienceAPI(Interface):
+ '''Convenience API to be provided by the __init__ module.'''
+
+ def getType(filename=None, file=None):
+ '''Guess content type either by file name or contents or both
+
+ See IMIMETypesUtility.getType documentation.
+ '''
+
+ def lookup(media, subtype=None):
+ '''Return a IMIMEType object for given string representation.
+
+ If ``media`` argument is in the "media/subtype" form, then the
+ ``subtype`` argument should't be specified and will be extracted
+ from the ``media`` argument.
+ '''
Modified: Sandbox/nadako/z3c.mimetype/src/z3c/mimetype/mimetype.py
===================================================================
--- Sandbox/nadako/z3c.mimetype/src/z3c/mimetype/mimetype.py 2009-02-24 12:46:39 UTC (rev 97204)
+++ Sandbox/nadako/z3c.mimetype/src/z3c/mimetype/mimetype.py 2009-02-24 12:55:10 UTC (rev 97205)
@@ -79,3 +79,6 @@
def __str__(self):
return self.media + '/' + self.subtype
+
+ def __repr__(self):
+ return '<%s %s/%s>' % (self.__class__.__name__, self.media, self.subtype)
More information about the Checkins
mailing list