factory.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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-2015 Oz Nahum Tiram <nahumoz@gmail.com>
  18. # ============================================================================
  19. import sys
  20. if sys.version_info.major > 2: # pragma: no cover
  21. from urllib.parse import urlparse
  22. else:
  23. from urlparse import urlparse
  24. import os
  25. from pwman.data.database import DatabaseException
  26. from pwman.data import drivers
  27. def parse_sqlite_uri(dburi):
  28. filename = os.path.abspath(dburi.path)
  29. return filename
  30. def parse_postgres_uri(dburi):
  31. return dburi.geturl()
  32. def no_parse_uri(dburi):
  33. return dburi
  34. class_db_map = {'sqlite':
  35. ['SQLite', parse_sqlite_uri],
  36. 'postgresql': ['PostgresqlDatabase', parse_postgres_uri,
  37. 'python-psycopg2'],
  38. 'mysql': ['MySQLDatabase', no_parse_uri, 'pymysql'],
  39. 'mongodb': ['MongoDB', no_parse_uri, 'pymongo']
  40. }
  41. create_db_map = {'sqlite':
  42. ['SQLite', parse_sqlite_uri],
  43. 'postgresql': ['PostgresqlDatabase', no_parse_uri,
  44. 'python-psycopg2'],
  45. 'mysql': ['MySQLDatabase', no_parse_uri, 'pymysql'],
  46. 'mongodb': ['MongoDB', no_parse_uri, 'pymongo']
  47. }
  48. def check_db_version(dburi):
  49. dburi = urlparse(dburi)
  50. dbtype = dburi.scheme
  51. try:
  52. cls = getattr(drivers, class_db_map[dbtype][0])
  53. ver = cls.check_db_version(class_db_map[dbtype][1](dburi))
  54. return ver
  55. except AttributeError:
  56. raise DatabaseException(
  57. '%s not installed? ' % class_db_map[dbtype][-1])
  58. def createdb(dburi, version):
  59. dburi = urlparse(dburi)
  60. dbtype = dburi.scheme
  61. try:
  62. cls = getattr(drivers, create_db_map[dbtype][0])
  63. return cls(create_db_map[dbtype][1](dburi))
  64. except AttributeError:
  65. raise DatabaseException(
  66. '%s not installed? ' % class_db_map[dbtype][-1])
  67. except KeyError:
  68. raise DatabaseException('Unknown database [%s] given ...' % (dbtype))