• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

maxmind / MaxMind-DB-Reader-python / 422

pending completion
422

Pull #50

travis-ci

web-flow
Use duck typing and make error messages consistent

Also, add a missing return when getting packed attribute fails in
extension.
Pull Request #50: Add support for ipaddress objects in "get" method

10 of 10 new or added lines in 1 file covered. (100.0%)

562 of 632 relevant lines covered (88.92%)

2.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.5
/maxminddb/__init__.py
1
# pylint:disable=C0111
2
import os
3✔
3

4
import maxminddb.reader
3✔
5

6
try:
3✔
7
    import maxminddb.extension
3✔
8
except ImportError:
×
9
    maxminddb.extension = None
×
10

11
from maxminddb.const import (MODE_AUTO, MODE_MMAP, MODE_MMAP_EXT, MODE_FILE,
3✔
12
                             MODE_MEMORY, MODE_FD)
13
from maxminddb.decoder import InvalidDatabaseError
3✔
14

15

16
def open_database(database, mode=MODE_AUTO):
3✔
17
    """Open a Maxmind DB database
18

19
    Arguments:
20
        database -- A path to a valid MaxMind DB file such as a GeoIP2 database
21
                    file, or a file descriptor in the case of MODE_FD.
22
        mode -- mode to open the database with. Valid mode are:
23
            * MODE_MMAP_EXT - use the C extension with memory map.
24
            * MODE_MMAP - read from memory map. Pure Python.
25
            * MODE_FILE - read database as standard file. Pure Python.
26
            * MODE_MEMORY - load database into memory. Pure Python.
27
            * MODE_FD - the param passed via database is a file descriptor, not
28
                        a path. This mode implies MODE_MEMORY.
29
            * MODE_AUTO - tries MODE_MMAP_EXT, MODE_MMAP, MODE_FILE in that
30
                          order. Default mode.
31
    """
32
    has_extension = maxminddb.extension and hasattr(maxminddb.extension,
3✔
33
                                                    'Reader')
34
    if (mode == MODE_AUTO and has_extension) or mode == MODE_MMAP_EXT:
3✔
35
        if not has_extension:
3✔
36
            raise ValueError(
3✔
37
                "MODE_MMAP_EXT requires the maxminddb.extension module to be available"
38
            )
39
        return maxminddb.extension.Reader(database)
3✔
40
    if mode in (MODE_AUTO, MODE_MMAP, MODE_FILE, MODE_MEMORY, MODE_FD):
3✔
41
        return maxminddb.reader.Reader(database, mode)
3✔
42
    raise ValueError('Unsupported open mode: {0}'.format(mode))
×
43

44

45
def Reader(database):  # pylint: disable=invalid-name
3✔
46
    """This exists for backwards compatibility. Use open_database instead"""
47
    return open_database(database)
3✔
48

49

50
__title__ = 'maxminddb'
3✔
51
__version__ = '1.4.1'
3✔
52
__author__ = 'Gregory Oschwald'
3✔
53
__license__ = 'Apache License, Version 2.0'
3✔
54
__copyright__ = 'Copyright 2013-2018 Maxmind, Inc.'
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc