<commit_before>"""
    Classes and function for creating and starting web server
"""
import asyncio
from aiohttp import web

from command_line import define, parse_command_line


__all__ = ('init_server', 'run_server')


@asyncio.coroutine
def init_server(loop, ip, port, router=None):
    """
        Initialize instance of web server

        Args:
            ip - used IP
            port - listened port on web server
            router - instance of RestWSRouter class with user defined endpoints
    """
    app = web.Application(loop=loop, router=router)
    srv = yield from loop.create_server(app.make_handler(), str(ip), int(port))
    print("Server started at http://{0}:{1}".format(ip, port))
    return srv


def run_server(router=None):
    """
        Create and start web server with typed IP and PORT via command line

        Args:
            router - instance of RestWSRouter class with user defined endpoints
    """
    define('-ip', default='127.0.0.1', type=str, help="used ip for server")
    define('-port', default=8080, type=int, help="listened port on the server")
    args = parse_command_line()
    loop = asyncio.get_event_loop()
    server_instance = init_server(loop, args.ip, args.port, router)
    loop.run_until_complete(server_instance)
    loop.run_forever()
<commit_msg>Rewrite functions<commit_after>"""
    Classes and function for creating and starting web server
"""
import asyncio
from aiohttp import web


__all__ = ('init_server', 'deinit_server', 'run_server')


@asyncio.coroutine
def init_server(loop, ip, port, router=None):
    """
        Initialize instance of web server

        Args:
            ip - used IP
            port - listened port on web server
            router - instance of RestWSRouter class with user defined endpoints
    """
    app = web.Application(loop=loop, router=router)
    app['sockets'] = []
    handler = app.make_handler()
    srv = yield from loop.create_server(handler, str(ip), int(port))
    print("Server started at http://{0}:{1}".format(ip, port))
    return app, srv, handler


@asyncio.coroutine
def deinit_server(app, server, handler):
    """
        Deinitialize instance of web server
    """
    for ws in app['sockets']:
        ws.close()
    app['sockets'].clear()
    yield from asyncio.sleep(0.1)
    server.close()
    yield from handler.finish_connections()
    yield from server.wait_closed()


def run_server(ip='127.0.0.1', port=8080, router=None):
    """
        Create and start web server with some IP and PORT

        Args:
            ip - used IP for server
            port - listened port
            router - instance of RestWSRouter
    """
    loop = asyncio.get_event_loop()
    server_instance = init_server(loop, ip, port, router)
    app, srv, handler = loop.run_until_complete(server_instance)
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        loop.run_until_complete(deinit_server(app, srv, handler))

{'fraction_non_alphanumeric': 0.10622462787550745, 'fraction_numerical': 0.008795669824086604, 'mean_word_length': 2.9638069705093835, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3785462', 'n_tokens_mistral': 887, 'n_tokens_neox': 864, 'n_words': 264}