factory.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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) 2006 Ivan Kelly <ivan@ivankelly.net>
  18. #============================================================================
  19. """Factory to create Database instances
  20. Usage:
  21. import pwlib.db.DatabaseFactory as DBFactory
  22. db = DBFactory.create(params)
  23. db.open()
  24. .....
  25. """
  26. from pwman.data.database import Database, DatabaseException
  27. import pwman.util.config as config
  28. def create(type):
  29. """
  30. create(params) -> Database
  31. Create a Database instance.
  32. 'type' can only be 'SQLite' at the moment
  33. """
  34. if type == "BerkeleyDB":
  35. pass
  36. # db = BerkeleyDatabase.BerkeleyDatabase(params)
  37. elif (type == "SQLite"):
  38. try:
  39. from pwman.data.drivers import sqlite
  40. db = sqlite.SQLiteDatabase()
  41. except ImportError, e:
  42. raise DatabaseException("python-sqlite not installed")
  43. elif (type == "Postgresql"):
  44. try:
  45. from pwman.data.drivers import postgresql
  46. db = postgresql.PostgresqlDatabase()
  47. except ImportError, e:
  48. raise DatabaseException("python-pygresql not installed")
  49. elif (type == "MySQL"):
  50. try:
  51. from pwman.data.drivers import mysql
  52. db = mysql.MySQLDatabase()
  53. except ImportError, e:
  54. raise DatabaseException("python-mysqldb not installed")
  55. else:
  56. raise DatabaseException("Unknown database type specified")
  57. return db