@@ -64,39 +64,7 @@ def __init__(
64
64
a path. This mode implies MODE_MEMORY.
65
65
66
66
"""
67
- filename : Any
68
- if (mode == MODE_AUTO and mmap ) or mode == MODE_MMAP :
69
- with open (database , "rb" ) as db_file : # type: ignore[arg-type]
70
- self ._buffer = mmap .mmap (db_file .fileno (), 0 , access = mmap .ACCESS_READ )
71
- self ._buffer_size = self ._buffer .size ()
72
- filename = database
73
- elif mode in (MODE_AUTO , MODE_FILE ):
74
- self ._buffer = FileBuffer (database ) # type: ignore[arg-type]
75
- self ._buffer_size = self ._buffer .size ()
76
- filename = database
77
- elif mode == MODE_MEMORY :
78
- with open (database , "rb" ) as db_file : # type: ignore[arg-type]
79
- buf = db_file .read ()
80
- self ._buffer = buf
81
- self ._buffer_size = len (buf )
82
- filename = database
83
- elif mode == MODE_FD :
84
- self ._buffer = database .read () # type: ignore[union-attr]
85
- self ._buffer_size = len (self ._buffer ) # type: ignore[arg-type]
86
- # io buffers are not guaranteed to have a name attribute
87
- if hasattr (database , "name" ):
88
- filename = database .name # type: ignore[union-attr]
89
- else :
90
- filename = f"<{ type (database )} >"
91
- else :
92
- msg = (
93
- f"Unsupported open mode ({ mode } ). Only MODE_AUTO, MODE_FILE, "
94
- "MODE_MEMORY and MODE_FD are supported by the pure Python "
95
- "Reader"
96
- )
97
- raise ValueError (
98
- msg ,
99
- )
67
+ self ._load_buffer (database , mode )
100
68
101
69
metadata_start = self ._buffer .rfind (
102
70
self ._METADATA_START_MARKER ,
@@ -106,7 +74,7 @@ def __init__(
106
74
if metadata_start == - 1 :
107
75
self .close ()
108
76
msg = (
109
- f"Error opening database file ({ filename } ). "
77
+ f"Error opening database file ({ self . filename } ). "
110
78
"Is this a valid MaxMind DB file?"
111
79
)
112
80
raise InvalidDatabaseError (
@@ -118,7 +86,7 @@ def __init__(
118
86
(metadata , _ ) = metadata_decoder .decode (metadata_start )
119
87
120
88
if not isinstance (metadata , dict ):
121
- msg = f"Error reading metadata in database file ({ filename } )."
89
+ msg = f"Error reading metadata in database file ({ self . filename } )."
122
90
raise InvalidDatabaseError (
123
91
msg ,
124
92
)
@@ -280,6 +248,43 @@ def _resolve_data_pointer(self, pointer: int) -> Record:
280
248
(data , _ ) = self ._decoder .decode (resolved )
281
249
return data
282
250
251
+ def _load_buffer (
252
+ self , database : AnyStr | int | PathLike | IO , mode : int = MODE_AUTO
253
+ ) -> None :
254
+ self .filename : Any
255
+ if (mode == MODE_AUTO and mmap ) or mode == MODE_MMAP :
256
+ with open (database , "rb" ) as db_file : # type: ignore[arg-type]
257
+ self ._buffer = mmap .mmap (db_file .fileno (), 0 , access = mmap .ACCESS_READ )
258
+ self ._buffer_size = self ._buffer .size ()
259
+ self .filename = database
260
+ elif mode in (MODE_AUTO , MODE_FILE ):
261
+ self ._buffer = FileBuffer (database ) # type: ignore[arg-type]
262
+ self ._buffer_size = self ._buffer .size ()
263
+ self .filename = database
264
+ elif mode == MODE_MEMORY :
265
+ with open (database , "rb" ) as db_file : # type: ignore[arg-type]
266
+ buf = db_file .read ()
267
+ self ._buffer = buf
268
+ self ._buffer_size = len (buf )
269
+ self .filename = database
270
+ elif mode == MODE_FD :
271
+ self ._buffer = database .read () # type: ignore[union-attr]
272
+ self ._buffer_size = len (self ._buffer ) # type: ignore[arg-type]
273
+ # io buffers are not guaranteed to have a name attribute
274
+ if hasattr (database , "name" ):
275
+ self .filename = database .name # type: ignore[union-attr]
276
+ else :
277
+ self .filename = f"<{ type (database )} >"
278
+ else :
279
+ msg = (
280
+ f"Unsupported open mode ({ mode } ). Only MODE_AUTO, MODE_FILE, "
281
+ "MODE_MEMORY and MODE_FD are supported by the pure Python "
282
+ "Reader"
283
+ )
284
+ raise ValueError (
285
+ msg ,
286
+ )
287
+
283
288
def close (self ) -> None :
284
289
"""Close the MaxMind DB file and returns the resources to the system."""
285
290
with contextlib .suppress (AttributeError ):
0 commit comments