Mini Shell

Direktori : /lib/python3.6/site-packages/xdg/__pycache__/
Upload File :
Current File : //lib/python3.6/site-packages/xdg/__pycache__/Mime.cpython-36.opt-1.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 typecCs||_||_d|_dS)z?Don't use this constructor directly; use mime.lookup() instead.N)rr�_comment)�selfrrrrr�__init__>szMIMEtype.__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-r2r3r4rrrrr<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�crIZhbZlb�w�rrrrrss^










zMagicRule.__init__cCs|j|j|jS)N)rIrKrO)rrrr�	getLength�szMagicRule.getLengthcCs0|j|jkr||_||_n|jr,|jj|�dS)N)rFrCrD�
appendRule)r�rulerrrrV�s
zMagicRule.appendRulecCs$|j|�r |jr|jj|�SdSdS)NT)�match0rC�match)r�bufferrrrrY�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)	�lenrOrIrKrM�PY3rJ�chrrL)	rrZ�l�or,�eZtest�irRrrrrX�s 


zMagicRule.match0cCs$d|j|j|j|j|j|j|jfS)Nz!<MagicRule %d>%d=[%d]%r&%r~%d+%d>)rFrIrKrLrMrNrO)rrrrr4�szMagicRule.__repr__N)	r5r6r7rrUrVrYrXr4rrrrr9rs@r9c@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�	MagicTypecCs||_g|_d|_dS)N)�mtype�	top_rules�	last_rule)rrcrrrr�szMagicType.__init__cCs8t|�}|jr"|jr"|jj|�n|jj|�||_|S)N)r9rFrerVrd�append)rrQZnrulerrr�getLine�szMagicType.getLinecCs$x|jD]}|j|�r|jSqWdS)N)rdrYrc)rrZrWrrrrY�s
zMagicType.matchcCs
d|jS)Nz<MagicType %s>)rc)rrrrr4�szMagicType.__repr__N)r5r6r7rrgrYr4rrrrrb�srbc@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�:�[���rn���ro)�open�readline�IOErrorrHrPrrGrrrbrE�seekrgrUrirf)rZfnamerQ�lineZsheadZpriZtnamercZentsZ	magictyperRrWrrr�	mergeFiles<




zMagicDB.mergeFile�drcCsZxTt|jj�dd�D]>}||kr"q||kr,Px$|j|D]}|j|�}|r8|Sq8WqWdS)NT)�reverse)�sortedr�keysrY)r�data�max_pri�min_priZpriority�type�mrrr�
match_data-s
zMagicDB.match_datacCsBy0t|d��}|j|j�}WdQRX|j|||�SYnXdS)Nrj)rprErir)rr r{r|rQZbufrrrrY9sz
MagicDB.matchcCs
d|jS)Nz<MagicDB %s>)r)rrrrr4AszMagicDB.__repr__N)rvr)rvr)r5r6r7rrurrYr4rrrrrh�s
,

rh�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.�#Nrrlz*.r�*rk�?ro)rp�
startswithrr�exts�globsrf�literals)r rQrtZ	type_name�patternrc�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+rTrZ
subclasses)�_cache_uptodater�r�r�r+rr0r1rhr�rr!rr r
ru�sortrprr�add)r�r rQrt�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*rr �basenamer��lower�findr�r��fnmatch)r ZleafZlleafZext�pZglobZ	mime_typerrr�get_type_by_name�s:

r�rvcCst�tj|||�S)z:Returns type of file by its contents, or None if not known)r*r�rY)r r{r|rrr�get_type_by_contents�sr�cCst�tj|||�S)z0Returns type of the data, which should be bytes.)r*r�r)rzr{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.xmlrZpackagesNFrSzupdate-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/)rprErr 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)rvr)rvr)Trv),r8rr��sysr�r$rZ
xdg.LocaleZxml.domrr�collectionsrr#rr�r�r�r��version_infor\rrrr9rbrhr�r�r�r�r�r�r�r�r�r�r�r*r�r�r�r�r�rrrr�<module>sL
6pJ








7!


*