[GRASS-SVN] r41821 - in grass/trunk/gui/wxpython: gui_modules xml

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Apr 12 12:42:08 EDT 2010


Author: martinl
Date: 2010-04-12 12:42:08 -0400 (Mon, 12 Apr 2010)
New Revision: 41821

Modified:
   grass/trunk/gui/wxpython/gui_modules/gmodeler.py
   grass/trunk/gui/wxpython/xml/menudata_modeler.xml
Log:
wxGUI/modeler: export to Python script


Modified: grass/trunk/gui/wxpython/gui_modules/gmodeler.py
===================================================================
--- grass/trunk/gui/wxpython/gui_modules/gmodeler.py	2010-04-12 15:07:10 UTC (rev 41820)
+++ grass/trunk/gui/wxpython/gui_modules/gmodeler.py	2010-04-12 16:42:08 UTC (rev 41821)
@@ -25,6 +25,8 @@
 import shlex
 import time
 import traceback
+import getpass
+import stat
 
 try:
     import xml.etree.ElementTree as etree
@@ -348,6 +350,120 @@
                      message = _('Model is valid.'),
                      msgType = 'info')
 
+    def OnExportPython(self, event):
+        """!Export model to Python script"""
+        filename = ''
+        dlg = wx.FileDialog(parent = self,
+                            message = _("Choose file to save"),
+                            defaultDir = os.getcwd(),
+                            wildcard=_("Python script (*.py)|*.py"),
+                            style=wx.FD_SAVE)
+        
+        if dlg.ShowModal() == wx.ID_OK:
+            filename = dlg.GetPath()
+        
+        if not filename:
+            return
+
+        # check for extension
+        if filename[-3:] != ".py":
+            filename += ".py"
+        
+        if os.path.exists(filename):
+            dlg = wx.MessageDialog(self, message=_("File <%s> already exists. "
+                                                   "Do you want to overwrite this file?") % filename,
+                                   caption=_("Save file"),
+                                   style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
+            if dlg.ShowModal() == wx.ID_NO:
+                dlg.Destroy()
+                return
+            
+            dlg.Destroy()
+
+        fd = open(filename, "w")
+        try:
+            self._writePython(fd)
+        finally:
+            fd.close()
+        
+        # executable file
+        os.chmod(filename, stat.S_IRWXU | stat.S_IWUSR)
+        
+    def _writePython(self, fd):
+        """!Write model to file"""
+        fd.write(
+r"""#!/usr/bin/env python
+#
+############################################################################
+#
+# MODULE:       Graphical modeler script
+#
+# AUTHOR(S):	%s
+#               
+# PURPOSE:      Script generated by wxGUI Graphical Modeler
+#
+# DATE:         %s
+#
+#############################################################################
+""" % (getpass.getuser(), time.asctime()))
+        
+        fd.write(
+r"""
+import sys
+import os
+import grass.script as grass
+import atexit
+""")
+ 
+        fd.write(
+r"""
+
+def cleanup():
+    pass
+""")
+
+        fd.write("\ndef main():\n")
+        for action in self.actions:
+            task = menuform.GUI().ParseCommand(cmd = action.GetLog(string = False),
+                                               show = None)
+            opts = task.get_options()
+            flags = ''
+            params = list()
+            strcmd = "    grass.run_command("
+            indent = len(strcmd)
+            fd.write(strcmd + "'%s',\n" % task.get_name())
+            for f in opts['flags']:
+                if f.get('value', False) == True:
+                    name = f.get('name', '')
+                    if len(name) > 1:
+                        params.append('%s=True' % name)
+                    else:
+                        flags += name
+            
+            for p in opts['params']:
+                name = p.get('name', None)
+                value = p.get('value', None)
+                if name and value:
+                    ptype = p.get('type', 'string')
+                    if ptype == 'string':
+                        params.append("%s='%s'" % (name, value))
+                    else:
+                        params.append("%s=%s" % (name, value))
+
+            for opt in params[:-1]:
+                fd.write("%s%s,\n" % (' ' * indent, opt))
+            fd.write("%s%s)\n" % (' ' * indent, params[-1]))
+    
+        fd.write("\n    return 0\n")
+        
+        fd.write(
+r"""
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    atexit.register(cleanup)
+    sys.exit(main())
+""")
+
     def _validateModel(self):
         """!Validate model"""
         self.SetStatusText(_('Validating model...'), 0)

Modified: grass/trunk/gui/wxpython/xml/menudata_modeler.xml
===================================================================
--- grass/trunk/gui/wxpython/xml/menudata_modeler.xml	2010-04-12 15:07:10 UTC (rev 41820)
+++ grass/trunk/gui/wxpython/xml/menudata_modeler.xml	2010-04-12 16:42:08 UTC (rev 41821)
@@ -33,6 +33,12 @@
 	</menuitem>
 	<separator />
 	<menuitem>
+	  <label>Export to Python</label>
+	  <help>Export model to Python script</help>
+	  <handler>OnExportPython</handler>
+	</menuitem>
+	<separator />
+	<menuitem>
 	  <label>Close modeler</label>
 	  <help>Close modeler window</help>
 	  <handler>OnCloseWindow</handler>



More information about the grass-commit mailing list