Exemplo n.º 1
0
def test_nested_contextualize(writer):
    logger.add(writer, format="{message} {extra[foobar]}")

    with logger.contextualize(foobar="a"):
        with logger.contextualize(foobar="b"):
            logger.info("B")

        logger.info("A")

        with logger.contextualize(foobar="c"):
            logger.info("C")

    assert writer.read() == "B b\nA a\nC c\n"
Exemplo n.º 2
0
async def confirm_call(ans: SimpleBotEvent):
    with logger.contextualize(user_id=ans.object.object.message.from_id):
        admin_id = db.students.get_system_id_of_student(
            ans.object.object.message.peer_id, )
        msg = call.generate_message(admin_id)
        store = db.admin.get_admin_storage(admin_id)
        chat_id = Chat.get_by_id(store.current_chat_id).chat_id
        try:
            query = await api.messages.get_conversations_by_id(chat_id)
            chat_name = query.response.items[0].chat_settings.title
        except IndexError:
            chat_name = "???"
        if not msg and not store.attaches:
            raise EmptyCallMessage("Сообщение призыва не может быть пустым")
        db.shortcuts.update_admin_storage(
            db.students.get_system_id_of_student(
                ans.object.object.message.peer_id),
            state_id=db.bot.get_id_of_state("confirm_call"),
        )
        await ans.answer(
            f'Сообщение будет отправлено в чат "{chat_name}":\n{msg}',
            keyboard=kbs.call.call_prompt(
                db.students.get_system_id_of_student(
                    ans.object.object.message.peer_id), ),
            attachment=store.attaches or "",
        )
Exemplo n.º 3
0
        def wrapped(task, *args, **kwargs):
            try:
                corr_id = task.request.chord["options"]["task_id"]
            except Exception:  # noqa
                corr_id = "N/A"

            # Get worker name
            worker_name = task.request.delivery_info["routing_key"].replace(
                "journey.", "")

            with logger.contextualize(corrid=corr_id, task_id=task.request.id):
                try:
                    if timing:
                        start = perf_counter()
                    result = func(self=task, *args, **kwargs)
                    payload = {
                        "status": "success",
                        "worker": worker_name,
                        "result": result,
                    }
                except Exception as e:  # noqa
                    logger.error("Exception in task: {}", e)
                    payload = {
                        "status": "error",
                        "worker": worker_name,
                        "error": str(e),
                    }
                finally:
                    if timing:
                        end = perf_counter() - start
                        logger.info("Task took {}s to execute", end)
            return payload
async def executor(
    episode_setups: List[EpisodeSetup], cuda_device: int, process_num: int
) -> None:
    with logger.contextualize(cuda_device=cuda_device, process_num=process_num):
        while len(episode_setups) > 0:
            setup = episode_setups.pop(0)

            logger.info(
                f"Starting an episode of {setup.name} at {setup.town} in {setup.weather}"
            )

            sleep_time = random.random() * 15
            logger.trace(f"Waiting {sleep_time:.2f}s")
            await asyncio.sleep(sleep_time)

            success = await execute(setup, cuda_device, process_num)
            if success:
                logger.success(
                    f"Successfully collected an episode of {setup.name} at {setup.town} in {setup.weather}"
                )
            else:
                logger.warning(
                    f"Collection was unsuccessful, rescheduling an episode of {setup.name} at {setup.town} in {setup.weather}"
                )
                episode_setups.append(setup)
Exemplo n.º 5
0
 async def on_rx_raw(self, raw: str):
     """ underlying implementation that handles websocket data """
     raw_data = json.loads(raw)
     event_or_uid = raw_data[0]
     if try_parse_uuid(event_or_uid):
         response = Response(*raw_data)
         with logger.contextualize(state=response.state):
             return await self._handle_response(response)
     logger.debug("handling raw event data: {}", raw_data)
     with logger.contextualize(event=event_or_uid):
         if event_or_uid not in CLS_FOR_EVENT:
             logger.error("no structure defined for event {}", event_or_uid)
             return  # no structure definition, eject.
         event = event_converter.structure(raw_data,
                                           CLS_FOR_EVENT[event_or_uid])
         await self._handle_event(event)
Exemplo n.º 6
0
def test_contextualize(writer):
    logger.add(writer, format="{message} {extra[foo]} {extra[baz]}")

    with logger.contextualize(foo="bar", baz=123):
        logger.info("Contextualized")

    assert writer.read() == "Contextualized bar 123\n"
Exemplo n.º 7
0
async def select_student(ans: SimpleBotEvent):
    with logger.contextualize(user_id=ans.object.object.message.from_id):
        payload = hyperjson.loads(ans.object.object.message.payload)
        student_id = payload["student_id"]
        letter = payload["letter"]
        name = payload["name"]
        if student_id in db.shortcuts.get_list_of_calling_students(
                db.students.get_system_id_of_student(
                    ans.object.object.message.peer_id), ):
            db.shortcuts.pop_student_from_calling_list(
                db.students.get_system_id_of_student(
                    ans.object.object.message.peer_id),
                student_id,
            )
            label = "удален из списка призыва"
        else:
            db.shortcuts.add_student_to_calling_list(
                db.students.get_system_id_of_student(
                    ans.object.object.message.peer_id),
                student_id,
            )
            label = "добавлен в список призыва"
        await ans.answer(
            f"{name} {label}",
            keyboard=kbs.call.CallNavigator(
                db.students.get_system_id_of_student(
                    ans.object.object.message.peer_id), ).render().students(
                        letter),
        )
Exemplo n.º 8
0
async def delete_chat(ans: SimpleBotEvent):
    with logger.contextualize(user_id=ans.object.object.message.from_id):
        payload = hyperjson.loads(ans.object.object.message.payload)
        db.chats.delete_chat(payload["chat"])
        chats = db.chats.get_list_of_chats_by_group(
            db.admin.get_active_group(
                db.students.get_system_id_of_student(ans.object.object.message.from_id),
            ),
        )
        try:
            chat_id = chats[0].id
        except IndexError:
            chat_id = None
        db.shortcuts.update_admin_storage(
            db.students.get_system_id_of_student(ans.object.object.message.from_id),
            current_chat_id=chat_id,
        )
        await ans.answer(
            "Чат удален",
            keyboard=await kbs.preferences.connected_chats(
                db.students.get_system_id_of_student(
                    ans.object.object.message.from_id,
                ),
            ),
        )
Exemplo n.º 9
0
async def register_students(ans: SimpleBotEvent):
    with logger.contextualize(user_id=ans.object.object.message.from_id):
        payload = hyperjson.loads(ans.object.object.message.payload)
        data = []
        raw_html = requests.get(payload["students"])
        soup = BeautifulSoup(raw_html.text, "html.parser")
        students_ids = list(map(int, soup.find_all("pre")[1].text.split(",")))
        students = await api.users.get(user_ids=students_ids)
        student_last_id = Student.select().order_by(Student.id.desc()).get().id
        for student in students.response:
            student_last_id += 1
            data.append(
                {
                    "id": student_last_id,
                    "first_name": student.first_name,
                    "second_name": student.last_name,
                    "vk_id": student.id,
                    "group_id": payload["group"],
                    "academic_status": 1,
                },
            )
        query = Student.insert_many(data).execute()

        await ans.answer(
            f"{len(query)} студент(ов) зарегистрировано",
            keyboard=kbs.preferences.configure_chat(payload["chat_id"]),
        )
Exemplo n.º 10
0
async def call_them_all(ans: SimpleBotEvent):
    with logger.contextualize(user_id=ans.object.object.message.from_id):
        admin_id = db.students.get_system_id_of_student(
            ans.object.object.message.peer_id, )
        group_id = db.admin.get_active_group(admin_id)
        students = [st.id for st in db.students.get_active_students(group_id)]
        db.shortcuts.update_calling_list(admin_id, students)
        await confirm_call(ans)
Exemplo n.º 11
0
def log_out(msg, additional_context=None):
    if additional_context is None:
        logger.info("logging out")
        _logger.info("logging.logger out")
    else:
        with logger.contextualize(**additional_context):
            logger.info("logging out with-in context")
            _logger.info("logging.logger out with-in context")
Exemplo n.º 12
0
async def add_expense(ans: SimpleBotEvent):
    with logger.contextualize(user_id=ans.object.object.message.from_id):
        db.shortcuts.update_admin_storage(
            db.students.get_system_id_of_student(
                ans.object.object.message.from_id),
            state_id=db.bot.get_id_of_state("enter_expense_summ"),
        )
        await ans.answer("Введите сумму расхода")
Exemplo n.º 13
0
async def finances(ans: SimpleBotEvent):
    with logger.contextualize(user_id=ans.object.object.message.from_id):
        await ans.answer(
            "Список финансовых категорий",
            keyboard=kbs.finances.list_of_fin_categories(
                db.students.get_system_id_of_student(
                    ans.object.object.message.from_id), ),
        )
Exemplo n.º 14
0
 async def _do_work_transmit(self, work):
     """ Actually emits messages to the underlying transport. """
     with logger.contextualize(state=work.state):
         if "representing" in work.query:
             # FIXME remove this hack once the API actually has production data...
             # TODO: check drill mode and selectively not emit?
             del work.query["representing"]
         await self._socket.send(work.serialize())
Exemplo n.º 15
0
async def register_chat(ans: SimpleBotEvent):
    with logger.contextualize(user_id=ans.object.object.message.from_id):
        store = db.admin.get_admin_storage(
            db.students.get_system_id_of_student(ans.object.object.message.from_id),
        )
        if (
            store.confirm_message in ans.object.object.message.text
            and ans.object.object.message.from_id == Student.get_by_id(store.id).vk_id
        ):
            db.shortcuts.clear_admin_storage(
                db.students.get_system_id_of_student(ans.object.object.message.from_id),
            )

            group = db.admin.get_active_group(
                db.students.get_system_id_of_student(
                    ans.object.object.message.from_id,
                ),
            )
            if db.chats.is_chat_registered(
                ans.object.object.message.peer_id,
                group,
            ):
                await api.messages.send(
                    message="Чат уже зарегистрирован в этой группе",
                    peer_id=ans.object.object.message.from_id,
                    random_id=0,
                    keyboard=await kbs.preferences.connected_chats(
                        db.students.get_system_id_of_student(
                            ans.object.object.message.from_id,
                        ),
                    ),
                )
            else:
                chat = db.chats.register_chat(ans.object.object.message.peer_id, group)
                db.shortcuts.update_admin_storage(
                    db.students.get_system_id_of_student(
                        ans.object.object.message.from_id,
                    ),
                    current_chat_id=chat.id,
                )
                try:
                    chat_object = await api.messages.get_conversations_by_id(
                        peer_ids=ans.object.object.message.peer_id,
                    )
                    chat_name = chat_object.response.items[0].chat_settings.title
                except IndexError:
                    chat_name = "???"
                await api.messages.send(
                    message=f'Чат "{chat_name}" зарегистрирован',
                    peer_id=ans.object.object.message.from_id,
                    random_id=0,
                    keyboard=await kbs.preferences.connected_chats(
                        db.students.get_system_id_of_student(
                            ans.object.object.message.from_id,
                        ),
                    ),
                )
                await ans.answer("Привет!")
Exemplo n.º 16
0
async def deny_call_debtors(ans: SimpleBotEvent):
    with logger.contextualize(user_id=ans.object.object.message.from_id):
        db.shortcuts.update_admin_storage(
            db.students.get_system_id_of_student(
                ans.object.object.message.from_id),
            state_id=db.bot.get_id_of_state("main"),
        )
        await ans.answer("Отправка отменена",
                         keyboard=kbs.finances.fin_category())
Exemplo n.º 17
0
async def tr_command(ans: SimpleBotEvent):
    with logger.contextualize(user_id=ans.object.object.message.from_id):
        if msg := ans.object.object.message.reply_message:
            await ans.answer(translate_string(msg.text), )
        for msg in ans.object.object.message.fwd_messages:
            await ans.answer(translate_string(msg.text))
        if (not ans.object.object.message.reply_message
                and not ans.object.object.message.fwd_messages):
            await ans.answer("Команда используется в ответ на сообщение")
Exemplo n.º 18
0
async def select_half(ans: SimpleBotEvent):
    with logger.contextualize(user_id=ans.object.object.message.from_id):
        payload = hyperjson.loads(ans.object.object.message.payload)
        await ans.answer(
            "Выберите студента, сдавшего деньги",
            keyboard=kbs.finances.IncomeNavigator(
                db.students.get_system_id_of_student(
                    ans.object.object.message.from_id), ).render().submenu(
                        payload["half"]),
        )
Exemplo n.º 19
0
 def emit(self, record):
     with logger.contextualize(identifier=self.log_identifier):
         level = record.levelno
         # Downgrade anything INFO and lower to DEBUG5 to match legacy behavior.
         # Werkzeug is a bit too chatty at INFO level for it to be useful.
         if level <= logging.INFO:
             level = "DEBUG5"
         else:
             level = logging.getLevelName(level)
         logger.opt(depth=6, exception=record.exc_info).log(level, record.getMessage())
Exemplo n.º 20
0
def test_contextualize_after_configure(writer):
    logger.add(writer, format="{message} {extra[foobar]}")

    with logger.contextualize(foobar="baz"):
        logger.configure(extra={"foobar": "baz_2"})
        logger.info("A")

    logger.info("B")

    assert writer.read() == "A baz\nB baz_2\n"
Exemplo n.º 21
0
async def list_of_chats(ans: SimpleBotEvent):
    with logger.contextualize(user_id=ans.object.object.message.from_id):
        await ans.answer(
            "Список подключенных чатов",
            keyboard=await kbs.preferences.connected_chats(
                db.students.get_system_id_of_student(
                    ans.object.object.message.from_id,
                ),
            ),
        )
Exemplo n.º 22
0
async def select_student(ans: SimpleBotEvent):
    with logger.contextualize(user_id=ans.object.object.message.from_id):
        payload = hyperjson.loads(ans.object.object.message.payload)
        db.shortcuts.update_admin_storage(
            db.students.get_system_id_of_student(
                ans.object.object.message.from_id, ),
            selected_students=str(payload["student_id"]),
            state_id=db.bot.get_id_of_state("enter_donate_sum"),
        )
        await ans.answer("Введите сумму дохода", keyboard=kbs.common.empty())
Exemplo n.º 23
0
async def select_half(ans: SimpleBotEvent):
    with logger.contextualize(user_id=ans.object.object.message.from_id):
        payload = hyperjson.loads(ans.object.object.message.payload)
        await ans.answer(
            "Выберите призываемых студентов",
            keyboard=kbs.call.CallNavigator(
                db.students.get_system_id_of_student(
                    ans.object.object.message.peer_id), ).render().submenu(
                        payload["half"]),
        )
Exemplo n.º 24
0
async def select_half(ans: SimpleBotEvent):
    with logger.contextualize(user_id=ans.object.object.message.from_id):
        payload = hyperjson.loads(ans.object.object.message.payload)
        await ans.answer(
            "Выберите студента",
            keyboard=kbs.contacts.ContactsNavigator(
                db.students.get_system_id_of_student(
                    ans.object.object.message.from_id, ), ).render().submenu(
                        payload.get("half"), ),
        )
Exemplo n.º 25
0
def test_contextualize_in_function(writer):
    logger.add(writer, format="{message} {extra}")

    def foobar():
        logger.info("Foobar!")

    with logger.contextualize(foobar="baz"):
        foobar()

    assert writer.read() == "Foobar! {'foobar': 'baz'}\n"
Exemplo n.º 26
0
async def cancel_call(ans: SimpleBotEvent):
    with logger.contextualize(user_id=ans.object.object.message.from_id):
        db.shortcuts.clear_admin_storage(
            db.students.get_system_id_of_student(
                ans.object.object.message.peer_id), )
        await ans.answer(
            "Призыв отменён. Возврат на главную.",
            keyboard=kbs.main.main_menu(
                db.students.get_system_id_of_student(
                    ans.object.object.message.peer_id), ),
        )
Exemplo n.º 27
0
async def select_letter(ans: SimpleBotEvent):
    with logger.contextualize(user_id=ans.object.object.message.from_id):
        payload = hyperjson.loads(ans.object.object.message.payload)
        letter = payload.get("value")
        await ans.answer(
            f"Список студентов на букву {letter}",
            keyboard=kbs.contacts.ContactsNavigator(
                db.students.get_system_id_of_student(
                    ans.object.object.message.peer_id), ).render().students(
                        letter),
        )
Exemplo n.º 28
0
async def _traverse_gallery(
    worker_id: int,
    session: aiohttp.ClientSession,
    traverse_queue: URLQueue,
    gallery_queue: GalleryQueue,
) -> None:
    with logger.contextualize(
            worker_id=worker_id,
            task='traverse_gallery',
    ):
        while True:
            root_url = await traverse_queue.get()
            domain_url = yarl.URL(
                f'{root_url.scheme}://{root_url.parent.host}')

            logger.info(
                'Looking for downloadable video files at {root_url}',
                root_url=root_url,
            )

            soup = BS(
                await (await session.get(root_url)).text(),
                'html.parser',
                parse_only=SS('a'),
            )

            video_anchors = list(
                map(
                    lambda h: VideoPage(
                        title=h.img.get('alt').strip(),
                        url=domain_url.with_path(h.get('href')),
                    ),
                    soup.find_all(
                        'a',
                        class_='thumbnail-link',
                        href=re.compile('video'),
                    ),
                ), )
            logger.info(
                'Located {c} videos to download via {u}',
                c=len(video_anchors),
                u=root_url,
            )

            for va in video_anchors:
                await gallery_queue.put(va)

            next_anchor = soup.find('a', rel='next')
            if next_anchor:
                _, page_number = next_anchor['href'].replace('?',
                                                             '').split('=')
                await traverse_queue.put(
                    root_url.with_query(page=int(page_number)))
            traverse_queue.task_done()
Exemplo n.º 29
0
def test_context_reset_despite_error(writer):
    logger.add(writer, format="{message} {extra}")

    try:
        with logger.contextualize(foobar=456):
            logger.info("Division")
            1 / 0
    except ZeroDivisionError:
        logger.info("Error")

    assert writer.read() == "Division {'foobar': 456}\nError {}\n"
Exemplo n.º 30
0
def test_contextualize_after_bind(writer):
    logger.add(writer, format="{message} {extra[foobar]}")

    with logger.contextualize(foobar="baz"):
        logger_2 = logger.bind(foobar="baz_2")
        logger.info("A")
        logger_2.info("B")

    logger_2.info("C")

    assert writer.read() == "A baz\nB baz_2\nC baz_2\n"