U
    [e*                     @   s   d Z zddlZW n ek
r(   dZY nX ddlZddlZddlmZmZ ddlmZ ddl	m
Z
mZmZmZmZmZ ddlmZmZmZmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ G dd dZG dd dZdS )zo
maxminddb.reader
~~~~~~~~~~~~~~~~

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

    N)IPv4AddressIPv6Address)PathLike)AnyAnyStrIOOptionalTupleUnion)	MODE_AUTO	MODE_MMAP	MODE_FILEMODE_MEMORYMODE_FD)Decoder)InvalidDatabaseError)
FileBuffer)Recordc                   @   s$  e Zd ZU dZdZdZeeedf e	d< dZ
ee e	d< efeeeeef eddd	d
ZddddZeeeef ee dddZeeeef eee ef dddZeeeef dddZeedddZeeedddZeedddZddd d!Zddd"d#Z d dd$d%Z!dS )&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.comz	mmap.mmap_bufferN_ipv4_start)databasemodereturnc           	   	   C   s  |t krts|tkrTt|d*}tj| dtjd| _| j | _W 5 Q R X |}n|t t	fkr|t
|| _| j | _|}np|tkrt|d}| | _t| j| _W 5 Q R X |}n6|tkr| | _t| j| _|j}ntd| d| j| jtd| jd }|dkr*|   td| d	|t| j7 }t| j|}||\}}t|tsntd
| dtf || _t| j| jj| 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   )accesszUnsupported open mode (z]). Only MODE_AUTO, MODE_FILE, MODE_MEMORY and MODE_FD are supported by the pure Python Readeri   zError opening database file (z#). Is this a valid MaxMind DB file?z)Error reading metadata in database file (z).FN) r   mmapr   openfilenoACCESS_READr   size_buffer_sizer   r   r   readlenr   name
ValueErrorrfind_METADATA_START_MARKERmaxcloser   r   decode
isinstancedictMetadata	_metadatasearch_tree_size_DATA_SECTION_SEPARATOR_SIZE_decoderclosed)	selfr   r   Zdb_filefilenameZmetadata_startZmetadata_decodermetadata_ r9   R/var/www/html/services/stratfitenv/lib/python3.8/site-packages/maxminddb/reader.py__init__'   sV    



 


zReader.__init__r/   r   c                 C   s   | j S )z7Return the metadata associated with the MaxMind DB file)r0   r5   r9   r9   r:   r7   m   s    zReader.metadata)
ip_addressr   c                 C   s   |  |\}}|S )zReturn 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)r5   r>   recordr8   r9   r9   r:   getq   s    z
Reader.getc              
   C   s   t |trt|}n|}zt|j}W n, tk
rT } ztd|W 5 d}~X Y nX |jdkr|| j	j
dkr|td| d| |\}}|r| ||fS d|fS )zReturn 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 objectN      zError looking up zD. You attempted to look up an IPv6 address in an IPv4-only database.)r-   str	ipaddressr>   	bytearraypackedAttributeError	TypeErrorversionr0   
ip_versionr'   _find_address_in_tree_resolve_data_pointer)r5   r>   addressZpacked_addressexpointer
prefix_lenr9   r9   r:   r?   {   s    	

zReader.get_with_prefix_len)rG   r   c                 C   s   t |d }| |}| jj}d}||k rd||k rdd||d?  d|d  ? @ }| ||}|d }q"||krtd|fS ||kr||fS tdd S )N   r            zInvalid node in search tree)r%   _start_noder0   
node_count
_read_noder   )r5   rG   Z	bit_countnoderW   ibitr9   r9   r:   rL      s    

zReader._find_address_in_tree)lengthr   c                 C   s\   | j jdks|dkrdS | jr$| jS d}tdD ] }|| j jkrD qR| |d}q0|| _|S )NrB      r   `   )r0   rK   r   rangerW   rX   )r5   r\   rY   r8   r9   r9   r:   rV      s    zReader._start_node)node_numberindexr   c                 C   s   || j j }| j j}|dkr@||d  }d| j||d   }n|dkr|d|  }t| j||d  }|rd|d @ |d< qd| @ d? }|d| n6|d	kr||d  }| j||d  }ntd
| t	d|d S )N   rT          rC      r          zUnknown record size: s   !I)
r0   node_byte_sizerecord_sizer   rF   popinsertr   structunpack)r5   r`   ra   Zbase_offsetri   offsetZ
node_bytesmiddler9   r9   r:   rX      s"    zReader._read_node)rP   r   c                 C   s:   || j j | j j }|| jkr&td| j|\}}|S )Nz,The MaxMind DB file's search tree is corrupt)r0   rW   r1   r#   r   r3   r,   )r5   rP   resolveddatar8   r9   r9   r:   rM      s
    
zReader._resolve_data_pointerc                 C   s.   z| j   W n tk
r"   Y nX d| _dS )zBCloses the MaxMind DB file and returns the resources to the systemTN)r   r+   rH   r4   r=   r9   r9   r:   r+      s
    zReader.closec                 G   s   |    d S )N)r+   r5   argsr9   r9   r:   __exit__   s    zReader.__exit__c                 C   s   | j rtd| S )Nz%Attempt to reopen a closed MaxMind DB)r4   r'   r=   r9   r9   r:   	__enter__   s    zReader.__enter__)"__name__
__module____qualname____doc__r2   r)   r
   bytesr   __annotations__r   r   intr   r   r   r   r;   r7   rD   r   r   r   rA   r	   r?   rF   rL   rV   rX   rM   r+   rt   ru   r9   r9   r9   r:   r      s.   
 F	r   c                   @   sJ   e Zd ZdZddddZeedddZeeddd	Zd
d Z	d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

    Nr<   c                 K   s^   |d | _ |d | _|d | _|d | _|d | _|d | _|d | _|d | _|d	 | _d
S )zACreates new Metadata object. kwargs are key/value pairs from specrW   ri   rK   database_type	languagesbinary_format_major_versionbinary_format_minor_versionbuild_epochdescriptionN)	rW   ri   rK   r}   r~   r   r   r   r   )r5   kwargsr9   r9   r:   r;   /  s    







zMetadata.__init__c                 C   s
   | j d S )z8The size of a node in bytes

        :type: int
        rC   )ri   r=   r9   r9   r:   rh   =  s    zMetadata.node_byte_sizec                 C   s   | j | j S )z8The size of the search tree

        :type: int
        )rW   rh   r=   r9   r9   r:   r1   E  s    zMetadata.search_tree_sizec                 C   s6   d dd | j D }| j d| jj d| dS )Nz, c                 s   s    | ]\}}| d |V  qdS )=Nr9   ).0kvr9   r9   r:   	<genexpr>N  s     z$Metadata.__repr__.<locals>.<genexpr>.())join__dict__itemsrw   	__class__rv   rr   r9   r9   r:   __repr__M  s    zMetadata.__repr__)
rv   rw   rx   ry   r;   propertyr|   rh   r1   r   r9   r9   r9   r:   r/      s   @r/   ) ry   r   ImportErrorrE   rl   r   r   osr   typingr   r   r   r   r	   r
   Zmaxminddb.constr   r   r   r   r   Zmaxminddb.decoderr   Zmaxminddb.errorsr   Zmaxminddb.filer   Zmaxminddb.typesr   r   r/   r9   r9   r9   r:   <module>   s"   
  T