| 1 | [[TOC]] |
| 2 | |
| 3 | = Submitting Python = |
| 4 | |
| 5 | |
| 6 | |
| 7 | == Structure of the file == |
| 8 | |
| 9 | Instructions for the GRASS script parser can be found in the g.parser |
| 10 | module's help page. |
| 11 | http://grass.osgeo.org/grass70/manuals/g.parser.html |
| 12 | |
| 13 | |
| 14 | 2. Use the directory structure to place your script appropriately into |
| 15 | the source tree |
| 16 | - scripts go into scripts/ |
| 17 | |
| 18 | Also add a Makefile and a <module>.html file into this directory. |
| 19 | See existing Python scripts for examples. |
| 20 | |
| 21 | 3. Add a header section to the script you submit and make sure you |
| 22 | include the copyright. The purpose section is meant to contain a |
| 23 | general over view of the code in the file to assist other |
| 24 | programmers that will need to make changes to your code. For this |
| 25 | purpose use Python Docstring, see |
| 26 | http://epydoc.sourceforge.net/docstrings.html |
| 27 | |
| 28 | Example (fictitious header for a script called g.myscript): |
| 29 | |
| 30 | {{{ |
| 31 | !python |
| 32 | """ |
| 33 | MODULE: g.myscript |
| 34 | |
| 35 | AUTHOR(S): John Doe <email AT some domain> |
| 36 | |
| 37 | PURPOSE: Describe your script here... |
| 38 | |
| 39 | COPYRIGHT: (C) 2007 John Doe, and by the GRASS Development Team |
| 40 | |
| 41 | This program is free software under the GNU General Public |
| 42 | License (>=v2). Read the file COPYING that comes with GRASS |
| 43 | for details. |
| 44 | """ |
| 45 | }}} |
| 46 | |
| 47 | The copyright protects your rights according to GNU General Public License (www.gnu.org). |
| 48 | |
| 49 | You can easily autogenerate the header and parameters from an existing |
| 50 | module using the --script flag. Example: |
| 51 | |
| 52 | d.rast --script |
| 53 | |
| 54 | Just select an existing module which is close to your application to save efforts. |
| 55 | |
| 56 | |
| 57 | == Style == |
| 58 | |
| 59 | Follow PEP8 and use pep8 tool. |
| 60 | |
| 61 | As Python determines nesting based upon indentation, it isn't just |
| 62 | a stylistic issue. |
| 63 | |
| 64 | Use 4-space indentation (GNU Emacs python-mode default). |
| 65 | |
| 66 | Make sure a new line is at the end of each file. |
| 67 | |
| 68 | Use named parameters in functions (without space around '='), e.g. |
| 69 | |
| 70 | {{{ |
| 71 | !python |
| 72 | dlg = wx.FileDialog(parent=self, message=_("Choose file to save current workspace"), |
| 73 | wildcard=_("GRASS Workspace File (*.gxw)|*.gxw"), style=wx.FD_SAVE) |
| 74 | }}} |
| 75 | |
| 76 | instead of |
| 77 | |
| 78 | {{{ |
| 79 | !python |
| 80 | |
| 81 | dlg = wx.FileDialog(self, _("Choose file to save current workspace"), |
| 82 | _("GRASS Workspace File (*.gxw)|*.gxw"), wx.FD_SAVE) |
| 83 | }}} |
| 84 | |
| 85 | |
| 86 | |
| 87 | == Writing the code == |
| 88 | |
| 89 | === Temporary files === |
| 90 | |
| 91 | 5. Create and use secure temporary files and directories. Use the |
| 92 | grass.tempfile() or grass.tempdir() functions to do this. e.g. |
| 93 | |
| 94 | {{{ |
| 95 | #!python |
| 96 | # setup temporary file |
| 97 | TMP = grass.tempfile() |
| 98 | if TMP is None: |
| 99 | grass.fatal("Unable to create temporary files") |
| 100 | }}} |
| 101 | |
| 102 | 6. Use grass.findfile() when there is a need to test if a map exists. |
| 103 | |
| 104 | |
| 105 | {{{ |
| 106 | #!python |
| 107 | # test for input raster map |
| 108 | result = grass.find_file(name = map_name, element = 'cell', quiet = True) |
| 109 | if not result['file'] |
| 110 | grass.fatal("Raster map <%s> not found" % map_name) |
| 111 | |
| 112 | # test for input vector map |
| 113 | result = grass.find_file(name = map_name, element = 'vector', quiet = True) |
| 114 | if not result['file'] |
| 115 | grass.fatal("Vector map <%s> not found" % map_name) |
| 116 | }}} |
| 117 | |
| 118 | ... and so forth. See 'g.manual g.findfile' for details. |
| 119 | |
| 120 | === Messages === |
| 121 | |
| 122 | 7. For any informational output, use the grass.message() |
| 123 | function. For error messages should be used grass.fatal_error() or |
| 124 | grass.error() and for warnings grass.warning(). For debugging |
| 125 | purposes grass.debug(). |
| 126 | |
| 127 | |
| 128 | {{{ |
| 129 | #!python |
| 130 | #normal message: |
| 131 | grass.message("Done") |
| 132 | |
| 133 | # warning: |
| 134 | grass.warning("No input values found, using default values") |
| 135 | |
| 136 | # error: |
| 137 | grass.error("No map found") |
| 138 | |
| 139 | # fatal error: |
| 140 | grass.fatal_error("No map found, exiting") |
| 141 | |
| 142 | # debug output (use g.gisenv to enable/disable) |
| 143 | grass.debug("Our calculated value is: %d" % value) |
| 144 | }}} |
| 145 | |
| 146 | Try to omit any usage of the 'print' command for informational output. |
| 147 | |
| 148 | |
| 149 | === Translations === |
| 150 | |
| 151 | To enable translating of messages to other languages (than English), use full strings, e.g. |
| 152 | |
| 153 | {{{ |
| 154 | !python |
| 155 | if ...: |
| 156 | win.SetLabel(_("Name for new 3D raster map to create")) |
| 157 | else: |
| 158 | win.SetLabel(_("Name for new raster map to create")) |
| 159 | }}} |
| 160 | |
| 161 | instead of constructing string from several parts |
| 162 | |
| 163 | {{{ |
| 164 | !python |
| 165 | if ...: |
| 166 | maplabel = 'raster map' |
| 167 | else: |
| 168 | maplabel = '3D raster map' |
| 169 | win.SetLabel(_("Name for new %s to create") % maplabel) |
| 170 | }}} |
| 171 | |
| 172 | == Documentation and comments == |
| 173 | |
| 174 | |
| 175 | |
| 176 | 6. Comment your classes and functions with docstrings. Use Sphinx syntax. |
| 177 | |
| 178 | Comment also the code itself such as the meaning of variables, |
| 179 | conditions etc. |
| 180 | |
| 181 | |
| 182 | 8. PLEASE take the time to add comments throughout your code explaining what |
| 183 | the code is doing. It will save a HUGE amount of time and frustration for |
| 184 | other programmers that may have to change your code in the future. |
| 185 | |
| 186 | |
| 187 | |
| 188 | 9. Use tools such as pylint and pep8 to check your code (both style and |
| 189 | correctness). Just note that default settings of these tools is not fully |
| 190 | compatible with wxGUI/wxPython style and that some of the reported errors |
| 191 | may not apply to your code. |
| 192 | |
| 193 | |
| 194 | == Testing == |
| 195 | |
| 196 | |
| 197 | |
| 198 | == See also == |
| 199 | |
| 200 | === Related submitting rules === |
| 201 | |
| 202 | * wiki:Submitting/General general GRASS and svn instructions |
| 203 | * wiki:Submitting/Docs user documentation of modules and GUI |
| 204 | * wiki:Submitting/wxGUI wxPython-based GUI has its own rules |
| 205 | |
| 206 | |
| 207 | === GRASS documentation === |
| 208 | |
| 209 | * GRASS Programming manual for API and existing classes: http://grass.osgeo.org/programming7/ (add more specific link) |
| 210 | * GRASS wiki has pages about how to develop GRASS scripts in Python: http://grasswiki.osgeo.org |
| 211 | |
| 212 | === External documentation === |
| 213 | |
| 214 | * Style Guide for Python Code: http://www.python.org/dev/peps/pep-0008/ |
| 215 | * Python Style Guide by Guido van Rossum: http://www.python.org/doc/essays/styleguide.html |
| 216 | * Additional info on Python docstrings: http://epydoc.sourceforge.net/docstrings.html |