Skip to content

Commit 9189c4a

Browse files
committed
[scripts] Add odml_view
1 parent ed8c0fe commit 9189c4a

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

odml/scripts/odml_view.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""odmlView
2+
3+
odmlView sets up a minimal webserver and serves
4+
and renders odml files locally from the directory
5+
the server is started in.
6+
7+
Usage: odmlView [-d DIRECTORY] [-p PORT]
8+
9+
Options:
10+
-p PORT Port the server will use.
11+
Default is port 8000.
12+
-h --help Show this screen.
13+
--version Show version.
14+
"""
15+
16+
import http.server as hs
17+
import socketserver
18+
import sys
19+
import webbrowser
20+
21+
from docopt import docopt
22+
23+
PORT = 8000
24+
25+
26+
def run(port=PORT):
27+
handler = hs.SimpleHTTPRequestHandler
28+
# files with odML extensions should be interpreted as XML
29+
handler.extensions_map.update({'.odml': 'application/xml'})
30+
31+
server_address = ('', port)
32+
33+
with socketserver.TCPServer(server_address, handler) as httpd:
34+
webbrowser.open_new_tab('http://localhost:%s' % port)
35+
try:
36+
httpd.serve_forever()
37+
except KeyboardInterrupt:
38+
print("Received Keyboard interrupt, shutting down")
39+
httpd.server_close()
40+
41+
42+
def main(args=None):
43+
parser = docopt(__doc__, argv=args, version="0.1.0")
44+
45+
server_port = int(parser['-p']) if parser['-p'] else PORT
46+
47+
run(server_port)
48+
49+
50+
if __name__ == "__main__":
51+
main(sys.argv[1:])

0 commit comments

Comments
 (0)