o
    ¼Ü¸`   ã                   @   sÆ  d Z ddlZddlmZ ddlT ddlmZ ddlmZ i Ze 	¡ ed< d Z
Zd ZZd	 ZZd
 ZZd ZZd ZZd ZZd ZZd ZZd ZZd ZZd Z Z!d Z"Z#d Z$Z%d Z&Z'd Z(Z)d Z*Z+d Z,Z-d Z.Z/d Z0Z1d Z2Z3d Z4Z5d Z6Z7d Z8Z9edd ƒZ:G d!d"„ d"e;ƒZ<g e<_=e>e<ƒZ?ed j@ZAe?eA_BeCgeA_Ded jEZFdeF_Be?geF_Ded jGZHeIeH_Be?eIgeH_Ded jJZKeIeK_Be?eCgeK_Ded jLZMeIeM_Be?eNeOgeM_Ded jPZQeIeQ_Be?geQ_Ded jRZSeCeS_Be?eCgeS_Ded jTZUeCeU_Be?eIgeU_Ded jVZWeCeW_Be?eIgeW_Ded jXZYeCeY_Be?eIgeY_Ded jZZ[eCe[_Be?eIge[_Ded j\Z]eCe]_Be?ge]_DG d#d$„ d$e^ƒZ_d%d&„ Z`e_eAe"ƒƒZaea b¡  e_eAe
ƒƒZcec b¡  d'd(„ Zdd)d*„ Zed+d,„ Zfd-d.„ ZgdS )/z
Python bindings for libmagic
é    N)Ú
namedtuple)Ú*)Úfind_libraryé   )ÚloaderÚmagicé   é   é   é   é    é@   é€   é   i   i   i  i   i   i    i @  i €  i   i   i   i   i    i °? Ú	FileMagic)Ú	mime_typeÚencodingÚnamec                   @   s   e Zd ZdS )Ú	magic_setN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú./usr/lib/python3/dist-packages/magic/compat.pyr   3   s    r   c                   @   s†   e Zd Zdd„ Zdd„ Zedd„ ƒZedd„ ƒZd	d
„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zddd„Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚMagicc                 C   s
   || _ d S ©N)Ú_magic_t)ÚselfÚmsr   r   r   Ú__init__l   s   
zMagic.__init__c                 C   s   t | jƒ dS )zO
        Closes the magic database and deallocates any resources used.
        N)Ú_closer   ©r   r   r   r   Úcloseo   s   zMagic.closec                 C   óB   | d u rd S t | tƒr| S zt| dƒW S  ty    t| ƒ Y S w ©Nzutf-8)Ú
isinstanceÚstrÚ	TypeError)Úsr   r   r   Ú__tostru   ó   
ÿzMagic.__tostrc                 C   r#   r$   )r%   Úbytesr'   )Úbr   r   r   Ú	__tobytes€   r*   zMagic.__tobytesc                 C   s   t  t| jt  |¡ƒ¡S )zè
        Returns a textual description of the contents of the argument passed
        as a filename or None if an error occurred and the MAGIC_ERROR flag
        is set. A call to errno() will return the numeric error code.
        )r   Ú_Magic__tostrÚ_filer   Ú_Magic__tobytes©r   Úfilenamer   r   r   Úfile‹   ó   z
Magic.filec                 C   s   t  t| j|ƒ¡S )zï
        Returns a textual description of the contents of the argument passed
        as a file descriptor or None if an error occurred and the MAGIC_ERROR
        flag is set. A call to errno() will return the numeric error code.
        )r   r.   Ú_descriptorr   )r   Úfdr   r   r   Ú
descriptor“   s   zMagic.descriptorc                 C   s   t  t| j|t|ƒƒ¡S )zæ
        Returns a textual description of the contents of the argument passed
        as a buffer or None if an error occurred and the MAGIC_ERROR flag
        is set. A call to errno() will return the numeric error code.
        )r   r.   Ú_bufferr   Úlen)r   Úbufr   r   r   Úbuffer›   r4   zMagic.bufferc                 C   s   t  t| jƒ¡S )zh
        Returns a textual explanation of the last error or None
        if there was no error.
        )r   r.   Ú_errorr   r!   r   r   r   Úerror£   s   zMagic.errorc                 C   s   t | j|ƒS )a*  
        Set flags on the magic object which determine how magic checking
        behaves; a bitwise OR of the flags described in libmagic(3), but
        without the MAGIC_ prefix.

        Returns -1 on systems that don't support utime(2) or utimes(2)
        when PRESERVE_ATIME is set.
        )Ú	_setflagsr   )r   Úflagsr   r   r   Úsetflagsª   s   	zMagic.setflagsNc                 C   ó   t | jt |¡ƒS )a	  
        Must be called to load entries in the colon separated list of database
        files passed as argument or the default database file if no argument
        before any magic queries can be performed.

        Returns 0 on success and -1 on failure.
        )Ú_loadr   r   r0   r1   r   r   r   Úloadµ   ó   z
Magic.loadc                 C   rA   )aD  
        Compile entries in the colon separated list of database files
        passed as argument or the default database file if no argument.
        The compiled files created are named from the basename(1) of each file
        argument with ".mgc" appended to it.

        Returns 0 on success and -1 on failure.
        )Ú_compiler   r   r0   ©r   Údbsr   r   r   Úcompile¿   s   	zMagic.compilec                 C   rA   ©zÞ
        Check the validity of entries in the colon separated list of
        database files passed as argument or the default database file
        if no argument.

        Returns 0 on success and -1 on failure.
        )Ú_checkr   r   r0   rF   r   r   r   ÚcheckÊ   rD   zMagic.checkc                 C   rA   rI   )Ú_listr   r   r0   rF   r   r   r   ÚlistÔ   rD   z
Magic.listc                 C   s
   t | jƒS )a  
        Returns a numeric error code. If return value is 0, an internal
        magic error occurred. If return value is non-zero, the value is
        an OS error code. Use the errno module or os.strerror() can be used
        to provide detailed error information.
        )Ú_errnor   r!   r   r   r   ÚerrnoÞ   s   
zMagic.errnor   )r   r   r   r   r"   Ústaticmethodr.   r0   r3   r7   r;   r=   r@   rC   rH   rK   rM   rO   r   r   r   r   r   k   s"    







r   c                 C   s   t t| ƒƒS )zd
    Returns a magic object on success and None on failure.
    Flags argument as for setflags.
    )r   Ú_open)r?   r   r   r   Úopenè   s   rR   c                 C   s$   |   d¡\}}t||| dd¡d�S )Nz; zcharset=Ú )r   r   r   )Úsplitr   Úreplace)Úmime_detectedÚtype_detectedr   Úmime_encodingr   r   r   Ú_create_filemagic÷   s   
ÿrY   c                 C   ó   t t | ¡t | ¡ƒS )zdDetect mime type, encoding and file type from a filename

    Returns a `FileMagic` namedtuple.
    )rY   Ú
mime_magicr3   Ú
none_magic)r2   r   r   r   Údetect_from_filenameþ   ó   
ÿr]   c                 C   s   |   ¡ }tt |¡t |¡ƒS )zjDetect mime type, encoding and file type from file-like object

    Returns a `FileMagic` namedtuple.
    )ÚfilenorY   r[   r7   r\   )ÚfobjÚfile_descriptorr   r   r   Údetect_from_fobj  s   
ÿrb   c                 C   rZ   )z_Detect mime type, encoding and file type from bytes

    Returns a `FileMagic` namedtuple.
    )rY   r[   r;   r\   )Úbyte_contentr   r   r   Údetect_from_content  r^   rd   )hÚ__doc__ÚctypesÚcollectionsr   Úctypes.utilr   rS   r   Ú
_librariesÚload_libÚ
MAGIC_NONEÚNONEÚMAGIC_DEBUGÚDEBUGÚMAGIC_SYMLINKÚSYMLINKÚMAGIC_COMPRESSÚCOMPRESSÚMAGIC_DEVICESÚDEVICESÚMAGIC_MIME_TYPEÚ	MIME_TYPEÚMAGIC_CONTINUEÚCONTINUEÚMAGIC_CHECKÚCHECKÚMAGIC_PRESERVE_ATIMEÚPRESERVE_ATIMEÚ	MAGIC_RAWÚRAWÚMAGIC_ERRORÚERRORÚMAGIC_MIME_ENCODINGÚMIME_ENCODINGÚ
MAGIC_MIMEÚMIMEÚMAGIC_APPLEÚAPPLEÚMAGIC_NO_CHECK_COMPRESSÚNO_CHECK_COMPRESSÚMAGIC_NO_CHECK_TARÚNO_CHECK_TARÚMAGIC_NO_CHECK_SOFTÚNO_CHECK_SOFTÚMAGIC_NO_CHECK_APPTYPEÚNO_CHECK_APPTYPEÚMAGIC_NO_CHECK_ELFÚNO_CHECK_ELFÚMAGIC_NO_CHECK_TEXTÚNO_CHECK_TEXTÚMAGIC_NO_CHECK_CDFÚNO_CHECK_CDFÚMAGIC_NO_CHECK_TOKENSÚNO_CHECK_TOKENSÚMAGIC_NO_CHECK_ENCODINGÚNO_CHECK_ENCODINGÚMAGIC_NO_CHECK_BUILTINÚNO_CHECK_BUILTINr   Ú	Structurer   Ú_fields_ÚPOINTERÚmagic_tÚ
magic_openrQ   ÚrestypeÚc_intÚargtypesÚmagic_closer    Ú
magic_filer/   Úc_char_pÚmagic_descriptorr5   Úmagic_bufferr8   Úc_void_pÚc_size_tÚmagic_errorr<   Úmagic_setflagsr>   Ú
magic_loadrB   Úmagic_compilerE   Úmagic_checkrJ   Ú
magic_listrL   Úmagic_errnorN   Úobjectr   rR   r[   rC   r\   rY   r]   rb   rd   r   r   r   r   Ú<module>   s¤   



















}	
