factory.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 DatabaseException
  31. from pwman.data.drivers import sqlite
  32. #from pwman.data.drivers import osqlite
  33. class FactoryException(Exception):
  34. def __init__(self, message):
  35. self.message
  36. def __str__(self):
  37. return self.message
  38. def check_db_version(type):
  39. if type == "SQLite":
  40. ver = sqlite.check_db_version()
  41. try:
  42. return float(ver.strip("\'"))
  43. except ValueError:
  44. return 0.3
  45. # TODO: implement version checks for other supported DBs.
  46. def create(dbtype, version=None, filename=None):
  47. """
  48. create(params) -> Database
  49. Create a Database instance.
  50. 'type' can only be 'SQLite' at the moment
  51. """
  52. if dbtype == "SQLite":
  53. from pwman.data.drivers import sqlite
  54. if filename:
  55. db = sqlite.SQLiteDatabaseNewForm(filename)
  56. elif version >= 0.4:
  57. db = sqlite.SQLiteDatabaseNewForm()
  58. elif dbtype == "Postgresql": # pragma: no cover
  59. try:
  60. from pwman.data.drivers import postgresql
  61. db = postgresql.PostgresqlDatabase()
  62. except ImportError:
  63. raise DatabaseException("python-pygresql not installed")
  64. elif dbtype == "MySQL": # pragma: no cover
  65. try:
  66. from pwman.data.drivers import mysql
  67. db = mysql.MySQLDatabase()
  68. except ImportError:
  69. raise DatabaseException("python-mysqldb not installed")
  70. else:
  71. raise DatabaseException("Unknown database type specified")
  72. return db