SUBMITTING_PYTHON 6.1 KB

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