factory.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 Oz Nahum <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. from pwman.data.database import Database, DatabaseException
  31. import pwman.util.config as config
  32. def create(type):
  33. """
  34. create(params) -> Database
  35. Create a Database instance.
  36. 'type' can only be 'SQLite' at the moment
  37. """
  38. if type == "BerkeleyDB":
  39. pass
  40. # db = BerkeleyDatabase.BerkeleyDatabase(params)
  41. elif (type == "SQLite"):
  42. try:
  43. from pwman.data.drivers import sqlite
  44. db = sqlite.SQLiteDatabase()
  45. except ImportError, e:
  46. raise DatabaseException("python-sqlite not installed")
  47. elif (type == "Postgresql"):
  48. try:
  49. from pwman.data.drivers import postgresql
  50. db = postgresql.PostgresqlDatabase()
  51. except ImportError, e:
  52. raise DatabaseException("python-pygresql not installed")
  53. elif (type == "MySQL"):
  54. try:
  55. from pwman.data.drivers import mysql
  56. db = mysql.MySQLDatabase()
  57. except ImportError, e:
  58. raise DatabaseException("python-mysqldb not installed")
  59. else:
  60. raise DatabaseException("Unknown database type specified")
  61. return db