def test_file_name_conditions(self): with tempfile.TemporaryDirectory() as tmp_dir: bar_file = Path(tmp_dir, "foo.bar") bar2_file = Path(tmp_dir, "bar.bar") foo_file = Path(tmp_dir, "bar.foo") foo2_file = Path(tmp_dir, "bar2.foo") open(bar_file, "a").close() open(bar2_file, "a").close() open(foo_file, "a").close() open(foo2_file, "a").close() self.assertTrue(bar_file.exists()) self.assertTrue(bar2_file.exists()) self.assertTrue(foo_file.exists()) self.assertTrue(foo2_file.exists()) gf.remove_matching_files( tmp_dir, exts={".bar"}, filter_func=lambda f: f.startswith("bar.")) self.assertTrue(bar_file.exists()) self.assertFalse(bar2_file.exists()) self.assertTrue(foo_file.exists()) self.assertTrue(foo2_file.exists()) gf.remove_matching_files( tmp_dir, filter_func=lambda f: f.startswith("bar.")) self.assertTrue(bar_file.exists()) self.assertFalse(foo_file.exists()) self.assertTrue(foo2_file.exists())
def test_remove_files(self): with tempfile.TemporaryDirectory() as tmp_dir: bar_file = Path(tmp_dir, "foo.bar") foo_file = Path(tmp_dir, "bar.foo") foo2_file = Path(tmp_dir, "bar.foo2") bar_dir = Path(tmp_dir, "x.bar") open(bar_file, "a").close() open(foo_file, "a").close() open(foo2_file, "a").close() os.makedirs(bar_dir) self.assertTrue(bar_file.exists()) self.assertTrue(foo_file.exists()) self.assertTrue(foo2_file.exists()) self.assertTrue(bar_dir.exists()) gf.remove_matching_files(tmp_dir, exts={".bar"}) self.assertFalse(bar_file.exists()) self.assertTrue(foo_file.exists()) self.assertTrue(foo2_file.exists()) self.assertTrue(bar_dir.exists()) gf.remove_matching_files(tmp_dir, exts={".foo", ".foo2"}) self.assertFalse(foo_file.exists()) self.assertFalse(foo2_file.exists()) self.assertTrue(bar_dir.exists())
def _remove_extra_files(self): gf.remove_matching_files( self.measurement.directory, exts={".measurement", ".profile", ".target"}) gf.remove_matching_files( self.measurement.directory / "Detector", exts={".detector"})
def test_remove_files_with_bad_inputs(self): with tempfile.TemporaryDirectory() as tmp_dir: # Nonexistent directories cause no changes path = Path(tmp_dir, "foo.bar") self.assertFalse(path.exists()) gf.remove_matching_files(path, exts={".bar"}) self.assertFalse(path.exists()) # Neither if the directory is actually a file open(path, "a").close() self.assertTrue(path.is_file()) gf.remove_matching_files(path, exts={".bar"}) self.assertTrue(path.is_file())
def test_remove_files_no_ext(self): with tempfile.TemporaryDirectory() as tmp_dir: no_ext = Path(tmp_dir, "foo") open(no_ext, "a").close() self.assertTrue(no_ext.exists()) self.assertRaises(TypeError, lambda: gf.remove_matching_files(tmp_dir)) gf.remove_matching_files(tmp_dir, exts=set()) self.assertTrue(no_ext.exists()) gf.remove_matching_files(tmp_dir, exts={""}) self.assertFalse(no_ext.exists())
def remove_selected(self): """Remove selected selection. """ reply = QtWidgets.QMessageBox.question( self, "Confirmation", "Deleting this selection will " "delete possible cut and split " "files.\n\n" "Are you sure you want to " "delete selected selection?", QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No | QtWidgets.QMessageBox.Cancel, QtWidgets.QMessageBox.Cancel) if reply == QtWidgets.QMessageBox.No or reply == \ QtWidgets.QMessageBox.Cancel: return # If clicked Yes, then continue normally self.measurement.remove_selected() self.measurement.reset_select( ) # Nothing is now selected, reset colors self.measurement.selector.auto_save() self.measurement.save_cuts() # Update energy spectrum es_widget = self.parent.tab.energy_spectrum_widget if es_widget: delete_es = False for cut in es_widget.use_cuts: if not os.path.exists(cut): delete_es = True # Remove unnecessary tof_list and hist files # TODO check that also no_foil.hist file is removed cut_file_name = Path(cut).stem gf.remove_matching_files( self.measurement.get_energy_spectra_dir(), exts={".hist", ".tof_list"}, filter_func=lambda f: Path(f).stem == cut_file_name) if delete_es: save_file = os.path.join( self.measurement.get_energy_spectra_dir(), es_widget.save_file) if os.path.exists(save_file): os.remove(save_file) self.parent.tab.del_widget(es_widget) # Update depth profile delete_depth = False depth_widget = self.parent.tab.depth_profile_widget if depth_widget: for cut in depth_widget.use_cuts: if not os.path.exists(cut): delete_depth = True # TODO: Delete depth files if delete_depth: gf.remove_files(self.measurement.get_depth_profile_dir() / depth_widget.save_file) self.parent.tab.del_widget(depth_widget) # Update composition changes delete_comp = False comp_widget = self.parent.tab.elemental_losses_widget if comp_widget: for cut in comp_widget.checked_cuts: if not os.path.exists(cut): delete_comp = True if delete_comp: gf.remove_files( self.measurement.get_composition_changes_dir() / comp_widget.save_file) self.parent.tab.del_widget(comp_widget) self.elementSelectDeleteButton.setEnabled(False) self.__on_draw_legend() self.canvas.draw_idle() self.__emit_selections_changed()