a 3dgT@sdZddlZddlZddlZddlZz ddlZWneyFdZYn0gdZ gdZ da da GdddZ dd d Zdd d Zdd dZdddZdddZddZddZeddZedkredS)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_types) z/etc/mime.typesz/etc/httpd/mime.typesz/etc/httpd/conf/mime.typesz/etc/apache/mime.typesz/etc/apache2/mime.types$/usr/local/etc/httpd/conf/mime.typesz"/usr/local/lib/netscape/mime.typesrz/usr/local/etc/mime.typesFc@s`eZdZdZdddZdddZddd Zdd d Zdd d ZdddZ 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 tt|_t|_iif|_iif|_t D]\}}| ||dq:t D]\}}| ||dqZ|D]}| ||qvdSNTF)rr _encodings_map_defaultcopyr _suffix_map_defaultr r types_map_inv_types_map_defaultitemsr_common_types_defaultread)self filenamesstrictexttypenamerr%/usr/local/lib/python3.9/mimetypes.py__init__Bs    zMimeTypes.__init__cCs6||j||<|j||g}||vr2||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)rrrrZextsrrr rPs zMimeTypes.add_typec Csrt|}tj|\}}|dkr|d}|dkr8dS|dd|}|dkr\|d|}n |d|}d|vsxd|vr|d }|dfSt|\}}||jvrt||j|\}}q||j vr|j |} t|\}}nd} |j d } || vr| || fS| | vr| | | fS|r(d| fS|j d } || vrH| || fS| | vrf| | | fSd| fSdS) aUGuess the type of a file which is either a URL or a path-like object. 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,r)NN;N=/ text/plainTF) osfspathurllibparse _splittypefind posixpathsplitextr r r lower) rurlrschemeZcommaZsemirbaserencodingr rrr rasB           zMimeTypes.guess_typecCsL|}|jd|g}|sH|jd|gD]}||vr0||q0|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)r2rgetr#)rrr extensionsrrrr rs  zMimeTypes.guess_all_extensionscCs|||}|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)rrrr8rrr rs zMimeTypes.guess_extensioncCs<t|dd}|||Wdn1s.0YdS)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. utf-8r6N)openreadfp)rfilenamerfprrr rszMimeTypes.readc Cs|}|sq|}tt|D]"}||ddkr"||d=qFq"|sLq|d|dd}}|D]}||d||qfqdS)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) rr>rlinewordsirsuffixesZsuffrrr r<s  zMimeTypes.readfpc CstsdSdd}ttjd}||D]}zt||b}|dsVWdWq(t|d\}}|tjkrWdWq(||||Wdn1s0YWq(tyYq(Yq(0q(Wdn1s0YdS)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. NcssHd}zt||}Wnty*YqDYn0d|vr:|V|d7}qdS)Nrr@)_winregZEnumKeyOSError)ZmimedbrHZctyperrr enum_typess  z3MimeTypes.read_windows_registry..enum_typesrAz Content Type)rKOpenKeyZHKEY_CLASSES_ROOT startswithZ QueryValueExZREG_SZrrL)rrrMZhkcrZ subkeynameZsubkeyZmimetypeZdatatyperrr read_windows_registrys"     0 zMimeTypes.read_windows_registryN)rT)T)T)T)T)T)T)T) __name__ __module__ __qualname____doc__r!rrrrrr<rQrrrr r:s   ?   rTcCstdurtt||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)r3rrrr rsrcCstdurtt||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)rVr rrrrrr r's rcCstdurtt||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)rVr rrWrrr r8s rcCstdurtt|||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)rVr r)rrrrrr rHs rcCsda|dustdurBt}tr&||dur4t}qFtt|}nt}|D]}tj |rJ| |qJ|j a |j a |j da |j da|adSr)rrVrrKrQrlistr*pathisfilerr r r r)filesdbfilerrr r Ys"    r cCsjzt|dd}Wnty$YdS0|,t}||d|jdWdS1s\0YdS)Nr9r:T)r;rLrr<r )r]fr\rrr r us  r cCsXdddddddaadddd d d aad d d ddddddddddddddddddddddddddddddddddd d!d!d"d"d#d$d$d%d&d'd(d)d*d+d,d-d-d.d.d.d/d0d1d2d3d4d4d4d4d5d6d6d7d7d8d8d8d9d:d;dd>d>d?d@dAdAdBdCdDdEdFdGdHdIdJdKdLdMdMdMdMdNdOdPdPdQdQdQdQdQdQdRdSdTdUdVdVdWdXdYdZdZdZdZdZd[d[d\d]d^d_aad`dadadbdcdcdcdddeaadS)fNz.svg.gzz.tar.gzz.tar.bz2z.tar.xz)z.svgzz.tgzz.tazz.tzz.tbz2z.txzgzipcompressbzip2xzbr)z.gzz.Zz.bz2z.xzz.brzapplication/javascriptzapplication/jsonzapplication/manifest+jsonzapplication/mswordzapplication/octet-streamzapplication/odazapplication/pdfzapplication/pkcs7-mimezapplication/postscriptzapplication/vnd.apple.mpegurlzapplication/vnd.ms-excelzapplication/vnd.ms-powerpointzapplication/wasmzapplication/x-bcpiozapplication/x-cpiozapplication/x-cshzapplication/x-dvizapplication/x-gtarzapplication/x-hdfzapplication/x-latexzapplication/x-mifzapplication/x-netcdfzapplication/x-pkcs12zapplication/x-pn-realaudiozapplication/x-python-codezapplication/x-shzapplication/x-sharzapplication/x-shockwave-flashzapplication/x-sv4cpiozapplication/x-sv4crczapplication/x-tarzapplication/x-tclzapplication/x-texzapplication/x-texinfozapplication/x-troffzapplication/x-troff-manzapplication/x-troff-mezapplication/x-troff-mszapplication/x-ustarzapplication/x-wais-sourcezapplication/xmlzapplication/zipz audio/basicz audio/mpegz audio/x-aiffzaudio/x-pn-realaudioz audio/x-wavz image/bmpz image/gifz image/iefz image/jpegz image/pngz image/svg+xmlz image/tiffzimage/vnd.microsoft.iconzimage/x-cmu-rasterzimage/x-ms-bmpzimage/x-portable-anymapzimage/x-portable-bitmapzimage/x-portable-graymapzimage/x-portable-pixmapz image/x-rgbzimage/x-xbitmapzimage/x-xpixmapzimage/x-xwindowdumpzmessage/rfc822ztext/cssztext/csvz text/htmlr)z text/richtextztext/tab-separated-valuesz text/x-pythonz text/x-setextz text/x-sgmlz text/x-vcardztext/xmlz video/mp4z video/mpegzvideo/quicktimez video/webmzvideo/x-msvideozvideo/x-sgi-movie)z.jsz.mjsz.jsonz .webmanifestz.docz.dotz.wizz.binz.az.dllz.exez.oz.objz.soz.odaz.pdfz.p7cz.psz.aiz.epsz.m3uz.m3u8z.xlsz.xlbz.pptz.potz.ppaz.ppsz.pwzz.wasmz.bcpioz.cpioz.cshz.dviz.gtarz.hdfz.latexz.mifz.cdfz.ncz.p12z.pfxz.ramz.pycz.pyoz.shz.sharz.swfz.sv4cpioz.sv4crcz.tarz.tclz.texz.texiz.texinfoz.roffz.tz.trz.manz.mez.msz.ustarz.srcz.xslz.rdfz.wsdlz.xpdlz.zipz.auz.sndz.mp3z.mp2z.aifz.aifcz.aiffz.raz.wav.bmpz.gifz.ief.jpgz.jpez.jpegz.pngz.svgz.tiffz.tifz.icoz.rasrdz.pnmz.pbmz.pgmz.ppmz.rgbz.xbmz.xpmz.xwdz.emlz.mhtz.mhtmlz.nwsz.cssz.csvz.htmlz.htmz.txtz.batz.cz.hz.kshz.plz.rtxz.tsvz.pyz.etxz.sgmz.sgmlz.vcfz.xmlz.mp4z.mpegz.m1vz.mpaz.mpez.mpgz.movz.qtz.webmz.aviz.moviezapplication/rtfz audio/midiz image/jpgz image/pictztext/xul)z.rtfz.midiz.midrez.pictz.pctz.picz.xul)r rr rr rrrrrrr _default_mime_typess8    rfc sddl}ddfdd }z$|tjdddgd\}}Wn0|jyn}z|d|WYd}~n d}~00d}d}|D]4\}}|d vr|dq||d vrd}q||d vr|d}q||D]Z} |rt| |} | std | nt| qt| |\} } | std | qtd | d| qdS)Nra4Usage: 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. rNcs"t|rt|t|dS)N)printsysexit)codemsgZUSAGErr usageFs z_main..usager@Zhle)helpZlenient extension)z-hz--help)z-lz --lenient)z-ez --extensionz I don't know anything about typeztype:z encoding:)rN)getoptrhargverrorrrgr) rprmZoptsargsrkrrooptargZgtypeZguessr6rrlr _main7s4       rv__main__)T)T)T)T)N)rUr*rhr0 urllib.parser,winregrK ImportError__all__rrrVrrrrrr r rfrvrRrrrr s4    W      5.