__init__.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from ._base import IncorrectDatabaseSetup
  16. from .postgres import PostgresEngine
  17. from .sqlite3 import Sqlite3Engine
  18. import importlib
  19. import platform
  20. SUPPORTED_MODULE = {
  21. "sqlite3": Sqlite3Engine,
  22. "psycopg2": PostgresEngine,
  23. }
  24. def create_engine(database_config):
  25. name = database_config["name"]
  26. engine_class = SUPPORTED_MODULE.get(name, None)
  27. if engine_class:
  28. # pypy requires psycopg2cffi rather than psycopg2
  29. if (name == "psycopg2" and
  30. platform.python_implementation() == "PyPy"):
  31. name = "psycopg2cffi"
  32. module = importlib.import_module(name)
  33. return engine_class(module, database_config)
  34. raise RuntimeError(
  35. "Unsupported database engine '%s'" % (name,)
  36. )
  37. __all__ = ["create_engine", "IncorrectDatabaseSetup"]