Exemplo n.º 1
0
    def dict_to_tro(dict_obj):
        '''
        Converts a dictionary representation into a
        TranslationRequestObject protobuf.
        '''
        tr_obj = TranslationRequestObject()
        # TODO: check if we really need "blah" which is questionable ;
        defined_keys = set(['blah', 'shortname', 'request_id',
                            'worker', 'owner', 'created', 'ready',
                            'result', 'source_language',
                            'target_language'])
        tr_obj.shortname = dict_obj['shortname']
        tr_obj.request_id = dict_obj['request_id']
        tr_obj.worker = dict_obj['worker']
        tr_obj.owner = dict_obj['owner']
        tr_obj.created = dict_obj['created'].isoformat()
        tr_obj.ready = dict_obj['ready']
        if 'result' in dict_obj:
            result_obj = TranslationRequestMessage()
            result_obj.request_id = dict_obj['request_id']
            result_obj.source_language = dict_obj['source_language']
            result_obj.target_language = dict_obj['target_language']
            result_obj.source_text = ''
            result_obj.target_text = dict_obj['result']

            # pylint: disable-msg=E1101
            for key in set(dict_obj.keys()) - defined_keys:
                kv_pair = result_obj.packet_data.add()
                kv_pair.key = key
                kv_pair.value = dict_obj[key]
            tr_obj.serialized = b64encode(result_obj.SerializeToString())
        else:
            tr_obj.serialized = ''
        return tr_obj
Exemplo n.º 2
0
 def dict_to_tro(dict_obj):
     """
     Converts a dictionary representation into a
     TranslationRequestObject protobuf.
     """
     tr_obj = TranslationRequestObject()
     defined_keys = set(
         [
             "blah",
             "shortname",
             "request_id",
             "worker",
             "owner",
             "created",
             "ready",
             "result",
             "source_language",
             "target_language",
         ]
     )
     tr_obj.shortname = dict_obj["shortname"]
     tr_obj.request_id = dict_obj["request_id"]
     tr_obj.worker = dict_obj["worker"]
     tr_obj.owner = dict_obj["owner"]
     tr_obj.created = dict_obj["created"].isoformat()
     tr_obj.ready = dict_obj["ready"]
     if "result" in dict_obj:
         result_obj = TranslationRequestMessage()
         result_obj.request_id = dict_obj["request_id"]
         result_obj.source_language = dict_obj["source_language"]
         result_obj.target_language = dict_obj["target_language"]
         result_obj.source_text = ""
         result_obj.target_text = dict_obj["result"]
         for key in set(dict_obj.keys()) - defined_keys:
             kv_pair = result_obj.packet_data.add()
             kv_pair.key = key
             kv_pair.value = dict_obj[key]
         tr_obj.serialized = b64encode(result_obj.SerializeToString())
     else:
         tr_obj.serialized = ""
     return tr_obj