|
@@ -9,50 +9,71 @@ for details.
|
|
|
.. sectionauthor:: Vaclav Petras <wenzeslaus gmail com>
|
|
|
"""
|
|
|
|
|
|
+import datetime
|
|
|
+import glob
|
|
|
import os
|
|
|
import sys
|
|
|
-import datetime
|
|
|
from pathlib import Path
|
|
|
-from grass.script import gisenv
|
|
|
-import grass.script as gs
|
|
|
-import glob
|
|
|
|
|
|
import grass.grassdb.config as cfg
|
|
|
+import grass.script as gs
|
|
|
+from grass.script import gisenv
|
|
|
|
|
|
|
|
|
-def mapset_exists(database, location, mapset):
|
|
|
- """Returns True whether mapset path exists."""
|
|
|
- location_path = os.path.join(database, location)
|
|
|
- mapset_path = os.path.join(location_path, mapset)
|
|
|
- if os.path.exists(mapset_path):
|
|
|
- return True
|
|
|
- return False
|
|
|
+def mapset_exists(path, location=None, mapset=None):
|
|
|
+ """Returns True whether mapset path exists.
|
|
|
|
|
|
+ Either only *path* is provided or all three parameters need to be provided.
|
|
|
+
|
|
|
+ :param path: Path to a Mapset or to a GRASS GIS database directory
|
|
|
+ :param location: name of a Location if not part of *path*
|
|
|
+ :param mapset: name of a Mapset if not part of *path*
|
|
|
+ """
|
|
|
+ if location and mapset:
|
|
|
+ path = os.path.join(path, location, mapset)
|
|
|
+ elif location or mapset:
|
|
|
+ raise ValueError(_("Provide only path or all three parameters, not two"))
|
|
|
+ return os.path.exists(path)
|
|
|
|
|
|
-def location_exists(database, location):
|
|
|
- """Returns True whether location path exists."""
|
|
|
- location_path = os.path.join(database, location)
|
|
|
- if os.path.exists(location_path):
|
|
|
- return True
|
|
|
- return False
|
|
|
+
|
|
|
+def location_exists(path, location=None):
|
|
|
+ """Returns True whether location path exists.
|
|
|
+
|
|
|
+ :param path: Path to a Location or to a GRASS GIS database directory
|
|
|
+ :param location: name of a Location if not part of *path*
|
|
|
+ """
|
|
|
+ if location:
|
|
|
+ path = os.path.join(path, location)
|
|
|
+ return os.path.exists(path)
|
|
|
|
|
|
|
|
|
# TODO: distinguish between valid for getting maps and usable as current
|
|
|
# https://lists.osgeo.org/pipermail/grass-dev/2016-September/082317.html
|
|
|
# interface created according to the current usage
|
|
|
-def is_mapset_valid(mapset_path):
|
|
|
- """Return True if GRASS Mapset is valid"""
|
|
|
+def is_mapset_valid(path, location=None, mapset=None):
|
|
|
+ """Return True if GRASS Mapset is valid
|
|
|
+
|
|
|
+ Either only *path* is provided or all three parameters need to be provided.
|
|
|
+
|
|
|
+ :param path: Path to a Mapset or to a GRASS GIS database directory
|
|
|
+ :param location: name of a Location if not part of *path*
|
|
|
+ :param mapset: name of a Mapset if not part of *path*
|
|
|
+ """
|
|
|
# WIND is created from DEFAULT_WIND by `g.region -d` and functions
|
|
|
# or modules which create a new mapset. Most modules will fail if
|
|
|
# WIND doesn't exist (assuming that neither GRASS_REGION nor
|
|
|
# WIND_OVERRIDE environmental variables are set).
|
|
|
- return os.access(os.path.join(mapset_path, "WIND"), os.R_OK)
|
|
|
+ if location and mapset:
|
|
|
+ path = os.path.join(path, location, mapset)
|
|
|
+ elif location or mapset:
|
|
|
+ raise ValueError(_("Provide only path or all three parameters, not two"))
|
|
|
+ return os.access(os.path.join(path, "WIND"), os.R_OK)
|
|
|
|
|
|
|
|
|
def is_location_valid(path, location=None):
|
|
|
"""Return True if GRASS Location is valid
|
|
|
|
|
|
- :param database: Path to a Location or to a GRASS GIS database directory
|
|
|
+ :param path: Path to a Location or to a GRASS GIS database directory
|
|
|
:param location: name of a Location if not part of *path*
|
|
|
"""
|
|
|
# DEFAULT_WIND file should not be required until you do something
|
|
@@ -60,11 +81,12 @@ def is_location_valid(path, location=None):
|
|
|
# containing a PERMANENT/DEFAULT_WIND file is probably a GRASS
|
|
|
# location, while a directory lacking it probably isn't.
|
|
|
if location:
|
|
|
- path = os.path.join(location)
|
|
|
+ path = os.path.join(path, location)
|
|
|
return os.access(os.path.join(path, "PERMANENT", "DEFAULT_WIND"), os.F_OK)
|
|
|
|
|
|
|
|
|
def is_mapset_current(database, location, mapset):
|
|
|
+ """Return True if the given GRASS Mapset is the current mapset"""
|
|
|
genv = gisenv()
|
|
|
if (
|
|
|
database == genv["GISDBASE"]
|
|
@@ -76,6 +98,7 @@ def is_mapset_current(database, location, mapset):
|
|
|
|
|
|
|
|
|
def is_location_current(database, location):
|
|
|
+ """Return True if the given GRASS Location is the current location"""
|
|
|
genv = gisenv()
|
|
|
if database == genv["GISDBASE"] and location == genv["LOCATION_NAME"]:
|
|
|
return True
|