diff options
| author | bozokopic <bozo.kopic@gmail.com> | 2017-04-24 23:40:00 +0200 |
|---|---|---|
| committer | bozokopic <bozo.kopic@gmail.com> | 2017-04-24 23:40:00 +0200 |
| commit | 085b236d9a169a1815a6117caab5894521e59672 (patch) | |
| tree | 3d3220936a877fbe75206e2effc581c548d568ec /src_py | |
| parent | ed9f4f8991f4e8b31270dbbf17215732d73c9e37 (diff) | |
heroku init
Diffstat (limited to 'src_py')
| -rw-r--r-- | src_py/opcut/main.py | 160 | ||||
| -rw-r--r-- | src_py/opcut/util.py | 80 |
2 files changed, 120 insertions, 120 deletions
diff --git a/src_py/opcut/main.py b/src_py/opcut/main.py index d6b42a9..917cbb2 100644 --- a/src_py/opcut/main.py +++ b/src_py/opcut/main.py @@ -1,80 +1,80 @@ -import sys
-import argparse
-import yaml
-import logging.config
-import urllib.parse
-import aiohttp.web
-import ssl
-import asyncio
-import contextlib
-
-from opcut import util
-import opcut.json_validator
-
-
-def main():
- args = _create_parser().parse_args()
-
- if args.log_conf_path:
- with open(args.log_conf_path, encoding='utf-8') as log_conf_file:
- log_conf = yaml.safe_load(log_conf_file)
- opcut.json_validator.validate(log_conf, 'opcut://logging.yaml#')
- logging.config.dictConfig(log_conf)
-
- util.run_until_complete_without_interrupt(async_main(args))
-
-
-async def async_main(args):
-
- addr = urllib.parse.urlparse(args.ui_addr)
-
- if addr.scheme == 'https':
- ssl_ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
- ssl_ctx.load_cert_chain(args.ui_pem_path)
- else:
- ssl_ctx = None
-
- app = aiohttp.web.Application()
- app.router.add_route('GET', '/',
- lambda req: aiohttp.web.HTTPFound('/index.html'))
- app.router.add_static('/', args.ui_path)
- app_handler = app.make_handler()
-
- srv = await asyncio.get_event_loop().create_server(
- app_handler, host=addr.hostname, port=addr.port, ssl=ssl_ctx)
-
- with contextlib.suppress(asyncio.CancelledError):
- await asyncio.Future()
-
- srv.close()
- await srv.wait_closed()
- await app.shutdown()
- await app_handler.finish_connections(0)
- await app.cleanup()
-
-
-def _create_parser():
- parser = argparse.ArgumentParser(prog='opcut')
- parser.add_argument(
- '--ui-addr', default='http://0.0.0.0:8080',
- metavar='addr', dest='ui_addr',
- help="address of listening web ui socket formated as "
- "'<type>://<host>:<port>' - <type> is 'http' or 'https'; "
- "<host> is hostname; <port> is tcp port number "
- "(default http://0.0.0.0:8080)")
- parser.add_argument(
- '--ui-path', default='web',
- metavar='path', dest='ui_path',
- help="web front-end path (default web)")
- parser.add_argument(
- '--ui-pem', default=None,
- metavar='path', dest='ui_pem_path',
- help="web front-end pem file path - required for https")
- parser.add_argument(
- '--log', default=None, metavar='path', dest='log_conf_path',
- help="logging configuration")
- return parser
-
-
-if __name__ == '__main__':
- sys.exit(main())
+import sys +import argparse +import yaml +import logging.config +import urllib.parse +import aiohttp.web +import ssl +import asyncio +import contextlib + +from opcut import util +import opcut.json_validator + + +def main(): + args = _create_parser().parse_args() + + if args.log_conf_path: + with open(args.log_conf_path, encoding='utf-8') as log_conf_file: + log_conf = yaml.safe_load(log_conf_file) + opcut.json_validator.validate(log_conf, 'opcut://logging.yaml#') + logging.config.dictConfig(log_conf) + + util.run_until_complete_without_interrupt(async_main(args)) + + +async def async_main(args): + + addr = urllib.parse.urlparse(args.ui_addr) + + if addr.scheme == 'https': + ssl_ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23) + ssl_ctx.load_cert_chain(args.ui_pem_path) + else: + ssl_ctx = None + + app = aiohttp.web.Application() + app.router.add_route('GET', '/', + lambda req: aiohttp.web.HTTPFound('/index.html')) + app.router.add_static('/', args.ui_path) + app_handler = app.make_handler() + + srv = await asyncio.get_event_loop().create_server( + app_handler, host=addr.hostname, port=addr.port, ssl=ssl_ctx) + + with contextlib.suppress(asyncio.CancelledError): + await asyncio.Future() + + srv.close() + await srv.wait_closed() + await app.shutdown() + await app_handler.finish_connections(0) + await app.cleanup() + + +def _create_parser(): + parser = argparse.ArgumentParser(prog='opcut') + parser.add_argument( + '--ui-addr', default='http://0.0.0.0:8080', + metavar='addr', dest='ui_addr', + help="address of listening web ui socket formated as " + "'<type>://<host>:<port>' - <type> is 'http' or 'https'; " + "<host> is hostname; <port> is tcp port number " + "(default http://0.0.0.0:8080)") + parser.add_argument( + '--ui-path', default='web', + metavar='path', dest='ui_path', + help="web front-end path (default web)") + parser.add_argument( + '--ui-pem', default=None, + metavar='path', dest='ui_pem_path', + help="web front-end pem file path - required for https") + parser.add_argument( + '--log', default=None, metavar='path', dest='log_conf_path', + help="logging configuration") + return parser + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/src_py/opcut/util.py b/src_py/opcut/util.py index 4855bc5..3ceb843 100644 --- a/src_py/opcut/util.py +++ b/src_py/opcut/util.py @@ -1,40 +1,40 @@ -import contextlib
-import asyncio
-import sys
-
-
-def run_until_complete_without_interrupt(future):
- """Run event loop until future or coroutine is done
-
- Args:
- future (Awaitable): future or coroutine
-
- Returns:
- Any: provided future's result
-
- KeyboardInterrupt is suppressed (while event loop is running) and is mapped
- to single cancelation of running task. If multipple KeyboardInterrupts
- occur, task is canceled only once.
-
- """
- async def ping_loop():
- with contextlib.suppress(asyncio.CancelledError):
- while True:
- await asyncio.sleep(1)
-
- task = asyncio.ensure_future(future)
- if sys.platform == 'win32':
- ping_loop_task = asyncio.ensure_future(ping_loop())
- with contextlib.suppress(KeyboardInterrupt):
- asyncio.get_event_loop().run_until_complete(task)
- asyncio.get_event_loop().call_soon(task.cancel)
- if sys.platform == 'win32':
- asyncio.get_event_loop().call_soon(ping_loop_task.cancel)
- while not task.done():
- with contextlib.suppress(KeyboardInterrupt):
- asyncio.get_event_loop().run_until_complete(task)
- if sys.platform == 'win32':
- while not ping_loop_task.done():
- with contextlib.suppress(KeyboardInterrupt):
- asyncio.get_event_loop().run_until_complete(ping_loop_task)
- return task.result()
+import contextlib +import asyncio +import sys + + +def run_until_complete_without_interrupt(future): + """Run event loop until future or coroutine is done + + Args: + future (Awaitable): future or coroutine + + Returns: + Any: provided future's result + + KeyboardInterrupt is suppressed (while event loop is running) and is mapped + to single cancelation of running task. If multipple KeyboardInterrupts + occur, task is canceled only once. + + """ + async def ping_loop(): + with contextlib.suppress(asyncio.CancelledError): + while True: + await asyncio.sleep(1) + + task = asyncio.ensure_future(future) + if sys.platform == 'win32': + ping_loop_task = asyncio.ensure_future(ping_loop()) + with contextlib.suppress(KeyboardInterrupt): + asyncio.get_event_loop().run_until_complete(task) + asyncio.get_event_loop().call_soon(task.cancel) + if sys.platform == 'win32': + asyncio.get_event_loop().call_soon(ping_loop_task.cancel) + while not task.done(): + with contextlib.suppress(KeyboardInterrupt): + asyncio.get_event_loop().run_until_complete(task) + if sys.platform == 'win32': + while not ping_loop_task.done(): + with contextlib.suppress(KeyboardInterrupt): + asyncio.get_event_loop().run_until_complete(ping_loop_task) + return task.result() |
