[GRASS-SVN] r58614 - grass/trunk/gui/wxpython/gmodeler

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Jan 5 08:26:13 PST 2014


Author: martinl
Date: 2014-01-05 08:26:13 -0800 (Sun, 05 Jan 2014)
New Revision: 58614

Modified:
   grass/trunk/gui/wxpython/gmodeler/model.py
Log:
wxGUI/modeler: fix python export & variable substitution

Modified: grass/trunk/gui/wxpython/gmodeler/model.py
===================================================================
--- grass/trunk/gui/wxpython/gmodeler/model.py	2014-01-05 13:56:47 UTC (rev 58613)
+++ grass/trunk/gui/wxpython/gmodeler/model.py	2014-01-05 16:26:13 UTC (rev 58614)
@@ -2300,17 +2300,17 @@
 """)
         if rast:
             self.fd.write(
-r"""    grass.run_command('g.remove',
+r"""    run_command('g.remove',
                       rast=%s)
 """ % ','.join(map(lambda x: "'" + x + "'", rast)))
         if vect:
             self.fd.write(
-r"""    grass.run_command('g.remove',
+r"""    run_command('g.remove',
                       vect = %s)
 """ % ','.join(map(lambda x: "'" + x + "'", vect)))
         if rast3d:
             self.fd.write(
-r"""    grass.run_command('g.remove',
+r"""    run_command('g.remove',
                       rast3d = %s)
 """ % ','.join(map(lambda x: "'" + x + "'", rast3d)))
         if not rast and not vect and not rast3d:
@@ -2325,7 +2325,7 @@
         self.fd.write(
 r"""
 if __name__ == "__main__":
-    options, flags = grass.parser()
+    options, flags = parser()
     atexit.register(cleanup)
     sys.exit(main())
 """)
@@ -2380,7 +2380,7 @@
     def _writePythonAction(self, item, variables = []):
         """!Write model action to Python file"""
         task = GUI(show = None).ParseCommand(cmd = item.GetLog(string = False, substitute = self.model.GetVariables()))
-        strcmd = "%sgrass.run_command(" % (' ' * self.indent)
+        strcmd = "%srun_command(" % (' ' * self.indent)
         self.fd.write(strcmd + self._getPythonActionCmd(task, len(strcmd), variables) + '\n')
         
     def _getPythonActionCmd(self, task, cmdIndent, variables = []):
@@ -2403,12 +2403,16 @@
             value = p.get('value', None)
             if name and value:
                 ptype = p.get('type', 'string')
-                if value[0] == '%':
-                    params.append("%s = %s" % (name, value[1:]))
-                elif ptype == 'string':
+                foundVar = False
+                for var in variables:
+                    if '%' + var in value:
+                        value = self._substituteVariable(value, var)
+                        foundVar = True
+                
+                if foundVar or ptype != 'string':
+                    params.append("%s = %s" % (name, value))
+                else:
                     params.append('%s = "%s"' % (name, value))
-                else:
-                    params.append("%s = %s" % (name, value))
         
         ret += '"%s"' % task.get_name()
         if flags:
@@ -2428,6 +2432,28 @@
         for line in item.GetLabel().splitlines():
             self.fd.write('#' + line + '\n')
 
+    def _substituteVariable(self, string, variable):
+        """!Substitute variable in the string
+
+        @param string string to be modified
+        @param variable variable to be substituted
+        
+        @return modified string
+        """
+        result = ''
+        ss = re.split(r"%"+variable, string)
+        if len(ss) == 2:
+            return variable
+        
+        for s in ss:
+            if not s:
+                result += '+%s+' % variable
+            else:
+                result += '"' + s + '"'
+        
+        return result.strip('+')
+
+
 class ModelParamDialog(wx.Dialog):
     def __init__(self, parent, params, id = wx.ID_ANY, title = _("Model parameters"),
                  style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER, **kwargs):



More information about the grass-commit mailing list