__init__.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. """
  2. /***************************************************************************
  3. Name : DB Manager
  4. Description : Database manager plugin for QGIS
  5. Date : May 23, 2011
  6. copyright : (C) 2011 by Giuseppe Sucameli
  7. email : brush.tyler@gmail.com
  8. ***************************************************************************/
  9. /***************************************************************************
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. ***************************************************************************/
  17. """
  18. class NotSupportedDbType(Exception):
  19. def __init__(self, dbtype):
  20. from qgis.PyQt.QtWidgets import QApplication
  21. self.msg = QApplication.translate("DBManagerPlugin", "{0} is not supported yet").format(dbtype)
  22. Exception(self, self.msg)
  23. def __str__(self):
  24. return self.msg.encode('utf-8')
  25. def initDbPluginList():
  26. import os
  27. current_dir = os.path.dirname(__file__)
  28. for name in os.listdir(current_dir):
  29. if name == '__pycache__':
  30. continue
  31. if not os.path.isdir(os.path.join(current_dir, name)):
  32. continue
  33. try:
  34. exec("from .%s import plugin as mod" % name, globals())
  35. except ImportError as e:
  36. DBPLUGIN_ERRORS.append("%s: %s" % (name, str(e)))
  37. continue
  38. pluginclass = mod.classFactory() # NOQA
  39. SUPPORTED_DBTYPES[pluginclass.typeName()] = pluginclass
  40. return len(SUPPORTED_DBTYPES) > 0
  41. def supportedDbTypes():
  42. return sorted(SUPPORTED_DBTYPES.keys())
  43. def getDbPluginErrors():
  44. return DBPLUGIN_ERRORS
  45. def createDbPlugin(dbtype, conn_name=None):
  46. if dbtype not in SUPPORTED_DBTYPES:
  47. raise NotSupportedDbType(dbtype)
  48. dbplugin = SUPPORTED_DBTYPES[dbtype]
  49. return dbplugin if conn_name is None else dbplugin(conn_name)
  50. # initialize the plugin list
  51. SUPPORTED_DBTYPES = {}
  52. DBPLUGIN_ERRORS = []
  53. initDbPluginList()