Beispiel #1
0
        def datetime_nanban2str_out(dt_nanban):
            filepath = os.path.join(FILE_DIR, "tmplt.{}.part.txt".format(lang))
            has_dt_nanban = (dt_nanban is not None)
            if not has_dt_nanban:
                data = {
                    "server": Server.server_lang2name(server, lang),
                    "has_dt_nanban": has_dt_nanban,
                }
                str_out = HenriqueJinja2.textfile2text(filepath, data)
                return str_out

            td_nanban = dt_nanban - utc_now

            tzdb = HenriqueLocale.lang2tzdb(lang)

            str_timedelta_nanban = NanbanTimedelta.timedelta_lang2text(
                td_nanban, lang)

            logger.debug({
                "server_codename": server_codename,
                "server": server,
                "dt_nanban": dt_nanban,
            })

            data = {
                "server": Server.server_lang2name(server, lang),
                "dt_nanban": NanbanTimedelta.datetime2text(dt_nanban, tzdb),
                "dt_now": NanbanTimedelta.datetime2text(utc_now, tzdb),
                "timedelta_nanban": str_timedelta_nanban,
                "has_dt_nanban": has_dt_nanban,
            }
            str_out = HenriqueJinja2.textfile2text(filepath, data)
            return str_out
Beispiel #2
0
    def test_03(self):
        logger = HenriqueLogger.func_level2logger(self.test_02, logging.DEBUG)

        Chatroom.chatrooms2upsert([ChatroomKakaotalk.chatroom()])

        sender_name = "iris"
        channel_user_codename = ChannelUserKakaotalk.sender_name2codename(
            sender_name)
        ChannelUser.channel_users2upsert(
            [ChannelUserKakaotalk.sender_name2channel_user(sender_name)])

        now_seoul = datetime.now(
            tz=pytz.timezone(HenriqueLocale.lang2tzdb("ko")))
        dt_target = now_seoul - timedelta(seconds=3 * 60)
        text = "?남만 {}".format(dt_target.strftime("%I:%M %p").lstrip("0"))
        logger.debug({
            "text": text,
            "now_seoul": now_seoul,
        })

        packet = {
            KhalaPacket.Field.TEXT: text,
            KhalaPacket.Field.CHATROOM: KakaotalkUWOChatroom.codename(),
            KhalaPacket.Field.CHANNEL_USER: channel_user_codename,
            KhalaPacket.Field.SENDER_NAME: sender_name,
        }
        response = NanbanSkill.packet2response(packet)

        # pprint(text)
        # pprint(response)

        response_lines = response.splitlines()

        span = (
            len("다음 남만 시각: "),
            len("다음 남만 시각: 3:58:00 PM (KST) "),
        )

        hyp = SpanTool.list_span2sublist(response_lines[2], span).strip()
        dt_nanban = dt_target + NanbanTimedelta.period()
        ref = dt_nanban.strftime("%I:%M:00 %p (KST)").lstrip("0")
        self.assertEqual(
            hyp,
            ref,
        )
Beispiel #3
0
    def packet2response(cls, packet):
        logger = HenriqueLogger.func_level2logger(cls.packet2response,
                                                  logging.DEBUG)
        logger.debug({"packet": packet})

        server_codename = HenriquePacket.packet2server(packet)
        chatroom = Chatroom.codename2chatroom(
            KhalaPacket.packet2chatroom(packet))
        locale = Chatroom.chatroom2locale(chatroom) or HenriqueLocale.DEFAULT
        lang = LocaleTool.locale2lang(locale)
        tz = pytz.timezone(HenriqueLocale.lang2tzdb(lang))
        dt_now = datetime.now(tz)

        text_in = KhalaPacket.packet2text(packet)
        config = {HenriqueEntity.Config.Field.LOCALE: locale}
        # entity_list = RelativeTimedeltaEntity.text2entity_list(text_in, config=config)

        entity_list = HenriqueEntity.text_extractors2entity_list(
            text_in,
            cls.config2extractors(config),
        )
        logger.debug({
            "len(entity_list)": len(entity_list),
            "entity_list": entity_list,
        })

        if not entity_list:
            return cls.server_lang2lookup(server_codename, lang)

        if len(entity_list) != 1:
            return  # Invalid request

        entity = l_singleton2obj(entity_list)
        if FoxylibEntity.entity2type(
                entity) == RelativeTimedeltaEntity.entity_type():
            reldelta = RelativeTimedeltaEntity.entity2relativedelta(entity)
            dt_in = cls.relativedelta2nanban_datetime(
                server_codename,
                reldelta,
            )

            # raise Exception({"dt_in":dt_in, "reldelta":reldelta})

            if dt_in is None:
                msg_error = NanbanSkillError.codename_lang2text(
                    NanbanSkillError.Codename.NO_PREV_NANBAN_ERROR, lang)
                raise HenriqueCommandError(msg_error)

            logger.debug({
                "reldelta": reldelta,
            })

        elif FoxylibEntity.entity2type(entity) == TimeEntity.entity_type():
            time_in = TimeEntity.value2datetime_time(
                FoxylibEntity.entity2value(entity))
            dt_in = PytzTool.localize(datetime.combine(dt_now.date(), time_in),
                                      tz)
            logger.debug({
                "time_in": time_in,
                "dt_in": dt_in,
            })
        else:
            raise RuntimeError({
                "Invalid entity type: {}".format(
                    FoxylibEntity.entity2type(entity))
            })

        dt_nearest = DatetimeTool.datetime2nearest(dt_in, dt_now,
                                                   NanbanTimedelta.period(),
                                                   Nearest.COMING)

        logger.debug({
            "text_in": text_in,
            "dt_now": dt_now,
            "dt_in": dt_in,
            "dt_nearest": dt_nearest,
        })

        cls.nanban_datetime2upsert_mongo(packet, dt_nearest)
        return cls.server_lang2lookup(server_codename, lang)