def export_file(filename, table_name, file_type=None): """ Export a table to a csv file. """ if not file_type: # Get file's extension name. file_type = os.path.splitext(filename)[1].lower() if len(file_type) > 0: file_type = file_type[1:] writer_class = writers.get_writer(file_type) if not writer_class: raise (MudderyError(ERR.export_data_error, "Unsupport file type %s" % file_type)) writer = writer_class(filename) if not writer: raise (MudderyError(ERR.export_data_error, "Can not export table %s" % table_name)) fields = general_query_mapper.get_all_fields(table_name) header = [field.name for field in fields] writer.writeln(header) records = general_query_mapper.get_all_records(table_name) for record in records: line = [ str(record.serializable_value(field.get_attname())) for field in fields ] writer.writeln(line) writer.save()
def func(self, args, request): if ('table' not in args): raise MudderyError(ERR.missing_args, 'Missing the table name.') table_name = args['table'] file_type = args.get("type", "csv") writer_class = writers.get_writer(file_type) if not writer_class: raise MudderyError(ERR.download_error, "Unknown file type: %s" % file_type) # Get tempfile's name. temp_name = tempfile.mktemp() exporter.export_file(temp_name, table_name, file_type) fp = open(temp_name, "rb") try: filename = table_name + "." + writer_class.file_ext return file_response(fp, filename) except Exception as e: if fp: fp.close() logger.log_tracemsg("Download error: %s" % e) raise MudderyError(ERR.download_error, "Download file error: %s" % e)
def export_zip_all(file_obj, file_type=None): """ Export all tables to a zip file which contains a group of csv files. """ if not file_type: # Set default file type. file_type = "csv" writer_class = writers.get_writer(file_type) if writer_class: # Get tempfile's name. temp = tempfile.mktemp() file_ext = writer_class.file_ext try: archive = zipfile.ZipFile(file_obj, 'w', zipfile.ZIP_DEFLATED) # get model names app_config = apps.get_app_config(settings.WORLD_DATA_APP) for model in app_config.get_models(): model_name = model._meta.object_name export_file(temp, model_name, file_type) filename = model_name + "." + file_ext archive.write(temp, filename) # add version file version_file = os.path.join(GAME_DIR, configs.CONFIG_FILE) archive.write(version_file, configs.CONFIG_FILE) finally: os.remove(temp)
def export_path_all(path, file_type=None): """ Export all tables to a path which contains a group of csv files. """ if not file_type: # Set default file type. file_type = "csv" writer_class = writers.get_writer(file_type) if writer_class: # Get tempfile's name. file_ext = writer_class.file_ext # get model names app_config = apps.get_app_config(settings.WORLD_DATA_APP) for model in app_config.get_models(): model_name = model._meta.object_name filename = os.path.join(path, model_name + "." + file_ext) export_file(filename, model_name, file_type)
def export_zip_all(file_obj, file_type=None): """ Export all tables to a zip file which contains a group of csv files. """ if not file_type: # Set default file type. file_type = "csv" writer_class = writers.get_writer(file_type) if not writer_class: raise (MudderyError(ERR.export_data_error, "Unsupport file type %s" % file_type)) # Get tempfile's name. temp_filename = tempfile.mktemp() file_ext = writer_class.file_ext try: archive = zipfile.ZipFile(file_obj, 'w', zipfile.ZIP_DEFLATED) # get model names models = model_mapper.get_all_models() for model in models: model_name = model._meta.object_name export_file(temp_filename, model_name, file_type) filename = model_name + "." + file_ext archive.write(temp_filename, filename) # add version file version_file = os.path.join(GAME_DIR, configs.CONFIG_FILE) archive.write(version_file, configs.CONFIG_FILE) finally: try: os.remove(temp_filename) except PermissionError: pass
def export_file(filename, model_name, file_type=None): """ Export a table to a csv file. """ if not file_type: # Get file's extension name. file_type = os.path.splitext(filename)[1].lower() if len(file_type) > 0: file_type = file_type[1:] writer_class = writers.get_writer(file_type) if not writer_class: print("Can not export file %s" % filename) return writer = writer_class(filename) if not writer: print("Can not export file %s" % filename) return for line in get_lines(model_name): writer.writeln(line) writer.save()