g.manual.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 manual pages of GRASS modules
  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) and os.getenv('GRASS_ADDON_BASE'):
  44. path = os.path.join(os.getenv('GRASS_ADDON_BASE'), 'docs', 'html', entry + '.html')
  45. if not os.path.exists(path):
  46. grass.fatal(_("No HTML manual page entry for <%s>") % entry)
  47. grass.verbose(_("Starting browser <%s> for module %s...") % (browser_name, entry))
  48. os.execlp(browser, browser_name, "file://%s" % (path))
  49. grass.fatal(_("Error starting browser <%s> for HTML file <%s>") % (browser, entry))
  50. def start_man(entry):
  51. path = os.path.join(gisbase, 'docs', 'man', 'man1', entry + '.1')
  52. if not os.path.exists(path) and os.getenv('GRASS_ADDON_BASE'):
  53. path = os.path.join(os.getenv('GRASS_ADDON_BASE'), 'docs', 'man', 'man1', entry + '.1')
  54. for ext in ['', '.gz', '.bz2']:
  55. if os.path.exists(path + ext):
  56. os.execlp('man', 'man', path + ext)
  57. grass.fatal(_("Error starting 'man' for <%s>") % path)
  58. grass.fatal(_("No manual page entry for <%s>") % entry)
  59. def main():
  60. global gisbase, browser, browser_name
  61. index = flags['i']
  62. manual = flags['m']
  63. entry = options['entry']
  64. gisbase = os.environ['GISBASE']
  65. browser = os.getenv('GRASS_HTML_BROWSER')
  66. if sys.platform == 'darwin':
  67. # hack for MacOSX
  68. browser_name = os.getenv('GRASS_HTML_BROWSER_MACOSX', '..').split('.')[2]
  69. elif sys.platform == 'cygwin':
  70. # hack for Cygwin
  71. browser_name = grass.basename(browser, 'exe')
  72. else:
  73. browser_name = grass.basename(browser)
  74. # keep order!
  75. # first test for index...
  76. if index:
  77. if manual:
  78. start_man('index')
  79. else:
  80. start_browser('index')
  81. return 0
  82. if manual:
  83. start_man(entry)
  84. else:
  85. start_browser(entry)
  86. return 0
  87. if __name__ == "__main__":
  88. options, flags = grass.parser()
  89. sys.exit(main())