factory.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # ============================================================================
  2. # This file is part of Pwman3.
  3. #
  4. # Pwman3 is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License, version 2
  6. # as published by the Free Software Foundation;
  7. #
  8. # Pwman3 is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with Pwman3; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. # ============================================================================
  17. # Copyright (C) 2012-2014 Oz Nahum Tiram <nahumoz@gmail.com>
  18. # ============================================================================
  19. # Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>
  20. # ============================================================================
  21. """
  22. Factory to create Database instances
  23. A Generic interface for all DB engines.
  24. Usage:
  25. import pwman.data.factory as DBFactory
  26. db = DBFactory.create(params)
  27. db.open()
  28. .....
  29. """
  30. import sys
  31. if sys.version_info.major > 2: # pragma: no cover
  32. from urllib.parse import urlparse
  33. else:
  34. from urlparse import urlparse
  35. import os
  36. from pwman.data.database import DatabaseException, __DB_FORMAT__
  37. from pwman.data import drivers
  38. def check_db_version(dburi):
  39. ver = str(__DB_FORMAT__)
  40. dburi = urlparse(dburi)
  41. dbtype = dburi.scheme
  42. filename = os.path.abspath(dburi.path)
  43. if dbtype == "sqlite":
  44. ver = drivers.SQLite.check_db_version(filename)
  45. elif dbtype == "postgresql":
  46. ver = drivers.PostgresqlDatabase.check_db_version(dburi.geturl())
  47. elif dbtype == "mysql":
  48. ver = drivers.MySQLDatabase.check_db_version(dburi)
  49. elif dbtype == "mongodb":
  50. pass
  51. return float(ver.strip("\'"))
  52. def createdb(dburi, version):
  53. dburi = urlparse(dburi)
  54. dbtype = dburi.scheme
  55. filename = dburi.path
  56. if dbtype == "sqlite":
  57. from pwman.data.drivers import sqlite
  58. db = sqlite.SQLite(filename, dbformat=version)
  59. elif dbtype == "postgresql":
  60. try:
  61. from pwman.data.drivers import postgresql
  62. db = postgresql.PostgresqlDatabase(dburi)
  63. except ImportError: # pragma: no cover
  64. raise DatabaseException("python-psycopg2 not installed")
  65. elif dbtype == "mysql": # pragma: no cover
  66. try:
  67. from pwman.data.drivers import mysql
  68. db = mysql.MySQLDatabase(dburi)
  69. except ImportError:
  70. raise DatabaseException("python-mysqldb not installed")
  71. elif dbtype == 'mongodb':
  72. try:
  73. from pwman.data.drivers import mongodb
  74. db = mongodb.MongoDB(dburi)
  75. except ImportError:
  76. raise DatabaseException("pymongo not installed")
  77. else:
  78. raise DatabaseException("Unknown database type specified")
  79. return db