def delete_mapsets_interactively(guiparent, mapsets): """Delete multiple mapsets with user interaction. Parameter *mapsets* is a list of tuples (database, location, mapset). If PERMANENT or current mapset found, delete operation is not performed. Exceptions during deletation are handled in this function. Retuns True if there was a change, i.e., all mapsets were successfuly deleted or at least one mapset was deleted. Returns False if one or more mapsets cannot be deleted (see above the possible reasons) or if an error was encountered when deleting the first mapset in the list. """ genv = gisenv() issues = [] deletes = [] # Check selected mapsets and remember issue. # Each error is reported only once (using elif). for grassdb, location, mapset in mapsets: mapset_path = os.path.join(grassdb, location, mapset) # Check for permanent mapsets if mapset == "PERMANENT": issue = _("<{}> is required for a valid location.").format( mapset_path) issues.append(issue) # Check for current mapset elif (grassdb == genv['GISDBASE'] and location == genv['LOCATION_NAME'] and mapset == genv['MAPSET']): issue = _("<{}> is the current mapset.").format(mapset_path) issues.append(issue) # No issue detected else: deletes.append(mapset_path) modified = False # True after first successful delete # If any issues, display the warning message and do not delete anything if issues: issues = "\n".join(issues) dlg = wx.MessageDialog( parent=guiparent, message=_( "Cannot delete one or more mapsets for the following reasons:\n\n" "{}\n\n" "No mapsets will be deleted.").format(issues), caption=_("Unable to delete selected mapsets"), style=wx.OK | wx.ICON_WARNING) dlg.ShowModal() else: deletes = "\n".join(deletes) dlg = wx.MessageDialog( parent=guiparent, message=_( "Do you want to continue with deleting" " one or more of the following mapsets?\n\n" "{}\n\n" "All maps included in these mapsets will be permanently deleted!" ).format(deletes), caption=_("Delete selected mapsets"), style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION, ) if dlg.ShowModal() == wx.ID_YES: try: for grassdb, location, mapset in mapsets: delete_mapset(grassdb, location, mapset) modified = True dlg.Destroy() return modified except OSError as error: wx.MessageBox( parent=guiparent, caption=_("Error when deleting mapsets"), message=_( "The following error occured when deleting mapset <{path}>:" "\n\n{error}\n\n" "Deleting of mapsets was interrupted.").format( path=os.path.join(grassdb, location, mapset), error=error, ), style=wx.OK | wx.ICON_ERROR | wx.CENTRE, ) dlg.Destroy() return modified
def delete_mapsets_interactively(guiparent, mapsets): """Delete multiple mapsets with user interaction. Parameter *mapsets* is a list of tuples (database, location, mapset). Exceptions during deletation are handled in get_reasons_mapsets_not_removable function. Returns True if there was a change, i.e., all mapsets were successfully deleted or at least one mapset was deleted. Returns False if one or more mapsets cannot be deleted (see reasons given by get_reasons_mapsets_not_removable function) or if an error was encountered when deleting the first mapset in the list. """ deletes = [] modified = False # Check selected mapsets messages = get_reasons_mapsets_not_removable(mapsets, check_permanent=True) if messages: dlg = wx.MessageDialog( parent=guiparent, message=_( "Cannot delete one or more mapsets for the following reasons:\n\n" "{reasons}\n\n" "No mapsets will be deleted.").format( reasons="\n".join(messages)), caption=_("Unable to delete selected mapsets"), style=wx.OK | wx.ICON_WARNING) dlg.ShowModal() dlg.Destroy() return modified # No error occurs, create list of mapsets for deleting for grassdb, location, mapset in mapsets: mapset_path = os.path.join(grassdb, location, mapset) deletes.append(mapset_path) # Display question dialog dlg = wx.MessageDialog( parent=guiparent, message=_( "Do you want to continue with deleting" " one or more of the following mapsets?\n\n" "{deletes}\n\n" "All maps included in these mapsets will be permanently deleted!"). format(deletes="\n".join(deletes)), caption=_("Delete selected mapsets"), style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION, ) if dlg.ShowModal() == wx.ID_YES: try: for grassdb, location, mapset in mapsets: delete_mapset(grassdb, location, mapset) modified = True dlg.Destroy() return modified except OSError as error: wx.MessageBox( parent=guiparent, caption=_("Error when deleting mapsets"), message=_( "The following error occured when deleting mapset <{path}>:" "\n\n{error}\n\n" "Deleting of mapsets was interrupted.").format( path=os.path.join(grassdb, location, mapset), error=error, ), style=wx.OK | wx.ICON_ERROR | wx.CENTRE, ) dlg.Destroy() return modified