ok

Mini Shell

Direktori : /opt/alt/python38/lib64/python3.8/site-packages/maxminddb/__pycache__/
Upload File :
Current File : //opt/alt/python38/lib64/python3.8/site-packages/maxminddb/__pycache__/reader.cpython-38.pyc

U

 ��^j(�@s�dZddlmZzddlZWnek
r4dZYnXddlZddlmZmZddl	m
Z
mZmZm
Z
mZddlmZddlmZddlmZGd	d
�d
e�ZGdd�de�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@sxeZdZdZdZdZdZefdd�Zdd�Z	d	d
�Z
dd�Zd
d�Zdd�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		Cs||tkrts|tkrTt|d��*}tj|��dtjd�|_|j��|_W5QRX|}n�|tt	fkr|t
|�|_|j��|_|}nn|tkr�t|d��}|��|_t
|j�|_W5QRX|}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_sizerrr�read�lenr	�name�
ValueError�format�rfind�_METADATA_START_MARKER�max�closerr
�decode�Metadata�	_metadata�search_tree_size�_DATA_SECTION_SEPARATOR_SIZE�_decoder�closed)	�selfZdatabase�modeZdb_file�filenameZmetadata_startZmetadata_decoder�metadata�_�r-�
/reader.py�__init__$sV


���
���zReader.__init__cCs|jS)z7Return the metadata associated with the MaxMind DB file)r#�r(r-r-r.r+bszReader.metadatacCs|�|�\}}|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.�getfsz
Reader.getcCs�t|t�rt|�}n|}zt|j�}Wntk
rBtd��YnX|jdkrh|jj	dkrht
d�|���|�|�\}}|r�|�
|�|fSd|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)�
isinstancerr�	bytearray�packed�AttributeError�	TypeError�versionr#�
ip_versionrr�_find_address_in_tree�_resolve_data_pointer)r(r2ZaddressZpacked_address�pointer�
prefix_lenr-r-r.r1ps"

��zReader.get_with_prefix_lencCs�t|�d}|�|�}|jj}d}||krd||krdd||d?d|d?@}|�||�}|d}q"||krtd|fS||kr�||fStd��dS)N�r���zInvalid node in search tree)r�_start_noder#�
node_count�
_read_noder)r(r9Z	bit_count�noderG�i�bitr-r-r.r>�s

zReader._find_address_in_treecCs\|jjdks|dkrdS|jr$|jSd}td�D] }||jjkrDqR|�|d�}q0||_|S)Nr5�r�`)r#r=�_ipv4_start�rangerGrH)r(ZlengthrIr,r-r-r.rF�szReader._start_nodecCs�||jj}|jj}|dkr@||d}d|j||d�}n�|dkr�|d|}t|j||d��}|r�d|d@|d<q�d|��@d?}|�d|�n6|d	kr�||d}|j||d�}ntd
�|���t	�
d|�dS)N�rD��r6�r��� zUnknown record size: {0}s!I)r#�node_byte_size�record_sizerr8�pop�insertrr�struct�unpack)r(Znode_number�indexZbase_offsetrW�offsetZ
node_bytesZmiddler-r-r.rH�s"zReader._read_nodecCs:||jj|jj}||jkr&td��|j�|�\}}|S)Nz,The MaxMind DB file's search tree is corrupt)r#rGr$rrr&r!)r(r@Zresolved�datar,r-r-r.r?�s

zReader._resolve_data_pointercCs&t|j�ttfkr|j��d|_dS)zBCloses the MaxMind DB file and returns the resources to the systemTN)�typer�str�bytesr r'r0r-r-r.r �s
zReader.closecGs|��dS)N)r �r(�argsr-r-r.�__exit__�szReader.__exit__cCs|jrtd��|S)Nz%Attempt to reopen a closed MaxMind DB)r'rr0r-r-r.�	__enter__�szReader.__enter__)�__name__�
__module__�__qualname__�__doc__r%rrNrr/r+r4r1r>rFrHr?r rdrer-r-r-r.r
s>
	r
c@s8eZdZdZdd�Zedd��Zedd��Zdd	�Zd
S)r"a�Metadata for the MaxMind DB reader


    .. attribute:: binary_format_major_version

      The major version number of the binary format used when creating the
      database.

      :type: int

    .. attribute:: binary_format_minor_version

      The minor version number of the binary format used when creating the
      database.

      :type: int

    .. attribute:: build_epoch

      The Unix epoch for the build time of the database.

      :type: int

    .. attribute:: database_type

      A string identifying the database type, e.g., "GeoIP2-City".

      :type: str

    .. attribute:: description

      A map from locales to text descriptions of the database.

      :type: dict(str, str)

    .. attribute:: ip_version

      The IP version of the data in a database. A value of "4" means the
      database only supports IPv4. A database with a value of "6" may support
      both IPv4 and IPv6 lookups.

      :type: int

    .. attribute:: languages

      A list of locale codes supported by the databse.

      :type: list(str)

    .. attribute:: node_count

      The number of nodes in the database.

      :type: int

    .. attribute:: record_size

      The bit size of a record in the search tree.

      :type: int

    cKs^|d|_|d|_|d|_|d|_|d|_|d|_|d|_|d|_|d	|_d
S)zACreates new Metadata object. kwargs are key/value pairs from specrGrWr=�
database_type�	languages�binary_format_major_version�binary_format_minor_version�build_epoch�descriptionN)	rGrWr=rjrkrlrmrnro)r(�kwargsr-r-r.r/!s







zMetadata.__init__cCs
|jdS)z8The size of a node in bytes

        :type: int
        r6)rWr0r-r-r.rV/szMetadata.node_byte_sizecCs|j|jS)z8The size of the search tree

        :type: int
        )rGrVr0r-r-r.r$7szMetadata.search_tree_sizecCs0d�dd�|j��D��}dj|j|jj|d�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_namer^)�join�__dict__�itemsrrg�	__class__rfrbr-r-r.�__repr__?s�zMetadata.__repr__N)	rfrgrhrir/�propertyrVr$rzr-r-r-r.r"�s@

r")riZ
__future__rr�ImportErrorrZZmaxminddb.compatrrZmaxminddb.constrrrrr	Zmaxminddb.decoderr
Zmaxminddb.errorsrZmaxminddb.filer�objectr
r"r-r-r-r.�<module>s
H

Zerion Mini Shell 1.0