Mini Shell

Direktori : /lib/python3.6/site-packages/xdg/__pycache__/
Upload File :
Current File : //lib/python3.6/site-packages/xdg/__pycache__/Mime.cpython-36.pyc

3

�Y�]�>�@sddZddlZddlZddlZddlZddlmZddlZddlm	Z	m
Z
ddlmZdZ
iZdadadadaejddkZdd	�Zd1d
d�ZGdd
�d
�ZGdd�d�ZGdd�d�ZGdd�d�Zedd�Zedd�Zedd�Zedd�Zedd�Zedd�Z edd�Z!edd�Z"edd�Z#d a$d!d"�Z%d#d$�Z&d%d&�Z'd2d(d)�Z(d3d*d+�Z)d4d-d.�Z*d/d0�Z+dS)5a�
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�cCsdjdd�|jD��j�S)zGet text of XML node�cSsg|]
}|j�qS�)Z	nodeValue)�.0�nrr�/usr/lib/python3.6/Mime.py�
<listcomp>.sz"_get_node_data.<locals>.<listcomp>)�joinZ
childNodes�strip)Znoderrr�_get_node_data,srcCsJ|dkr d|kr |jdd�\}}||ftkr>t||�t||f<t||fS)z�Get the MIMEtype object for this type, creating a new one if needed.
    
    The name can either be passed as one part ('text/plain'), or as two
    ('text', 'plain').
    N�/�)�split�types�MIMEtype)�media�subtyperrr�lookup0s
rc@sHeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dS)rz#Type holding data about a MIME typecCsF|rd|kst�|rd|ks t�||ftks0t�||_||_d|_dS)z?Don't use this constructor directly; use mime.lookup() instead.rN)�AssertionErrorrrr�_comment)�selfrrrrr�__init__>szMIMEtype.__init__cCs�tjjd|j|jd�}x�tj|�D]x}tj|�}|dkr<q$x^|j	j
td�D]L}|jt
d�p^d}d|tjjk}||jdkr�|t|�f|_|d	krLdSqLWq$WdS)
z>Loads comment for current language. Use get_comment() instead.�mimez.xmlN�comment�langZenrr�)�os�pathr
rrr�load_data_pathsr�parseZdocumentElementZgetElementsByTagNameNS�FREE_NSZgetAttributeNSr�xdgZLocaleZlangsrr)r�resourcer!�docrrZgoodnessrrr�_loadHs
zMIMEtype._loadcCs*|jdkr dt|�f|_|j�|jdS)z;Returns comment for current language, loading it if needed.Nrr)r�strr()rrrr�get_commentWs
zMIMEtype.get_commentcCs&t�t|�}|tkr"tt|�S|S)z:Returns the canonical MimeType object if this is an alias.)�update_cacher)�aliasesr)r�srrr�	canonical_s
zMIMEtype.canonicalcCs t�tdd�tt|�D��S)z5Returns a set of Mime types which this inherits from.css|]}t|�VqdS)N)r)r	�trrr�	<genexpr>jsz)MIMEtype.inherits_from.<locals>.<genexpr>)r+�set�inheritancer))rrrr�
inherits_fromgszMIMEtype.inherits_fromcCs|jd|jS)Nr)rr)rrrr�__str__lszMIMEtype.__str__cCsd||jpdfS)Nz<%s: %s>z(comment not loaded))r)rrrr�__repr__oszMIMEtype.__repr__N)�__name__�
__module__�__qualname__�__doc__rr(r*r.r3r4r5rrrrr<s
rc@s<eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)�	MagicRulec	Cs�d|_d|_d}x |jd�}|dkr&P||7}qW|s>d|_nt|jd��|_d}x |jd�}|dkrhP||7}qTWt|jd��|_|jd�}|jd�}t|�t|�d>|_|j|j�|_	|jd�}|dkr�|j|j�|_
|jd�}nd|_
|d	k�rRd}xB|d
k�r>|dk�r>|jd�}|d
k�s0|dk�r2P||7}�q�Wt|jd��|_nd|_|d
k�r�d}x.|dk�r�|jd�}|dk�r�P||7}�qhWt|jd��|_nd|_|dk�r�t
d��dS)
N�r�>r�ascii�=��&�~�+�
zMalformed MIME magic line)�next�prev�read�nest�int�decode�start�ord�lenvalue�value�mask�word�range�
ValueError)	r�fZind�crJZhbZlb�w�rrrrrss^










zMagicRule.__init__cCs|j|j|jS)N)rJrLrP)rrrr�	getLength�szMagicRule.getLengthcCs0|j|jkr||_||_n|jr,|jj|�dS)N)rGrDrE�
appendRule)r�rulerrrrW�s
zMagicRule.appendRulecCs$|j|�r |jr|jj|�SdSdS)NT)�match0rD�match)r�bufferrrrrZ�s
zMagicRule.matchc	Cs�t|�}x�t|j�D]�}|j|}||j}||kr8dS|jr�d}xft|j�D]J}trn||||j|@}nt|||�t|j|�@}|t|�7}qNWn|||�}||jkrdSqWdS)NFrT)	�lenrPrJrLrN�PY3rK�chrrM)	rr[�l�or-�eZtest�irSrrrrY�s 


zMagicRule.match0cCs$d|j|j|j|j|j|j|jfS)Nz!<MagicRule %d>%d=[%d]%r&%r~%d+%d>)rGrJrLrMrNrOrP)rrrrr5�szMagicRule.__repr__N)	r6r7r8rrVrWrZrYr5rrrrr:rs@r:c@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�	MagicTypecCs||_g|_d|_dS)N)�mtype�	top_rules�	last_rule)rrdrrrr�szMagicType.__init__cCs8t|�}|jr"|jr"|jj|�n|jj|�||_|S)N)r:rGrfrWre�append)rrRZnrulerrr�getLine�szMagicType.getLinecCs$x|jD]}|j|�r|jSqWdS)N)rerZrd)rr[rXrrrrZ�s
zMagicType.matchcCs
d|jS)Nz<MagicType %s>)rd)rrrrr5�szMagicType.__repr__N)r6r7r8rrhrZr5rrrrrc�srcc@s8eZdZdd�Zdd�Zddd�Zdd	d
�Zdd�Zd
S)�MagicDBcCsi|_d|_dS)Nr)r�maxlen)rrrrr�szMagicDB.__init__cCsFt|d���0}|j�}|dkr&td���x|j�jd�}|s>P|ddksZ|d
d�dkrbtd	��|d
d�jd�\}}t|�}t|�}y|j|}Wng}||j|<YnXt	|�}	|j
d
�}
|jdd
�xR|
o�|
dk�r"|	j|�}|�r
|j
�|jk�r
|j
�|_|j
d
�}
|jdd
�q�W|j|	�|
s*Pq*WWdQRXdS)N�rbsMIME-Magic
zNot a MIME magic filer=r�[rz]
zMalformed section headingr�:�[���ro���rp)�open�readline�IOErrorrIrQrrHrrrcrF�seekrhrVrjrg)rZfnamerR�lineZsheadZpriZtnamerdZentsZ	magictyperSrXrrr�	mergeFiles<




zMagicDB.mergeFile�drcCsZxTt|jj�dd�D]>}||kr"q||kr,Px$|j|D]}|j|�}|r8|Sq8WqWdS)NT)�reverse)�sortedr�keysrZ)r�data�max_pri�min_priZpriority�type�mrrr�
match_data-s
zMagicDB.match_datacCsBy0t|d��}|j|j�}WdQRX|j|||�SYnXdS)Nrk)rqrFrjr�)rr!r|r}rRZbufrrrrZ9sz
MagicDB.matchcCs
d|jS)Nz<MagicDB %s>)r)rrrrr5AszMagicDB.__repr__N)rwr)rwr)r6r7r8rrvr�rZr5rrrrri�s
,

ri�textZplain�inodeZblockdeviceZ
chardeviceZ	directoryZfifoZsocket�symlinkZdoor�application�
executableFcCsPdaiagaiaiatt�at�a	dd�}x$t
jtj
jdd��D]}||�q@Wx&t
jtj
jdd��D]}t	j|�qfWtjdd�d	�x\t
jtj
jdd
��D]D}t|d��0}x(|D] }|j�jdd�\}}|t|<q�WWdQRXq�Wxdt
jtj
jdd
��D]L}t|d��8}x0|D](}|j�jdd�\}}t|j|��qWWdQRXq�WdS)NTcSs�t|���}x�|D]�}|jd�r q|dd
�}|jdd�\}}t|�}|jd�r�|dd�}d|kppd|kppd	|ks�|tkr|t|<qd|ks�d|ks�d	|kr�tj||f�q|t|<qWWdQRXdS)z6Loads name matching information from a MIME directory.�#Nrrmz*.r�*rl�?rp)rq�
startswithrr�exts�globsrg�literals)r!rRruZ	type_name�patternrd�restrrr�_import_glob_file^s 



z*_cache_database.<locals>._import_glob_filerr��magiccSst|d�S)Nr)r\)�xrrr�<lambda>ysz!_cache_database.<locals>.<lambda>)�keyr,rUrZ
subclasses)�_cache_uptodater�r�r�r,rr1r2rir�rr"r r!r
rv�sortrqrr�add)r�r!rRru�aliasr.�sub�parentrrr�_cache_databaseRs.

r�cCsts
t�dS)N)r�r�rrrrr+�sr+cCs�t�tjj|�}|tkr"t|S|j�}|tkr:t|S|}x8|jd�}|dkrTP||dd�}|tkr@t|Sq@W|}x8|jd�}|dkr�P||dd�}|tkr~t|Sq~Wx0tD](\}}t	j	||�r�|St	j	||�r�|Sq�WdS)z6Returns type of file by its name, or None if not known�.rrN)
r+r r!�basenamer��lower�findr�r��fnmatch)r!ZleafZlleafZext�pZglobZ	mime_typerrr�get_type_by_name�s:

r�rwcCst�tj|||�S)z:Returns type of file by its contents, or None if not known)r+r�rZ)r!r|r}rrr�get_type_by_contents�sr�cCst�tj|||�S)z0Returns type of the data, which should be bytes.)r+r�r�)r{r|r}rrr�get_type_by_data�sr�Tc
Cs�t�y|rtj|�}n
tj|�}Wnt|�}|p:tStj|j�r�t||d�}|s`t|�}|spt||d�}|dkr�tj	|j�d@r�t
StS|Stj|j�r�tStj
|j�r�tStj|j�r�tStj|j�r�tStj|j�r�tStj|j��r�tStS)a�Returns type of file indicated by path.
    
    path :
      pathname to check (need not exist)
    follow :
      when reading file, follow symbolic links
    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|N�I)r+r �stat�lstatr�r��S_ISREG�st_moder��S_IMODE�app_exe�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)r!ZfollowZname_pri�str/rrr�get_type�s@
r�c	
Cs�|d7}t|�j�}tjjdd�}tjj||�}x>tj|�D]0}yt|�j�}Wnw<YnX||kr<dSq<Wdatjjtj|�|�}t|d�j	|�d}tj
tj||tjd��r�tj|�t
d|��dS)	aCopy '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)z.xmlrZpackagesNFrTzupdate-mime-databasez�The '%s' command returned an error code!
Make sure you have the freedesktop.org shared MIME package:
http://standards.freedesktop.org/shared-mime-info/)rqrFr r!r
rr"r�Zsave_data_path�write�spawnlp�P_WAIT�unlink�	Exception)	r�Zpackage_fileZnew_dataZpackage_dirr&r�Zold_dataZnew_fileZcommandrrr�install_mime_info�s&
r�)N)rwr)rwr)Trw),r9r r��sysr�r%rZ
xdg.LocaleZxml.domrr�collectionsrr$rr�r�r�r��version_infor]rrrr:rcrir�r�r�r�r�r�r�r�r�r�r�r+r�r�r�r�r�rrrr�<module>sL
6pJ








7!


*