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