1616import struct
1717from ipaddress import IPv4Address , IPv6Address
1818from os import PathLike
19- from typing import Any , AnyStr , Dict , IO , List , Optional , Tuple , Union
19+ from typing import IO , Any , AnyStr , Dict , List , Optional , Tuple , Union
2020
21- from maxminddb .const import MODE_AUTO , MODE_MMAP , MODE_FILE , MODE_MEMORY , MODE_FD
21+ from maxminddb .const import MODE_AUTO , MODE_FD , MODE_FILE , MODE_MEMORY , MODE_MMAP
2222from maxminddb .decoder import Decoder
2323from maxminddb .errors import InvalidDatabaseError
2424from maxminddb .file import FileBuffer
@@ -44,7 +44,9 @@ class Reader:
4444 _ipv4_start : int
4545
4646 def __init__ (
47- self , database : Union [AnyStr , int , PathLike , IO ], mode : int = MODE_AUTO
47+ self ,
48+ database : Union [AnyStr , int , PathLike , IO ],
49+ mode : int = MODE_AUTO ,
4850 ) -> None :
4951 """Reader for the MaxMind DB file format
5052
@@ -58,6 +60,7 @@ def __init__(
5860 * MODE_AUTO - tries MODE_MMAP and then MODE_FILE. Default.
5961 * MODE_FD - the param passed via database is a file descriptor, not
6062 a path. This mode implies MODE_MEMORY.
63+
6164 """
6265 filename : Any
6366 if (mode == MODE_AUTO and mmap ) or mode == MODE_MMAP :
@@ -83,18 +86,19 @@ def __init__(
8386 raise ValueError (
8487 f"Unsupported open mode ({ mode } ). Only MODE_AUTO, MODE_FILE, "
8588 "MODE_MEMORY and MODE_FD are supported by the pure Python "
86- "Reader"
89+ "Reader" ,
8790 )
8891
8992 metadata_start = self ._buffer .rfind (
90- self ._METADATA_START_MARKER , max (0 , self ._buffer_size - 128 * 1024 )
93+ self ._METADATA_START_MARKER ,
94+ max (0 , self ._buffer_size - 128 * 1024 ),
9195 )
9296
9397 if metadata_start == - 1 :
9498 self .close ()
9599 raise InvalidDatabaseError (
96100 f"Error opening database file ({ filename } ). "
97- "Is this a valid MaxMind DB file?"
101+ "Is this a valid MaxMind DB file?" ,
98102 )
99103
100104 metadata_start += len (self ._METADATA_START_MARKER )
@@ -103,7 +107,7 @@ def __init__(
103107
104108 if not isinstance (metadata , dict ):
105109 raise InvalidDatabaseError (
106- f"Error reading metadata in database file ({ filename } )."
110+ f"Error reading metadata in database file ({ filename } )." ,
107111 )
108112
109113 self ._metadata = Metadata (** metadata ) # pylint: disable=bad-option-value
@@ -134,21 +138,22 @@ def metadata(self) -> "Metadata":
134138 def get (self , ip_address : Union [str , IPv6Address , IPv4Address ]) -> Optional [Record ]:
135139 """Return the record for the ip_address in the MaxMind DB
136140
137-
138141 Arguments:
139142 ip_address -- an IP address in the standard string notation
143+
140144 """
141145 (record , _ ) = self .get_with_prefix_len (ip_address )
142146 return record
143147
144148 def get_with_prefix_len (
145- self , ip_address : Union [str , IPv6Address , IPv4Address ]
149+ self ,
150+ ip_address : Union [str , IPv6Address , IPv4Address ],
146151 ) -> Tuple [Optional [Record ], int ]:
147152 """Return a tuple with the record and the associated prefix length
148153
149-
150154 Arguments:
151155 ip_address -- an IP address in the standard string notation
156+
152157 """
153158 if isinstance (ip_address , str ):
154159 address = ipaddress .ip_address (ip_address )
@@ -163,7 +168,7 @@ def get_with_prefix_len(
163168 if address .version == 6 and self ._metadata .ip_version == 4 :
164169 raise ValueError (
165170 f"Error looking up { ip_address } . You attempted to look up "
166- "an IPv6 address in an IPv4-only database."
171+ "an IPv6 address in an IPv4-only database." ,
167172 )
168173
169174 (pointer , prefix_len ) = self ._find_address_in_tree (packed_address )
@@ -187,7 +192,7 @@ def _generate_children(self, node, depth, ip_acc):
187192 if ip_acc <= _IPV4_MAX_NUM and bits == 128 :
188193 depth -= 96
189194 yield ipaddress .ip_network ((ip_acc , depth )), self ._resolve_data_pointer (
190- node
195+ node ,
191196 )
192197 elif node < node_count :
193198 left = self ._read_node (node , 0 )
0 commit comments