16
16
import struct
17
17
from ipaddress import IPv4Address , IPv6Address
18
18
from 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
20
20
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
22
22
from maxminddb .decoder import Decoder
23
23
from maxminddb .errors import InvalidDatabaseError
24
24
from maxminddb .file import FileBuffer
@@ -44,7 +44,9 @@ class Reader:
44
44
_ipv4_start : int
45
45
46
46
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 ,
48
50
) -> None :
49
51
"""Reader for the MaxMind DB file format
50
52
@@ -58,6 +60,7 @@ def __init__(
58
60
* MODE_AUTO - tries MODE_MMAP and then MODE_FILE. Default.
59
61
* MODE_FD - the param passed via database is a file descriptor, not
60
62
a path. This mode implies MODE_MEMORY.
63
+
61
64
"""
62
65
filename : Any
63
66
if (mode == MODE_AUTO and mmap ) or mode == MODE_MMAP :
@@ -83,18 +86,19 @@ def __init__(
83
86
raise ValueError (
84
87
f"Unsupported open mode ({ mode } ). Only MODE_AUTO, MODE_FILE, "
85
88
"MODE_MEMORY and MODE_FD are supported by the pure Python "
86
- "Reader"
89
+ "Reader" ,
87
90
)
88
91
89
92
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 ),
91
95
)
92
96
93
97
if metadata_start == - 1 :
94
98
self .close ()
95
99
raise InvalidDatabaseError (
96
100
f"Error opening database file ({ filename } ). "
97
- "Is this a valid MaxMind DB file?"
101
+ "Is this a valid MaxMind DB file?" ,
98
102
)
99
103
100
104
metadata_start += len (self ._METADATA_START_MARKER )
@@ -103,7 +107,7 @@ def __init__(
103
107
104
108
if not isinstance (metadata , dict ):
105
109
raise InvalidDatabaseError (
106
- f"Error reading metadata in database file ({ filename } )."
110
+ f"Error reading metadata in database file ({ filename } )." ,
107
111
)
108
112
109
113
self ._metadata = Metadata (** metadata ) # pylint: disable=bad-option-value
@@ -134,21 +138,22 @@ def metadata(self) -> "Metadata":
134
138
def get (self , ip_address : Union [str , IPv6Address , IPv4Address ]) -> Optional [Record ]:
135
139
"""Return the record for the ip_address in the MaxMind DB
136
140
137
-
138
141
Arguments:
139
142
ip_address -- an IP address in the standard string notation
143
+
140
144
"""
141
145
(record , _ ) = self .get_with_prefix_len (ip_address )
142
146
return record
143
147
144
148
def get_with_prefix_len (
145
- self , ip_address : Union [str , IPv6Address , IPv4Address ]
149
+ self ,
150
+ ip_address : Union [str , IPv6Address , IPv4Address ],
146
151
) -> Tuple [Optional [Record ], int ]:
147
152
"""Return a tuple with the record and the associated prefix length
148
153
149
-
150
154
Arguments:
151
155
ip_address -- an IP address in the standard string notation
156
+
152
157
"""
153
158
if isinstance (ip_address , str ):
154
159
address = ipaddress .ip_address (ip_address )
@@ -163,7 +168,7 @@ def get_with_prefix_len(
163
168
if address .version == 6 and self ._metadata .ip_version == 4 :
164
169
raise ValueError (
165
170
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." ,
167
172
)
168
173
169
174
(pointer , prefix_len ) = self ._find_address_in_tree (packed_address )
@@ -187,7 +192,7 @@ def _generate_children(self, node, depth, ip_acc):
187
192
if ip_acc <= _IPV4_MAX_NUM and bits == 128 :
188
193
depth -= 96
189
194
yield ipaddress .ip_network ((ip_acc , depth )), self ._resolve_data_pointer (
190
- node
195
+ node ,
191
196
)
192
197
elif node < node_count :
193
198
left = self ._read_node (node , 0 )
0 commit comments