g.manual.py 3.4 KB

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