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