Skip to content

Commit 5ce419e

Browse files
committed
refactor init logic to reduce complexity
1 parent 0fc074b commit 5ce419e

File tree

1 file changed

+40
-35
lines changed

1 file changed

+40
-35
lines changed

maxminddb/reader.py

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -64,39 +64,7 @@ def __init__(
6464
a path. This mode implies MODE_MEMORY.
6565
6666
"""
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)
10068

10169
metadata_start = self._buffer.rfind(
10270
self._METADATA_START_MARKER,
@@ -106,7 +74,7 @@ def __init__(
10674
if metadata_start == -1:
10775
self.close()
10876
msg = (
109-
f"Error opening database file ({filename}). "
77+
f"Error opening database file ({self.filename}). "
11078
"Is this a valid MaxMind DB file?"
11179
)
11280
raise InvalidDatabaseError(
@@ -118,7 +86,7 @@ def __init__(
11886
(metadata, _) = metadata_decoder.decode(metadata_start)
11987

12088
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})."
12290
raise InvalidDatabaseError(
12391
msg,
12492
)
@@ -280,6 +248,43 @@ def _resolve_data_pointer(self, pointer: int) -> Record:
280248
(data, _) = self._decoder.decode(resolved)
281249
return data
282250

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+
283288
def close(self) -> None:
284289
"""Close the MaxMind DB file and returns the resources to the system."""
285290
with contextlib.suppress(AttributeError):

0 commit comments

Comments
 (0)