浏览代码

init: more detailed info about creating locations and mapsets and error states

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@73199 15284696-431f-4ddb-bdfa-cd5b030d7da7
Vaclav Petras 6 年之前
父节点
当前提交
0ea3dac995
共有 1 个文件被更改,包括 65 次插入9 次删除
  1. 65 9
      lib/init/grass.py

+ 65 - 9
lib/init/grass.py

@@ -894,6 +894,42 @@ def get_mapset_invalid_reason(gisdbase, location, mapset):
                      mapset=mapset, loc=location)
                      mapset=mapset, loc=location)
 
 
 
 
+def can_create_location(gisdbase, location):
+    """Checks if location can be created"""
+    path = os.path.join(gisdbase, location)
+    if os.path.exists(path):
+        return False
+    return True
+
+
+def cannot_create_location_reason(gisdbase, location):
+    """Returns a message describing why location cannot be created
+
+    The goal is to provide the most suitable error message
+    (rather than to do a quick check).
+
+    :param gisdbase: Path to GRASS GIS database directory
+    :param location: name of a Location
+    :returns: translated message
+    """
+    path = os.path.join(gisdbase, location)
+    if is_location_valid(gisdbase, location):
+        return _("Unable to create new location because"
+                 " the location <{location}>"
+                 " already exists.").format(**locals())
+    elif os.path.isfile(path):
+        return _("Unable to create new location <{location}> because"
+                 " <{path}> is a file.").format(**locals())
+    elif os.path.isdir(path):
+        return _("Unable to create new location <{location}> because"
+                 " the directory <{path}>"
+                 " already exists.").format(**locals())
+    else:
+        return _("Unable to create new location in"
+                 " the directory <{path}>"
+                 " for an unknown reason.").format(**locals())
+
+
 def set_mapset(gisrc, arg=None, geofile=None, create_new=False,
 def set_mapset(gisrc, arg=None, geofile=None, create_new=False,
                tmp_location=False, tmpdir=None):
                tmp_location=False, tmpdir=None):
     """Selected Location and Mapset are checked and created if requested
     """Selected Location and Mapset are checked and created if requested
@@ -932,15 +968,21 @@ def set_mapset(gisrc, arg=None, geofile=None, create_new=False,
 
 
     if gisdbase and location_name and mapset:
     if gisdbase and location_name and mapset:
         path = os.path.join(gisdbase, location_name, mapset)
         path = os.path.join(gisdbase, location_name, mapset)
-
         # check if 'path' is a valid GRASS location/mapset
         # check if 'path' is a valid GRASS location/mapset
-        if not is_mapset_valid(path):
+        path_is_valid_mapset = is_mapset_valid(path)
+
+        if path_is_valid_mapset and create_new:
+            warning(_("Mapset <{}> already exists. Ignoring the"
+                      " request to create it. Note that this warning"
+                      " may become an error in future versions.")
+                    .format(path))
+
+        if not path_is_valid_mapset:
             if not create_new:
             if not create_new:
-                # 'path' is not a valid mapset and users does not
+                # 'path' is not a valid mapset and user does not
                 # want to create anything new
                 # want to create anything new
                 fatal(get_mapset_invalid_reason(gisdbase, location_name, mapset))
                 fatal(get_mapset_invalid_reason(gisdbase, location_name, mapset))
             else:
             else:
-                message(_("Creating new GRASS GIS location/mapset..."))
                 # 'path' is not valid and the user wants to create
                 # 'path' is not valid and the user wants to create
                 # mapset on the fly
                 # mapset on the fly
                 # check if 'location_name' is a valid GRASS location
                 # check if 'location_name' is a valid GRASS location
@@ -954,18 +996,32 @@ def set_mapset(gisrc, arg=None, geofile=None, create_new=False,
                         gisdbase = os.path.join(gisdbase, location_name)
                         gisdbase = os.path.join(gisdbase, location_name)
                         location_name = mapset
                         location_name = mapset
                         mapset = "PERMANENT"
                         mapset = "PERMANENT"
-                    if is_location_valid(gisdbase, location_name):
-                        fatal(_("Failed to create new location. "
-                                "The location <%s> already exists." % location_name))
+                    if not can_create_location(gisdbase, location_name):
+                        fatal(cannot_create_location_reason(
+                            gisdbase, location_name))
                     # create new location based on the provided EPSG/...
                     # create new location based on the provided EPSG/...
+                    message(_("Creating new GRASS GIS location <{}>...")
+                            .format(location_name))
                     create_location(gisdbase, location_name, geofile)
                     create_location(gisdbase, location_name, geofile)
                 else:
                 else:
                     # 'location_name' is a valid GRASS location,
                     # 'location_name' is a valid GRASS location,
                     # create new mapset
                     # create new mapset
-                    if os.path.exists(path):
+                    message(_("Creating new GRASS GIS mapset <{}>...")
+                            .format(mapset))
+                    if os.path.isfile(path):
+                        # not a valid mapset, but dir exists, assuming
+                        # broken/incomplete mapset
+                        fatal(_("Unable to create new mapset <{mapset}>"
+                                 " because <{path}> is a file.")
+                                .format(mapset=mapset, path=path))
+                    elif os.path.isdir(path):
                         # not a valid mapset, but dir exists, assuming
                         # not a valid mapset, but dir exists, assuming
                         # broken/incomplete mapset
                         # broken/incomplete mapset
-                        warning(_("Missing WIND file"))
+                        warning(_("The mapset <{}> is missing the WIND file"
+                                " (computational region). It will be"
+                                " fixed now. Note that this warning"
+                                " may become an error in future versions.")
+                                .format(mapset))
                     else:
                     else:
                         # create mapset directory
                         # create mapset directory
                         os.mkdir(path)
                         os.mkdir(path)