SUBMITTING_PYTHON 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. NOTE: Please improve this list!
  2. Dear (new) GRASS developer,
  3. when submitting Python code to GRASS SVN repository, please take
  4. care of following rules:
  5. [ see SUBMITTING for C hints ]
  6. [ see SUBMITTING_DOCS for documentation ]
  7. See also http://www.python.org/dev/peps/pep-0008/
  8. 0. Indentation
  9. As Python determines nesting based upon indentation, it isn't just
  10. a stylistic issue.
  11. Please use 4-space indentation (GNU Emacs python-mode default).
  12. See also "Python Style Guide" by Guido van Rossum
  13. http://www.python.org/doc/essays/styleguide.html
  14. 1. Instructions for the GRASS script parser can be found in the g.parser
  15. module's help page.
  16. http://grass.osgeo.org/grass70/manuals/html70_user/g.parser.html
  17. 2. Use the directory structure to place your script appropriately into
  18. the source tree
  19. - scripts go into scripts/
  20. Also add a Makefile and a <module>.html file into this directory.
  21. See existing Python scripts for examples.
  22. 3. Add a header section to the script you submit and make sure you
  23. include the copyright. The purpose section is meant to contain a
  24. general over view of the code in the file to assist other
  25. programmers that will need to make changes to your code. For this
  26. purpose use Python Docstring, see
  27. http://epydoc.sourceforge.net/docstrings.html
  28. Example (fictitious header for a script called g.myscript):
  29. """
  30. MODULE: g.myscript
  31. AUTHOR(S): John Doe <email AT some domain>
  32. PURPOSE: Describe your script here...
  33. COPYRIGHT: (C) 2007 John Doe, and by the GRASS Development Team
  34. This program is free software under the GNU General Public
  35. License (>=v2). Read the file COPYING that comes with GRASS
  36. for details.
  37. """
  38. The copyright protects your rights according to GNU General Public
  39. License (www.gnu.org).
  40. You can easily autogenerate the header and parameters from an existing
  41. module using the --script flag. Example:
  42. d.rast --script
  43. Just select an existing module which is close to your application to save
  44. efforts.
  45. 4. We don't want the $ ID $ in source code any more as it causes problems
  46. for the branches.
  47. 5. Create and use secure temporary files and directories. Use the
  48. grass.tempfile() or grass.tempdir() functions to do this. e.g.
  49. # setup temporary file
  50. TMP = grass.tempfile()
  51. if TMP is None:
  52. grass.fatal("Unable to create temporary files")
  53. 6. Use grass.findfile() when there is a need to test if a map exists.
  54. # test for input raster map
  55. result = grass.find_file(name = map_name, element = 'cell', quiet = True)
  56. if not result['file']
  57. grass.fatal("Raster map <%s> not found" % map_name)
  58. # test for input vector map
  59. result = grass.find_file(name = map_name, element = 'vector', quiet = True)
  60. if not result['file']
  61. grass.fatal("Vector map <%s> not found" % map_name)
  62. ... and so forth. See 'g.manual g.findfile' for details.
  63. 7. For any informational output, use the grass.message()
  64. function. For error messages should be used grass.fatal_error() or
  65. grass.error() and for warnings grass.warning(). For debugging
  66. purposes grass.debug().
  67. #normal message:
  68. grass.message("Done")
  69. # warning:
  70. grass.warning("No input values found, using default values")
  71. # error:
  72. grass.error("No map found")
  73. # fatal error:
  74. grass.fatal_error("No map found, exiting")
  75. # debug output (use g.gisenv to enable/disable)
  76. grass.debug("Our calculated value is: %d" % value)
  77. Try to omit any usage of the 'print' command for informational output.
  78. 8. PLEASE take the time to add comments throughout your code explaining what
  79. the code is doing. It will save a HUGE amount of time and frustration for
  80. other programmers that may have to change your code in the future.
  81. 9. Make sure a new line is at the end of each file.
  82. 10. For consistency, use README rather than README.txt for any README files.
  83. 11. Be sure to develop on top of the LATEST GRASS code (which is in SVN repository).
  84. You can re-check before submission with 'svn diff':
  85. Be sure to create unified ("diff -u") format. "Plain" diffs (the default
  86. format) are risky, because they will apply without warning to code which
  87. has been substantially changed; they are also harder to read than unified.
  88. Such diffs should be made from the top-level directory, e.g.
  89. "svn diff gui/wxpython/wxgui.py"; that way, the diff will
  90. include the pathname rather than just "wxgui.py".
  91. 12. When submitting new files to the repository set SVN properties,
  92. usually for directory
  93. svn:ignore : *.pyc
  94. or e.g. for Python file
  95. svn:mime-type : text/python
  96. svn:keywords : Author Date Id
  97. svn:eol-style : native
  98. See
  99. http://svnbook.red-bean.com/en/1.4/svn.advanced.props.html
  100. 13. wxGUI (gui/wxpython)
  101. See http://wiki.wxpython.org/wxPython_Style_Guide
  102. Major rules:
  103. - use named parameters in functions, e.g.
  104. dlg = wx.FileDialog(parent = self, message = _("Choose file to save current workspace"),
  105. wildcard = _("GRASS Workspace File (*.gxw)|*.gxw"), style = wx.FD_SAVE)
  106. instead of
  107. dlg = wx.FileDialog(self, _("Choose file to save current workspace"),
  108. _("GRASS Workspace File (*.gxw)|*.gxw"), wx.FD_SAVE)
  109. - use wx.ID_ANY instead of `-1`
  110. - use gcmd.GError(), gcmd.GWarning and gcmd.GMessage instead of wx.MessageBox()
  111. - use gcmd.RunCommand() instead of grass.run_command() or grass.read_command()
  112. - use full strings, eg.
  113. if ...:
  114. win.SetLabel(_('Name for new 3D raster map to create'))
  115. else:
  116. win.SetLabel(_('Name for new raster map to create'))
  117. instead of
  118. _('Name for new %s to create') % maplabel
  119. where `maplabel` can be 'raster map' or '3D raster map'
  120. 14. Tell the other developers about the new code using the following e-mail:
  121. grass-dev@lists.osgeo.org
  122. To subscribe to this mailing list, see
  123. http://lists.osgeo.org/mailman/listinfo/grass-dev
  124. 15. In case of questions feel free to contact the developers at the above
  125. mailing list.
  126. http://grass.osgeo.org/devel/index.php#submission
  127. ...
  128. [please add further hints if required]
  129. "Your attention to detail is appreciated."