Your IP : 216.73.216.213


Current Path : /opt/alt/python35/lib64/python3.5/site-packages/maxminddb/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib64/python3.5/site-packages/maxminddb/__pycache__/reader.cpython-35.pyc



�jvXn�@s�dZddlmZyddlZWnek
r@dZYnXddlZddlmZmZddl	m
Z
mZmZm
Z
ddlmZddlmZddlmZGd	d
�d
e�ZGdd�de�ZdS)
zo
maxminddb.reader
~~~~~~~~~~~~~~~~

This module contains the pure Python database reader and related classes.

�)�unicode_literalsN)�
byte_from_int�compat_ip_address)�	MODE_AUTO�	MODE_MMAP�	MODE_FILE�MODE_MEMORY)�Decoder)�InvalidDatabaseError)�
FileBufferc@s�eZdZdZdZdZdZedd�Zdd�Z	d	d
�Z
dd�Zd
d�Zdd�Z
dd�Zdd�ZdS)�Readerz�
    Instances of this class provide a reader for the MaxMind DB format. IP
    addresses can be looked up using the ``get`` method.
    �s���MaxMind.comNcCs�|tkrts|tkrpt|d��=}tj|j�ddtj�|_|jj�|_WdQRXn�|tt	fkr�t
|�|_|jj�|_n^|tkr�t|d��(}|j�|_t
|j�|_WdQRXntdj|���|jj|jtd|jd
��}|dkrW|j�td	j|���|t
|j�7}t|j|�}|j|�\}}t|�|_t|j|jj|j�|_dS)a�Reader for the MaxMind DB file format

        Arguments:
        database -- A path to a valid MaxMind DB file such as a GeoIP2
                    database file.
        mode -- mode to open the database with. Valid mode are:
            * MODE_MMAP - read from memory map.
            * MODE_FILE - read database as standard file.
            * MODE_MEMORY - load database into memory.
            * MODE_AUTO - tries MODE_MMAP and then MODE_FILE. Default.
        �rbr�accessNznUnsupported open mode ({0}). Only MODE_AUTO,  MODE_FILE, and MODE_MEMORY are support by the pure Python Reader�i�zCError opening database file ({0}). Is this a valid MaxMind DB file?i���)r�mmapr�open�filenoZACCESS_READ�_buffer�size�_buffer_sizerrr�read�len�
ValueError�format�rfind�_METADATA_START_MARKER�max�closer
r	�decode�Metadata�	_metadata�search_tree_size�_DATA_SECTION_SEPARATOR_SIZE�_decoder)�selfZdatabase�modeZdb_fileZmetadata_startZmetadata_decoder�metadata�_�r+�C/opt/alt/python35/lib64/python3.5/site-packages/maxminddb/reader.py�__init__$s8
	
	zReader.__init__cCs|jS)z7Return the metadata associated with the MaxMind DB file)r#)r'r+r+r,r)UszReader.metadatacCsht|�}|jdkrB|jjdkrBtdj|���|j|�}|rd|j|�SdS)z�Return the record for the ip_address in the MaxMind DB


        Arguments:
        ip_address -- an IP address in the standard string notation
        ��zXError looking up {0}. You attempted to look up an IPv6 address in an IPv4-only database.N)r�versionr#�
ip_versionrr�_find_address_in_tree�_resolve_data_pointer)r'�
ip_address�address�pointerr+r+r,�getYs!z
Reader.getcCs�t|j�}t|�d}|j|�}xWt|�D]I}||jjkrTPd||d?d|d?@}|j||�}q;W||jjkr�dS||jjkr�|Std��dS)N�r��rzInvalid node in search tree)	�	bytearray�packedr�_start_node�ranger#�
node_count�
_read_noder
)r'r4r<Z	bit_count�node�i�bitr+r+r,r2kszReader._find_address_in_treecCs�|jjdks|dkr"dS|jr2|jSd}x9td�D]+}||jjkr^P|j|d�}qEW||_|S)Nr.rr�`)r#r1�_ipv4_startr>r?r@)r'�lengthrAr*r+r+r,r=~s		zReader._start_nodecCs:||jj}|jj}|dkrT||d}d|j||d�}n�|dkr�tjd|j|d|d��\}|r�|dM}nd|@d?}||d}t|�|j||d�}nI|d	kr||d}|j||d�}ntd
j|���tjd|�dS)
N�r9s�s!Br/���� zUnknown record size: {0}s!Ir)	r#�node_byte_size�record_sizer�struct�unpackrr
r)r'Znode_number�indexZbase_offsetrM�offsetZ
node_bytesZmiddler+r+r,r@�s($
	zReader._read_nodecCsQ||jj|jj}||jkr5td��|jj|�\}}|S)Nz,The MaxMind DB file's search tree is corrupt)r#r?r$rr
r&r!)r'r6Zresolved�datar*r+r+r,r3�s

	zReader._resolve_data_pointercCs,t|j�ttfkr(|jj�dS)zBCloses the MaxMind DB file and returns the resources to the systemN)�typer�str�bytesr )r'r+r+r,r �szReader.close)�__name__�
__module__�__qualname__�__doc__r%rrErr-r)r7r2r=r@r3r r+r+r+r,rs1rc@sReZdZdZdd�Zedd��Zedd��Zdd	�Zd
S)r"z"Metadata for the MaxMind DB readercKsy|d|_|d|_|d|_|d|_|d|_|d|_|d|_|d|_|d	|_d
S)zACreates new Metadata object. kwargs are key/value pairs from specr?rMr1�
database_type�	languages�binary_format_major_version�binary_format_minor_version�build_epoch�descriptionN)	r?rMr1rZr[r\r]r^r_)r'�kwargsr+r+r,r-�s







zMetadata.__init__cCs|jdS)zThe size of a node in bytesr/)rM)r'r+r+r,rL�szMetadata.node_byte_sizecCs|j|jS)zThe size of the search tree)r?rL)r'r+r+r,r$�szMetadata.search_tree_sizecCsJdjdd�|jj�D��}djd|jd|jjd|�S)Nz, css|]}d|VqdS)z%s=%rNr+)�.0�xr+r+r,�	<genexpr>�sz$Metadata.__repr__.<locals>.<genexpr>z{module}.{class_name}({data})�module�
class_namerR)�join�__dict__�itemsrrW�	__class__rV)r'�argsr+r+r,�__repr__�s
%		zMetadata.__repr__N)	rVrWrXrYr-�propertyrLr$rkr+r+r+r,r"�s
r")rY�
__future__rr�ImportErrorrNZmaxminddb.compatrrZmaxminddb.constrrrrZmaxminddb.decoderr	Zmaxminddb.errorsr
Zmaxminddb.filer�objectrr"r+r+r+r,�<module>s
"�