YfoQ@sdZddlZddlZddlZddlZyddlZWnek r`dZYnXddddddgZ d d d d d ddddg Z da da GdddZ dddZdddZdddZdddZdddZddZddZeedkrddlZdZdd d!Zy5ejejd"dd#d$d%d&g\ZZWn5ejk rZzed"eWYddZ[XnXd"ZdZxTeD]L\Z Z!e d0kredqe d1kr dZqe d2krd"ZqWxeD]|Z"ereee"eZ#e#sXe$d-e"qe$e#q'ee"e\Z#Z%e#se$d-e"q'e$d.e#d/e%q'WdS)3aGuess 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 guess_typeguess_extensionguess_all_extensionsadd_typeread_mime_typesinitz/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@seZdZdZfdddZdddZdddZdd d Zdd d Zdd dZ dddZ dddZ dS) MimeTypeszMIME-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||dqVWx-t jD]\}}|j||dqWx|D]}|j ||qWdS)NTF) initedr encodings_mapcopy suffix_map types_map types_map_invitemsr common_typesread)self filenamesstrictexttypenamer./opt/alt/python35/lib64/python3.5/mimetypes.py__init__@s zMimeTypes.__init__cCsG||j||<|j|j|g}||krC|j|dS)aAdd 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)r r setdefaultappend)rrrrZextsrrrrNs  zMimeTypes.add_typec Cstjj|\}}|dkr|jd}|dkrCd S|jdd|}|dkrw|d|}n|d|}d|ksd|krd}|dfStj|\}}x3||jkrtj||j|\}}qW||jkr.|j|} tj|\}}nd} |jd } || kr[| || fS|j | kr| |j | fS|rd| fS|jd } || kr| || fS|j | kr| |j | fSd| fSdS) a:Guess 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. data,rN;=/z text/plainTF)NN) urllibparseZ splittypefind posixpathsplitextr r r lower) rurlrschemeZcommaZsemirbaserencodingr rrrr_s@    $      zMimeTypes.guess_typecCsl|j}|jdj|g}|shx:|jdj|gD]}||krE|j|qEW|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(). Optional `strict' argument when false adds a bunch of commonly found, but non-standard types. TF)r'rgetr)rrr extensionsrrrrrs   zMimeTypes.guess_all_extensionscCs$|j||}|sdS|dS)a Guess 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. Nr)r)rrrr-rrrrs zMimeTypes.guess_extensionc Cs0t|dd}|j||WdQRXdS)z Read a single mime.types-format file, specified by pathname. If strict is true, information will be added to list of standard types, else to the list of non-standard types. r+zutf-8N)openreadfp)rfilenamerfprrrrszMimeTypes.readc Csx|j}|sP|j}x<tt|D](}||ddkr5||d=Pq5W|sjq|d|dd}}x%|D]}|j|d||qWqWdS)z Read a single mime.types-format file. If strict is true, information will be added to list of standard types, else to the list of non-standard types. r#N.)readlinesplitrangelenr) rr1rlineZwordsirsuffixesZsuffrrrr/s    zMimeTypes.readfpcCsts dSdd}tjtjd}x||D]}yotj||V}|jdskw;tj|d\}}|tjkrw;|j|||WdQRXWq;tk rw;Yq;Xq;WWdQRXdS)z Load the MIME types database from Windows registry. If strict is true, information will be added to list of standard types, else to the list of non-standard types. Nc ssXd}xKytj||}Wntk r4PYnXd|krF|V|d7}q WdS)Nrr3)_winregZEnumKeyEnvironmentError)Zmimedbr:Zctyperrr enum_typess  z3MimeTypes.read_windows_registry..enum_typesr4z Content Type)r=OpenKeyZHKEY_CLASSES_ROOT startswithZ QueryValueExZREG_SZrr>)rrr?ZhkcrZ subkeynameZsubkeyZmimetypeZdatatyperrrread_windows_registrys   zMimeTypes.read_windows_registryN) __name__ __module__ __qualname____doc__rrrrrrr/rCrrrrr8s > rTcCs#tdkrttj||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)_dbrr)r(rrrrr s cCs#tdkrttj||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)rHrr)rrrrrr$s cCs#tdkrttj||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)rHrr)rrrrrr5s cCs&tdkrttj|||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)rHrr)rrrrrrrEs cCsdat}|dkr1tr+|jt}x-|D]%}tjj|r8|j|q8W|j a |j a |j da |j da |a dS)NTF)r rr=rC knownfilesospathisfilerr r r rrH)filesdbfilerrrrVs        cCs_yt|}Wntk r(dSYnX|)t}|j|d|jdSWdQRXdS)NT)r.OSErrorrr/r )rOfrNrrrrjs   cCsddddddddddd d iad d d dddddiadddddddddddddddd d!d"d#dd$d%d&d d'd(d'd)d*d+d,d-d.d/d0d1d2dd3d4d5d4d6d7d8d9d:dd;d<d=dd>d?d@dAdBd dCdDdEdFdGdFdHdIdJdKdLdMdNdMdOdMdPdQdRd dSdTdUdVdWdXdYdXdZd[d\d]d^d9d_d9d`dadbdcdddedfdgdhdgdidjdkdVdldVdmdVdndVdodpdqd)drd9dsddtddudvdwdxdydzd{d|d}d~ddxdddd ddddddddddddddddddddddddddcdddddddddddddddddddddddddddddddddddddddddddddddddddddddddd dddddddddd4ddddddddddddddddddddddiadOdddddddddddddddiadS)Nz.svgzz.svg.gzz.tgzz.tar.gzz.tazz.tzz.tbz2z.tar.bz2z.txzz.tar.xzz.gzZgzipz.Zcompressz.bz2Zbzip2z.xzZxzz.azapplication/octet-streamz.aizapplication/postscriptz.aifz audio/x-aiffz.aifcz.aiffz.auz audio/basicz.avizvideo/x-msvideoz.batz text/plainz.bcpiozapplication/x-bcpioz.binz.bmpzimage/x-ms-bmpz.cz.cdfzapplication/x-cdfzapplication/x-netcdfz.cpiozapplication/x-cpioz.cshzapplication/x-cshz.cssztext/cssz.csvztext/csvz.dllz.doczapplication/mswordz.dotz.dvizapplication/x-dviz.emlzmessage/rfc822z.epsz.etxz text/x-setextz.exez.gifz image/gifz.gtarzapplication/x-gtarz.hz.hdfzapplication/x-hdfz.htmz text/htmlz.htmlz.icozimage/vnd.microsoft.iconz.iefz image/iefz.jpez image/jpegz.jpegz.jpgz.jszapplication/javascriptz.kshz.latexzapplication/x-latexz.m1vz video/mpegz.m3uzapplication/vnd.apple.mpegurlz.m3u8z.manzapplication/x-troff-manz.mezapplication/x-troff-mez.mhtz.mhtmlz.mifzapplication/x-mifz.movzvideo/quicktimez.moviezvideo/x-sgi-moviez.mp2z audio/mpegz.mp3z.mp4z video/mp4z.mpaz.mpez.mpegz.mpgz.mszapplication/x-troff-msz.ncz.nwsz.oz.objz.odazapplication/odaz.p12zapplication/x-pkcs12z.p7czapplication/pkcs7-mimez.pbmzimage/x-portable-bitmapz.pdfzapplication/pdfz.pfxz.pgmzimage/x-portable-graymapz.plz.pngz image/pngz.pnmzimage/x-portable-anymapz.potzapplication/vnd.ms-powerpointz.ppaz.ppmzimage/x-portable-pixmapz.ppsz.pptz.psz.pwzz.pyz text/x-pythonz.pyczapplication/x-python-codez.pyoz.qtz.razaudio/x-pn-realaudioz.ramzapplication/x-pn-realaudioz.raszimage/x-cmu-rasterz.rdfzapplication/xmlz.rgbz image/x-rgbz.roffzapplication/x-troffz.rtxz text/richtextz.sgmz text/x-sgmlz.sgmlz.shzapplication/x-shz.sharzapplication/x-sharz.sndz.soz.srczapplication/x-wais-sourcez.sv4cpiozapplication/x-sv4cpioz.sv4crczapplication/x-sv4crcz.svgz image/svg+xmlz.swfzapplication/x-shockwave-flashz.tz.tarzapplication/x-tarz.tclzapplication/x-tclz.texzapplication/x-texz.texizapplication/x-texinfoz.texinfoz.tifz image/tiffz.tiffz.trz.tsvztext/tab-separated-valuesz.txtz.ustarzapplication/x-ustarz.vcfz text/x-vcardz.wavz audio/x-wavz.webmz video/webmz.wizz.wsdlz.xbmzimage/x-xbitmapz.xlbzapplication/vnd.ms-excelz.xlszapplication/excelz.xmlztext/xmlz.xpdlz.xpmzimage/x-xpixmapz.xslz.xwdzimage/x-xwindowdumpz.zipzapplication/zipz image/jpgz.midz audio/midiz.midiz.pctz image/pictz.picz.pictz.rtfzapplication/rtfz.xulztext/xul)r r r rrrrr_default_mime_typesus$   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. r@cCs+tt|rt|tj|dS)N)printUSAGEsysexit)codemsgrrrusage7s  r[r3ZhlehelpZlenient extension-h--help-l --lenient-e --extensionz I don't know anything about typeztype:z encoding:)r^r_)r`ra)rbrc)&rGrJrWr%Z urllib.parser"winregr= ImportError__all__rIr rHrrrrrrrrSrDZgetoptrVr[argvZoptsargserrorrZrr]ZoptargZgtypeZguessrUr+rrrrst