1
0

__init__.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # pylint:disable=C0111
  2. import os
  3. import maxminddb.reader
  4. try:
  5. import maxminddb.extension
  6. except ImportError:
  7. maxminddb.extension = None
  8. from maxminddb.const import (MODE_AUTO, MODE_MMAP, MODE_MMAP_EXT, MODE_FILE,
  9. MODE_MEMORY)
  10. from maxminddb.decoder import InvalidDatabaseError
  11. def open_database(database, mode=MODE_AUTO):
  12. """Open a Maxmind DB database
  13. Arguments:
  14. database -- A path to a valid MaxMind DB file such as a GeoIP2
  15. database file.
  16. mode -- mode to open the database with. Valid mode are:
  17. * MODE_MMAP_EXT - use the C extension with memory map.
  18. * MODE_MMAP - read from memory map. Pure Python.
  19. * MODE_FILE - read database as standard file. Pure Python.
  20. * MODE_MEMORY - load database into memory. Pure Python.
  21. * MODE_AUTO - tries MODE_MMAP_EXT, MODE_MMAP, MODE_FILE in that
  22. order. Default mode.
  23. """
  24. if (mode == MODE_AUTO and maxminddb.extension and
  25. hasattr(maxminddb.extension, 'Reader')) or mode == MODE_MMAP_EXT:
  26. return maxminddb.extension.Reader(database)
  27. elif mode in (MODE_AUTO, MODE_MMAP, MODE_FILE, MODE_MEMORY):
  28. return maxminddb.reader.Reader(database, mode)
  29. raise ValueError('Unsupported open mode: {0}'.format(mode))
  30. def Reader(database): # pylint: disable=invalid-name
  31. """This exists for backwards compatibility. Use open_database instead"""
  32. return open_database(database)
  33. __title__ = 'maxminddb'
  34. __version__ = '1.2.0'
  35. __author__ = 'Gregory Oschwald'
  36. __license__ = 'Apache License, Version 2.0'
  37. __copyright__ = 'Copyright 2014 Maxmind, Inc.'