Ejemplo n.º 1
0
def delete_location_interactively(guiparent, grassdb, location):
    """
    Delete selected location
    """
    dlg = wx.MessageDialog(
        parent=guiparent,
        message=_("Do you want to continue with deleting "
                  "location {}?\n\n"
                  "ALL MAPS included in this location will be "
                  "PERMANENTLY DELETED!").format(location),
        caption=_("Delete selected location"),
        style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION,
    )

    if dlg.ShowModal() == wx.ID_YES:
        try:
            delete_location(grassdb, location)
            dlg.Destroy()
            return True
        except OSError as err:
            wx.MessageBox(
                parent=guiparent,
                caption=_("Error"),
                message=_("Unable to delete location.\n\n{}").format(err),
                style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
            )
    dlg.Destroy()
    return False
Ejemplo n.º 2
0
def switch_mapset_interactively(
    guiparent, giface, dbase, location, mapset, show_confirmation=False
):
    """Switch current mapset. Emits giface.currentMapsetChanged signal."""
    # Decide if a user is in a fallback session
    fallback_session = is_fallback_session()

    if dbase:
        if (
            RunCommand(
                "g.mapset",
                parent=guiparent,
                location=location,
                mapset=mapset,
                dbase=dbase,
            )
            == 0
        ):
            if show_confirmation:
                GMessage(
                    parent=guiparent,
                    message=_(
                        "Current GRASS database is <%(dbase)s>.\n"
                        "Current location is <%(loc)s>.\n"
                        "Current mapset is <%(mapset)s>."
                    )
                    % {"dbase": dbase, "loc": location, "mapset": mapset},
                )
            giface.currentMapsetChanged.emit(
                dbase=dbase, location=location, mapset=mapset
            )
    elif location:
        if (
            RunCommand("g.mapset", parent=guiparent, location=location, mapset=mapset)
            == 0
        ):
            if show_confirmation:
                GMessage(
                    parent=guiparent,
                    message=_(
                        "Current location is <%(loc)s>.\n"
                        "Current mapset is <%(mapset)s>."
                    )
                    % {"loc": location, "mapset": mapset},
                )
            giface.currentMapsetChanged.emit(
                dbase=None, location=location, mapset=mapset
            )
    else:
        if RunCommand("g.mapset", parent=guiparent, mapset=mapset) == 0:
            if show_confirmation:
                GMessage(
                    parent=guiparent, message=_("Current mapset is <%s>.") % mapset
                )
            giface.currentMapsetChanged.emit(dbase=None, location=None, mapset=mapset)

    if fallback_session:
        tmp_dbase = os.environ["TMPDIR"]
        tmp_loc = cfg.temporary_location
        if tmp_dbase != gisenv()["GISDBASE"]:
            # Delete temporary location
            delete_location(tmp_dbase, tmp_loc)
            # Remove useless temporary grassdb node
            giface.grassdbChanged.emit(
                location=location, grassdb=tmp_dbase, action="delete", element="grassdb"
            )
Ejemplo n.º 3
0
def delete_locations_interactively(guiparent, locations):
    """Delete multiple locations with user interaction.

    Parameter *locations* is a list of tuples (database, location).

    Exceptions during deletation are handled in get_reasons_locations_not_removable
    function.

    Returns True if there was a change, i.e., all locations were successfully
    deleted or at least one location was deleted.
    Returns False if one or more locations cannot be deleted (see reasons given
    by get_reasons_locations_not_removable function) or if an error was
    encountered when deleting the first location in the list.
    """
    deletes = []
    modified = False

    # Check selected locations
    messages = get_reasons_locations_not_removable(locations)
    if messages:
        dlg = wx.MessageDialog(
            parent=guiparent,
            message=_(
                "Cannot delete one or more locations for the following reasons:\n\n"
                "{reasons}\n\n"
                "No locations will be deleted.").format(
                    reasons="\n".join(messages)),
            caption=_("Unable to delete selected locations"),
            style=wx.OK | wx.ICON_WARNING)
        dlg.ShowModal()
        dlg.Destroy()
        return modified

    # No error occurs, create list of locations for deleting
    for grassdb, location in locations:
        location_path = os.path.join(grassdb, location)
        deletes.append(location_path)

    # Display question dialog
    dlg = wx.MessageDialog(
        parent=guiparent,
        message=_(
            "Do you want to continue with deleting"
            " one or more of the following locations?\n\n"
            "{deletes}\n\n"
            "All mapsets included in these locations will be permanently deleted!"
        ).format(deletes="\n".join(deletes)),
        caption=_("Delete selected locations"),
        style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION,
    )
    if dlg.ShowModal() == wx.ID_YES:
        try:
            for grassdb, location in locations:
                delete_location(grassdb, location)
                modified = True
            dlg.Destroy()
            return modified
        except OSError as error:
            wx.MessageBox(
                parent=guiparent,
                caption=_("Error when deleting locations"),
                message=_(
                    "The following error occured when deleting location <{path}>:"
                    "\n\n{error}\n\n"
                    "Deleting of locations was interrupted.").format(
                        path=os.path.join(grassdb, location),
                        error=error,
                    ),
                style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
            )
    dlg.Destroy()
    return modified
Ejemplo n.º 4
0
def delete_location_interactively(guiparent, grassdb, location):
    """Delete a location with user interaction.

    If current location found, delete operation is not performed.

    Exceptions during deletation are handled in this function.

    Returns True if there was a change, returns False if a location cannot be
    deleted (see above the possible reasons) or if another error was encountered.
    """
    genv = gisenv()
    issue = None

    # Check selected location and remember issue.
    # Each error is reported only once (using elif).
    location_path = os.path.join(grassdb, location)

    # Check for current location
    if (grassdb == genv['GISDBASE'] and location == genv['LOCATION_NAME']):
        issue = _("<{}> is the current location.").format(location_path)

    modified = False  # True after first successful delete

    # If any issues, display the warning message and do not delete anything
    if issue:
        dlg = wx.MessageDialog(
            parent=guiparent,
            message=_(
                "Cannot delete selected location for the following reasons:\n\n"
                "{}\n\n"
                "No location will be deleted.").format(issue),
            caption=_("Unable to delete selected location"),
            style=wx.OK | wx.ICON_WARNING)
        dlg.ShowModal()
    else:
        dlg = wx.MessageDialog(
            parent=guiparent,
            message=_(
                "Do you want to continue with deleting"
                "the following location?\n\n"
                "{}\n\n"
                "All mapsets included in this location will be permanently deleted!"
            ).format(location_path),
            caption=_("Delete selected location"),
            style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION,
        )
        if dlg.ShowModal() == wx.ID_YES:
            try:
                delete_location(grassdb, location)
                modified = True
                dlg.Destroy()
                return modified
            except OSError as error:
                wx.MessageBox(
                    parent=guiparent,
                    caption=_("Error when deleting location"),
                    message=_(
                        "The following error occured when deleting location <{path}>:"
                        "\n\n{error}\n\n"
                        "Deleting was interrupted.").format(
                            path=os.path.join(grassdb, location),
                            error=error,
                        ),
                    style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
                )
    dlg.Destroy()
    return modified