factory.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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-2016 Oz Nahum Tiram <oz.tiram@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. """return dburi.netloc if on windows, because this was someone break"""
  29. if not dburi.path:
  30. return dburi.netloc
  31. filename = os.path.abspath(dburi.path)
  32. return filename
  33. def parse_postgres_uri(dburi):
  34. return dburi.geturl()
  35. def no_parse_uri(dburi):
  36. return dburi
  37. class_db_map = {'sqlite':
  38. ['SQLite', parse_sqlite_uri],
  39. 'postgresql': ['PostgresqlDatabase', parse_postgres_uri,
  40. 'python-psycopg2'],
  41. 'mysql': ['MySQLDatabase', no_parse_uri, 'pymysql'],
  42. 'mongodb': ['MongoDB', no_parse_uri, 'pymongo']
  43. }
  44. create_db_map = {'sqlite':
  45. ['SQLite', parse_sqlite_uri],
  46. 'postgresql': ['PostgresqlDatabase', no_parse_uri,
  47. 'python-psycopg2'],
  48. 'mysql': ['MySQLDatabase', no_parse_uri, 'pymysql'],
  49. 'mongodb': ['MongoDB', no_parse_uri, 'pymongo']
  50. }
  51. def check_db_version(dburi):
  52. dburi = urlparse(dburi)
  53. dbtype = dburi.scheme
  54. try:
  55. cls = getattr(drivers, class_db_map[dbtype][0])
  56. ver = cls.check_db_version(class_db_map[dbtype][1](dburi))
  57. return ver
  58. except AttributeError:
  59. raise DatabaseException(
  60. '%s not installed? ' % class_db_map[dbtype][-1])
  61. def createdb(dburi, version):
  62. dburi = urlparse(dburi)
  63. print("dburi : {}".format(dburi))
  64. dbtype = dburi.scheme
  65. try:
  66. cls = getattr(drivers, create_db_map[dbtype][0])
  67. return cls(create_db_map[dbtype][1](dburi))
  68. except AttributeError:
  69. raise DatabaseException(
  70. '%s not installed? ' % class_db_map[dbtype][-1])
  71. except KeyError:
  72. raise DatabaseException('Unknown database [%s] given ...' % (dbtype))