9
9
import maxminddb
10
10
# pylint: disable=unused-import
11
11
from maxminddb import (MODE_AUTO , MODE_MMAP , MODE_MMAP_EXT , MODE_FILE ,
12
- MODE_MEMORY )
12
+ MODE_MEMORY , MODE_FD )
13
13
14
14
import geoip2
15
15
import geoip2 .models
@@ -23,9 +23,9 @@ class Reader(object):
23
23
IP addresses can be looked up using the ``country`` and ``city`` methods.
24
24
25
25
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.
29
29
30
30
If the request succeeds, the method call will return a model class for the
31
31
method you called. This model in turn contains multiple record classes,
@@ -40,10 +40,12 @@ class Reader(object):
40
40
41
41
"""
42
42
43
- def __init__ (self , filename , locales = None , mode = MODE_AUTO ):
43
+ def __init__ (self , fileish , locales = None , mode = MODE_AUTO ):
44
44
"""Create GeoIP2 Reader.
45
45
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.
47
49
:param locales: This is list of locale codes. This argument will be
48
50
passed on to record classes to use when their name properties are
49
51
called. The default value is ['en'].
@@ -73,13 +75,15 @@ def __init__(self, filename, locales=None, mode=MODE_AUTO):
73
75
* MODE_MMAP - read from memory map. Pure Python.
74
76
* MODE_FILE - read database as standard file. Pure Python.
75
77
* 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.
76
80
* MODE_AUTO - try MODE_MMAP_EXT, MODE_MMAP, MODE_FILE in that order.
77
81
Default.
78
82
79
83
"""
80
84
if locales is None :
81
85
locales = ['en' ]
82
- self ._db_reader = maxminddb .open_database (filename , mode )
86
+ self ._db_reader = maxminddb .open_database (fileish , mode )
83
87
self ._locales = locales
84
88
85
89
def __enter__ (self ):
0 commit comments