Skip to content

Commit 3f8c7ca

Browse files
committed
Adds ability to pass an open file descriptor to the Reader class, instead of a string path to the database.
1 parent 5fbc9a4 commit 3f8c7ca

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

geoip2/database.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import maxminddb
1010
# pylint: disable=unused-import
1111
from maxminddb import (MODE_AUTO, MODE_MMAP, MODE_MMAP_EXT, MODE_FILE,
12-
MODE_MEMORY)
12+
MODE_MEMORY, MODE_FD)
1313

1414
import geoip2
1515
import geoip2.models
@@ -23,9 +23,9 @@ class Reader(object):
2323
IP addresses can be looked up using the ``country`` and ``city`` methods.
2424
2525
The basic API for this class is the same for every database. First, you
26-
create a reader object, specifying a file name. You then call the method
27-
corresponding to the specific database, passing it the IP address you want
28-
to look up.
26+
create a reader object, specifying a file name or file descriptor.
27+
You then call the method corresponding to the specific database, passing
28+
it the IP address you want to look up.
2929
3030
If the request succeeds, the method call will return a model class for the
3131
method you called. This model in turn contains multiple record classes,
@@ -40,10 +40,12 @@ class Reader(object):
4040
4141
"""
4242

43-
def __init__(self, filename, locales=None, mode=MODE_AUTO):
43+
def __init__(self, fileish, locales=None, mode=MODE_AUTO):
4444
"""Create GeoIP2 Reader.
4545
46-
:param filename: The path to the GeoIP2 database.
46+
:param fileish: The string path to the GeoIP2 database, or an existing
47+
file descriptor pointing to the database. Note that this latter
48+
usage is only valid when mode is MODE_FD.
4749
:param locales: This is list of locale codes. This argument will be
4850
passed on to record classes to use when their name properties are
4951
called. The default value is ['en'].
@@ -73,13 +75,15 @@ def __init__(self, filename, locales=None, mode=MODE_AUTO):
7375
* MODE_MMAP - read from memory map. Pure Python.
7476
* MODE_FILE - read database as standard file. Pure Python.
7577
* MODE_MEMORY - load database into memory. Pure Python.
78+
* MODE_FD - the param passed via fileish is a file descriptor, not a
79+
path. This mode implies MODE_MEMORY. Pure Python.
7680
* MODE_AUTO - try MODE_MMAP_EXT, MODE_MMAP, MODE_FILE in that order.
7781
Default.
7882
7983
"""
8084
if locales is None:
8185
locales = ['en']
82-
self._db_reader = maxminddb.open_database(filename, mode)
86+
self._db_reader = maxminddb.open_database(fileish, mode)
8387
self._locales = locales
8488

8589
def __enter__(self):

tests/database_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626

2727
class BaseTestReader(object):
28+
2829
def test_language_list(self):
2930
reader = geoip2.database.Reader(
3031
'tests/data/test-data/GeoIP2-Country-Test.mmdb',
@@ -216,5 +217,9 @@ class TestMemoryReader(BaseTestReader, unittest.TestCase):
216217
mode = geoip2.database.MODE_MEMORY
217218

218219

220+
class TestFDReader(unittest.TestCase):
221+
mode = geoip2.database.MODE_FD
222+
223+
219224
class TestAutoReader(BaseTestReader, unittest.TestCase):
220225
mode = geoip2.database.MODE_AUTO

0 commit comments

Comments
 (0)