From 2cdf45cd01b26ebf2f700ea17c1200457278189e Mon Sep 17 00:00:00 2001 From: bspeice Date: Tue, 17 May 2016 16:27:09 -0400 Subject: [PATCH] Move the exception handling to the code that needs it --- src/server.py | 49 +++++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/src/server.py b/src/server.py index 823c9b9..94f44ac 100644 --- a/src/server.py +++ b/src/server.py @@ -10,16 +10,30 @@ from conf_parser import build_configuration # noinspection PyUnresolvedReferences def start_server(server_conf: dict, configurator: Configurator) -> None: + app = configurator.make_wsgi_app() + + port = server_conf['port'] if 'port' in server_conf else cmd_args.port + host = server_conf['host'] if 'host' in server_conf else cmd_args.host + server = make_server(host, port, app) + + server.serve_forever() + +if __name__ == '__main__': + default_rc = join(expanduser('~'), '.repodrc') + parser = argparse.ArgumentParser() + parser.add_argument('--verbose', action='store_true', + help='Run server in verbose mode') + parser.add_argument('--port', type=int, default=10000, + help='Port to use when starting the server') + parser.add_argument('--host', type=str, default='0.0.0.0', + help='Host address to start the server') + parser.add_argument('--configuration', type=str, default=default_rc, + help='Configuration file to start the server') + + cmd_args = parser.parse_args() try: - app = configurator.make_wsgi_app() - - port = server_conf['port'] if 'port' in server_conf else cmd_args.port - host = server_conf['host'] if 'host' in server_conf else cmd_args.host - server = make_server(host, port, app) - - if cmd_args.verbose: - print("Starting server {}:{}".format(host, port)) - server.serve_forever() + server_conf, configurator = build_configuration(cmd_args.configuration) + start_server(server_conf, configurator) except FileNotFoundError: print("Unable to find configuration file. Does {} exist?" .format(cmd_args.configuration)) @@ -35,20 +49,3 @@ def start_server(server_conf: dict, configurator: Configurator) -> None: 'section?') if cmd_args.verbose: print(traceback.format_exc()) - - -if __name__ == '__main__': - default_rc = join(expanduser('~'), '.repodrc') - parser = argparse.ArgumentParser() - parser.add_argument('--verbose', action='store_true', - help='Run server in verbose mode') - parser.add_argument('--port', type=int, default=10000, - help='Port to use when starting the server') - parser.add_argument('--host', type=str, default='0.0.0.0', - help='Host address to start the server') - parser.add_argument('--configuration', type=str, default=default_rc, - help='Configuration file to start the server') - - args = parser.parse_args() - server_conf, configurator = build_configuration(args.configuration) - start_server(server_conf, configurator)