Exemple #1
0
    def test_decode_object(self):
        obj = {
            "key_one".encode('utf-8'): [
                "value_one".encode('utf-8'), "value_two".encode('utf-8'),
                ("value_one".encode('utf-8'), "value_two".encode('utf-8'),
                 bintools), {
                     "no_decode_key_one".encode('utf-8'):
                     ("me".encode('utf-8'), "you".encode('utf-8')),
                     "decode_key".encode('utf-8'):
                     "test".encode('utf-8')
                 }
            ],
            "no_decode_key_two".encode('utf-8'):
            "test".encode('utf-8')
        }

        decoded_obj = bintools.decode_object(obj,
                                             encoding='utf-8',
                                             except_keys=('no_decode_key_one',
                                                          'no_decode_key_two'))

        self.assertEqual(
            decoded_obj, {
                "key_one": [
                    "value_one", "value_two",
                    ("value_one", "value_two", bintools), {
                        "no_decode_key_one":
                        ("me".encode('utf-8'), "you".encode('utf-8')),
                        "decode_key":
                        "test"
                    }
                ],
                "no_decode_key_two":
                "test".encode('utf-8')
            })
Exemple #2
0
    def __handle_received_data(self, data):
        self.__unpacker.feed(data)

        for packet in self.__unpacker:
            try:
                unpacked_packet = bintools.decode_object(
                    packet,
                    except_keys=('hash', 'binary_data', 'checksums')
                )
            except Exception as ex:
                self.logger.error(
                    "Error trying to decode strings to utf-8 in packet from {}"
                    .format(self.address[0])
                )
                self.logger.exception(ex)

                return False
            else:
                try:
                    self.packet_received.notify(unpacked_packet)
                except Exception as ex:
                    self.logger.error("Error processing packet from {}"
                                      .format(self.address[0]))
                    self.logger.exception(ex)

                    return False

        return True
Exemple #3
0
    def load_index(self):
        with self.fs_access_lock:
            if os.path.isfile(self.index_path):
                with open(self.index_path, 'rb') as index_file:
                    index = msgpack.unpackb(index_file.read())

                # Decode the object to utf strings except the 'hash' values
                self._index = bintools.decode_object(index,
                                                     except_keys=('hash', ))

            else:
                self._index = dict()

            self.last_update = datetime.now().timestamp()
Exemple #4
0
    def handle(self):
        try:
            self.logger.debug("Received UDP packet from {}".format(
                self.client_address[0]))

            data = decode_object(msgpack.unpackb(self.packet))
            self.server.packet_received.notify({
                'data': data,
                'source': self.client_address[0],
                'server': self.server
            })
        except msgpack.exceptions.UnpackException as ex:
            self.logger.exception("Error unpacking UDP data", ex)
            self.server.packet_received_error.notify(self.packet)
        except Exception as ex:
            self.logger.exception("Error processing UDP data", ex)
    def handle(self):
        try:
            self.logger.debug("Received UDP packet from {}"
                              .format(self.client_address[0]))

            data = decode_object(msgpack.unpackb(self.packet))
            self.server.packet_received.notify({
                'data': data,
                'source': self.client_address[0],
                'server': self.server
            })
        except msgpack.exceptions.UnpackException as ex:
            self.logger.exception("Error unpacking UDP data", ex)
            self.server.packet_received_error.notify(self.packet)
        except Exception as ex:
            self.logger.exception("Error processing UDP data", ex)
Exemple #6
0
    def load_index(self):
        with self.fs_access_lock:
            if os.path.isfile(self.index_path):
                with open(self.index_path, 'rb') as index_file:
                    index = msgpack.unpackb(index_file.read())

                # Decode the object to utf strings except the 'hash' values
                self._index = bintools.decode_object(
                    index,
                    except_keys=('hash',)
                )

            else:
                self._index = dict()

            self.last_update = datetime.now().timestamp()
Exemple #7
0
    def test_decode_object(self):
        obj = {
            "key_one".encode('utf-8'): [
                "value_one".encode('utf-8'),
                "value_two".encode('utf-8'),
                (
                    "value_one".encode('utf-8'),
                    "value_two".encode('utf-8'),
                    bintools
                ),
                {
                    "no_decode_key_one".encode('utf-8'): (
                        "me".encode('utf-8'),
                        "you".encode('utf-8')
                    ),
                    "decode_key".encode('utf-8'): "test".encode('utf-8')
                }
            ],
            "no_decode_key_two".encode('utf-8'): "test".encode('utf-8')
        }

        decoded_obj = bintools.decode_object(
            obj,
            encoding='utf-8',
            except_keys=('no_decode_key_one', 'no_decode_key_two')
        )

        self.assertEqual(decoded_obj, {
            "key_one": [
                "value_one",
                "value_two",
                (
                    "value_one",
                    "value_two",
                    bintools
                ),
                {
                    "no_decode_key_one": (
                        "me".encode('utf-8'),
                        "you".encode('utf-8')
                    ),
                    "decode_key": "test"
                }
            ],
            "no_decode_key_two": "test".encode('utf-8')
        })