[GRASS-SVN] r65276 - grass/trunk/lib/init

svn_grass at osgeo.org svn_grass at osgeo.org
Mon May 18 15:27:23 PDT 2015


Author: wenzeslaus
Date: 2015-05-18 15:27:23 -0700 (Mon, 18 May 2015)
New Revision: 65276

Modified:
   grass/trunk/lib/init/grass.py
Log:
init: introduce class for mapset and related variables to allow for return by value semantics

Modified: grass/trunk/lib/init/grass.py
===================================================================
--- grass/trunk/lib/init/grass.py	2015-05-18 21:57:19 UTC (rev 65275)
+++ grass/trunk/lib/init/grass.py	2015-05-18 22:27:23 UTC (rev 65276)
@@ -838,24 +838,57 @@
                 "Please advise GRASS developers of this error."))
 
 
+# we don't follow the LOCATION_NAME legacy naming here but we have to still
+# translate to it, so always double check
+class MapsetSettings(object):
+    """Holds GRASS GIS database directory, Location and Mapset
+
+    Provides few convenient functions.
+    """
+    def __init__(self):
+        self.gisdbase = None
+        self.location = None
+        self.mapset = None
+        self._full_mapset = None
+
+    # TODO: perhaps full_mapset would be better as mapset_path
+    # TODO: works only when set for the first time
+    # this follows the current usage but we must invalidate when
+    # the others are changed (use properties for that)
+    @property
+    def full_mapset(self):
+        if self._full_mapset is None:
+            self._full_mapset = os.path.join(self.gisdbase, self.location,
+                                             self.mapset)
+        return self._full_mapset
+
+    def is_valid(self):
+        return self.gisdbase and self.location and self.mapset
+
+
 def load_gisrc(gisrc):
-    global gisdbase, location_name, mapset, location
+    """Get the settings of Location and Mapset from the gisrc file
+
+    :returns: MapsetSettings object
+    """
+    mapset_settings = MapsetSettings()
     kv = read_gisrc(gisrc)
-    gisdbase = kv.get('GISDBASE')
-    location_name = kv.get('LOCATION_NAME')
-    mapset = kv.get('MAPSET')
-    if not gisdbase or not location_name or not mapset:
+    mapset_settings.gisdbase = kv.get('GISDBASE')
+    mapset_settings.location = kv.get('LOCATION_NAME')
+    mapset_settings.mapset = kv.get('MAPSET')
+    if not mapset_settings.is_valid():
         fatal(_("Error reading data path information from g.gisenv.\n"
-                "GISDBASE=%(gisbase)s\n"
-                "LOCATION_NAME=%(location)s\n"
-                "MAPSET=%(mapset)s\n\n"
-                "Check the <%(file)s> file." % \
-                    {'gisbase': gisdbase, 'location': location_name,
-                     'mapset': mapset, 'file': gisrcrc}))
+                "GISDBASE={gisbase}\n"
+                "LOCATION_NAME={location}\n"
+                "MAPSET={mapset}\n\n"
+                "Check the <{file}> file.").format(
+                    gisbase=mapset_settings.gisdbase,
+                    location=mapset_settings.location,
+                    mapset=mapset_settings.mapset,
+                    file=gisrcrc))
+    return mapset_settings
 
-    location = os.path.join(gisdbase, location_name, mapset)
 
-
 # load environmental variables from grass_env_file
 def load_env():
     if not os.access(grass_env_file, os.R_OK):
@@ -1605,8 +1638,13 @@
 # e.g. wxGUI startup screen writes to the gisrc file,
 # so loading it is the only universal way to obtain the values
 # this suppose that both programs share the right path to gisrc file
-load_gisrc(gisrc)
+mapset_settings = load_gisrc(gisrc)
 
+gisdbase = mapset_settings.gisdbase
+location_name = mapset_settings.location
+mapset = mapset_settings.mapset
+location = mapset_settings.full_mapset
+
 # Check .gislock file
 check_lock(params.force_gislock_removal)
 



More information about the grass-commit mailing list