1111import geoip2 .errors
1212import 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
1519class 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 ):
0 commit comments