g.manual.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: g.manual
  5. # AUTHOR(S): Markus Neteler
  6. # Converted to Python by Glynn Clements
  7. # PURPOSE: Display the HTML/MAN pages
  8. # COPYRIGHT: (C) 2003,2008,2010 by the GRASS Development Team
  9. #
  10. # This program is free software under the GNU General Public
  11. # License (>=v2). Read the file COPYING that comes with GRASS
  12. # for details.
  13. #
  14. #############################################################################
  15. #%Module
  16. #% description: Display the HTML man pages of GRASS GIS
  17. #% keywords: general
  18. #% keywords: manual
  19. #% keywords: help
  20. #%End
  21. #%flag
  22. #% key: i
  23. #% description: Display index
  24. #%End
  25. #%flag
  26. #% key: m
  27. #% description: Display as MAN text page instead of HTML page in browser
  28. #%End
  29. #%option
  30. #% key: entry
  31. #% type: string
  32. #% description: Manual entry to be displayed
  33. #% required : no
  34. #%End
  35. import sys
  36. import os
  37. from grass.script import core as grass
  38. def start_browser(entry):
  39. if not grass.find_program(browser):
  40. grass.fatal(_("Browser <%s> not found") % browser)
  41. path = os.path.join(gisbase, 'docs', 'html', entry + '.html')
  42. if not os.path.exists(path):
  43. grass.fatal(_("No HTML manual page entry for <%s>.") % entry)
  44. verbose = os.getenv("GRASS_VERBOSE")
  45. if not verbose or int(verbose) > 1:
  46. grass.message(_("Starting browser <%s> for module %s...") % (browser_name, entry))
  47. os.execlp(browser, browser_name, "file://%s/docs/html/%s.html" % (gisbase, entry))
  48. grass.fatal(_("Error starting browser <%s> for HTML file <%s>") % (browser, entry))
  49. def start_man(entry):
  50. path = os.path.join(gisbase, 'man', 'man1', entry + '.1')
  51. for ext in ['', '.gz', '.bz2']:
  52. if os.path.exists(path + ext):
  53. os.execlp('man', 'man', path + ext)
  54. grass.fatal(_("Error starting 'man' for <%s>") % path)
  55. grass.fatal(_("No manual page entry for <%s>.") % entry)
  56. def main():
  57. global gisbase, browser, browser_name
  58. index = flags['i']
  59. manual = flags['m']
  60. entry = options['entry']
  61. gisbase = os.environ['GISBASE']
  62. browser = os.getenv('GRASS_HTML_BROWSER')
  63. if "html_browser_mac.sh" in browser:
  64. #hack for MacOSX:
  65. browser_name = os.getenv('GRASS_HTML_BROWSER_MACOSX','..').split('.')[2]
  66. elif os.getenv('OSTYPE') == "cygwin":
  67. #hack for Cygwin:
  68. browser_name = grass.basename(browser, 'exe')
  69. else:
  70. browser_name = grass.basename(browser)
  71. #keep order!
  72. #first test for index...
  73. if index:
  74. if manual:
  75. start_man('index')
  76. else:
  77. start_browser('index')
  78. sys.exit(0)
  79. #... then for help parameter:
  80. if not entry:
  81. grass.run_command('g.manual', '--help')
  82. sys.exit(0)
  83. if manual:
  84. start_man(entry)
  85. else:
  86. start_browser(entry)
  87. if __name__ == "__main__":
  88. options, flags = grass.parser()
  89. main()