Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions geoip2/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import maxminddb
# pylint: disable=unused-import
from maxminddb import (MODE_AUTO, MODE_MMAP, MODE_MMAP_EXT, MODE_FILE,
MODE_MEMORY)
MODE_MEMORY, MODE_FD)

import geoip2
import geoip2.models
Expand All @@ -23,9 +23,9 @@ class Reader(object):
IP addresses can be looked up using the ``country`` and ``city`` methods.

The basic API for this class is the same for every database. First, you
create a reader object, specifying a file name. You then call the method
corresponding to the specific database, passing it the IP address you want
to look up.
create a reader object, specifying a file name or file descriptor.
You then call the method corresponding to the specific database, passing
it the IP address you want to look up.

If the request succeeds, the method call will return a model class for the
method you called. This model in turn contains multiple record classes,
Expand All @@ -40,10 +40,12 @@ class Reader(object):

"""

def __init__(self, filename, locales=None, mode=MODE_AUTO):
def __init__(self, fileish, locales=None, mode=MODE_AUTO):
"""Create GeoIP2 Reader.

:param filename: The path to the GeoIP2 database.
:param fileish: The string path to the GeoIP2 database, or an existing
file descriptor pointing to the database. Note that this latter
usage is only valid when mode is MODE_FD.
:param locales: This is list of locale codes. This argument will be
passed on to record classes to use when their name properties are
called. The default value is ['en'].
Expand Down Expand Up @@ -73,13 +75,15 @@ def __init__(self, filename, locales=None, mode=MODE_AUTO):
* MODE_MMAP - read from memory map. Pure Python.
* MODE_FILE - read database as standard file. Pure Python.
* MODE_MEMORY - load database into memory. Pure Python.
* MODE_FD - the param passed via fileish is a file descriptor, not a
path. This mode implies MODE_MEMORY. Pure Python.
* MODE_AUTO - try MODE_MMAP_EXT, MODE_MMAP, MODE_FILE in that order.
Default.

"""
if locales is None:
locales = ['en']
self._db_reader = maxminddb.open_database(filename, mode)
self._db_reader = maxminddb.open_database(fileish, mode)
self._locales = locales

def __enter__(self):
Expand Down
4 changes: 4 additions & 0 deletions tests/database_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,9 @@ class TestMemoryReader(BaseTestReader, unittest.TestCase):
mode = geoip2.database.MODE_MEMORY


class TestFDReader(unittest.TestCase):
mode = geoip2.database.MODE_FD


class TestAutoReader(BaseTestReader, unittest.TestCase):
mode = geoip2.database.MODE_AUTO