factory.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. from urllib.parse import urlparse
  21. import os
  22. from pwman.data.database import DatabaseException
  23. from pwman.data import drivers
  24. def parse_sqlite_uri(dburi):
  25. """return dburi.netloc if on windows, because this was someone break"""
  26. if not dburi.path:
  27. return dburi.netloc
  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. if not dbtype:
  52. print("Your URI seems incorrect ...")
  53. sys.exit(0)
  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. dbtype = dburi.scheme
  64. try:
  65. cls = getattr(drivers, create_db_map[dbtype][0])
  66. return cls(create_db_map[dbtype][1](dburi))
  67. except AttributeError:
  68. raise DatabaseException(
  69. '%s not installed? ' % class_db_map[dbtype][-1])
  70. except KeyError:
  71. raise DatabaseException('Unknown database [%s] given ...' % (dbtype))