dict_import_export_util.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # pylint: disable=C,R,W
  2. import logging
  3. from superset.connectors.druid.models import DruidCluster
  4. from superset.models.core import Database
  5. DATABASES_KEY = 'databases'
  6. DRUID_CLUSTERS_KEY = 'druid_clusters'
  7. def export_schema_to_dict(back_references):
  8. """Exports the supported import/export schema to a dictionary"""
  9. databases = [Database.export_schema(recursive=True,
  10. include_parent_ref=back_references)]
  11. clusters = [DruidCluster.export_schema(recursive=True,
  12. include_parent_ref=back_references)]
  13. data = dict()
  14. if databases:
  15. data[DATABASES_KEY] = databases
  16. if clusters:
  17. data[DRUID_CLUSTERS_KEY] = clusters
  18. return data
  19. def export_to_dict(session,
  20. recursive,
  21. back_references,
  22. include_defaults):
  23. """Exports databases and druid clusters to a dictionary"""
  24. logging.info('Starting export')
  25. dbs = session.query(Database)
  26. databases = [database.export_to_dict(recursive=recursive,
  27. include_parent_ref=back_references,
  28. include_defaults=include_defaults) for database in dbs]
  29. logging.info('Exported %d %s', len(databases), DATABASES_KEY)
  30. cls = session.query(DruidCluster)
  31. clusters = [cluster.export_to_dict(recursive=recursive,
  32. include_parent_ref=back_references,
  33. include_defaults=include_defaults) for cluster in cls]
  34. logging.info('Exported %d %s', len(clusters), DRUID_CLUSTERS_KEY)
  35. data = dict()
  36. if databases:
  37. data[DATABASES_KEY] = databases
  38. if clusters:
  39. data[DRUID_CLUSTERS_KEY] = clusters
  40. return data
  41. def import_from_dict(session, data, sync=[]):
  42. """Imports databases and druid clusters from dictionary"""
  43. if isinstance(data, dict):
  44. logging.info('Importing %d %s',
  45. len(data.get(DATABASES_KEY, [])),
  46. DATABASES_KEY)
  47. for database in data.get(DATABASES_KEY, []):
  48. Database.import_from_dict(session, database, sync=sync)
  49. logging.info('Importing %d %s',
  50. len(data.get(DRUID_CLUSTERS_KEY, [])),
  51. DRUID_CLUSTERS_KEY)
  52. for datasource in data.get(DRUID_CLUSTERS_KEY, []):
  53. DruidCluster.import_from_dict(session, datasource, sync=sync)
  54. session.commit()
  55. else:
  56. logging.info('Supplied object is not a dictionary.')