g.manual.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #% suppress_required: yes
  25. #%end
  26. #%flag
  27. #% key: m
  28. #% description: Display as MAN text page instead of HTML page in browser
  29. #%end
  30. #%option
  31. #% key: entry
  32. #% type: string
  33. #% description: Manual entry to be displayed
  34. #% required : yes
  35. #%end
  36. import sys
  37. import os
  38. from grass.script import core as grass
  39. def start_browser(entry):
  40. if browser != 'xdg-open' and not grass.find_program(browser):
  41. grass.fatal(_("Browser <%s> not found") % browser)
  42. path = os.path.join(gisbase, 'docs', 'html', entry + '.html')
  43. if not os.path.exists(path):
  44. grass.fatal(_("No HTML manual page entry for <%s>") % entry)
  45. grass.verbose(_("Starting browser <%s> for module %s...") % (browser_name, entry))
  46. os.execlp(browser, browser_name, "file://%s/docs/html/%s.html" % (gisbase, entry))
  47. grass.fatal(_("Error starting browser <%s> for HTML file <%s>") % (browser, entry))
  48. def start_man(entry):
  49. path = os.path.join(gisbase, 'man', 'man1', entry + '.1')
  50. for ext in ['', '.gz', '.bz2']:
  51. if os.path.exists(path + ext):
  52. os.execlp('man', 'man', path + ext)
  53. grass.fatal(_("Error starting 'man' for <%s>") % path)
  54. grass.fatal(_("No manual page entry for <%s>") % entry)
  55. def main():
  56. global gisbase, browser, browser_name
  57. index = flags['i']
  58. manual = flags['m']
  59. entry = options['entry']
  60. gisbase = os.environ['GISBASE']
  61. browser = os.getenv('GRASS_HTML_BROWSER')
  62. if sys.platform == 'darwin':
  63. # hack for MacOSX
  64. browser_name = os.getenv('GRASS_HTML_BROWSER_MACOSX', '..').split('.')[2]
  65. elif sys.platform == 'cygwin':
  66. # hack for Cygwin
  67. browser_name = grass.basename(browser, 'exe')
  68. else:
  69. browser_name = grass.basename(browser)
  70. # keep order!
  71. # first test for index...
  72. if index:
  73. if manual:
  74. start_man('index')
  75. else:
  76. start_browser('index')
  77. return 0
  78. if manual:
  79. start_man(entry)
  80. else:
  81. start_browser(entry)
  82. return 0
  83. if __name__ == "__main__":
  84. options, flags = grass.parser()
  85. sys.exit(main())