def get_gifty_file_name(project_id, desired_name): """ Compute non-existent file name, in the TEMP folder of the given project. Try desired_name, and if already exists, try adding a number. """ if project_id: project = dao.get_project_by_id(project_id) file_helper = FilesHelper() temp_path = file_helper.get_project_folder(project, FilesHelper.TEMP_FOLDER) return get_unique_file_name(temp_path, desired_name)[0] return get_unique_file_name(cfg.TVB_STORAGE, desired_name)[0]
def test_get_unique_file_name(self): """ Test that we get unique file names no matter if we pass the same folder as input. """ file_names = [] nr_of_files = 100 for _ in range(nr_of_files): file_name, _ = get_unique_file_name("", "file_name") fp = open(file_name, 'w') fp.write('test') fp.close() file_names.append(file_name) self.assertEqual(len(file_names), len(set(file_names)), 'No duplicate files should be generated.') for file_n in file_names: os.remove(file_n)
def test_get_unique_file_name(self): """ Test that we get unique file names no matter if we pass the same folder as input. """ file_names = [] nr_of_files = 100 for _ in range(nr_of_files): file_name, _ = get_unique_file_name("", "file_name") fp = open(file_name, 'w') fp.write('test') fp.close() file_names.append(file_name) assert len(file_names) == len(set(file_names)), 'No duplicate files should be generated.' for file_n in file_names: os.remove(file_n)
def _create_c_data(file_prefix, src, metadata_dict, fileformat='Other'): """ Creates a CData object from the given parameters """ if fileformat == 'NumPy': if src: numpy_data = numpy.loadtxt(src) if not os.path.exists(NUMPY_TEMP_FOLDER): os.makedirs(NUMPY_TEMP_FOLDER) temp_file, uq_name = get_unique_file_name(NUMPY_TEMP_FOLDER, file_prefix + ".npy") numpy.save(temp_file, numpy_data) data = CData(uq_name, temp_file, fileformat='Other') data.update_metadata(metadata_dict) else: data = None else: if src: data = CData(file_prefix, src, fileformat=fileformat) data.update_metadata(metadata_dict) else: data = None return data
def store_result_figure(self, project, user, img_type, operation_id, export_data): """ Store into a file, Result Image and reference in DB. """ session_name = "Default" file_name = "snapshot." + img_type # Generate path where to store image store_path = self.file_helper.get_images_folder(project.name, operation_id) store_path = utils.get_unique_file_name(store_path, file_name)[0] file_name = os.path.split(store_path)[1] if img_type == "jpg": # JPG file from canvas # Store image img_data = base64.b64decode(export_data) dest = open(store_path, 'wb') dest.write(img_data) dest.close() elif img_type == 'svg': # SVG file from svg viewer # Generate path where to store image dest = open(store_path, 'w') dest.write(export_data) dest.close() # Store entity into DB entity = model.ResultFigure(operation_id, user.id, project.id, session_name, "Snapshot-" + operation_id, file_name) entity = dao.store_entity(entity) # Load instance from DB to have lazy fields loaded figure = dao.load_figure(entity.id) # Write image meta data to disk self.file_helper.write_image_metadata(figure) # Force writing operation meta data on disk. # This is important later for operation import operation = dao.get_operation_by_id(operation_id) self.file_helper.write_operation_metadata(operation)
def _image_path(self, project_name, img_type): "Generate path where to store image" images_folder = self.file_helper.get_images_folder(project_name) file_name = FigureService._DEFAULT_IMAGE_FILE_NAME + img_type return utils.get_unique_file_name(images_folder, file_name)
def store_result_figure(self, project, user, img_type, operation_id, export_data): """ Store into a file, Result Image and reference in DB. """ # Generate path where to store image store_path = self.file_helper.get_images_folder(project.name, operation_id) store_path = utils.get_unique_file_name(store_path, FigureService._DEFAULT_IMAGE_FILE_NAME + img_type)[0] file_path = os.path.split(store_path)[1] if img_type == FigureService._TYPE_PNG: # PNG file from canvas imgData = base64.b64decode(export_data) # decode the image fakeImgFile = StringIO(imgData) # PIL.Image only opens from file, so fake one origImg = Image.open(fakeImgFile) brandingBar = Image.open(FigureService._BRANDING_BAR_PNG) finalSize = (origImg.size[0], # original width origImg.size[1] + brandingBar.size[1]) # original height + brandingBar height finalImg = Image.new("RGBA", finalSize) finalImg.paste(origImg, (0, 0)) # add the original image finalImg.paste(brandingBar, (0, origImg.size[1])) # add the branding bar, below the original # the extra width will be discarded finalImg.save(store_path) # store to disk elif img_type == FigureService._TYPE_SVG: # SVG file from svg viewer dom = xml.dom.minidom.parseString(export_data) figureSvg = dom.getElementsByTagName('svg')[0] # get the original image dom = xml.dom.minidom.parse(FigureService._BRANDING_BAR_SVG) brandingSvg = dom.getElementsByTagName('svg')[0] # get the branding bar brandingSvg.setAttribute("y", figureSvg.getAttribute("height")) # position it below the figure finalSvg = dom.createElement('svg') # prepare the final svg width = figureSvg.getAttribute('width').replace('px', '') # same width as original figure finalSvg.setAttribute("width", width) height = float(figureSvg.getAttribute('height').replace('px', '')) # increase original height with height += float(brandingSvg.getAttribute('height').replace('px', '')) # branding bar's height finalSvg.setAttribute("height", str(height)) finalSvg.appendChild(figureSvg) # add the image finalSvg.appendChild(brandingSvg) # and the branding bar # Generate path where to store image dest = open(store_path, 'w') finalSvg.writexml(dest) # store to disk dest.close() operation = dao.get_operation_by_id(operation_id) file_name = 'TVB-%s-%s' % (operation.algorithm.name.replace(' ', '-'), operation_id) # e.g. TVB-Algo-Name-352 # Store entity into DB entity = model.ResultFigure(operation_id, user.id, project.id, FigureService._DEFAULT_SESSION_NAME, file_name, file_path, img_type) entity = dao.store_entity(entity) # Load instance from DB to have lazy fields loaded figure = dao.load_figure(entity.id) # Write image meta data to disk self.file_helper.write_image_metadata(figure) # Force writing operation meta data on disk. # This is important later for operation import self.file_helper.write_operation_metadata(operation)