Пример #1
0
async def draw(messages_queue, sending_queue, status_updates_queue):
    root = tk.Tk()

    root.title('Чат Майнкрафтера')

    root_frame = tk.Frame()
    root_frame.pack(fill="both", expand=True)

    status_labels = create_status_panel(root_frame)

    input_frame = tk.Frame(root_frame)
    input_frame.pack(side="bottom", fill=tk.X)

    input_field = tk.Entry(input_frame)
    input_field.pack(side="left", fill=tk.X, expand=True)

    input_field.bind(
        "<Return>",
        lambda event: process_new_message(input_field, sending_queue))

    send_button = tk.Button(input_frame)
    send_button["text"] = "Отправить"
    send_button["command"] = lambda: process_new_message(
        input_field, sending_queue)
    send_button.pack(side="left")

    conversation_panel = ScrolledText(root_frame, wrap='none')
    conversation_panel.pack(side="top", fill="both", expand=True)

    async with aionursery.Nursery() as nursery:
        nursery.start_soon(update_tk(root_frame))
        nursery.start_soon(
            update_conversation_history(conversation_panel, messages_queue))
        nursery.start_soon(
            update_status_panel(status_labels, status_updates_queue))
Пример #2
0
async def create_handy_nursery():
    try:
        async with aionursery.Nursery() as nursery:
            yield nursery
    except aionursery.MultiError as e:
        if len(e.exceptions) == 1:
            raise e.exceptions[0]
        raise
Пример #3
0
    async def test_split_by_words(self):
        morph = pymorphy2.MorphAnalyzer()
        async with aionursery.Nursery() as nursery:
            case1 = await nursery.start_soon(
                split_by_words(morph, 'Во-первых, он хочет, чтобы'))
            case2 = await nursery.start_soon(
                split_by_words(morph, 'удивительно, но это стало началом!'))

        self.assertListEqual(case1, ['во-первых', 'хотеть', 'чтобы'])
        self.assertListEqual(case2, ['удивительно', 'это', 'стать', 'начало'])
Пример #4
0
async def create_handy_nursery() -> aionursery.Nursery:
    try:
        async with aionursery.Nursery() as nursery:
            yield nursery
    except aionursery.MultiError as e:
        if len(e.exceptions) == 1:
            # suppress exception chaining
            # https://docs.python.org/3/reference/simple_stmts.html#the-raise-statement
            raise e.exceptions[0] from None
        raise
Пример #5
0
async def main():
    time_out = 4
    logging.basicConfig(level=logging.WARNING)
    logger = logging.getLogger('main')

    parser = argparse.ArgumentParser(description='connect to secret chat')
    parser.add_argument('--host', default='minechat.dvmn.org', help='Host to connect')
    parser.add_argument('--rport', default=5000, type=int, help='Specify port to receive msg')
    parser.add_argument('--sport', default=5050, type=int, help='Specify port to send msg')
    parser.add_argument('--user', help="set a username, it's oblicated for first run")
    parser.add_argument('--token_file', default="token.txt", help="set a file with token")
    parser.add_argument('--log_file', default="text.txt", help="set file to store text logs")
    args = parser.parse_args()
    try:
        with open(args.token_file, 'r') as _file:
            token = _file.read()
    except FileNotFoundError:
        token = None
    if not args.user and not token:
        logger.error("It's obligated to specidy login if you do not have the correct token file")
        logger.error('exiting')
        sys.exit()
    if args.user:
        user = '******'.format(args.user.replace('\n', ' '))
    else:
        user = None

    messages_to_gui = asyncio.Queue()
    messages_to_file = asyncio.Queue()
    sending_queue = asyncio.Queue()
    status_updates_queue = asyncio.Queue()
    watchdog_queue = asyncio.Queue()

    try:
        with open(args.log_file, 'r') as _file:
            for line in _file.readlines():
                messages_to_gui.put_nowait(line)
    except FileNotFoundError:
        pass

    async with aionursery.Nursery() as nursery:
        nursery.start_soon(gui.draw(messages_to_gui, sending_queue, status_updates_queue))
        nursery.start_soon(save_messages(messages_to_file, messages_to_gui, args.log_file))
        nursery.start_soon(handle_connection(
            messages_to_file,
            status_updates_queue,
            watchdog_queue,
            sending_queue,
            time_out,
            user,
            args.host,
            args.rport,
            args.sport,
            args.token_file,
            token))
Пример #6
0
async def handle(morph, charged_words, request, max_url_to_process=5):
    urls = request.query.get('urls')
    if not urls:
        return json_response(data={'error': 'no articles to process'}, status=400)
    urls_list = urls.split(',')
    if len(urls_list) > max_url_to_process:
        return json_response(data={'error': 'to many articles to process'}, status=400)

    async with ClientSession() as session:
        async with aionursery.Nursery() as nursery:
            tasks = []
            for article in urls_list:
                tasks.append(nursery.start_soon(process_article(article, morph, session, charged_words)))
                results, _ = await asyncio.wait(tasks)
                data = [result.result() for result in results]
            return web.json_response(data, status=200)
Пример #7
0
async def handle_connection(messages_to_file, status_updates_queue, watchdog_queue, sending_queue, time_out, user, host, rport, sport, token_file, token):
    logger = logging.getLogger('watchdog_logger')
    while True:
        try:
            async with aionursery.Nursery() as nursery:
                nursery.start_soon(read_from_socket(host, rport, messages_to_file, status_updates_queue, watchdog_queue))
                nursery.start_soon(send_msgs(host, sport, sending_queue, messages_to_file, status_updates_queue, watchdog_queue, user, token_file, token, time_out))
                nursery.start_soon(watchdog(watchdog_queue, time_out))
        except aionursery.MultiError as err:
            status_updates_queue.put_nowait(gui.SendingConnectionStateChanged.CLOSED)
            status_updates_queue.put_nowait(gui.ReadConnectionStateChanged.CLOSED)
            if isinstance(err.exceptions[0], (asyncio.TimeoutError, gaierror, ConnectionError)):
                logger.error('timeout error')
                await asyncio.sleep(5)
            else:
                raise
Пример #8
0
 async def score_many_articles(
     self,
     urls: List[str],
     session: aiohttp.ClientSession,
     request_timeout: float = 2,
     processing_timeout: float = 3,
 ) -> List[Result]:
     async with aionursery.Nursery() as nursery:
         tasks = [
             nursery.start_soon(
                 self.score_article(
                     url=url,
                     session=session,
                     morph=self.morph,
                     charged_words=self.charged_words,
                     request_timeout=request_timeout,
                     processing_timeout=processing_timeout,
                 )) for url in urls
         ]
         results, _ = await asyncio.wait(tasks)
     return [task.result() for task in results]
Пример #9
0
async def get_parsed_articles(morph, charged_words, urls):
    async with aiohttp.ClientSession() as session:
        tasks = []
        async with aionursery.Nursery() as nursery:
            for url in urls:
                # add all child tasks (one task per one article) to general list
                tasks.append(
                    nursery.start_soon(
                        process_article(session, morph, charged_words, url)))
            done, pending = await asyncio.wait(tasks)  # run all tasks together

            results = []

            for future in done:
                url, status, score, words_count = future.result()
                results.append({
                    "status": status.value,
                    "url": url,
                    "score": score,
                    "words_count": words_count,
                })

    return results