[GRASS-SVN] r48997 - grass/branches/develbranch_6/gui/wxpython/gui_modules

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Oct 29 17:56:46 EDT 2011


Author: martinl
Date: 2011-10-29 14:56:45 -0700 (Sat, 29 Oct 2011)
New Revision: 48997

Modified:
   grass/branches/develbranch_6/gui/wxpython/gui_modules/gmodeler.py
Log:
wxGUI/modeler: substitute variables also in input files (ascii)
	       (merge r48994 from trunk)


Modified: grass/branches/develbranch_6/gui/wxpython/gui_modules/gmodeler.py
===================================================================
--- grass/branches/develbranch_6/gui/wxpython/gui_modules/gmodeler.py	2011-10-29 21:56:20 UTC (rev 48996)
+++ grass/branches/develbranch_6/gui/wxpython/gui_modules/gmodeler.py	2011-10-29 21:56:45 UTC (rev 48997)
@@ -50,6 +50,7 @@
 import tempfile
 import copy
 import re
+import mimetypes
 
 try:
     import xml.etree.ElementTree as etree
@@ -433,11 +434,74 @@
         
         if statusbar:
             statusbar.SetStatusText(_('Running model...'), 0)
+        
+        # collect ascii inputs
+        self.fileInput = dict()
+        for p in item.GetParams()['params']:
+            if p.get('element', '') == 'file' and \
+                    p.get('prompt', '') == 'input' and \
+                    p.get('age', '') == 'old':
+                filename = p.get('value', p.get('default', ''))
+                if filename and \
+                        mimetypes.guess_type(filename)[0] == 'text/plain':
+                    self.fileInput[filename] = None
+        
+        for finput in self.fileInput:
+            # read lines
+            fd = open(finput, "r")
+            try:
+                data = self.fileInput[finput] = fd.read()
+            finally:
+                fd.close()
+            
+            # substitute variables
+            write = False
+            variables = self.GetVariables()
+            for variable in variables:
+                pattern= re.compile('%' + variable)
+                value = ''
+                if params and 'variables' in params:
+                    for p in params['variables']['params']:
+                        if variable == p.get('name', ''):
+                            value = p.get('value', '')
+                            break
+                
+                if not value:
+                    value = variables[variable].get('value', '')
+                
+                data = pattern.sub(value, data)
+                write = True
+            
+            if write:
+                fd = open(finput, "w")
+                try:
+                    fd.write(data)
+                finally:
+                    fd.close()
+            else:
+                self.fileInput[finput] = None
+        
         log.RunCmd(command = item.GetLog(string = False, substitute = True),
                    onDone = onDone)
         
         if name in params:
             item.SetParams(paramsOrig)
+
+    def CleanUp(self):
+        """!Clean up model"""
+        if hasattr(self, "fileInput"):
+            # restore original files
+            for finput in self.fileInput:
+                data = self.fileInput[finput]
+                if not data:
+                    continue
+                
+                fd = open(finput, "w")
+                try:
+                    fd.write(data)
+                finally:
+                    fd.close()
+            del self.fileInput
         
     def Run(self, log, onDone, parent = None):
         """!Run model
@@ -1079,6 +1143,7 @@
     def OnDone(self, cmd, returncode):
         """!Computation finished"""
         self.SetStatusText('', 0)
+        self.model.CleanUp()
         
     def OnValidateModel(self, event, showMsg = True):
         """!Validate entire model"""



More information about the grass-commit mailing list