Skip to content

Commit ddc1f48

Browse files
committed
Merge pull request maxmind#19 from maxmind/greg/db-modes
Add support for DB open modes
2 parents ebdd56c + 1d43599 commit ddc1f48

File tree

4 files changed

+82
-5
lines changed

4 files changed

+82
-5
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ install:
2727

2828
script:
2929
- coverage run --source=geoip2 setup.py test
30-
- if [[ $TRAVIS_PYTHON_VERSION != '3.3' ]]; then pylint geoip2; fi
30+
- if [[ $TRAVIS_PYTHON_VERSION == '2.7' ]]; then pylint geoip2; fi
3131

3232
after_success:
3333
- coveralls

HISTORY.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
History
44
-------
55

6+
2.1.0 (2014-12-XX)
7+
++++++++++++++++++
8+
9+
* The reader now supports pure Python file and memory modes. If you are not
10+
using the C extension and your Python does not provide the ``mmap`` module,
11+
the file mode will be used by default. You can explicitly set the mode using
12+
the ``mode`` keyword argument with the ``MODE_AUTO``, ``MODE_MMAP``,
13+
``MODE_MMAP_EXT``, ``MODE_FILE``, and ``MODE_MEMORY`` constants exported by
14+
``geoip2.database``.
15+
616
2.0.2 (2014-10-28)
717
++++++++++++++++++
818

geoip2/database.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111
import geoip2.errors
1212
import maxminddb
1313

14+
# pylint: disable=unused-import
15+
from maxminddb import (MODE_AUTO, MODE_MMAP, MODE_MMAP_EXT, MODE_FILE,
16+
MODE_MEMORY)
17+
1418

1519
class Reader(object):
1620

17-
"""Creates a new GeoIP2 database Reader object.
21+
"""GeoIP2 database Reader object.
1822
1923
Instances of this class provide a reader for the GeoIP2 database format.
2024
IP addresses can be looked up using the ``country`` and ``city`` methods.
@@ -40,10 +44,46 @@ class Reader(object):
4044
4145
"""
4246

43-
def __init__(self, filename, locales=None):
47+
def __init__(self, filename, locales=None, mode=MODE_AUTO):
48+
"""Create GeoIP2 Reader
49+
50+
:param filename: The path to the GeoIP2 database.
51+
:param locales: This is list of locale codes. This argument will be
52+
passed on to record classes to use when their name properties are
53+
called. The default value is ['en'].
54+
55+
The order of the locales is significant. When a record class has
56+
multiple names (country, city, etc.), its name property will return
57+
the name in the first locale that has one.
58+
59+
Note that the only locale which is always present in the GeoIP2
60+
data is "en". If you do not include this locale, the name property
61+
may end up returning None even when the record has an English name.
62+
63+
Currently, the valid locale codes are:
64+
65+
* de -- German
66+
* en -- English names may still include accented characters if that
67+
is the accepted spelling in English. In other words, English does
68+
not mean ASCII.
69+
* es -- Spanish
70+
* fr -- French
71+
* ja -- Japanese
72+
* pt-BR -- Brazilian Portuguese
73+
* ru -- Russian
74+
* zh-CN -- Simplified Chinese.
75+
:param mode: The mode to open the database with. Valid mode are:
76+
* MODE_MMAP_EXT - use the C extension with memory map.
77+
* MODE_MMAP - read from memory map. Pure Python.
78+
* MODE_FILE - read database as standard file. Pure Python.
79+
* MODE_MEMORY - load database into memory. Pure Python.
80+
* MODE_AUTO - try MODE_MMAP_EXT, MODE_MMAP, MODE_FILE in that order.
81+
Default.
82+
83+
"""
4484
if locales is None:
4585
locales = ['en']
46-
self._db_reader = maxminddb.Reader(filename)
86+
self._db_reader = maxminddb.open_database(filename, mode)
4787
self._locales = locales
4888

4989
def country(self, ip_address):

tests/database_test.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
sys.path.append('..')
88

99
import geoip2.database
10+
import maxminddb
11+
12+
try:
13+
import maxminddb.extension
14+
except ImportError:
15+
maxminddb.extension = None
1016

1117
if sys.version_info[:2] == (2, 6):
1218
import unittest2 as unittest
@@ -18,7 +24,7 @@
1824
unittest.TestCase.assertRegex = unittest.TestCase.assertRegexpMatches
1925

2026

21-
class TestReader(unittest.TestCase):
27+
class BaseTestReader(object):
2228

2329
def test_default_locale(self):
2430
reader = geoip2.database.Reader(
@@ -142,3 +148,24 @@ def test_isp(self):
142148
"ISP repr can be eval'd")
143149

144150
reader.close()
151+
152+
153+
@unittest.skipUnless(maxminddb.extension, 'No C extension module found. Skipping tests')
154+
class TestExtensionReader(BaseTestReader, unittest.TestCase):
155+
mode = geoip2.database.MODE_MMAP_EXT
156+
157+
158+
class TestMMAPReader(BaseTestReader, unittest.TestCase):
159+
mode = geoip2.database.MODE_MMAP
160+
161+
162+
class TestFileReader(BaseTestReader, unittest.TestCase):
163+
mode = geoip2.database.MODE_FILE
164+
165+
166+
class TestMemoryReader(BaseTestReader, unittest.TestCase):
167+
mode = geoip2.database.MODE_MEMORY
168+
169+
170+
class TestMemoryReader(BaseTestReader, unittest.TestCase):
171+
mode = geoip2.database.MODE_AUTO

0 commit comments

Comments
 (0)