3 \2R@sdZddlZddlZddlZddlZy ddlZWnek rHdZYnXddddddd d d d d ddg Z dddddddddg Z da da GdddZ d6ddZd7ddZd8ddZd9dd Zd:dd Zd d Zd!d"Zeed#kr ddlZd$Zd;d&d'Zy&ejejd(dd)d*d+d,g\ZZWn0ejk rRZzed(eWYddZ[XnXd(ZdZxDeD]<\Z Z!e dkrbd(ZqbWxheD]`Z"eree"eZ#e#se$d3e"ne$e#n.ee"e\Z#Z%e#se$d3e"ne$d4e#d5e%qWdS)?aGuess the MIME type of a file. This module defines two useful functions: guess_type(url, strict=True) -- guess the MIME type and encoding of a URL. guess_extension(type, strict=True) -- guess the extension for a given MIME type. It also contains the following, for tuning the behavior: Data: knownfiles -- list of files to parse inited -- flag set when init() has been called suffix_map -- dictionary mapping suffixes to suffixes encodings_map -- dictionary mapping suffixes to encodings types_map -- dictionary mapping suffixes to types Functions: init([files]) -- parse a list of files, default knownfiles (on Windows, the default values are taken from the registry) read_mime_types(file) -- parse one file, return a dictionary or None N knownfilesinited MimeTypes guess_typeguess_all_extensionsguess_extensionadd_typeinitread_mime_types suffix_map encodings_map types_map common_typesz/etc/mime.typesz/etc/httpd/mime.typesz/etc/httpd/conf/mime.typesz/etc/apache/mime.typesz/etc/apache2/mime.typesz$/usr/local/etc/httpd/conf/mime.typesz"/usr/local/lib/netscape/mime.typesz/usr/local/etc/mime.typesFc@sdeZdZdZfdfddZdddZdddZdd d Zdd d Zdd dZ dddZ dddZ dS)rzMIME-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. TcCsts ttj|_tj|_iif|_iif|_x"tjD]\}}|j||dq.enum_typesr5z Content Type)r>OpenKeyZHKEY_CLASSES_ROOT startswithZ QueryValueExZREG_SZrr?)rrr@ZhkcrZ subkeynameZsubkeyZmimetypeZdatatyperrrread_windows_registrys     zMimeTypes.read_windows_registryN)T)T)T)T)T)T)T) __name__ __module__ __qualname____doc__rrrrrrr0rDrrrrr:s  >   TcCstdkrttj||S)aGuess 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. N)_dbr r)r)rrrrrscCstdkrttj||S)aGuess the extensions for a file based on its MIME type. Return value is a list of strings giving the possible filename extensions, 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. N)rIr r)rrrrrr&s cCstdkrttj||S)aGuess 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. N)rIr r)rrrrrr7s cCstdkrttj|||S)aiAdd a mapping between a type and an extension. When the extension is already known, the new type will replace the old one. When the type is already known the extension will be added to the list of known extensions. If strict is true, information will be added to list of standard types, else to the list of non-standard types. N)rIr r)rrrrrrrGs cCsndat}|dkr"tr|jt}x"|D]}tjj|r(|j|q(W|j a |j a |j da |j da |a dS)NTF)rrr>rDrospathisfilerr r r rrI)filesdbfilerrrr Xs    cCsNy t|}Wntk r dSX|t}|j|d|jdSQRXdS)NT)r/OSErrorrr0r )rOfrNrrrr ls  cCsDdddddddadddd d ad d d d d ddddd ddddddddd ddddd dd ddddd d d!d"d#d#d#d$d%dd&d'd(d(d)d*ddd+d$d,d-d.d.d/d'd'd'd'd0ddd d d1d2d3d4d5d2d6dd7d8d9d9d:d9d9d d9d;dd?d@dAdBdCdDdDdEdFdd dGdHdIdJdKdBdLdMdNdOdOdPdPdBdQddRdSdTdUdd@dVdWdXdWdYd@dZd@d[d\d]ad^d_d_d`d`d`dadbdcadS)dNz.svg.gzz.tar.gzz.tar.bz2z.tar.xz)z.svgzz.tgzz.tazz.tzz.tbz2z.txzZgzipcompressZbzip2Zxz)z.gzz.Zz.bz2z.xzzapplication/octet-streamzapplication/postscriptz audio/x-aiffz audio/basiczvideo/x-msvideoz text/plainzapplication/x-bcpiozimage/x-ms-bmpzapplication/x-cdfzapplication/x-netcdfzapplication/x-cpiozapplication/x-cshztext/cssztext/csvzapplication/mswordzapplication/x-dvizmessage/rfc822z text/x-setextz image/gifzapplication/x-gtarzapplication/x-hdfz text/htmlzimage/vnd.microsoft.iconz image/iefz image/jpegzapplication/javascriptzapplication/jsonzapplication/x-latexz video/mpegzapplication/vnd.apple.mpegurlzapplication/x-troff-manzapplication/x-troff-mezapplication/x-mifzvideo/quicktimezvideo/x-sgi-moviez audio/mpegz video/mp4zapplication/x-troff-mszapplication/odazapplication/x-pkcs12zapplication/pkcs7-mimezimage/x-portable-bitmapzapplication/pdfzimage/x-portable-graymapz image/pngzimage/x-portable-anymapzapplication/vnd.ms-powerpointzimage/x-portable-pixmapz text/x-pythonzapplication/x-python-codezaudio/x-pn-realaudiozapplication/x-pn-realaudiozimage/x-cmu-rasterzapplication/xmlz image/x-rgbzapplication/x-troffz text/richtextz text/x-sgmlzapplication/x-shzapplication/x-sharzapplication/x-wais-sourcezapplication/x-sv4cpiozapplication/x-sv4crcz image/svg+xmlzapplication/x-shockwave-flashzapplication/x-tarzapplication/x-tclzapplication/x-texzapplication/x-texinfoz image/tiffztext/tab-separated-valueszapplication/x-ustarz text/x-vcardz audio/x-wavz video/webmzimage/x-xbitmapzapplication/vnd.ms-excelzapplication/excelztext/xmlzimage/x-xpixmapzimage/x-xwindowdumpzapplication/zip)z.az.aiz.aifz.aifcz.aiffz.auz.aviz.batz.bcpioz.binz.bmpz.cz.cdfz.cdfz.cpioz.cshz.cssz.csvz.dllz.docz.dotz.dviz.emlz.epsz.etxz.exez.gifz.gtarz.hz.hdfz.htmz.htmlz.icoz.iefz.jpez.jpegz.jpgz.jsz.jsonz.kshz.latexz.m1vz.m3uz.m3u8z.manz.mez.mhtz.mhtmlz.mifz.mjsz.movz.moviez.mp2z.mp3z.mp4z.mpaz.mpez.mpegz.mpgz.msz.ncz.nwsz.oz.objz.odaz.p12z.p7cz.pbmz.pdfz.pfxz.pgmz.plz.pngz.pnmz.potz.ppaz.ppmz.ppsz.pptz.psz.pwzz.pyz.pycz.pyoz.qtz.raz.ramz.rasz.rdfz.rgbz.roffz.rtxz.sgmz.sgmlz.shz.sharz.sndz.soz.srcz.sv4cpioz.sv4crcz.svgz.swfz.tz.tarz.tclz.texz.texiz.texinfoz.tifz.tiffz.trz.tsvz.txtz.ustarz.vcfz.wavz.webmz.wizz.wsdlz.xbmz.xlbz.xlsz.xlsz.xmlz.xpdlz.xpmz.xslz.xwdz.zipz image/jpgz audio/midiz image/pictzapplication/rtfztext/xul)z.jpgz.midz.midiz.pctz.picz.pictz.rtfz.xul)r r r rrrrr_default_mime_typesws( rS__main__a4Usage: mimetypes.py [options] type Options: --help / -h -- print this message and exit --lenient / -l -- additionally search of some common, but non-standard types. --extension / -e -- guess extension instead of type More than one type argument may be given. rAcCs"tt|rt|tj|dS)N)printUSAGEsysexit)codemsgrrrusage;sr[r4ZhlehelpZlenient extension-h--help-l --lenient-e --extensionz I don't know anything about typeztype:z encoding:)T)T)T)T)N)rA)r^r_)r`ra)rbrc)&rHrJrWr&Z urllib.parser#winregr> ImportError__all__rrrIrrrrrr r rSrEZgetoptrVr[argvZoptsargserrorrZrr]optargZgtypeZguessrUr,rrrrs|   V      3