source: sasview/src/sas/sasgui/guiframe/documentation_window.py @ 914ba0a

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalcmagnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 914ba0a was 914ba0a, checked in by Paul Kienzle <pkienzle@…>, 7 years ago

merge with master

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