File: pyedit.py

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (864 lines) | stat: -rw-r--r-- 25,841 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
"""GRASS GIS Simple Python Editor

Copyright (C) 2016 by the GRASS Development Team

This program is free software under the GNU General Public
License (>=v2). Read the file COPYING that comes with GRASS GIS
for details.

:authors: Vaclav Petras
:authors: Martin Landa
"""

import sys
import os
import stat

from io import StringIO
import time

import wx

import grass.script as gscript
from grass.script.utils import try_remove

# needed just for testing
if __name__ == "__main__":
    from grass.script.setup import set_gui_path

    set_gui_path()

from core.gcmd import GError
from gui_core.pystc import PyStc, SetDarkMode
from core import globalvar
from core.menutree import MenuTreeModelBuilder
from gui_core.menu import RecentFilesMenu, Menu
from gui_core.toolbars import BaseToolbar, BaseIcons
from gui_core.wrap import IsDark
from icons.icon import MetaIcon
from core.debug import Debug

# TODO: add validation: call/import pep8 (error message if not available)
# TODO: run with parameters (alternatively, just use console or GUI)
# TODO: add more examples (better separate file)
# TODO: add test for templates and examples
# TODO: add pep8 test for templates and examples
# TODO: add snippets?


def script_template():
    """The most simple script which runs and gives something"""
    return r"""#!/usr/bin/env python3

import grass.script as gs


def main():
    gs.run_command('g.region', flags='p')


if __name__ == '__main__':
    main()
"""


def module_template():
    """Template from which to start writing GRASS module"""
    import getpass

    author = getpass.getuser()

    properties = {}
    properties["name"] = "module name"
    properties["author"] = author
    properties["description"] = "Module description"

    output = StringIO()
    # header
    output.write(
        r"""#!/usr/bin/env python3
#
#%s
#
# MODULE:       %s
#
# AUTHOR(S):    %s
#
# PURPOSE:      %s
#
# DATE:         %s
#
#%s
"""
        % (
            "#" * 72,
            properties["name"],
            properties["author"],
            "\n# ".join(properties["description"].splitlines()),
            time.asctime(),
            "#" * 72,
        )
    )

    # UI
    output.write(
        r"""
# %%module
# %% description: %s
# %%end
"""
        % (" ".join(properties["description"].splitlines()))
    )

    # import modules
    output.write(
        r"""
import sys
import os
import atexit

import grass.script as gs
"""
    )

    # cleanup()
    output.write(
        r"""
RAST_REMOVE = []

def cleanup():
"""
    )
    output.write(
        r"""    gs.run_command('g.remove', flags='f', type='raster',
                   name=RAST_REMOVE)
"""
    )
    output.write("\ndef main():\n")
    output.write(
        r"""    options, flags = gs.parser()
    gs.run_command('g.remove', flags='f', type='raster',
                   name=RAST_REMOVE)
"""
    )

    output.write("\n    return 0\n")

    output.write(
        r"""
if __name__ == "__main__":
    atexit.register(cleanup)
    sys.exit(main())
"""
    )
    return output.getvalue()


def script_example():
    """Example of a simple script"""
    return r"""#!/usr/bin/env python3

import grass.script as gs

def main():
    input_raster = 'elevation'
    output_raster = 'high_areas'
    stats = gs.parse_command('r.univar', map='elevation', flags='g')
    raster_mean = float(stats['mean'])
    raster_stddev = float(stats['stddev'])
    raster_high = raster_mean + raster_stddev
    gs.mapcalc('{r} = {a} > {m}'.format(r=output_raster, a=input_raster,
                                        m=raster_high))

if __name__ == "__main__":
    main()
"""


def module_example():
    """Example of a GRASS module"""
    return r"""#!/usr/bin/env python3

# %module
# % description: Adds the values of two rasters (A + B)
# % keyword: raster
# % keyword: algebra
# % keyword: sum
# %end
# %option G_OPT_R_INPUT
# % key: araster
# % description: Name of input raster A in an expression A + B
# %end
# %option G_OPT_R_INPUT
# % key: braster
# % description: Name of input raster B in an expression A + B
# %end
# %option G_OPT_R_OUTPUT
# %end


import sys

import grass.script as gs


def main():
    options, flags = gs.parser()
    araster = options['araster']
    braster = options['braster']
    output = options['output']

    gs.mapcalc('{r} = {a} + {b}'.format(r=output, a=araster, b=braster))

    return 0


if __name__ == "__main__":
    sys.exit(main())
"""


def module_error_handling_example():
    """Example of a GRASS module"""
    return r"""#!/usr/bin/env python3

# %module
# % description: Selects values from raster above value of mean plus standard deviation
# % keyword: raster
# % keyword: select
# % keyword: standard deviation
# %end
# %option G_OPT_R_INPUT
# %end
# %option G_OPT_R_OUTPUT
# %end


import sys

import grass.script as gs
from grass.exceptions import CalledModuleError


def main():
    options, flags = gs.parser()
    input_raster = options['input']
    output_raster = options['output']

    try:
        stats = gs.parse_command('r.univar', map=input_raster, flags='g')
    except CalledModuleError as e:
        gs.fatal('{0}'.format(e))
    raster_mean = float(stats['mean'])
    raster_stddev = float(stats['stddev'])
    raster_high = raster_mean + raster_stddev
    gs.mapcalc('{r} = {i} > {v}'.format(r=output_raster, i=input_raster,
                                        v=raster_high))
    return 0


if __name__ == "__main__":
    sys.exit(main())
"""


def open_url(url):
    import webbrowser

    webbrowser.open(url)


class PyEditController:
    # using the naming GUI convention, change for controller?
    # pylint: disable=invalid-name

    def __init__(self, panel, guiparent, giface):
        """Simple editor, this class could be a pure controller"""
        self.guiparent = guiparent
        self.giface = giface
        self.body = panel
        self.filename = None
        self.tempfile = None  # bool, make them strings for better code
        self.overwrite = False
        self.parameters = None

        # Get first (File) menu
        menu = guiparent.menubar.GetMenu(0)
        self.recent_files = RecentFilesMenu(
            app_name="pyedit",
            parent_menu=menu,
            pos=1,
        )  # pos=1 recent files menu position (index) in the parent (File) menu

        self.recent_files.file_requested.connect(self.OpenRecentFile)

    def _openFile(self, file_path):
        """Try open file and read content

        :param str file_path: file path

        :return str or None: file content or None
        """
        try:
            with open(file_path, "r") as f:
                content = f.read()
                return content
        except PermissionError:
            GError(
                message=_(
                    "Permission denied <{}>. Please change file "
                    "permission for reading.".format(file_path)
                ),
                parent=self.guiparent,
                showTraceback=False,
            )
        except OSError:
            GError(
                message=_("Couldn't read file <{}>.".format(file_path)),
                parent=self.guiparent,
            )

    def _writeFile(self, file_path, content, additional_err_message=""):
        """Try open file and write content

        :param str file_path: file path
        :param str content: content written to the file
        :param str additional_err_message: additional error message

        :return None or True: file written or None
        """
        try:
            with open(file_path, "w") as f:
                f.write(content)
                return True
        except PermissionError:
            GError(
                message=_(
                    "Permission denied <{}>. Please change file "
                    "permission for writing.{}".format(
                        file_path,
                        additional_err_message,
                    ),
                ),
                parent=self.guiparent,
                showTraceback=False,
            )
        except OSError:
            GError(
                message=_(
                    "Couldn't write file <{}>.{}".format(
                        file_path, additional_err_message
                    ),
                ),
                parent=self.guiparent,
            )

    def OnRun(self, event):
        """Run Python script"""
        if not self.filename:
            self.filename = gscript.tempfile() + ".py"
            self.tempfile = True
            file_is_written = self._writeFile(
                file_path=self.filename,
                content=self.body.GetText(),
                additional_err_message=" Unable to launch Python script.",
            )
            if file_is_written:
                mode = stat.S_IMODE(os.lstat(self.filename)[stat.ST_MODE])
                os.chmod(self.filename, mode | stat.S_IXUSR)
        else:
            # always save automatically before running
            file_is_written = self._writeFile(
                file_path=self.filename,
                content=self.body.GetText(),
                additional_err_message=" Unable to launch Python script.",
            )
            if file_is_written:
                # set executable file
                # (not sure if needed every time but useful for opened files)
                os.chmod(self.filename, stat.S_IRWXU | stat.S_IWUSR)

        if file_is_written:
            # run in console as other modules, avoid Python shell which
            # carries variables over to the next execution
            env = os.environ.copy()
            if self.overwrite:
                env["GRASS_OVERWRITE"] = "1"
            cmd = [self.filename]
            if self.parameters:
                cmd.extend(self.parameters)
            self.giface.RunCmd(cmd, env=env)

    def SaveAs(self):
        """Save python script to file"""
        if self.tempfile:
            try_remove(self.filename)
            self.tempfile = False

        filename = None
        dlg = wx.FileDialog(
            parent=self.guiparent,
            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(
                parent=self.guiparent,
                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()

        self.filename = filename
        self.tempfile = False
        self.Save()

    def Save(self):
        """Save current content to a file and set executable permissions"""
        assert self.filename
        file_is_written = self._writeFile(
            file_path=self.filename,
            content=self.body.GetText(),
        )
        if file_is_written:
            # executable file
            os.chmod(self.filename, stat.S_IRWXU | stat.S_IWUSR)

    def OnSave(self, event):
        """Save python script to file

        Just save if file already specified, save as action otherwise.
        """
        if self.filename and not self.tempfile:
            self.Save()
        else:
            self.SaveAs()

        if self.filename:
            self.recent_files.AddFileToHistory(
                filename=self.filename,
            )

    def IsModified(self):
        """Check if python script has been modified"""
        return self.body.modified

    def Open(self):
        """Ask for a filename and load its content"""
        filename = ""
        dlg = wx.FileDialog(
            parent=self.guiparent,
            message=_("Open file"),
            defaultDir=os.getcwd(),
            wildcard=_("Python script (*.py)|*.py"),
            style=wx.FD_OPEN,
        )

        if dlg.ShowModal() == wx.ID_OK:
            filename = dlg.GetPath()

        if not filename:
            return

        content = self._openFile(file_path=filename)
        if content:
            self.body.SetText(content)
        else:
            return

        self.filename = filename
        self.tempfile = False

    def OnOpen(self, event):
        """Handle open event but ask about replacing content first"""
        if self.CanReplaceContent("file"):
            self.Open()
            if self.filename:
                self.recent_files.AddFileToHistory(
                    filename=self.filename,
                )

    def OpenRecentFile(self, path, file_exists, file_history):
        """Try open recent file and read content

        :param str path: file path
        :param bool file_exists: file path exists
        :param bool file_history: file history obj instance

        :return: None
        """
        if not file_exists:
            GError(
                _(
                    "File <{}> doesn't exist."
                    "It was probably moved or deleted.".format(path)
                ),
                parent=self.guiparent,
            )
        else:
            if self.CanReplaceContent(by_message="file"):
                self.filename = path
                content = self._openFile(file_path=path)
                if content:
                    self.body.SetText(content)
                    file_history.AddFileToHistory(filename=path)  # move up the list
                    self.tempfile = False

    def IsEmpty(self):
        """Check if python script is empty"""
        return len(self.body.GetText()) == 0

    def IsContentValuable(self):
        """Check if content of the editor is valuable to user

        Used for example to check if content should be saved before closing.
        The content is not valuable for example if it already saved in a file.
        """
        Debug.msg(
            2,
            "pyedit IsContentValuable? empty=%s, modified=%s"
            % (self.IsEmpty(), self.IsModified()),
        )
        return not self.IsEmpty() and self.IsModified()

    def SetScriptTemplate(self, event):
        if self.CanReplaceContent("template"):
            self.body.SetText(script_template())
            self.filename = None

    def SetModuleTemplate(self, event):
        if self.CanReplaceContent("template"):
            self.body.SetText(module_template())
            self.filename = None

    def SetScriptExample(self, event):
        if self.CanReplaceContent("example"):
            self.body.SetText(script_example())
            self.filename = None

    def SetModuleExample(self, event):
        if self.CanReplaceContent("example"):
            self.body.SetText(module_example())
            self.filename = None

    def SetModuleErrorHandlingExample(self, event):
        if self.CanReplaceContent("example"):
            self.body.SetText(module_error_handling_example())
            self.filename = None

    def CanReplaceContent(self, by_message):
        """Check with user if we can replace content by something else

        Asks user if replacement is OK depending on the state of the editor.
        Use before replacing all editor content by some other text.

        :param by_message: message used to ask user if it is OK to replace
            the content with something else; special values are 'template',
            'example' and 'file' which will use predefined messages, otherwise
            a translatable, user visible string should be used.
        """
        if by_message == "template":
            message = _("Replace the content by the template?")
        elif by_message == "example":
            message = _("Replace the content by the example?")
        elif by_message == "file":
            message = _("Replace the current content by the file content?")
        else:
            message = by_message
        if self.IsContentValuable():
            dlg = wx.MessageDialog(
                parent=self.guiparent,
                message=message,
                caption=_("Replace content"),
                style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION,
            )
            if dlg.ShowModal() == wx.ID_NO:
                dlg.Destroy()
                return False
            dlg.Destroy()
        return True

    def OnSetParameters(self, event):
        """Handle setting CLI parameters for the script (asks for input)"""
        dlg = wx.TextEntryDialog(
            parent=self.guiparent,
            caption=_("Set parameters for the script"),
            message=_(
                "Specify command line parameters for the script separated by spaces:"
            ),
        )
        if self.parameters:
            dlg.SetValue(" ".join(self.parameters))
        # TODO: modality might not be needed here if we bind the events
        if dlg.ShowModal() == wx.ID_OK:
            text = dlg.GetValue().strip()
            # TODO: split in the same way as in console
            if text:
                self.parameters = text.split()
            else:
                self.parameters = None

    def OnHelp(self, event):
        # inspired by g.manual but simple not using GRASS_HTML_BROWSER
        # not using g.manual because it does not show
        entry = "libpython/script_intro.html"
        major, minor, patch = gscript.version()["version"].split(".")
        url = "https://grass.osgeo.org/grass%s%s/manuals/%s" % (major, minor, entry)
        open_url(url)

    def OnPythonHelp(self, event):
        url = "https://docs.python.org/%s/tutorial/" % sys.version_info[0]
        open_url(url)

    def OnModulesHelp(self, event):
        self.giface.Help("full_index")

    def OnSubmittingHelp(self, event):
        open_url(
            "https://github.com/OSGeo/grass/blob/main/doc/development/style_guide.md#python"  # noqa: E501
        )

    def OnAddonsHelp(self, event):
        open_url(
            "https://github.com/OSGeo/grass/blob/main/doc/development/style_guide.md#developing-grass-addons"  # noqa: E501
        )

    def OnSupport(self, event):
        open_url("https://grass.osgeo.org/support/")


class PyEditToolbar(BaseToolbar):
    # GUI class
    # pylint: disable=too-many-ancestors
    # pylint: disable=too-many-public-methods
    """PyEdit toolbar"""

    def __init__(self, parent):
        BaseToolbar.__init__(self, parent)

        self.icons = {
            "open": MetaIcon(img="open", label=_("Open (Ctrl+O)")),
            "save": MetaIcon(img="save", label=_("Save (Ctrl+S)")),
            "run": MetaIcon(img="execute", label=_("Run (Ctrl+R)")),
            # TODO: better icons for overwrite modes
            "overwriteTrue": MetaIcon(img="locked", label=_("Activate overwrite")),
            "overwriteFalse": MetaIcon(img="unlocked", label=_("Deactivate overwrite")),
            "help": BaseIcons["help"],
            "quit": BaseIcons["quit"],
        }

        # workaround for http://trac.wxwidgets.org/ticket/13888
        if sys.platform == "darwin":
            parent.SetToolBar(self)

        self.InitToolbar(self._toolbarData())

        # realize the toolbar
        self.Realize()

    def _toolbarData(self):
        """Toolbar data"""
        return self._getToolbarData(
            (
                (
                    ("open", self.icons["open"].label.rsplit(" ", 1)[0]),
                    self.icons["open"],
                    self.parent.OnOpen,
                ),
                (
                    ("save", self.icons["save"].label.rsplit(" ", 1)[0]),
                    self.icons["save"],
                    self.parent.OnSave,
                ),
                (None,),
                (
                    ("run", self.icons["run"].label.rsplit(" ", 1)[0]),
                    self.icons["run"],
                    self.parent.OnRun,
                ),
                (
                    ("overwrite", self.icons["overwriteTrue"].label),
                    self.icons["overwriteTrue"],
                    self.OnSetOverwrite,
                    wx.ITEM_CHECK,
                ),
                (None,),
                (
                    ("help", self.icons["help"].label),
                    self.icons["help"],
                    self.parent.OnHelp,
                ),
                (
                    ("quit", self.icons["quit"].label),
                    self.icons["quit"],
                    self.parent.OnClose,
                ),
            )
        )

    # TODO: add overwrite also to the menu and sync with toolbar
    def OnSetOverwrite(self, event):
        if self.GetToolState(self.overwrite):
            self.SetToolNormalBitmap(
                self.overwrite, self.icons["overwriteFalse"].GetBitmap()
            )
            self.SetToolShortHelp(
                self.overwrite, self.icons["overwriteFalse"].GetLabel()
            )
            self.parent.overwrite = True
        else:
            self.SetToolNormalBitmap(
                self.overwrite, self.icons["overwriteTrue"].GetBitmap()
            )
            self.SetToolShortHelp(
                self.overwrite, self.icons["overwriteTrue"].GetLabel()
            )
            self.parent.overwrite = False


class PyEditFrame(wx.Frame):
    # GUI class and a lot of trampoline methods
    # pylint: disable=missing-docstring
    # pylint: disable=too-many-public-methods
    # pylint: disable=invalid-name

    def __init__(
        self, parent, giface, id=wx.ID_ANY, title=_("Simple Python Editor"), **kwargs
    ):
        wx.Frame.__init__(self, parent=parent, id=id, title=title, **kwargs)
        self.parent = parent

        filename = os.path.join(globalvar.WXGUIDIR, "xml", "menudata_pyedit.xml")
        self.menubar = Menu(
            parent=self, model=MenuTreeModelBuilder(filename).GetModel(separators=True)
        )
        self.SetMenuBar(self.menubar)

        self.toolbar = PyEditToolbar(parent=self)
        # workaround for http://trac.wxwidgets.org/ticket/13888
        # TODO: toolbar is set in toolbar and here
        if sys.platform != "darwin":
            self.SetToolBar(self.toolbar)

        self.panel = PyStc(parent=self)
        if IsDark():
            SetDarkMode(self.panel)
        self.controller = PyEditController(
            panel=self.panel, guiparent=self, giface=giface
        )

        # don't start with an empty page
        self.panel.SetText(script_template())

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.panel, proportion=1, flag=wx.EXPAND)
        sizer.Fit(self)
        sizer.SetSizeHints(self)
        self.SetSizer(sizer)
        self.Fit()
        self.SetAutoLayout(True)
        self.Layout()
        self.Bind(wx.EVT_CLOSE, self.OnClose)

    # TODO: it would be nice if we can pass the controller to the menu
    # might not be possible on the side of menu
    # here we use self and self.controller which might make it harder
    def OnOpen(self, *args, **kwargs):
        self.controller.OnOpen(*args, **kwargs)

    def OnSave(self, *args, **kwargs):
        self.controller.OnSave(*args, **kwargs)

    def OnClose(self, *args, **kwargs):
        # this will be often true because PyStc is using EVT_KEY_DOWN
        # to say if it was modified, not actual user change in text
        if self.controller.IsContentValuable():
            self.controller.OnSave(*args, **kwargs)
        self.Destroy()

    def OnRun(self, *args, **kwargs):
        # save without asking
        self.controller.OnRun(*args, **kwargs)

    def OnHelp(self, *args, **kwargs):
        self.controller.OnHelp(*args, **kwargs)

    def OnSimpleScriptTemplate(self, *args, **kwargs):
        self.controller.SetScriptTemplate(*args, **kwargs)

    def OnGrassModuleTemplate(self, *args, **kwargs):
        self.controller.SetModuleTemplate(*args, **kwargs)

    def OnSimpleScriptExample(self, *args, **kwargs):
        self.controller.SetScriptExample(*args, **kwargs)

    def OnGrassModuleExample(self, *args, **kwargs):
        self.controller.SetModuleExample(*args, **kwargs)

    def OnGrassModuleErrorHandlingExample(self, *args, **kwargs):
        self.controller.SetModuleErrorHandlingExample(*args, **kwargs)

    def OnPythonHelp(self, *args, **kwargs):
        self.controller.OnPythonHelp(*args, **kwargs)

    def OnModulesHelp(self, *args, **kwargs):
        self.controller.OnModulesHelp(*args, **kwargs)

    def OnSubmittingHelp(self, *args, **kwargs):
        self.controller.OnSubmittingHelp(*args, **kwargs)

    def OnAddonsHelp(self, *args, **kwargs):
        self.controller.OnAddonsHelp(*args, **kwargs)

    def OnSupport(self, *args, **kwargs):
        self.controller.OnSupport(*args, **kwargs)

    def _get_overwrite(self):
        return self.controller.overwrite

    def _set_overwrite(self, overwrite):
        self.controller.overwrite = overwrite

    overwrite = property(
        _get_overwrite, _set_overwrite, doc="Tells if overwrite should be used"
    )

    def OnSetParameters(self, *args, **kwargs):
        self.controller.OnSetParameters(*args, **kwargs)


def main():
    """Test application (potentially useful as g.gui.pyedit)"""
    from core.giface import StandaloneGrassInterface

    app = wx.App()
    giface = StandaloneGrassInterface()
    simple_editor = PyEditFrame(parent=None, giface=giface)
    simple_editor.SetSize((600, 800))
    simple_editor.Show()
    app.MainLoop()


if __name__ == "__main__":
    main()