def setUp(self):

        self.converter = utils.CustomCattrConverter(tdg.tdlib_gen_globals,
                                                    tdg.tdlib_gen_locals)
        utils.register_custom_types_with_cattr_converter(self.converter)
예제 #2
0
    async def init_from_config(
            config: config.ApplicationConfiguration) -> TdlibHandle:

        try:

            logger.info("loading tdjson shared library at `%s`",
                        config.library_path)
            tdjson = ctypes.CDLL(str(config.library_path))
            logger.info("tdjson shared library loaded successfully")

            tdlib_parameters_config = TdlibHandle.create_tdlib_parameters(
                config)
            logger.debug("tdlibParameters: `%s`", tdlib_parameters_config)

            # load TDLib functions from shared library
            td_json_client_create = tdjson.td_json_client_create
            td_json_client_create.restype = ctypes.c_void_p
            td_json_client_create.argtypes = []

            td_json_client_receive = tdjson.td_json_client_receive
            td_json_client_receive.restype = ctypes.c_char_p
            td_json_client_receive.argtypes = [
                ctypes.c_void_p, ctypes.c_double
            ]

            td_json_client_send = tdjson.td_json_client_send
            td_json_client_send.restype = None
            td_json_client_send.argtypes = [ctypes.c_void_p, ctypes.c_char_p]

            td_json_client_execute = tdjson.td_json_client_execute
            td_json_client_execute.restype = ctypes.c_char_p
            td_json_client_execute.argtypes = [
                ctypes.c_void_p, ctypes.c_char_p
            ]

            td_json_client_destroy = tdjson.td_json_client_destroy
            td_json_client_destroy.restype = None
            td_json_client_destroy.argtypes = [ctypes.c_void_p]

            fatal_error_callback_type = ctypes.CFUNCTYPE(None, ctypes.c_char_p)

            td_set_log_fatal_error_callback = tdjson.td_set_log_fatal_error_callback
            td_set_log_fatal_error_callback.restype = None
            td_set_log_fatal_error_callback.argtypes = [
                TdlibHandle.fatal_error_callback_type
            ]

            # set the callback
            c_on_fatal_error_callback = fatal_error_callback_type(
                TdlibHandle.fatal_error_callback)
            td_set_log_fatal_error_callback(c_on_fatal_error_callback)

            # cattr converter
            converter = utils.CustomCattrConverter(
                tdlib_generated.tdlib_gen_globals,
                tdlib_generated.tdlib_gen_locals)
            utils.register_custom_types_with_cattr_converter(converter)

            tdlib_handle = TdlibHandle(
                tdlib_shared_library=tdjson,
                app_config=config,
                tdlib_parameters_config=tdlib_parameters_config,
                tdlib_client=None,  # no client yet
                func_client_create=td_json_client_create,
                func_client_receive=td_json_client_receive,
                func_client_send=td_json_client_send,
                func_client_execute=td_json_client_execute,
                func_client_destroy=td_json_client_destroy,
                func_set_log_fatal_error_callback=
                td_set_log_fatal_error_callback,
                cattr_converter=converter)

            logger.debug("new TdlibHandle from configuration: `%s`",
                         tdlib_handle)
            return tdlib_handle

        except Exception as e:
            logger.exception(
                "TdlibHandle.init_with_configuration: unknown exception")
            raise e
예제 #3
0
    def setUp(self):

        self.converter = utils.CustomCattrConverter(tdg.tdlib_gen_globals,
                                                    tdg.tdlib_gen_locals)
        utils.register_custom_types_with_cattr_converter(self.converter)

        self.fake_message_prefix = pathlib.Path(
            "message/messageText/textEntities/")

        self.test_params = [
            TextEntityTestParams(
                json_path=self.fake_message_prefix /
                "message_id_18874368_chat_id_-1001446368458_text_entity_bold.json",
                te_offset=11,
                te_length=4,
                msg_text="TESTING123 BOLD",
                should_skip=False,
                te_type=dbme.TextEntityTypeEnum.TEXT_ENTITY_TYPE_BOLD),
            TextEntityTestParams(
                json_path=self.fake_message_prefix /
                "message_id_33554432_chat_id_-1001446368458_text_entity_bot_command.json",
                te_offset=0,
                te_length=6,
                msg_text="/start",
                should_skip=False,
                te_type=dbme.TextEntityTypeEnum.TEXT_ENTITY_TYPE_BOT_COMMAND),
            TextEntityTestParams(
                json_path=self.fake_message_prefix /
                "message_id_32505856_chat_id_-1001446368458_text_entity_cashtag.json",
                te_offset=0,
                te_length=4,
                msg_text="$USD CASHTAG TESTING123",
                should_skip=False,
                te_type=dbme.TextEntityTypeEnum.TEXT_ENTITY_TYPE_CASHTAG),
            TextEntityTestParams(
                json_path=self.fake_message_prefix /
                "message_id_17825792_chat_id_-1001446368458_text_entity_code.json",
                te_offset=11,
                te_length=12,
                msg_text="TESTING123 preformatted",
                should_skip=False,
                te_type=dbme.TextEntityTypeEnum.TEXT_ENTITY_TYPE_CODE),
            TextEntityTestParams(
                json_path=self.fake_message_prefix /
                "message_id_24117248_chat_id_-1001446368458_text_entity_email_address.json",
                te_offset=11,
                te_length=16,
                msg_text="TESTING123 [email protected]",
                should_skip=False,
                te_type=dbme.TextEntityTypeEnum.TEXT_ENTITY_TYPE_EMAIL_ADDRESS
            ),
            TextEntityTestParams(
                json_path=self.fake_message_prefix /
                "message_id_27262976_chat_id_-1001446368458_text_entity_hash_tag.json",
                te_offset=11,
                te_length=8,
                msg_text="testing123 #hashtag",
                should_skip=False,
                te_type=dbme.TextEntityTypeEnum.TEXT_ENTITY_TYPE_HASHTAG),
            TextEntityTestParams(
                json_path=self.fake_message_prefix /
                "message_id_19922944_chat_id_-1001446368458_text_entity_italic.json",
                te_offset=11,
                te_length=6,
                msg_text="TESTING123 ITALIC",
                should_skip=False,
                te_type=dbme.TextEntityTypeEnum.TEXT_ENTITY_TYPE_ITALIC),
            TextEntityTestParams(
                json_path=self.fake_message_prefix /
                "message_id_31457280_chat_id_-1001446368458_text_entity_mention.json",
                te_offset=4,
                te_length=14,
                msg_text="hey @ExampleUser12 how are you TESTING123",
                should_skip=False,
                te_type=dbme.TextEntityTypeEnum.TEXT_ENTITY_TYPE_MENTION),

            # TODO: get a example of one of these
            TextEntityTestParams(
                json_path=None,
                te_offset=-1,
                te_length=-1,
                msg_text="",
                should_skip=True,
                te_type=dbme.TextEntityTypeEnum.TEXT_ENTITY_TYPE_MENTION_NAME),
            TextEntityTestParams(
                json_path=self.fake_message_prefix /
                "message_id_26214400_chat_id_-1001446368458_text_entity_phone_number.json",
                te_offset=11,
                te_length=14,
                msg_text="TESTING123 (415) 555-5555",
                should_skip=False,
                te_type=dbme.TextEntityTypeEnum.TEXT_ENTITY_TYPE_PHONE_NUMBER),

            # TODO: get a example of one of these
            TextEntityTestParams(
                json_path=None,
                te_offset=-1,
                te_length=-1,
                msg_text="",
                should_skip=True,
                te_type=dbme.TextEntityTypeEnum.TEXT_ENTITY_TYPE_PRE),

            # TODO: get a example of one of these
            TextEntityTestParams(
                json_path=None,
                te_offset=-1,
                te_length=-1,
                msg_text="",
                should_skip=True,
                te_type=dbme.TextEntityTypeEnum.TEXT_ENTITY_TYPE_PRE_CODE),
            TextEntityTestParams(
                json_path=self.fake_message_prefix /
                "message_id_22020096_chat_id_-1001446368458_text_entity_strikethrough.json",
                te_offset=11,
                te_length=6,
                msg_text="TESTING123 STRIKE",
                should_skip=False,
                te_type=dbme.TextEntityTypeEnum.TEXT_ENTITY_TYPE_STRIKETHROUGH
            ),
            TextEntityTestParams(
                json_path=self.fake_message_prefix /
                "message_id_23068672_chat_id_-1001446368458_text_entity_text_url.json",
                te_offset=16,
                te_length=17,
                msg_text="TESTING123 LINK https://zombo.com",
                should_skip=False,
                te_type=dbme.TextEntityTypeEnum.TEXT_ENTITY_TYPE_TEXT_URL),
            TextEntityTestParams(
                json_path=self.fake_message_prefix /
                "message_id_20971520_chat_id_-1001446368458_text_entity_underline.json",
                te_offset=11,
                te_length=9,
                msg_text="TESTING123 UNDERLINE",
                should_skip=False,
                te_type=dbme.TextEntityTypeEnum.TEXT_ENTITY_TYPE_UNDERLINE),
            TextEntityTestParams(
                json_path=self.fake_message_prefix /
                "message_id_46137344_chat_id_-1001446368458_text_entity_url.json",
                te_offset=0,
                te_length=22,
                msg_text="https://html5zombo.com",
                should_skip=False,
                te_type=dbme.TextEntityTypeEnum.TEXT_ENTITY_TYPE_URL),
        ]
예제 #4
0
    def test_cattr_converter_round_trip(self):
        '''
        `utils.CustomCattrConverter` round trip test
        '''

        binary_data = "this_is_binary_data 1234567890 🐧".encode("utf-8")
        integer_data = 1234
        float_data = float(1.2)
        boolean_data = True
        null_data = None
        str_data = "hello there"
        list_data = ["hello", "there"]
        dict_data = {"one": "hello", "two": "there"}

        uuid_str = "e4f5e051-b980-4837-9ed9-a63b23ac12f3"
        uuid_obj_data = uuid.UUID(uuid_str)

        test_instance = TestClass(binary_data=binary_data,
                                  integer_data=integer_data,
                                  float_data=float_data,
                                  boolean_data=boolean_data,
                                  str_data=str_data,
                                  list_data=list_data,
                                  dict_data=dict_data,
                                  uuid_data=uuid_obj_data)

        converter = utils.CustomCattrConverter(tdg.tdlib_gen_globals,
                                               tdg.tdlib_gen_locals)
        utils.register_custom_types_with_cattr_converter(converter)

        ##########################################################
        # serialize the object as a dictionary
        ##########################################################

        obj_as_dict = converter.unstructure(test_instance)

        ##########################################################
        # make sure the dictionary is what we expect
        ##########################################################

        self.assertEqual(obj_as_dict["binary_data"],
                         base64.b64encode(binary_data).decode("utf-8"),
                         "base64 data after obj converted to dict")

        self.assertEqual(obj_as_dict["integer_data"], integer_data,
                         "integer data after obj converted to dict")

        self.assertEqual(obj_as_dict["float_data"], float_data,
                         "float data after obj converted to dict")

        self.assertEqual(obj_as_dict["boolean_data"], boolean_data,
                         "boolean data after obj converted to dict")

        self.assertEqual(obj_as_dict["str_data"], str_data,
                         "str data after obj converted to dict")

        self.assertEqual(obj_as_dict["uuid_data"], uuid_str,
                         "uuid data after obj converted to dict")

        # list assertions
        self.assertEqual(obj_as_dict["list_data"], list_data,
                         "list data after obj converted to dict")

        self.assertEqual(obj_as_dict["list_data"][0], "hello",
                         "list data after obj converted to dict")

        self.assertEqual(obj_as_dict["list_data"][1], "there",
                         "list data after obj converted to dict")

        # dict assertions
        self.assertEqual(obj_as_dict["dict_data"], dict_data,
                         "dict data after obj converted to dict")

        self.assertEqual(obj_as_dict["dict_data"]["one"], "hello",
                         "dict data after obj converted to dict")

        self.assertEqual(obj_as_dict["dict_data"]["two"], "there",
                         "dict data after obj converted to dict")

        ##########################################################
        # assert the number of keys in the dictionary
        ##########################################################

        self.assertEqual(len(obj_as_dict.keys()), 8,
                         "number of keys in dictionary")

        ##########################################################
        # now convert back to an object and assert its the same as the one we created
        ##########################################################

        round_trip_obj = converter.structure(obj_as_dict, TestClass)

        self.assertEqual(round_trip_obj, test_instance,
                         "complete object after round trip")

        self.assertEqual(round_trip_obj.binary_data, binary_data,
                         "binary data after round trip")

        self.assertEqual(round_trip_obj.integer_data, integer_data,
                         "integer data after round trip")

        self.assertEqual(round_trip_obj.float_data, float_data,
                         "float data after round trip")

        self.assertEqual(round_trip_obj.boolean_data, boolean_data,
                         "boolean data after round trip")

        self.assertEqual(round_trip_obj.str_data, str_data,
                         "string data after round trip")

        self.assertEqual(round_trip_obj.uuid_data, uuid_obj_data,
                         "uuid data after round trip")

        # list assertions
        self.assertEqual(round_trip_obj.list_data, list_data,
                         "list data after round trip")

        self.assertEqual(round_trip_obj.list_data[0], "hello",
                         "list data after round trip")

        self.assertEqual(round_trip_obj.list_data[1], "there",
                         "list data after round trip")

        # dict assertions
        self.assertEqual(round_trip_obj.dict_data, dict_data,
                         "dict data after round trip")

        self.assertEqual(round_trip_obj.dict_data["one"], "hello",
                         "dict data after round trip")

        self.assertEqual(round_trip_obj.dict_data["two"], "there",
                         "dict data after round trip")