|
@@ -25,6 +25,8 @@ import sys
|
|
|
import shlex
|
|
|
import time
|
|
|
import traceback
|
|
|
+import getpass
|
|
|
+import stat
|
|
|
|
|
|
try:
|
|
|
import xml.etree.ElementTree as etree
|
|
@@ -348,6 +350,120 @@ class ModelFrame(wx.Frame):
|
|
|
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)
|