-
Notifications
You must be signed in to change notification settings - Fork 0
/
Http.py
170 lines (136 loc) · 5.86 KB
/
Http.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
from twisted.web.server import Site
from twisted.web.resource import Resource
from twisted.web.static import File
from twisted.internet import reactor
from twisted.web.resource import NoResource
from twisted.web import server, resource, static
import subprocess, os
import cgi
from calendar import calendar
from itertools import izip
class HTMLPreformattedTextFile(Resource):
def getChild(self, filename, request):
# Should check that the file exists here
# Also make the filename into a fully configured path
# Handle exceptions with error codes.
self.filename= filename
return FileContents(filename)
class FileContents(Resource):
def __init__(self, filename):
Resource.__init__(self)
self.filename= filename
def render_GET(self, request):
with open (self.filename, "r") as htmlFileHandle:
fileContents= htmlFileHandle.read()
return "<html><body><pre>%s</pre></body></html>" % (fileContents)
class Calendar(Resource):
def getChild(self, name, request):
try:
year = int(name)
except ValueError:
return NoResource()
else:
return YearPage(year)
class YearPage(Resource):
def __init__(self, year):
Resource.__init__(self)
self.year = year
def render_GET(self, request):
return "<html><body><pre>%s</pre></body></html>" % (calendar(self.year),)
class FormPage(Resource):
def render_GET(self, request):
return '<html><body><form method="POST"><input name="the-field" type="text" /></form></body></html>'
def render_POST(self, request):
return '<html><body>You submitted: %s</body></html>' % (request.args["the-field"][0])
class FormPageEscaped(Resource):
def render_GET(self, request):
return '<html><body><form method="POST"><input name="the-field" type="text" /></form></body></html>'
def render_POST(self, request):
return '<html><body>You submitted: %s</body></html>' % (cgi.escape(request.args["the-field"][0]),)
class Stop(Resource):
def render_GET(self, request):
reactor.callFromThread(reactor.stop)
# TODO: The Index should be in a file, and also styled with CSS
class Index(Resource):
def pairwise(self, iterable):
"s -> (s0,s1), (s2,s3), (s4, s5), ..."
a = iter(iterable)
return izip(a, a)
def __init__(self, config):
self.config= config
def render_GET(self, request):
outputMeshList= ''
if 'deltamesh' in self.config['outputs']:
if 'png' in self.config['outputs']['deltamesh']:
for png in self.config['outputs']['deltamesh']['png']:
outputMeshList = outputMeshList + "<li><img src='" + self.config['outputs']['outputDirectory'] + "/" + png + "_masked_heat_map.png' /></li>"
if 'mesh' in self.config['outputs']:
if 'png' in self.config['outputs']['mesh']:
for png in self.config['outputs']['mesh']['png']:
outputMeshList = outputMeshList + "<li><img src='" + self.config['outputs']['outputDirectory'] + "/" + png + "_heat_map.png' /></li>"
if 'deltamesh' in self.config['outputs']:
if 'png' in self.config['outputs']['deltamesh']:
for out1, out2 in self.pairwise(self.config['outputs']['deltamesh']['png']):
plotName= 'png' + '_' + out1 + '_' + out2
outputMeshList = outputMeshList + "<li><img src='" + self.config['outputs']['outputDirectory'] + "/" + plotName + "_heat_map.png' /></li>"
return """
<html>
<head><title>Thermonous</title></head>
<body>
<ul>
<li><a href="cal/2015">2015</a></li>
<li><a href="stop">Stop</a></li>
<li><a href="htmlfile/result.html">Matrix with diagnostics</a></li>
<li><a href="htmlfile/diri_AxRHS.html">Matrix solution</a></li>
<li><a href="htmlfile/stackup.html">Stackup</a></li>
%s
</ul>
<!-- <img src="thermpypng/aztecOO_heat_map.png"><br />
<img src="thermpypng/difference_heat_map.png"><br /> -->
</body>
</html>
""" % (outputMeshList)
class Http:
def __init__(self, config):
if config['http']['useHttp'] == 1:
self.config= config
print "Initializing web server"
self.popBrowser= config['http']['popBrowser']
self.port = self.config['http']['httpPort']
self.uri = "http://localhost:" + str(self.port)
self.root = Resource()
self.root.putChild("cal", Calendar())
self.root.putChild("formesc", FormPageEscaped())
self.root.putChild("form", FormPage())
self.root.putChild("stop", Stop())
self.root.putChild("htmlfile", HTMLPreformattedTextFile())
# Can use an absolute path or a relative path here
# pngdir= '/Users/toma/tools/trilinos/pytrilinos/matrixmnodal/thermpypng'
pngdir= 'thermpypng'
self.root.putChild("thermpypng", File(pngdir, defaultType="image/png"))
self.root.putChild("", Index(self.config))
self.factory = Site(self.root)
statusHTTP= reactor.listenTCP(self.port, self.factory)
print "HTTP Server status for listenTCP: " + str(statusHTTP)
# TODO: Add configuration variables for both starting
# the web browser and the server. It should be possible
# to run in a non-interactive batch mode without
# needing to kill the server. In this mode, getting
# an accurate set of profiling statistics and good
# logging is useful.
# Option for starting a server
# Option for popping web browser
# Options for file logging
# Command line option for choosing JSON config files
# Having more than one config file allows distinction
# between simulator setup and problem to be solved.
# This way one sim setups can be used across a suite
# of problems.
def startServer(self):
print "Serving web pages on on " + self.uri
reactor.run()
print "http server has shut down"
def openWebBrowser(self, delay):
if self.popBrowser == True:
cmd= "sleep " + str(delay) + ' ; open "'+ self.uri + '"'
subprocess.Popen(cmd,shell=True)