g.manual.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 <%s> for module %s...") % (browser_name, entry))
  53. os.execlp(browser, browser_name, "file://%s" % (path))
  54. grass.fatal(_("Error starting browser <%s> for HTML file <%s>") % (browser, entry))
  55. def start_man(entry):
  56. path = os.path.join(gisbase, 'docs', 'man', 'man1', entry + '.1')
  57. if not os.path.exists(path) and os.getenv('GRASS_ADDON_BASE'):
  58. path = os.path.join(os.getenv('GRASS_ADDON_BASE'), 'docs', 'man', 'man1', entry + '.1')
  59. for ext in ['', '.gz', '.bz2']:
  60. if os.path.exists(path + ext):
  61. os.execlp('man', 'man', path + ext)
  62. grass.fatal(_("Error starting 'man' for <%s>") % path)
  63. grass.fatal(_("No manual page entry for <%s>") % entry)
  64. def main():
  65. global gisbase, browser, browser_name
  66. if flags['i'] and flags['t']:
  67. grass.fatal(_("Flags -%c and -%c are mutually exclusive") % ('i', 't'))
  68. special = None
  69. if flags['i']:
  70. special = 'index'
  71. elif flags ['t']:
  72. special = 'topics'
  73. if flags['m']:
  74. start = start_man
  75. else:
  76. start = start_browser
  77. entry = options['entry']
  78. gisbase = os.environ['GISBASE']
  79. browser = os.getenv('GRASS_HTML_BROWSER', '')
  80. if sys.platform == 'darwin':
  81. # hack for MacOSX
  82. browser_name = os.getenv('GRASS_HTML_BROWSER_MACOSX', '..').split('.')[2]
  83. elif sys.platform == 'cygwin':
  84. # hack for Cygwin
  85. browser_name = grass.basename(browser, 'exe')
  86. else:
  87. browser_name = grass.basename(browser)
  88. # keep order!
  89. # first test for index...
  90. if special:
  91. start(special)
  92. else:
  93. start(entry)
  94. return 0
  95. if __name__ == "__main__":
  96. options, flags = grass.parser()
  97. sys.exit(main())