source: sasview/src/sas/sasgui/guiframe/documentation_window.py @ 60e20d8

ESS_GUIESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_opencl
Last change on this file since 60e20d8 was b963b20, checked in by Paul Kienzle <pkienzle@…>, 7 years ago

pull config out of sas.sasgui so it can be used without reference to wx

  • Property mode set to 100644
File size: 5.4 KB
Line 
1"""
2documentation module provides a simple means to add help throughout the
3application. It checks for the existence of html2 package needed to support
4fully html panel which supports css.  The class defined here takes a title for
5the particular help panel, a pointer to the html documentation file of interest
6within the documentation tree along with a 'command' string such as a page
7anchor or a query string etc.  The path to the doc directory is retrieved
8automatically by the class itself.  Thus with these three pieces of information
9the class generates a panel with the appropriate title bar and help file
10formatted according the style sheets called in the html file.  Finally, if an
11old version of Python is running and the html2 package is not available the
12class brings up the default browser and passes the file:/// string to it.  In
13this case however the instruction portion is usually not passed for security
14reasons.
15"""
16import os
17import logging
18import webbrowser
19import urllib
20import sys
21
22import wx
23try:
24    import wx.html2 as html
25    WX_SUPPORTS_HTML2 = True
26except ImportError:
27    WX_SUPPORTS_HTML2 = False
28
29from sas import get_app_dir
30
31# Don't use wx html renderer on windows.
32if os.name == 'nt':
33    WX_SUPPORTS_HTML2 = False
34
35logger = logging.getLogger(__name__)
36
37SPHINX_DOC_ENV = "SASVIEW_DOC_PATH"
38
39THREAD_STARTED = False
40def start_documentation_server(doc_root, port):
41    import thread
42    global THREAD_STARTED
43    if not THREAD_STARTED:
44        thread.start_new_thread(_documentation_server, (doc_root, port))
45        THREAD_STARTED = True
46
47def _documentation_server(doc_root, port):
48    from SimpleHTTPServer import SimpleHTTPRequestHandler
49    from SocketServer import TCPServer
50
51    os.chdir(doc_root)
52    httpd = TCPServer(("127.0.0.1", port), SimpleHTTPRequestHandler, bind_and_activate=False)
53    httpd.allow_reuse_address = True
54    try:
55        httpd.server_bind()
56        httpd.server_activate()
57        httpd.serve_forever()
58    finally:
59        httpd.server_close()
60
61class DocumentationWindow(wx.Frame):
62    """
63    DocumentationWindow inherits from wx.Frame and provides a centralized
64    coherent framework for all help documentation.  Help files must be html
65    files stored in an properly organized tree below the top 'doc' folder.  In
66    order to display the appropriate help file from anywhere in the gui, the
67    code simply needs to know the location below the top level where the
68    help file resides along with the name of the help file.
69    called
70    (self, parent, dummy_id, path, url_instruction, title, size=(850, 540))
71
72    :param path: path to html file beginning AFTER /doc/ and ending in the\
73    file.html.
74    :param url_instructions: anchor string or other query e.g. '#MyAnchor'
75    :param title: text to place in the title bar of the help panel
76    """
77    def __init__(self, parent, dummy_id, path, url_instruction, title, size=(850, 540)):
78        wx.Frame.__init__(self, parent, dummy_id, title, size=size)
79
80        if SPHINX_DOC_ENV in os.environ:
81            docs_path = os.path.join(os.environ[SPHINX_DOC_ENV])
82        else:
83            # For the installer, docs are in a top-level directory.  We're not
84            # bothering to worry about docs when running using the old
85            # (non - run.py) way.
86            docs_path = os.path.join(get_app_dir(), "doc")
87
88        #note that filepath for mac OS, at least in bundle starts with a
89        #forward slash as /Application, while the PC string begins C:\
90        #It seems that mac OS is happy with four slashes as in file:////App...
91        #Two slashes is not sufficient to indicate path from root.  Thus for now
92        #we use "file:///" +... If the mac behavior changes may need to make the
93        #file:/// be another constant at the beginning that yields // for Mac
94        #and /// for PC.
95        #Note added June 21, 2015     PDB
96        file_path = os.path.join(docs_path, path)
97        if path.startswith('http'):
98            url = path
99        elif not os.path.exists(file_path):
100            url = "index.html"
101            logger.error("Could not find Sphinx documentation at %s -- has it been built?",
102                         file_path)
103        elif False:
104            start_documentation_server(docs_path, port=7999)
105            url = "http://127.0.0.1:7999/" + path.replace('\\', '/') + url_instruction
106        else:
107            url = "file:///" + urllib.quote(file_path, r'/\:')+ url_instruction
108
109        #logger.info("showing url " + url)
110        if WX_SUPPORTS_HTML2:
111            # Complete HTML/CSS support!
112            self.view = html.WebView.New(self)
113            self.view.LoadURL(url)
114            self.Bind(html.EVT_WEBVIEW_ERROR, self.OnError, self.view)
115            self.Show()
116        else:
117            logger.error("No html2 support, popping up a web browser")
118            #For cases that do not build against current version dependency
119            # Wx 3.0 we provide a webbrowser call - this is particularly for
120            #Red hat used at SNS for which Wx 3.0 is not available.  This
121            #does not deal with issue of math in docs of course.
122            webbrowser.open_new_tab(url)
123
124    def OnError(self, evt):
125        logger.error("%d: %s", evt.GetInt(), evt.GetString())
126
127def main():
128    """
129    main loop function if running alone for testing.
130    """
131    url = "index.html" if len(sys.argv) <= 1 else sys.argv[1]
132    app = wx.App()
133    DocumentationWindow(None, -1, url, "", "Documentation",)
134    app.MainLoop()
135
136if __name__ == '__main__':
137    main()
Note: See TracBrowser for help on using the repository browser.