g.manual.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/usr/bin/env python3
  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-2015 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. # % keyword: general
  18. # % keyword: manual
  19. # % keyword: 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. # %flag
  36. # % key: o
  37. # % label: Display online manuals instead of locally installed
  38. # % description: Use online manuals available at https://grass.osgeo.org website. This flag has no effect when displaying MAN text pages.
  39. # %end
  40. # %option
  41. # % key: entry
  42. # % type: string
  43. # % description: Manual entry to be displayed
  44. # % required : yes
  45. # %end
  46. import sys
  47. import os
  48. try:
  49. from urllib2 import urlopen
  50. except ImportError:
  51. # python3
  52. from urllib.request import urlopen
  53. import webbrowser
  54. from grass.script.utils import basename
  55. from grass.script import core as grass
  56. def start_browser(entry):
  57. if (
  58. browser
  59. and browser not in ("xdg-open", "start")
  60. and not grass.find_program(browser)
  61. ):
  62. grass.fatal(_("Browser '%s' not found") % browser)
  63. if flags["o"]:
  64. major, minor, patch = grass.version()["version"].split(".")
  65. url_path = "https://grass.osgeo.org/grass%s%s/manuals/%s.html" % (
  66. major,
  67. minor,
  68. entry,
  69. )
  70. if urlopen(url_path).getcode() != 200:
  71. url_path = "https://grass.osgeo.org/grass%s%s/manuals/addons/%s.html" % (
  72. major,
  73. minor,
  74. entry,
  75. )
  76. else:
  77. path = os.path.join(gisbase, "docs", "html", entry + ".html")
  78. if not os.path.exists(path) and os.getenv("GRASS_ADDON_BASE"):
  79. path = os.path.join(
  80. os.getenv("GRASS_ADDON_BASE"), "docs", "html", entry + ".html"
  81. )
  82. if not os.path.exists(path):
  83. grass.fatal(_("No HTML manual page entry for '%s'") % entry)
  84. url_path = "file://" + path
  85. if browser and browser not in ("xdg-open", "start"):
  86. webbrowser.register(browser_name, None)
  87. grass.verbose(
  88. _("Starting browser '%(browser)s' for manual" " entry '%(entry)s'...")
  89. % dict(browser=browser_name, entry=entry)
  90. )
  91. try:
  92. webbrowser.open(url_path)
  93. except Exception:
  94. grass.fatal(
  95. _("Error starting browser '%(browser)s' for HTML file" " '%(path)s'")
  96. % dict(browser=browser, path=path)
  97. )
  98. def start_man(entry):
  99. path = os.path.join(gisbase, "docs", "man", "man1", entry + ".1")
  100. if not os.path.exists(path) and os.getenv("GRASS_ADDON_BASE"):
  101. path = os.path.join(
  102. os.getenv("GRASS_ADDON_BASE"), "docs", "man", "man1", entry + ".1"
  103. )
  104. for ext in ["", ".gz", ".bz2"]:
  105. if os.path.exists(path + ext):
  106. os.execlp("man", "man", path + ext)
  107. grass.fatal(_("Error starting 'man' for '%s'") % path)
  108. grass.fatal(_("No manual page entry for '%s'") % entry)
  109. def main():
  110. global gisbase, browser, browser_name
  111. if flags["i"] and flags["t"]:
  112. grass.fatal(_("Flags -%c and -%c are mutually exclusive") % ("i", "t"))
  113. special = None
  114. if flags["i"]:
  115. special = "index"
  116. elif flags["t"]:
  117. special = "topics"
  118. if flags["m"]:
  119. start = start_man
  120. else:
  121. start = start_browser
  122. entry = options["entry"]
  123. gisbase = os.environ["GISBASE"]
  124. browser = os.getenv("GRASS_HTML_BROWSER", "")
  125. if sys.platform == "darwin":
  126. # hack for MacOSX
  127. browser_name = os.getenv("GRASS_HTML_BROWSER_MACOSX", "..").split(".")[2]
  128. elif sys.platform == "cygwin":
  129. # hack for Cygwin
  130. browser_name = basename(browser, "exe")
  131. else:
  132. browser_name = basename(browser)
  133. # keep order!
  134. # first test for index...
  135. if special:
  136. start(special)
  137. else:
  138. start(entry)
  139. return 0
  140. if __name__ == "__main__":
  141. options, flags = grass.parser()
  142. sys.exit(main())