diff --git a/pyzebra/cli.py b/pyzebra/cli.py new file mode 100644 index 0000000..68c09c7 --- /dev/null +++ b/pyzebra/cli.py @@ -0,0 +1,61 @@ +import argparse +import logging +import os + +from bokeh.application.application import Application +from bokeh.application.handlers import ScriptHandler +from bokeh.server.server import Server + +logging.basicConfig(format="%(asctime)s %(message)s", level=logging.INFO) +logger = logging.getLogger(__name__) + + +def main(): + """The pyzebra command line interface. + + This is a wrapper around a bokeh server that provides an interface to launch the application, + bundled with the pyzebra package. + """ + app_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "app", "app.py") + + parser = argparse.ArgumentParser( + prog="pyzebra", formatter_class=argparse.ArgumentDefaultsHelpFormatter + ) + + parser.add_argument( + "--port", type=int, default=5006, help="port to listen on for HTTP requests" + ) + + parser.add_argument( + "--allow-websocket-origin", + metavar="HOST[:PORT]", + type=str, + action="append", + default=None, + help="hostname that can connect to the server websocket", + ) + + parser.add_argument( + "--args", + nargs=argparse.REMAINDER, + default=[], + help="command line arguments for the pyzebra application", + ) + + args = parser.parse_args() + + logger.info(app_path) + + handler = ScriptHandler(filename=app_path, argv=args.args) + server = Server( + {"/": Application(handler)}, + port=args.port, + allow_websocket_origin=args.allow_websocket_origin, + ) + + server.start() + server.io_loop.start() + + +if __name__ == "__main__": + main()