-
Notifications
You must be signed in to change notification settings - Fork 7
/
HelpInterface.py
194 lines (177 loc) · 8.93 KB
/
HelpInterface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
################################################################################
################################################################################
###############################################################################
# Copyright (C) 2013-2018 Jacob Barhak
# Copyright (C) 2009-2012 The Regents of the University of Michigan
#
# This file is part of the MIcroSimulation Tool (MIST).
# The MIcroSimulation Tool (MIST) is free software: you
# can redistribute it and/or modify it under the terms of the GNU General
# Public License as published by the Free Software Foundation, either
# version 3 of the License, or (at your option) any later version.
#
# The MIcroSimulation Tool (MIST) is distributed in the
# hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
###############################################################################
#
# ADDITIONAL CLARIFICATION
#
# The MIcroSimulation Tool (MIST) is distributed in the
# hope that it will be useful, but "as is" and WITHOUT ANY WARRANTY of any
# kind, including any warranty that it will not infringe on any property
# rights of another party or the IMPLIED WARRANTIES OF MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS assume no responsibilities
# with respect to the use of the MIcroSimulation Tool (MIST).
#
# The MIcroSimulation Tool (MIST) was derived from the Indirect Estimation
# and Simulation Tool (IEST) and uses code distributed under the IEST name.
# The change of the name signifies a split from the original design that
# focuses on microsimulation. For the sake of completeness, the copyright
# statement from the original tool developed by the University of Michigan
# is provided below and is also mentioned above.
#
###############################################################################
############################ Original Copyright ###############################
###############################################################################
# Copyright (C) 2009-2011 The Regents of the University of Michigan
# Initially developed by Deanna Isaman, Jacob Barhak
#
# This file is part of the Indirect Estimation and Simulation Tool (IEST).
# The Indirect Estimation and Simulation Tool (IEST) is free software: you
# can redistribute it and/or modify it under the terms of the GNU General
# Public License as published by the Free Software Foundation, either
# version 3 of the License, or (at your option) any later version.
#
# The Indirect Estimation and Simulation Tool (IEST) is distributed in the
# hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
################################################################################
# #
# This file contains the documentation interface. #
################################################################################
# usage example of public methods/members:
# >>> import doc
# >>> HelpClass = Documentation()
# >>> HelpClass.openDoc('modelexample.jpg')
# >>> HelpClass.openDoc(node = 'States')
# >>> if not HelpClass.error: print "It's working!"
# It's working!
import webbrowser, os
class Documentation:
def __init__(this, docPath=None, docTop='index.html', split=True, useDict=True):
# docPath: string, full directory path to documentation files
# docTop: string, top HTML documentation file
# split: Boolean, files are "split" if there is only one node per HTML file
docPathDefault = os.path.join(os.getcwd(), 'Documentation')
this.docPath = docPath or docPathDefault
this.docTop = docTop
this.error = None
this.split = split
this.useDict = useDict
if this.useDict:
this.initNodeDict()
# this is mostly to check if the path is valid
try:
open(os.path.join(this.docPath, this.docTop), 'r').close()
except IOError:
try:
open(os.path.join(docPathDefault, this.docTop), 'r').close()
this.docPath = docPathDefault
except:
this.raiseError('Documentation Error: Documentation top file ' + this.docTop + ' cannot be located/opened.')
except:
this.raiseError('Documentation Error: Unexpected documentation error.')
def openDoc(this, filename=None, node=None):
"""Launches the documentation in the default web browser. Returns true if file can be opened, false otherwise."""
# filename: string, file to be opened
# node: string, similar to chapter name
if this.split:
# multiple HTML files
if filename or node:
# filename or node given
openFile = filename or node + '.html'
try:
open(os.path.join(this.docPath, openFile), 'r').close()
# all good
except:
if this.useDict and node and node in this.nodeDict:
newNode = this.nodeDict[node]
# recursively call this function with the node name from the dictionary
return this.openDoc(node = newNode)
else:
this.raiseError('Documentation Error: Documentation file ' + openFile + ' cannot be located/opened.')
if openFile == this.docTop:
return False
else:
# try doc top file recursively
return this.openDoc(filename = this.docTop)
else:
# use doc top file
return this.openDoc(filename = this.docTop)
else:
# single HTML file
if filename:
try:
open(os.path.join(this.docPath, filename), 'r').close()
# file opened successfully
openFile = filename
except:
this.raiseError('Documentation Error: Documentation file ' + filename + ' cannot be located/opened; trying top file.')
try:
# try doc top file
open(os.path.join(this.docPath, this.docTop), 'r').close()
openFile = this.docTop
except:
this.raiseError('Documentation Error: Documentation file ' + this.docTop + ' cannot be located/opened.')
return False
else:
# no filename given; try doc top file
try:
open(os.path.join(this.docPath, this.docTop), 'r').close()
openFile = this.docTop
except:
this.raiseError('Documentation Error: Documentation file ' + this.docTop + ' cannot be located/opened.')
return False
if node:
# append node link
if this.useDict and node in this.nodeDict:
openFile = openFile + '#' + this.nodeDict[node]
else:
openFile = openFile + '#' + node
openFile = openFile.replace('/','%2f') # encode page slashes, such as 'Study/Model'
openFile = openFile.replace(' ', '%20') # encode spaces
url = os.path.join(this.docPath, openFile)
print 'Opening file: ' + url
webbrowser.open_new(url)
this.error = None
return True
def raiseError(this, message):
"""Used for non-fatal Documentation class errors. Message is a string."""
print message
this.error = message
def initNodeDict(this):
"""Initializes the dictionary that maps a node argument to the
actual documentation node. This essentially creates a back-end index."""
# To add a new mapping:
# Add another tuple to the dictionary below, using the request node
# as the Key, and the HTML filename as the value.
this.nodeDict = {'StudyModels' : 'Study_002fModel',
'Transitions' : 'Transitions',
'PopulationSets' : 'Populations',
'Params' : 'Parameters',
'SimulationResults' : 'Simulation',
'ReportViewer' : 'Reports',
'Wizard' : 'Simulation',
'PopulationData' : 'Populations',
'Main':'Getting-Started-with-IEST'}
if __name__ == "__main__":
d = Documentation()
print d.openDoc(node = 'States')
print d.openDoc(node = 'Params')
if d.error:
print 'Oh dear...'
else:
print 'It seems to be working swimmingly.'