{ "info": { "author": "Dan Korostelev and Zope Community", "author_email": "zope-dev@zope.org", "bugtrack_url": null, "classifiers": [], "description": "==================\nz3c.sharedmimeinfo\n==================\n\nThis package provides an utility for guessing MIME type from file name and/or \nactual contents. It's based on freedesktop.org's shared-mime-info database.\n\n.. contents::\n\nShared MIME info database\n-------------------------\n\nThe `shared-mime-info `_\nis a extensible database of common MIME types. It provides powerful MIME type\ndetection mechanism as well as multi-lingual type descriptions.\n\nThis package requires shared-mime-info to be installed and accessible. The\neasiest way to do that is to install it system-wide, for example installing\nthe ``shared-mime-info`` package on Ubuntu. The specification_ also describes\nother ways to install and extend the database.\n\n.. _specification: http://standards.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-0.13.html#s2_layout\n\nThread-safety\n-------------\n\nNote, that this package is currently not thread-safe, because data are meant to\nbe loaded only once, on module import. If there will be any problems because of\nthat, it could be changed in future.\n\nMIME type guessing\n------------------\n\nThe easiest way to use this package is to import the ``getType`` function from\nthe root module::\n\n >>> from z3c.sharedmimeinfo import getType\n\nThis function tries to guess the MIME type as specified in shared-mime-info\nspecification document and always returns some usable MIME type, using\napplication/octet-stream or text/plain as fallback. It can detect MIME type by\nfile name, its contents or both, so it accepts two arguments: filename (string)\nand/or file (file-like object). At least one of them should be given.\n\nAs said above, it needs at least one argument, so you can't call it with no\narguments::\n\n >>> getType()\n Traceback (most recent call last):\n ...\n TypeError: Either filename or file should be provided or both of them\n\nPassing file name is done via the ``filename`` argument::\n\n >>> print getType(filename='document.doc')\n application/msword\n\nPassing file contents is done via ``file`` argument, which accepts a file-like\nobject. Let's use our testing helper function to open a sample file and try\nto guess a type for it::\n\n >>> print getType(file=openSample('png'))\n image/png\n\nIf the MIME type cannot be detected, either ``text/plain`` or\n``application/octet-stream`` will be returned. The function will try to guess\nis it text or binary by checking the first 32 bytes:: \n\n >>> print getType(filename='somefile', file=openSample('text'))\n text/plain\n\n >>> print getType(filename='somefile', file=openSample('binary'))\n application/octet-stream\n\nMIME type objects\n-----------------\n\nObjects returned by ``getType`` and other functions (see below) are actually\nan extended unicode string objects, providing additional info about the MIME\ntype. They provide the IMIMEType interface::\n\n >>> from zope.interface.verify import verifyObject\n >>> from z3c.sharedmimeinfo.interfaces import IMIMEType\n\n >>> mt = getType(filename='document.doc')\n >>> verifyObject(IMIMEType, mt)\n True\n\nAs they are actually unicode objects, they can be compared like strings::\n\n >>> mt == 'application/msword'\n True\n\nThey also provides the ``media`` and ``subtype`` attributes::\n\n >>> mt.media\n u'application'\n\n >>> mt.subtype\n u'msword'\n\nAnd finally, they provide the ``title`` attribute that is a translatable\nmessage::\n\n >>> mt.title\n u'application/msword' \n\n >>> from zope.i18nmessageid.message import Message\n >>> isinstance(mt.title, Message)\n True\n\nLet's check the i18n features that comes with shared-mime-info and are\nsupported by this package. As seen above, the MIME type title message ID is\nactually its /, but if we translate it, we'll get a\nhuman-friendly string::\n\n >>> from zope.i18n import translate\n \n >>> translate(mt.title)\n u'Word document'\n\n >>> translate(mt.title, target_language='ru')\n u'\\u0434\\u043e\\u043a\\u0443\\u043c\\u0435\\u043d\\u0442 Word'\n\n >>> from z3c.sharedmimeinfo.mimetype import MIMEType\n\nWe can also create IMIMEType objects by hand, using the MIMEType class::\n\n >>> from z3c.sharedmimeinfo.mimetype import MIMEType\n\nWe can create them specifying media and subtype as two arguments or as a single\nargument in the \"media/subtype\" form::\n\n >>> MIMEType('text/plain')\n \n\n >>> MIMEType('image', 'png')\n \n\nNote, that the MIMEType objects are cached, so if you you'll create another\nobject for the same mime type, you'll get the same object::\n\n >>> mt = MIMEType('text/plain')\n >>> mt2 = MIMEType('text/plain')\n >>> mt2 is mt\n True\n\nAdvanced usage\n--------------\n\nThe ``getType`` function, described above is actually a method of the\nIMIMETypesUtility object. The IMIMETypesUtility is a core component for\nguessing MIME types.\n\nLet's import the utility directly and play with it::\n\n >>> from z3c.sharedmimeinfo.utility import mimeTypesUtility\n\n >>> from z3c.sharedmimeinfo.interfaces import IMIMETypesUtility\n >>> verifyObject(IMIMETypesUtility, mimeTypesUtility)\n True\n\nIt has three methods for getting mime type. Those three methods are\n``getType`` (described above), ``getTypeByFileName``, ``getTypeByContents``.\n\nDetection by file name\n~~~~~~~~~~~~~~~~~~~~~~\n\nThe ``getTypeByFileName`` method of the MIME types utility looks up the type\nby filename::\n\n >>> mt = mimeTypesUtility.getTypeByFileName('example.doc')\n\nshared-mime-info database is really nice, it can even detect mime type for\nfile names like ``Makefile``::\n\n >>> print mimeTypesUtility.getTypeByFileName('Makefile')\n text/x-makefile\n\nAlso, it know the difference in extension letter case. For example the ``.C``\nshould be detected as C++ file, when ``.c`` is plain C file::\n\n >>> print mimeTypesUtility.getTypeByFileName('hello.C')\n text/x-c++src\n \n >>> print mimeTypesUtility.getTypeByFileName('main.c')\n text/x-csrc\n\nThe method will return ``None`` if it fails determining type from file name::\n\n >>> print mimeTypesUtility.getTypeByFileName('somefilename')\n None\n\nDetection by contents\n~~~~~~~~~~~~~~~~~~~~~\n\nThe ``getTypeByContents`` method accepts a file-like object and two optional\narguments: min_priority and max_priority that can be used to specify the range\nof \"magic\" rules to be used. By default, min_priority is 0 and max_priority is\n100, so all rules will be in use. See shared-mime-info specification for\ndetails.\n\nWe have some sample files that should be detected by contents::\n\n >>> fdoc = openSample('doc')\n >>> print mimeTypesUtility.getTypeByContents(fdoc)\n application/msword\n\n >>> fhtml = openSample('html')\n >>> print mimeTypesUtility.getTypeByContents(fhtml)\n text/html\n \n >>> fpdf = openSample('pdf')\n >>> print mimeTypesUtility.getTypeByContents(fpdf)\n application/pdf\n\n >>> fpng = openSample('png')\n >>> print mimeTypesUtility.getTypeByContents(fpng)\n image/png\n\nIf we pass the file without any known magic bytes, it will return ``None``::\n\n >>> funknown = openSample('binary')\n >>> print mimeTypesUtility.getTypeByContents(funknown)\n None\n\n >>> del fdoc, fhtml, fpdf, fpng, funknown\n\n\n=======\nCHANGES\n=======\n\n0.1.0 (2009-09-08)\n------------------\n\n- Initial release.", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://pypi.python.org/pypi/z3c.sharedmimeinfo", "keywords": null, "license": "ZPL 2.1", "maintainer": null, "maintainer_email": null, "name": "z3c.sharedmimeinfo", "package_url": "https://pypi.org/project/z3c.sharedmimeinfo/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/z3c.sharedmimeinfo/", "project_urls": { "Download": "UNKNOWN", "Homepage": "http://pypi.python.org/pypi/z3c.sharedmimeinfo" }, "release_url": "https://pypi.org/project/z3c.sharedmimeinfo/0.1.0/", "requires_dist": null, "requires_python": null, "summary": "MIME type guessing framework for Zope, based on shared-mime-info", "version": "0.1.0" }, "last_serial": 802110, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "9f40c6aed4e14a0c7f13d9cc9f39f89e", "sha256": "7c982338a9ef38e3c6bfef10787dff9aacec3a288c5ca75317483a8de41d30cd" }, "downloads": -1, "filename": "z3c.sharedmimeinfo-0.1.0.tar.gz", "has_sig": false, "md5_digest": "9f40c6aed4e14a0c7f13d9cc9f39f89e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 29296, "upload_time": "2009-09-08T21:32:04", "url": "https://files.pythonhosted.org/packages/5c/cb/8e4a885bd7107d804a2ca05b53d85bcccef6791a0daddc72d585266c7943/z3c.sharedmimeinfo-0.1.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "9f40c6aed4e14a0c7f13d9cc9f39f89e", "sha256": "7c982338a9ef38e3c6bfef10787dff9aacec3a288c5ca75317483a8de41d30cd" }, "downloads": -1, "filename": "z3c.sharedmimeinfo-0.1.0.tar.gz", "has_sig": false, "md5_digest": "9f40c6aed4e14a0c7f13d9cc9f39f89e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 29296, "upload_time": "2009-09-08T21:32:04", "url": "https://files.pythonhosted.org/packages/5c/cb/8e4a885bd7107d804a2ca05b53d85bcccef6791a0daddc72d585266c7943/z3c.sharedmimeinfo-0.1.0.tar.gz" } ] }