factory.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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
  37. from pwman.data.drivers import sqlite
  38. def check_db_version(dburi):
  39. dburi = urlparse(dburi)
  40. dbtype = dburi.scheme
  41. filename = os.path.abspath(dburi.path)
  42. if dbtype == "sqlite":
  43. ver = sqlite.SQLite.check_db_version(filename)
  44. try:
  45. return float(ver.strip("\'"))
  46. except ValueError:
  47. return 0.3
  48. # TODO: implement version checks for other supported DBs.
  49. if dbtype == "Postgresql":
  50. ver = sqlite.PostgresqlDatabase.check_db_version(dburi)
  51. def create(dbtype, version=None, filename=None):
  52. """
  53. create(params) -> Database
  54. Create a Database instance.
  55. 'type' can only be 'SQLite' at the moment
  56. """
  57. if dbtype == "sqlite":
  58. from pwman.data.drivers import sqlite
  59. if str(version) == '0.6':
  60. db = sqlite.SQLite(filename)
  61. else:
  62. db = sqlite.SQLite(filename, dbformat=version)
  63. elif dbtype == "postgresql": # pragma: no cover
  64. try:
  65. from pwman.data.drivers import postgresql
  66. db = postgresql.PostgresqlDatabase()
  67. except ImportError:
  68. raise DatabaseException("python-psycopg2 not installed")
  69. elif dbtype == "mysql": # pragma: no cover
  70. try:
  71. from pwman.data.drivers import mysql
  72. db = mysql.MySQLDatabase()
  73. except ImportError:
  74. raise DatabaseException("python-mysqldb not installed")
  75. else:
  76. raise DatabaseException("Unknown database type specified")
  77. return db
  78. def createdb(dburi, version):
  79. dburi = urlparse(dburi)
  80. dbtype = dburi.scheme
  81. filename = dburi.path
  82. if dbtype == "sqlite":
  83. from pwman.data.drivers import sqlite
  84. if str(version) == '0.6':
  85. db = sqlite.SQLite(filename)
  86. else:
  87. db = sqlite.SQLite(filename, dbformat=version)
  88. elif dbtype == "postgresql":
  89. try:
  90. from pwman.data.drivers import postgresql
  91. db = postgresql.PostgresqlDatabase(dburi)
  92. except ImportError: # pragma: no cover
  93. raise DatabaseException("python-psycopg2 not installed")
  94. elif dbtype == "mysql": # pragma: no cover
  95. try:
  96. from pwman.data.drivers import mysql
  97. db = mysql.MySQLDatabase()
  98. except ImportError:
  99. raise DatabaseException("python-mysqldb not installed")
  100. else:
  101. raise DatabaseException("Unknown database type specified")
  102. return db