def export_data(self, data, exporter_id, project, user_public_key=None): """ Export provided data using given exporter :param data: data type to be exported :param exporter_id: identifier of the exporter to be used :param project: project that contains data to be exported :param user_public_key: public key file used for encrypting data before exporting :returns: a tuple with the following elements 1. name of the file to be shown to user 2. full path of the export file (available for download) 3. boolean which specify if file can be deleted after download """ if data is None: raise InvalidExportDataException("Could not export null data. Please select data to be exported") if exporter_id is None: raise ExportException("Please select the exporter to be used for this operation") if exporter_id not in self.all_exporters: raise ExportException("Provided exporter identifier is not a valid one") exporter = self.all_exporters[exporter_id] if user_public_key is not None: public_key_path, encryption_password = self.storage_interface.prepare_encryption(project.name) if isinstance(user_public_key, (FieldStorage, Part)): with open(public_key_path, 'wb') as file_obj: self.storage_interface.copy_file(user_public_key.file, file_obj) else: shutil.copy2(user_public_key, public_key_path) else: public_key_path, encryption_password = None, None if project is None: raise ExportException("Please provide the project where data files are stored") # Now we start the real export if not exporter.accepts(data): raise InvalidExportDataException("Current data can not be exported by specified exporter") # Now compute and create folder where to store exported data # This will imply to generate a folder which is unique for each export export_data = None try: self.logger.debug("Start export of data: %s" % data.type) export_data = exporter.export(data, project, public_key_path, encryption_password) except Exception: pass return export_data
def export_data(self, data, exporter_id, project): """ Export provided data using given exporter :param data: data type to be exported :param exporter_id: identifier of the exporter to be used :param project: project that contains data to be exported :returns: a tuple with the following elements 1. name of the file to be shown to user 2. full path of the export file (available for download) 3. boolean which specify if file can be deleted after download """ if data is None: raise InvalidExportDataException( "Could not export null data. Please select data to be exported" ) if exporter_id is None: raise ExportException( "Please select the exporter to be used for this operation") if exporter_id not in self.all_exporters: raise ExportException( "Provided exporter identifier is not a valid one") exporter = self.all_exporters[exporter_id] if project is None: raise ExportException( "Please provide the project where data files are stored") # Now we start the real export if not exporter.accepts(data): raise InvalidExportDataException( "Current data can not be exported by specified exporter") # Now compute and create folder where to store exported data # This will imply to generate a folder which is unique for each export data_export_folder = None try: data_export_folder = self._build_data_export_folder(data) # Here is the real export LOG.debug("Start export of data: %s" % data.type) export_data = exporter.export(data, data_export_folder, project) finally: # In case export did not generated any file delete folder if data_export_folder is not None and len( os.listdir(data_export_folder)) == 0: os.rmdir(data_export_folder) return export_data
def export_simulator_configuration(self, burst_id): burst = dao.get_burst_by_id(burst_id) if burst is None: raise InvalidExportDataException("Could not find burst with ID " + str(burst_id)) simulator_from_burst = dao.get_generic_entity(SimulatorIndex, burst.id, 'fk_parent_burst')[0] simulator_h5 = h5.path_for_stored_index(simulator_from_burst) simulator_config_folder = os.path.dirname(simulator_h5) now = datetime.now() date_str = now.strftime("%Y-%m-%d_%H-%M") zip_file_name = "%s_%s.%s" % (date_str, str(burst_id), self.ZIP_FILE_EXTENSION) tmp_export_folder = self._build_data_export_folder( simulator_from_burst) result_path = os.path.join(tmp_export_folder, zip_file_name) with TvbZip(result_path, "w") as zip_file: for filename in os.listdir(simulator_config_folder): zip_file.write(os.path.join(simulator_config_folder, filename), filename) return result_path
def export_burst(self, burst_id): """ :param burst_id: ID for existing burst :return: JSON of burst representation. """ burst = dao.get_burst_by_id(burst_id) if burst is None: raise InvalidExportDataException("Could not find burst with ID " + str(burst_id)) burst_info = self._build_burst_export_dict(burst) return json.dumps(burst_info)
def export_simulator_configuration(self, burst_id): burst = dao.get_burst_by_id(burst_id) if burst is None: raise InvalidExportDataException("Could not find burst with ID " + str(burst_id)) op_folder = self.storage_interface.get_project_folder(burst.project.name, str(burst.fk_simulation)) all_view_model_paths, all_datatype_paths = h5.gather_references_of_view_model(burst.simulator_gid, op_folder) burst_path = h5.determine_filepath(burst.gid, op_folder) all_view_model_paths.append(burst_path) zip_filename = ABCExporter.get_export_file_name(burst, self.storage_interface.TVB_ZIP_FILE_EXTENSION) result_path = self.storage_interface.export_simulator_configuration(burst, all_view_model_paths, all_datatype_paths, zip_filename) return result_path
def export_simulator_configuration(self, burst_id): burst = dao.get_burst_by_id(burst_id) if burst is None: raise InvalidExportDataException("Could not find burst with ID " + str(burst_id)) op_folder = self.files_helper.get_project_folder( burst.project, str(burst.fk_simulation)) tmp_export_folder = self._build_data_export_folder(burst) tmp_sim_folder = os.path.join(tmp_export_folder, self.EXPORTED_SIMULATION_NAME) if not os.path.exists(tmp_sim_folder): os.makedirs(tmp_sim_folder) all_view_model_paths, all_datatype_paths = h5.gather_references_of_view_model( burst.simulator_gid, op_folder) burst_path = h5.determine_filepath(burst.gid, op_folder) all_view_model_paths.append(burst_path) for vm_path in all_view_model_paths: dest = os.path.join(tmp_sim_folder, os.path.basename(vm_path)) self.files_helper.copy_file(vm_path, dest) for dt_path in all_datatype_paths: dest = os.path.join(tmp_sim_folder, self.EXPORTED_SIMULATION_DTS_DIR, os.path.basename(dt_path)) self.files_helper.copy_file(dt_path, dest) main_vm_path = h5.determine_filepath(burst.simulator_gid, tmp_sim_folder) H5File.remove_metadata_param(main_vm_path, 'history_gid') now = datetime.now() date_str = now.strftime("%Y-%m-%d_%H-%M") zip_file_name = "%s_%s.%s" % (date_str, str(burst_id), self.ZIP_FILE_EXTENSION) result_path = os.path.join(tmp_export_folder, zip_file_name) with TvbZip(result_path, "w") as zip_file: zip_file.write_folder(tmp_sim_folder) self.files_helper.remove_folder(tmp_sim_folder) return result_path
def get_exporters_for_data(self, data): """ Get available exporters for current data type. :returns: a dictionary with the {exporter_id : label} """ if data is None: raise InvalidExportDataException("Could not detect exporters for null data") self.logger.debug("Trying to determine exporters valid for %s" % data.type) results = {} # No exporter for None data if data is None: return results for exporterId in self.all_exporters.keys(): exporter = self.all_exporters[exporterId] if exporter.accepts(data): results[exporterId] = exporter.get_label() return results
def export_simulator_configuration(self, burst_id): burst = dao.get_burst_by_id(burst_id) if burst is None: raise InvalidExportDataException("Could not find burst with ID " + str(burst_id)) op_folder = FilesHelper().get_project_folder(burst.project, str(burst.fk_simulation)) now = datetime.now() date_str = now.strftime("%Y-%m-%d_%H-%M") zip_file_name = "%s_%s.%s" % (date_str, str(burst_id), self.ZIP_FILE_EXTENSION) tmp_export_folder = self._build_data_export_folder(burst) result_path = os.path.join(tmp_export_folder, zip_file_name) with TvbZip(result_path, "w") as zip_file: for filename in os.listdir(op_folder): zip_file.write(os.path.join(op_folder, filename), filename) return result_path