
    b_f                     \   d Z ddlZddlZddlZddlZddlZddlmZ ddlZddl	m
Z
mZ ddlmZ dZi ZdZdadZdaej(                  d   dk\  Zd Zd3d	Z G d
 de      Z G d de      Z G d de      Z G d d      Z G d de      Z G d d      Z  G d de      Z! edd      Z" edd      Z# edd      Z$ edd      Z% edd      Z& edd       Z' edd!      Z( edd"      Z) edd#      Z* edd$      Z+d%a,d& Z-d' Z.d( Z/d4d)Z0d4d*Z1d+ Z2d5d,Z3d6d-Z4d. Z5erd/ Z6nd0 Z6da7d%a8d1 Z9d2 Z:y)7a  
This module is based on a rox module (LGPL):

http://cvs.sourceforge.net/viewcvs.py/rox/ROX-Lib2/python/rox/mime.py?rev=1.21&view=log

This module provides access to the shared MIME database.

types is a dictionary of all known MIME types, indexed by the type name, e.g.
types['application/x-python']

Applications can install information about MIME types by storing an
XML file as <MIME>/packages/<application>.xml and running the
update-mime-database command, which is provided by the freedesktop.org
shared mime database package.

See http://www.freedesktop.org/standards/shared-mime-info-spec/ for
information about the format of these files.

(based on version 0.13)
    N)BaseDirectory)minidomXML_NAMESPACE)defaultdictz5http://www.freedesktop.org/standards/shared-mime-info   c                     dj                  | j                  D cg c]  }|j                   c}      j                         S c c}w )zGet text of XML node )join
childNodes	nodeValuestrip)nodens     (lib/python3.12/site-packages/xdg/Mime.py_get_node_datar   -   s4    779AAKK9:@@BB9s   Ac                     t        | |      S )zGet the MIMEtype object for the given type.
    
    This remains for backwards compatibility; calling MIMEtype now does
    the same thing.
    
    The name can either be passed as one part ('text/plain'), or as two
    ('text', 'plain').
    )MIMEtypemediasubtypes     r   lookupr   1   s     E7##    c                   T     e Zd ZdZd fd	Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Z xZS )r   zClass holding data about a MIME type.
    
    Calling the class will return a cached instance, so there is only one 
    instance for each MIME type. The name can either be passed as one part
    ('text/plain'), or as two ('text', 'plain').
    c                     |d|v r|j                  dd      \  }}d|vsJ |j                         }|j                         }	 t        ||f   S # t        $ r5 t        t
        |   |       }|j                  ||       |t        ||f<   |cY S w xY w)N/   )splitlowertypesKeyErrorsuperr   __new___init)clsr   r   mtype	__class__s       r   r"   zMIMEtype.__new__C   s    ?se|"[[a0NE7'!!!--/	%)** 	(C05EKKw'&+E5'"#L		s   
A ;BBc                 .    || _         || _        d | _        y N)r   r   _comment)selfr   r   s      r   r#   zMIMEtype._initU   s    
r   c                    t         j                  j                  d| j                  | j                  dz         }t        j                  |      D ]  }t        j                  |      }||j                  j                  t        d      D ]g  }|j                  t        d      xs d}d|t        j                  j                   v z   }|| j"                  d   kD  r|t%        |      f| _        |d	k(  sf  y  y)
z>Loads comment for current language. Use get_comment() instead.mime.xmlNcommentlangenr   r      )ospathr
   r   r   r   load_data_pathsr   parsedocumentElementgetElementsByTagNameNSFREE_NSgetAttributeNSr   xdgLocalelangsr)   r   )r*   resourcer3   docr.   r/   goodnesss          r   _loadzMIMEtype._loadZ   s    77<<

DLL64IJ!11(;D--%C{..EEgyY--mVDL

(8(8 89dmmA..%-~g/F$GDMq=& Z	 <r   c                 |    | j                   "dt        |       f| _         | j                          | j                   d   S )z;Returns comment for current language, loading it if needed.r   r   )r)   strr@   r*   s    r   get_commentzMIMEtype.get_commenti   s5     == D	NDMJJL}}Qr   c                 d    t                t        |       }|t        v rt        t        |         S | S )z:Returns the canonical MimeType object if this is an alias.)update_cacherB   aliasesr   )r*   ss     r   	canonicalzMIMEtype.canonicalq   s+    I<'!*%%r   c                 Z    t                t        d t        t        |          D              S )z5Returns a set of Mime types which this inherits from.c              3   2   K   | ]  }t        |        y wr(   )r   ).0ts     r   	<genexpr>z)MIMEtype.inherits_from.<locals>.<genexpr>|   s     =&<6!9&<s   )rF   setinheritancerB   rC   s    r   inherits_fromzMIMEtype.inherits_fromy   s!    =k#d)&<===r   c                 :    | j                   dz   | j                  z   S )Nr   r   rC   s    r   __str__zMIMEtype.__str__~   s    zzC$,,..r   c                 <    d| j                   d| j                  dS )Nz	MIMEtype(z, )r   rC   s    r   __repr__zMIMEtype.__repr__   s    %)ZZ>>r   c                 X    t        | j                        t        | j                        z  S r(   )hashr   r   rC   s    r   __hash__zMIMEtype.__hash__   s    DJJ$t||"444r   r(   )__name__
__module____qualname____doc__r"   r#   r@   rD   rI   rQ   rS   rV   rY   __classcell__)r&   s   @r   r   r   <   s5    $
) >
/?5r   r   c                       e Zd Zy)UnknownMagicRuleFormatN)rZ   r[   r\    r   r   r`   r`      s    r   r`   c                       e Zd ZdZy)DiscardMagicRuleszGRaised when __NOMAGIC__ is found, and caught to discard previous rules.N)rZ   r[   r\   r]   ra   r   r   rc   rc      s    Mr   rc   c                   b    e Zd ZdZd Z ej                  d      Zed        Z	d Z
d Zd Zd Zy)		MagicRuleNc                 J    || _         || _        || _        || _        || _        y r(   startvaluemaskwordrange)r*   rh   ri   rj   rk   rl   s         r   __init__zMagicRule.__init__   s%    

		
r   s   (?:~(\d+))?(?:\+(\d+))?\n$c                 h   |j                         }|j                  dd      \  }}|rt        |      nd}|j                  dd      \  }}t        |      }|dk(  rt        t        j
                  d   dk\  rt        j                  |dd d	
      }n t        |d         dz  t        |d         z   }|dd }t        |      |k  r"||j                         z  }t        |      |k  r"|d| ||d }}|j                  d      rAt        |      |k  r"||j                         z  }t        |      |k  r"|d|dz    ||dz   d }}nd}| j                  j                  |      }|st        t        |            |j                         \  }	}
|	t        |	      nd}	|
t        |
      nd}
| | ||||	|
      fS )zjRead a rule from the binary magics file. Returns a 2-tuple of
        the nesting depth and the MagicRule.   >r   r      =s   __NOMAGIC__
r   Nr1   big)	byteorder      &)readliner   intrc   sysversion_info
from_bytesordlen
startswithrule_ending_rematchr`   reprgroups)r$   fline
nest_depthrh   lenvalueri   rj   endingrk   rl   s              r   	from_filezMagicRule.from_file   s    zz|  ::dA.
D(2S_
 jjq)tE
#### A!#~~d2Ah%~@HDGaT!W5HABx $i8#AJJL D $i8#9HotHIt ??4 d)x'

$ d)x'a
+T(1*+->$DD ##))$/ )d44mmoe!-s4yA$0E
q3ueT4???r   c                     | j                   t        | j                        z   | j                  z   }| j                  r$t        || j                  j                               S |S r(   )rh   r{   ri   rl   alsomaxmaxlen)r*   ls     r   r   zMagicRule.maxlen   sG    JJTZZ(4::599q$))**,--r   c                 v    | j                  |      r(| j                  r| j                  j                  |      S yy )NT)match0r   r~   r*   buffers     r   r~   zMagicRule.match   s0    ;;vyyyyv.. r   c                    t        |      }t        | j                        }t        | j                        D ]  }| j                  |z   }||z   }||k  r y| j                  rjd}t        |      D ]Y  }t
        r|||z      | j                  |   z  }	n*t        |||z            t        | j                  |         z  }	|t        |	      z  }[ n||| }|| j                  k(  s y y )NFr	   T)r{   ri   rl   rh   rj   PY3rz   chr)
r*   r   r   r   orH   etestics
             r   r   zMagicRule.match0   s    
f+tzz?tzz"AjjlAjAsyyxA"1Q3K$))A,6qs,s499Q</@@CFND ) a{TZZ# #r   c                     d| j                   d| j                  d| j                  d| j                  d| j                  dS )NzMagicRule(start=z, value=z, mask=z, word=z, range=rU   rg   rC   s    r   rV   zMagicRule.__repr__   s/    "&**"&**"&))"&))"&**. 	.r   )rZ   r[   r\   r   rm   recompiler}   classmethodr   r   r~   r   rV   ra   r   r   re   re      sH    D  RZZ >?N2@ 2@h,.r   re   c                   B    e Zd ZdZd Zd Zd Zed        Zed        Z	y)MagicMatchAnyzMatch any of a set of magic rules.
    
    This has a similar interface to MagicRule objects (i.e. its match() and
    maxlen() methods), to allow for duck typing.
    c                     || _         y r(   )rules)r*   r   s     r   rm   zMagicMatchAny.__init__   s	    
r   c                 @    t        fd| j                  D              S )Nc              3   @   K   | ]  }|j                          y wr(   )r~   )rL   rr   s     r   rN   z&MagicMatchAny.match.<locals>.<genexpr>  s     7Jq1776?Js   )anyr   r   s    `r   r~   zMagicMatchAny.match  s    7DJJ777r   c                 :    t        d | j                  D              S )Nc              3   <   K   | ]  }|j                           y wr(   )r   )rL   r   s     r   rN   z'MagicMatchAny.maxlen.<locals>.<genexpr>  s     2z!188:zs   )r   r   rC   s    r   r   zMagicMatchAny.maxlen  s    2tzz222r   c                    |j                  d      }|j                  dd       g }|rW|dk7  rR	 |j                  t        j	                  |             |j                  d      }|r|j                  dd       |r|dk7  rRg }d|i}|D ]%  \  }}g }||   j                  ||f       |||dz   <   ' | j                  |      S # t
        $ r Y yw xY w)z/Read a set of rules from the binary magic file.r      [r   )readseekappendre   r   r`   from_rule_tree)	r$   r   r   depths_rulestreeinsert_pointsdepthrulesubruless	            r   r   zMagicMatchAny.from_file	  s     &&)	r1AI##I$7$7$:; ffQiAr1 AI 4'KE4H% ''x(89%-M%'" (
 !!$'' * s   $C   	CCc                     g }|D ].  \  }}|r| j                  |      |_        |j                  |       0 t        |      dk(  ryt        |      dk(  r|d   S  | |      S )a  From a nested list of (rule, subrules) pairs, build a MagicMatchAny
        instance, recursing down the tree.
        
        Where there's only one top-level rule, this is returned directly,
        to simplify the nested structure. Returns None if no rules were read.
        r   Nr   )r   r   r   r{   )r$   r   r   r   r   s        r   r   zMagicMatchAny.from_rule_tree#  sf     "ND(..x8	LL #
 u:q=u:q=8O5zr   N)
rZ   r[   r\   r]   rm   r~   r   r   r   r   ra   r   r   r   r      s>    
83 ( (2  r   r   c                   4    e Zd Zd Zd Zd ZddZddZd Zy)	MagicDBc                 ,    t        t              | _        y r(   )r   listbytyperC   s    r   rm   zMagicDB.__init__8  s    #D)r   c                 T   t        |d      5 }|j                         }|dk7  rt        d      	 |j                         j                  d      }|sn|d   dk7  s|dd d	k7  rt	        d
|      |dd j                  d      \  }}t        |      }t        |      }	 t        j                  |      }|| j                  |   j                  ||f       	 ddd       y# t        $ r4 | j                  j                  |d       t        j                  |      }Y jw xY w# 1 sw Y   yxY w)z<Read a magic binary file, and add its rules to this MagicDB.rbs   MIME-Magic 
zNot a MIME magic fileasciir   [Nz]
zMalformed section headingr   :)openru   IOErrordecode
ValueErrorr   rv   r   r   r   rc   r   popr   )	r*   fnamer   r   sheadpritnamer%   r   s	            r   
merge_filezMagicDB.merge_file;  s)   %!::<D((566

++G48s?eBCjE&9$%@%HH"1R[..s3
U#hu6(2215D < E"))3+6)   $ ) 6KKOOE40(2215D6% s0   BDC0$D:DDDDD'c                 (   d}g | _         | j                  j                         D ]G  \  }}|D ]=  \  }}| j                   j                  |||f       t	        ||j                               }? I || _        | j                   j                  d d       y)z{Prepare the MagicDB for matching.
        
        This should be called after all rules have been merged into it.
        r   c                     | d   S Nr   ra   xs    r   <lambda>z"MagicDB.finalise.<locals>.<lambda>f  s    1r   TkeyreverseN)alltypesr   itemsr   r   r   sort)r*   r   r%   r   r   r   s         r   finalisezMagicDB.finaliseX  s    
  KK--/LE5"	T$$c5$%78VT[[]3 # 0
 ~t<r   Nc                 
   |Hg }|D ]-  }| j                   |   D ]  \  }}|j                  |||f        / |j                  d        n| j                  }|D ](  \  }	}
}|	|kD  r|	|k  r y|j	                  |      s&|
c S  y)a]  Do magic sniffing on some bytes.
        
        max_pri & min_pri can be used to specify the maximum & minimum priority
        rules to look for. possible can be a list of mimetypes to check, or None
        (the default) to check all mimetypes until one matches.
        
        Returns the MIMEtype found, or None if no entries match.
        Nc                     | d   S r   ra   r   s    r   r   z$MagicDB.match_data.<locals>.<lambda>v  s    QqTr   )r   )r   r   r   r   r~   )r*   datamax_primin_pripossibler   mtr   r   prioritymimetypes              r   
match_datazMagicDB.match_datah  s     E!%RICLL#r41 "1  JJ>J*MME(-$Hh'!'!zz$ ).r   c                     t        |d      5 }|j                  | j                        }ddd       | j                  |||      S # 1 sw Y   xY w)a  Read data from the file and do magic sniffing on it.
        
        max_pri & min_pri can be used to specify the maximum & minimum priority
        rules to look for. possible can be a list of mimetypes to check, or None
        (the default) to check all mimetypes until one matches.
        
        Returns the MIMEtype found, or None if no entries match. Raises IOError
        if the file can't be opened.
        r   N)r   r   r   r   )r*   r3   r   r   r   r   bufs          r   r~   zMagicDB.match  sE     $&&%C sGWh?? s   AAc                 2    dt        | j                        z  S )Nz<MagicDB (%d types)>)r{   r   rC   s    r   rV   zMagicDB.__repr__  s    %DMM(:::r   )d   r   N)	rZ   r[   r\   rm   r   r   r   r~   rV   ra   r   r   r   r   7  s!    *7:=  8@;r   r   c                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)GlobDBc                 ,    t        t              | _        y)zPrepare the GlobDB. It can't actually be used until .finalise() is
        called, but merge_file() can be used to add data before that.
        N)r   rO   allglobsrC   s    r   rm   zGlobDB.__init__  s    
 $C(r   c           	         | j                   }t        |      5 }|D ]  }|j                  d      r|dd j                  d      }|dd \  }}}t	        |      }t        |      }	t        |      dkD  r|d   j                  d      }
nd}
|dk(  r|j                  |	d       ||	   j                  ||t        |
      f        	 ddd       y# 1 sw Y   yxY w)	z3Loads name matching information from a globs2 file.#Nr   r   r   ,ra   __NOGLOBS__)
r   r   r|   r   rv   r   r{   r   addtuple)r*   r3   r   r   r   fieldsweight	type_namepatternr%   flagss              r   r   zGlobDB.merge_file  s    ==$Z1??3'cr--3BQZ*	7Vy)v;?"1IOOC0EEm+LL-##VWeEl$CD#  ZZs   B*CCc                 d   t        t              | _        t        t              | _        g | _        i | _        i | _        | j                  j                         D ]7  \  }}|j                         }|D ]  \  }}}d|v }|j                  d      rc|dd }d|v sZd|v sVd|v sR|r!| j                  |   j                  ||f       n.| j                  |j                            j                  ||f       d|v sd|v sd|v r]|rdnt        j                  }t        j                  t!        j"                  |      |	      }| j                  j                  |||f       |r||f| j                  |<   ||f| j
                  |j                         <    : | j                  j%                  d
d        y)zzPrepare the GlobDB for matching.
        
        This should be called after all files have been merged into it.
        csz*.r1   N*r   ?r   )r   Tc                 <    | d   t        | d   j                        fS )Nr1   r   )r{   r   r   s    r   r   z!GlobDB.finalise.<locals>.<lambda>  s    QqT3qt||;L4Mr   )r   r   )r   r   exts
cased_extsglobsliteralscased_literalsr   r   rI   r|   r   r   r   Ir   fnmatch	translater   )	r*   r%   r   r   r   r   casedrestre_flagss	            r   r   zGlobDB.finalise  s}   
  %	%d+
  MM//1LE5//#%(-$fguEME!!$'qr{tsd{cTk-44eV_E		$**,/66vGw#.C7N %1244**W%6%6w%?xP

!!7E6":; 496?D''06;V_DMM'--/21 ). 2: 	

*MOr   c                 \    	 t        | j                  |            d   S # t        $ r Y yw xY w)zTReturn the first match found for a given path, or None if no match
        is found.r   N)next_match_pathStopIterationr*   r3   s     r   first_matchzGlobDB.first_match  s4    	((./22 		s    	++c                 6    t        | j                  |            S )z<Return a list of (MIMEtype, glob weight) pairs for the path.)r   r  r  s     r   all_matcheszGlobDB.all_matches  s    D$$T*++r   c              #   d  K   t         j                  j                  |      }|| j                  v r| j                  |    |j	                         }|| j
                  v r| j
                  |    |}	 |j                  d      }|dk  rn/||dz   d }|| j                  v r| j                  |   D ]  }|  F|}	 |j                  d      }|dk  rn/||dz   d }|| j                  v r| j                  |   D ]  }|  F| j                  D ]  \  }}}	|j                  |      s||	f   yw)z(Yields pairs of (mimetype, glob weight).r   .r   N)r2   r3   basenamer   r   r   findr   r   r   r~   )
r*   r3   leaflleafextpresregex	mime_typer   s
             r   r  zGlobDB._match_path  s>    ww% 4&&&%%d++

DMM!--&& A1uea!ef+Cdoo%??3/CI 0  A1ueacd)Cdii99S>CI *  +/**&UIv{{4  &)) +5s   D$D0'	D0N)	rZ   r[   r\   rm   r   r   r  r
  r  ra   r   r   r   r     s#    )E.(PT,!*r   r   textplainapplicationzoctet-streaminodeblockdevice
chardevice	directoryfifosocketsymlinkdoor
executableFc                  *   da i at        t              at        j                  t        j                  j                  dd            D ]K  } t        | d      5 }|D ].  }|j                         j                  d d      \  }}|t        |<   0 	 d d d        M t               at        j                  t        j                  j                  dd            D ]  } t        j                  |         t        j!                          t#               at        j                  t        j                  j                  dd            D ]  } t$        j                  |         t$        j!                          t        j                  t        j                  j                  dd            D ]Z  } t        | d      5 }|D ]=  }|j                         j                  d d      \  }}t        |   j'                  |       ? 	 d d d        \ y # 1 sw Y   xY w# 1 sw Y   txY w)	NTr,   rG   r   r   globs2magic
subclasses)_cache_uptodaterG   r   rO   rP   r   r4   r2   r3   r
   r   r   r   r   r   r   r   r   r$  r   )r3   r   r   aliasrI   subparents          r   _cache_databaser*    s    OGc"K --bggll69.MN$_#'::<#5#5dA#> y!*  _ O HE--bggll68.LM N	NN IE--bggll67.KL M	NN --bggll6<.PQ$_"jjl00q9VC $$V,  _ R% _& _s   4G<-AH	<H		H	c                  &    t         st                y y r(   )r&  r*  ra   r   r   rF   rF   =  s     r   c                 @    t                t        j                  |       S )z6Returns type of file by its name, or None if not known)rF   r   r  )r3   s    r   get_type_by_namer-  A  s    NT""r   c                 D    t                t        j                  | ||      S )z:Returns type of file by its contents, or None if not known)rF   r$  r~   )r3   r   r   s      r   get_type_by_contentsr/  F  s    N;;tWg..r   c                 D    t                t        j                  | ||      S )z0Returns type of the data, which should be bytes.)rF   r$  r   )r   r   r   s      r   get_type_by_datar1  L  s    ND'733r   c                 R   t        j                  |       rt        S t        j                  |       rt        S t        j
                  |       rt        S t        j                  |       rt        S t        j                  |       rt        S t        j                  |       rt        S t        S )z.Match special filesystem objects to Mimetypes.)statS_ISDIR	inode_dirS_ISCHR
inode_charS_ISBLKinode_blockS_ISFIFO
inode_fifoS_ISLNKinode_symlinkS_ISSOCKinode_socket
inode_door)st_modes    r   _get_type_by_statrB  R  sn    ||GY.	g	z 1	g	{ 2	w	
!2	g	} 4	w	!4r   c                    t                	 |rt        j                  |       }nt        j                  |       }t        j                  |j                        r[t        | |      }|st	        |       }|st        | |      }|.t        j                  |j                        dz  rt        S t
        S |S t        |j                        S #  t	        |       }|xs t
        cY S xY w)a  Returns type of file indicated by path.
    
    This function is *deprecated* - :func:`get_type2` is more accurate.
    
    :param path: pathname to check (need not exist)
    :param follow: when reading file, follow symbolic links
    :param name_pri: Priority to do name matches. 100=override magic
    
    This tries to use the contents of the file, and falls back to the name. It
    can also handle special filesystem objects like directories and sockets.
    )r   )r   I   )rF   r2   r3  lstatr-  r  S_ISREGrA  r/  S_IMODEapp_exerB  )r3   followname_pristrM   s        r   get_typerL  \  s     NB$B
 ||BJJ x8&t,!*4B!9||BJJ'%/ ,,!T"yDs   -C C!c                    t                	 |rt        j                  |       nt        j                  |       }t        j                  |j                        st        |j                        S t        t        j                  |       d d      }|rS|d   d   }d}|dd D ]  \  }}||k  r n|dz  } |d| }t        |      dk(  r|d   d   S |D cg c]  \  }}|	 }}}nd}	 t        j                  | |      }	|	r|	S |r|d   d   S t        j"                  |j                        dz  rt$        S t'        |       rt(        S t        S # t        $ r t        |       xs t        cY S w xY wc c}}w # t         $ r d}	Y w xY w)	a  Find the MIMEtype of a file using the XDG recommended checking order.
    
    This first checks the filename, then uses file contents if the name doesn't
    give an unambiguous MIMEtype. It can also handle special filesystem objects
    like directories and sockets.
    
    :param path: file path to examine (need not exist)
    :param follow: whether to follow symlinks
    
    :rtype: :class:`MIMEtype`
    
    .. versionadded:: 1.0
    c                     | d   S )Nr   ra   r   s    r   r   zget_type2.<locals>.<lambda>  s    AaDr   Tr   r   r   N)r   rD  )rF   r2   r3  rE  OSErrorr-  octet_streamrF  rA  rB  sortedr   r
  r{   r$  r~   r   rG  rH  is_text_filer  )
r3   rI  rK  mtypes
max_weightr   r   wr   rM   s
             r   	get_type2rV    s{    N6$RWWT]"((4. <<

# ,,E%%d+.4PFAYq\
ABZEB:~FA   v;!!9Q<#)*642aB6*KKxK0 		ay|	bjj	!E	)#D)t;|;G  6%556& +  s)   ,E
 E,&E2 
E)(E)2F ?F c                     	 t        | d      }|5  t        |j                  d            cddd       S # t        $ r Y yw xY w# 1 sw Y   yxY w)zGuess whether a file contains text or binary data.
    
    Heuristic: binary if the first 32 bytes include ASCII control characters.
    This rule may change in future versions.
    
    .. versionadded:: 1.0
    r   F    N)r   r   _is_textr   )r3   r   s     r   rR  rR    sK    t 
r
# 
   
s   4 A	A A Ac                 (    t        d | D               S )Nc              3   X   K   | ]"  }|d k  xs d|cxk  xr dk  nc xs |dk(   $ yw)rs      rX     Nra   rL   bs     r   rN   z_is_text.<locals>.<genexpr>  s.     NAqCx?3!?d??a4i?   (*r   r   s    r   rY  rY    s    NNNNNr   c                 (    t        d | D               S )Nc              3   X   K   | ]"  }|d k  xs d|cxk  xr dk  nc xs |dk(   $ yw) Nra   r^  s     r   rN   z_is_text.<locals>.<genexpr>  s7      +%) F{If&:F&:Ia6kI%)r`  ra  rb  s    r   rY  rY    s!     +%)+ + + 	+r   c                     t                t        sYt        t              at
        j                  j                         D ]'  \  }}|D ]  \  }}t        |   j                  |        ) dat        |    S )aB  Retrieve the set of filename extensions matching a given MIMEtype.
    
    Extensions are returned without a leading dot, e.g. 'py'. If no extensions
    are registered for the MIMEtype, returns an empty set.
    
    The extensions are stored in a cache the first time this is called.
    
    .. versionadded:: 1.0
    T)	rF   _mime2ext_cache_uptodater   rO   _mime2ext_cacher   r   r   r   )r   r  rS  r%   prios        r   get_extensionsrm    se     N#%c* ::++-KC%t&**3/  & . $( 8$$r   c                 .   | dz  } t        |      5 }|j                         }ddd       t        j                  j	                  dd      }t        j                  j	                  ||       }t        j                  |      D ].  }	 t        |      5 }|j                         }ddd       k(  s. y dat        j                  j	                  t        j                  |      |       }t        |d      5 }|j                         ddd       d}	t        j                  t        j                  |	|	t        j                  d            r#t        j                  |       t        d|	z        y# 1 sw Y   JxY w# 1 sw Y   xY w#  Y 	xY w# 1 sw Y   xY w)	a  Copy 'package_file' as ``~/.local/share/mime/packages/<application>.xml.``
    If package_file is None, install ``<app_dir>/<application>.xml``.
    If already installed, does nothing. May overwrite an existing
    file with the same name (if the contents are different)r-   Nr,   packagesFrU  zupdate-mime-databasezThe '%s' command returned an error code!
Make sure you have the freedesktop.org shared MIME package:
http://standards.freedesktop.org/shared-mime-info/)r   r   r2   r3   r
   r   r4   r&  save_data_pathwritespawnlpP_WAITunlink	Exception)
r  package_filer   new_datapackage_dirr=   r   old_datanew_filecommands
             r   install_mime_infor|    sT   
 6K	l	q668 
 '',,vz2Kww||K5H**84	aA668  x 5 O ww||M88E{SH 
h		 
 %G	zz"))Wg}/K/KF/ST
		( GIPQ R 	R U9 
	 	 
	s;   E*FE7F1F*E47F 	<FFFr(   )r   r   )Tr   )T);r]   r2   r   r3  rw   r   r:   r   
xdg.Localexml.domr   r   collectionsr   r8   r   r   r   r   r$  rx   r   r   r   objectr   r   r`   ru  rc   re   r   r   r   r  rP  r9  r7  r5  r;  r?  r=  r@  rH  r&  r*  rF   r-  r/  r1  rB  rL  rV  rR  rY  rk  rj  rm  r|  ra   r   r   <module>r     s  * 
 	  
    * #
A

aC	$I5v I5V	Z 			 	i. i.X;F ;z\; \;|v*V v*r fgm^4Wm,G\*
7K(	GV$
gx(w	*GV$


- -D#
/4#-J5<n$  O+   %,'Rr   