File: //opt/alt/python38/lib64/python3.8/site-packages/maxminddb/__pycache__/reader.cpython-38.pyc
U
��^j( � @ s� d Z ddlmZ zddlZW n ek
r4 dZY nX ddlZddlmZmZ ddl m
Z
mZmZm
Z
mZ ddlmZ ddlmZ ddlmZ G d d
� d
e�ZG dd� de�ZdS )
zo
maxminddb.reader
~~~~~~~~~~~~~~~~
This module contains the pure Python database reader and related classes.
� )�unicode_literalsN)�compat_ip_address�string_type)� MODE_AUTO� MODE_MMAP� MODE_FILE�MODE_MEMORY�MODE_FD)�Decoder)�InvalidDatabaseError)�
FileBufferc @ sx e Zd ZdZdZdZdZefdd�Zdd� Z d d
� Z
dd� Zd
d� Zdd� Z
dd� Zdd� Zdd� Zdd� 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.comNc C s| |t krts|tkrTt|d��*}tj|�� dtjd�| _| j�� | _W 5 Q R X |}n�|t t fkr|t
|�| _| j�� | _|}nn|tkr�t|d��}|�� | _t
| j�| _W 5 Q R X |}n4|tkr�|�� | _t
| j�| _|j}ntd�|���| j�| jtd| jd ��}|dk�r&| �� td�|���|t
| j�7 }t| j|�}|�|�\}}tf |�| _t| j| jj| j �| _d| _d S )
a� Reader for the MaxMind DB file format
Arguments:
database -- A path to a valid MaxMind DB file such as a GeoIP2 database
file, or a file descriptor in the case of MODE_FD.
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.
* MODE_FD - the param passed via database is a file descriptor, not
a path. This mode implies MODE_MEMORY.
�rbr )�accesszwUnsupported open mode ({0}). Only MODE_AUTO, MODE_FILE, MODE_MEMORY and MODE_FD are supported by the pure Python Readeri ���zCError opening database file ({0}). Is this a valid MaxMind DB file?FN)r �mmapr �open�filenoZACCESS_READ�_buffer�size�_buffer_sizer r r �read�lenr �name�
ValueError�format�rfind�_METADATA_START_MARKER�max�closer r
�decode�Metadata� _metadata�search_tree_size�_DATA_SECTION_SEPARATOR_SIZE�_decoder�closed) �selfZdatabase�modeZdb_file�filenameZmetadata_startZmetadata_decoder�metadata�_� r- �C/opt/alt/python38/lib64/python3.8/site-packages/maxminddb/reader.py�__init__$ sV
�� �
���zReader.__init__c C s | j S )z7Return the metadata associated with the MaxMind DB file)r# �r( r- r- r. r+ b s zReader.metadatac C s | � |�\}}|S )z�Return the record for the ip_address in the MaxMind DB
Arguments:
ip_address -- an IP address in the standard string notation
)�get_with_prefix_len)r( �
ip_address�recordr, r- r- r. �getf s z
Reader.getc C s� t |t�rt|�}n|}zt|j�}W n tk
rB td��Y nX |jdkrh| jj dkrht
d�|���| �|�\}}|r�| �
|�|fS d|fS )z�Return a tuple with the record and the associated prefix length
Arguments:
ip_address -- an IP address in the standard string notation
z/argument 1 must be a string or ipaddress object� � zXError looking up {0}. You attempted to look up an IPv6 address in an IPv4-only database.N)�
isinstancer r � bytearray�packed�AttributeError� TypeError�versionr# �
ip_versionr r �_find_address_in_tree�_resolve_data_pointer)r( r2 �addressZpacked_address�pointer�
prefix_lenr- r- r. r1 p s"