diff --git a/plotid/example.py b/plotid/example.py index 7a8beb2534c332191d1a9f23385076815b63a88d..f137a5a3541c3b0d137017196ca7eb6844154bb1 100644 --- a/plotid/example.py +++ b/plotid/example.py @@ -44,8 +44,8 @@ FIGS_AS_LIST = [FIG1, FIG2] IMGS_AS_LIST = [IMG1, IMG2] # Example for how to use tagplot with matplotlib figures -# FIGS_AND_IDS = tagplot(FIGS_AS_LIST, 'matplotlib', prefix=PROJECT_ID, -# id_method='time', location='west') +# FIGS_AND_IDS = tagplot(FIGS_AS_LIST, 'matplotlib', location='west', +# id_method='random', prefix=PROJECT_ID) # Example for how to use tagplot with image files FIGS_AND_IDS = tagplot(IMGS_AS_LIST, 'image', prefix=PROJECT_ID, @@ -57,4 +57,4 @@ FIGS_AND_IDS = tagplot(IMGS_AS_LIST, 'image', prefix=PROJECT_ID, # plots or images. publish(FIGS_AND_IDS, ['../README.md', '../docs', '../LICENSE'], - '/home/chief/Dokumente/fst/plotid_python/data/', 'Bild') + '/home/chief/Dokumente/fst/plotid_python/data/', 'image') diff --git a/plotid/publish.py b/plotid/publish.py index de95c9c9d6993d90bbc664d829e9051ed77042fb..8d55b26a2ba838a6fab46d19bb08025277119203 100644 --- a/plotid/publish.py +++ b/plotid/publish.py @@ -146,7 +146,7 @@ class PublishOptions: """ # Export plot figure to picture. plot_paths = save_plot(self.figure, self.plot_names) - + print(plot_paths) match self.data_storage: case 'centralized': self.centralized_data_storage() @@ -194,9 +194,9 @@ class PublishOptions: raise ValueError(f'The data storage method {self.data_storage}' ' is not available.') - print(f'Publish was successful.\nYour plot(s) {plot_paths},\nyour' - f' data {self.src_datapaths}\nand your script {sys.argv[0]}\n' - f'were copied to {self.dst_path}\nin {self.data_storage} mode.') + print(f'Publish was successful.\nYour plot(s), your' + f' data and your\nscript {sys.argv[0]}' + f'\nwere copied to {self.dst_path}.') def centralized_data_storage(self): """ @@ -240,10 +240,18 @@ class PublishOptions: # Copy script that calls this function to folder shutil.copy2(sys.argv[0], destination) - # Copy plot files to folder + if os.path.isfile(pic_path): + # Copy plot file to folder shutil.copy2(pic_path, destination) + # Remove by plotID exported .tmp plot os.remove(pic_path) + # Remove .tmp. from file name in destinaion + name_tmp, orig_ext = os.path.splitext(pic_path) + orig_name, _ = os.path.splitext(name_tmp) + final_file_path = orig_name + orig_ext + os.rename(os.path.join(destination, pic_path), + os.path.join(destination, final_file_path)) def publish(figs_and_ids, src_datapath, dst_path, plot_name, **kwargs): diff --git a/plotid/save_plot.py b/plotid/save_plot.py index 2c045854c15fc8b65ac7bc01ec3640fadc933ed7..5cf6f3b57a44f39e139d10af27d7bdcc4d18ae02 100644 --- a/plotid/save_plot.py +++ b/plotid/save_plot.py @@ -57,10 +57,10 @@ def save_plot(figures, plot_names, extension='png'): for i, fig in enumerate(figures): if isinstance(fig, matplotlib.figure.Figure): plt.figure(fig) - plot_path.append(plot_names[i] + '.' + extension) + plot_path.append(plot_names[i] + '.tmp.' + extension) plt.savefig(plot_path[i]) elif all(x in str(type(fig)) for x in ['PIL', 'ImageFile']): - plot_path.append(plot_names[i] + '.' + extension) + plot_path.append(plot_names[i] + '.tmp.' + extension) fig.save(plot_path[i]) else: raise TypeError(f'Figure number {i} is not a valid figure object.') diff --git a/tests/test_publish.py b/tests/test_publish.py index 58611fb421e6c69ee5efd6fe2093e1c78d3036f7..7a9c5d87f0971d22ea6f83fea010368f068e09eb 100644 --- a/tests/test_publish.py +++ b/tests/test_publish.py @@ -219,8 +219,8 @@ class TestPublish(unittest.TestCase): with self.assertRaises(RuntimeError): publish(FIGS_AND_IDS, SRC_DIR, DST_PATH, PIC_NAME) assert not os.path.isdir(invisible_path1) - os.remove('test_picture1.png') - os.remove('test_picture2.png') + os.remove('test_picture1.tmp.png') + os.remove('test_picture2.tmp.png') def test_plot_names(self): """ Test if Error is raised if plot_name is not a string. """ diff --git a/tests/test_save_plot.py b/tests/test_save_plot.py index a46f154666f541a1ec95b17a7fe849c14c422d84..a9f9f059c549f781480a2d51d87d523e68099a32 100644 --- a/tests/test_save_plot.py +++ b/tests/test_save_plot.py @@ -32,7 +32,7 @@ class TestSavePlot(unittest.TestCase): """ plot_paths = save_plot(FIGURE, [PLOT_NAME], extension='jpg') self.assertIsInstance(plot_paths, list) - os.remove(PLOT_NAME + '.jpg') + os.remove(PLOT_NAME + '.tmp.jpg') def test_save_plot_image_png(self): """ @@ -42,7 +42,7 @@ class TestSavePlot(unittest.TestCase): img1 = Image.open(IMG1) plot_paths = save_plot(img1, [PLOT_NAME]) self.assertIsInstance(plot_paths, list) - os.remove(PLOT_NAME + '.png') + os.remove(PLOT_NAME + '.tmp.png') def test_save_plot_image_jpg(self): """ @@ -53,8 +53,8 @@ class TestSavePlot(unittest.TestCase): imgs_as_list = [img2, img2] plot_paths = save_plot(imgs_as_list, [PLOT_NAME], extension='jpg') self.assertIsInstance(plot_paths, list) - os.remove(PLOT_NAME + '1.jpg') - os.remove(PLOT_NAME + '2.jpg') + os.remove(PLOT_NAME + '1.tmp.jpg') + os.remove(PLOT_NAME + '2.tmp.jpg') def test_more_figs_than_names(self): """ @@ -64,8 +64,8 @@ class TestSavePlot(unittest.TestCase): with self.assertWarns(Warning): save_plot([FIGURE, FIGURE, FIGURE], [PLOT_NAME]) for i in (1, 2, 3): - assert os.path.isfile(PLOT_NAME + f'{i}.png') - os.remove(PLOT_NAME + f'{i}.png') + assert os.path.isfile(PLOT_NAME + f'{i}.tmp.png') + os.remove(PLOT_NAME + f'{i}.tmp.png') def test_more_names_than_figs(self): """ Test if Error is raised if more names than figures are given. """ @@ -84,7 +84,7 @@ class TestSavePlot(unittest.TestCase): """ with self.assertRaises(TypeError): save_plot([FIGURE, 'figure', FIGURE], 'PLOT_NAME', extension='jpg') - os.remove(PLOT_NAME + '1.jpg') + os.remove('PLOT_NAME1.tmp.jpg') def tearDown(self): os.remove(IMG1)