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

svn_grass at osgeo.org svn_grass at osgeo.org
Fri May 15 06:52:25 PDT 2015


Author: wenzeslaus
Date: 2015-05-15 06:52:24 -0700 (Fri, 15 May 2015)
New Revision: 65246

Modified:
   grass/trunk/lib/init/grass.py
Log:
init: make gui type related decisions explicit

 * remove few more usages of global variables (still using the same name, so shadowing the globals, but this will disappear in the future)
 * provide details in no GUI message, fix strange (and harmless) syntax
 * call functions only when they are needed, rather then doing these checks inside the fuctions (be explicit)


Modified: grass/trunk/lib/init/grass.py
===================================================================
--- grass/trunk/lib/init/grass.py	2015-05-15 12:38:24 UTC (rev 65245)
+++ grass/trunk/lib/init/grass.py	2015-05-15 13:52:24 UTC (rev 65246)
@@ -325,25 +325,24 @@
     f.close()
 
 
-def read_gui():
-    global grass_gui
+def read_gui(default_gui):
+    grass_gui = None
     # At this point the GRASS user interface variable has been set from the
     # command line, been set from an external environment variable, or is 
     # not set. So we check if it is not set
-    if not grass_gui:
-        # Check for a reference to the GRASS user interface in the grassrc file
-        if os.access(gisrc, os.R_OK):
-            kv = read_gisrc(gisrc)
-            if 'GRASS_GUI' in os.environ:
-                grass_gui = os.environ['GRASS_GUI']
-            elif 'GUI' in kv:
-                grass_gui = kv['GUI']
-            elif 'GRASS_GUI' in kv:
-                # For backward compatibility (GRASS_GUI renamed to GUI)
-                grass_gui = kv['GRASS_GUI']
-            else:
-                # Set the GRASS user interface to the default if needed
-                grass_gui = default_gui
+    # Check for a reference to the GRASS user interface in the grassrc file
+    if os.access(gisrc, os.R_OK):
+        kv = read_gisrc(gisrc)
+        if 'GRASS_GUI' in os.environ:
+            grass_gui = os.environ['GRASS_GUI']
+        elif 'GUI' in kv:
+            grass_gui = kv['GUI']
+        elif 'GRASS_GUI' in kv:
+            # For backward compatibility (GRASS_GUI renamed to GUI)
+            grass_gui = kv['GRASS_GUI']
+        else:
+            # Set the GRASS user interface to the default if needed
+            grass_gui = default_gui
 
     if not grass_gui:
         grass_gui = default_gui
@@ -356,7 +355,9 @@
         warning(_("GUI <%s> not supported in this version") % grass_gui)
         grass_gui = default_gui
 
+    return grass_gui
 
+
 def path_prepend(dir, var):
     path = os.getenv(var)
     if path:
@@ -505,21 +506,22 @@
     os.environ['GRASS_HTML_BROWSER'] = browser
 
 
-def create_initial_gisrc():
+def create_initial_gisrc(filename):
     # for convenience, define GISDBASE as pwd:
     s = r"""GISDBASE: %s
 LOCATION_NAME: <UNKNOWN>
 MAPSET: <UNKNOWN>
 """ % os.getcwd()
-    writefile(gisrc, s)
+    writefile(filename, s)
 
 
-def check_gui():
+def check_gui(expected_gui):
     global grass_gui, wxpython_base
+    grass_gui = expected_gui
     # Check if we are running X windows by checking the DISPLAY variable
     if os.getenv('DISPLAY') or windows or macosx:
         # Check if python is working properly
-        if grass_gui in ('wxpython', 'gtext'):
+        if expected_gui in ('wxpython', 'gtext'):
             nul = open(os.devnull, 'w')
             p = Popen([os.environ['GRASS_PYTHON']], stdin=subprocess.PIPE,
                       stdout=nul, stderr=nul)
@@ -542,15 +544,19 @@
 
     else:
         # Display a message if a graphical interface was expected
-        if grass_gui != 'text':
+        if expected_gui != 'text':
             # Set the interface mode to text
             warning(_("It appears that the X Windows system is not active.\n"
                       "A graphical based user interface is not supported.\n"
+                      "(DISPLAY variable is not set.)\n"
                       "Switching to text based interface mode.\n\n"
-                      "Hit RETURN to continue"""))
+                      "Hit RETURN to continue.\n"))
             sys.stdin.readline()
             grass_gui = 'text'
+    return grass_gui
 
+
+def save_gui(gisrc, grass_gui):
     # Save the user interface variable in the grassrc file - choose a temporary
     # file name that should not match another file
     if os.access(gisrc, os.F_OK):
@@ -975,7 +981,7 @@
         message(_("Execution of '%s' finished.") % batch_job)
 
 
-def start_gui():
+def start_gui(grass_gui):
     # Start the chosen GUI but ignore text
     if grass_debug:
         message(_("GRASS GUI should be <%s>") % grass_gui)
@@ -1368,8 +1374,10 @@
 # Ensure GUI is set
 if batch_job:
     grass_gui = 'text'
-else:
-    read_gui()
+elif not grass_gui:
+    # if GUI was not set previously (e.g. command line),
+    # get it from rc file or env variable
+    grass_gui = read_gui(default_gui)
 
 # Set PATH, PYTHONPATH
 set_paths()
@@ -1397,7 +1405,7 @@
                 " - Launch GRASS GIS directly with path to "
                 "the location/mapset as an argument (`%s /path/to/location/mapset`)\n"
                 " - Create manually the GISRC file (%s)") % (cmd_name, cmd_name, gisrcrc))
-    create_initial_gisrc()
+    create_initial_gisrc(gisrc)
 else:
     clean_temp()
 
@@ -1406,8 +1414,10 @@
 else:
     message(_("Starting GRASS GIS..."))
 
-# Check that the GUI works
-check_gui()
+# check that the GUI works but only if not doing a batch job
+if not batch_job:
+    grass_gui = check_gui(expected_gui=grass_gui)
+    save_gui(gisrc, grass_gui)
 
 # Parsing argument to get LOCATION
 if not args:
@@ -1451,7 +1461,7 @@
     try_remove(lockfile)
     sys.exit(0)
 else:
-    start_gui()
+    start_gui(grass_gui)
     clear_screen()
     show_banner()
     say_hello()



More information about the grass-commit mailing list