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