*j+] S r SSKrSSKrSSKrSSKr SSKJr SSK r
/ SQr/ SQrSq
Sq " S S5 rSS
jrS S.S jrSS
jrSS jrSS jrSS jrS rS r\" 5 S r\S:X a \" 5 gg! \ a Sr Ndf = f! \ a Sr
Nmf = f)a Guess 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) _mimetypes_read_windows_registry)
knownfilesinited MimeTypes
guess_typeguess_file_typeguess_all_extensionsguess_extensionadd_typeinitread_mime_types
suffix_map
encodings_map types_mapcommon_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.typesr z/usr/local/etc/mime.typesFc \ rS rSrSrSS jrSS jrSS jrSS.S jrS r SS
jr
SS jrSS jrSS
jr
SS jr\S 5 rSrg)r @ zMIME-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.
Tc [ ( d
[ 5 [ R 5 U l [
R 5 U l 0 0 4U l 0 0 4U l [ R 5 H u p4U R XCS5 M [ R 5 H u p4U R XCS5 M U H nU R XR5 M g NTF)r r _encodings_map_defaultcopyr _suffix_map_defaultr r
types_map_inv_types_map_defaultitemsr _common_types_defaultread)self filenamesstrictexttypenames 0/opt/alt/python313/lib64/python3.13/mimetypes.py__init__MimeTypes.__init__H s vF388:-224b "X-335KSMM$T* 60668KSMM$U+ 9DIId# c U( d gXR U U' U R U R U/ 5 nX$; a UR U5 gg)I Add 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)r r# r" r! extss r% r MimeTypes.add_typeV sN &*vs#!!&)44T2>?KK r( c [ R " U5 n[ R R U5 nUR
( a2 [
UR
5 S: a UR
nUR nOU R XS9$ US:X aN UR S5 nUS: a gUR SSU5 nUS: a USU nOUSU nS U; d S
U; a SnUS4$ U R X[ R 5 $ )a Guess 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.
r! data,r )NN;N=/
text/plain)
osfspathurllibparseurlparseschemelenpathr find_guess_file_type posixpathsplitext)r urlr! pr= commasemir# s r% r MimeTypes.guess_typei s ( iinLL!!#&88AHH
)XXF&&C''';;V HHSMEqy!88CE*Dqy5Dz6E{d{co#:$$S)2D2DEEr( r1 c [ R " U5 n[ R R U5 S nU R X[ R R
5 $ )iGuess the type of a file based on its path.
Similar to guess_type(), but takes file path istead of URL.
r0 )r8 fsdecoder?
splitdriverA rC )r r? r! s r% r MimeTypes.guess_file_type sH
{{4 ww!!$'*$$T2773C3CDDr( c U" U5 u pEUR 5 =o`R ; a: U" X@R U - 5 u pEUR 5 =o`R ; a M: XPR ; a U R U nU" U5 u pEOS nUR 5 nU R S nXX; a X U4$ U( a S U4$ U R S nXX; a X U4$ S U4$ r )lowerr r r ) r r? r! rC baser" ext_lowerencodingr s r% rA MimeTypes._guess_file_type s TN IIK'yOO; (B!BCID IIK'yOO; $$$))#.H ID#HiikNN4( >8++
>!NN5) >8++>!r( c UR 5 n[ U R S R U/ 5 5 nU( d> U R S R U/ 5 H nXC; d M
UR U5 M U$ )a Guess 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)rO listr getr, )r r# r!
extensionsr" s r% r MimeTypes.guess_all_extensions sr zz|$,,T266tR@A
))%044T2>(%%c* ? r( c > U R X5 nU( d gUS $ )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 )r r# r! rW s r% r
MimeTypes.guess_extension s$ ..t<
!}r( c n [ USS9 nU R X25 SSS5 g! , ( d f g= f)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-8rR N)openreadfp)r filenamer! fps r% r MimeTypes.read s( (W
-KK# .
-
-s &
4c @ UR 5 =n( a UR 5 n[ [ U5 5 H nXE S S:X d M XES2 O U( d M\ US USS pvU H nU R USU- U5 M UR 5 =n( a M gg)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 #Nr0 .)readlinesplitranger> r ) r ra r! linewordsir# suffixessuffs r% r_ MimeTypes.readfp s kkm#d#JJLE3u:&8A;#%b ' "1XuQRy(
dC$J7 ! kkm#d##r( c ^ [ ( d [ ( d gT R nU( a U 4S jn[ ( a [ U5 g[ ( a T R U5 gg)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 ( > TR XS5 $ )NT)r )r# r" r s r%