Пример #1
0
    def __encode_cbor__(self, encoder):
        encode_byte_string = encoder.encode_byte_string
        if encoder.realize_il:
            return encode_byte_string(bjoin(self.generator))

        parts = (encode_byte_string(byte_string)
                 for byte_string in self.generator)
        return b'\x5f' + bjoin(parts) + b'\xff'
Пример #2
0
 def encode_sorted_dict(self, kv_pairs, sort_method):
     encode_item = self.encode_item
     pairs_gen = ((encode_item(key), value) for key, value in kv_pairs)
     length = pack_cbor_length(len(kv_pairs), 0xa0)
     return length + bjoin(
         encoded_key + encode_item(value)
         for encoded_key, value in sorted_pairs(pairs_gen, sort_method))
Пример #3
0
    def __encode_cbor__(self, encoder):
        if encoder.realize_il:
            return encoder.encode_sorted_list(tuple(self.generator))

        encode_item = encoder.encode_item
        parts = (encode_item(item) for item in self.generator)
        return b'\x9f' + bjoin(parts) + b'\xff'
Пример #4
0
 def decode_byte_string(self, initial_byte):
     length = self.decode_length(initial_byte)
     if length == -1:
         if self._deterministic & DeterministicFlags.REALIZE_IL:
             raise DeterministicError(f'indeterminate-length byte string')
         return bjoin(self._byte_string_parts())
     return self.read(length)
Пример #5
0
    def __encode_cbor__(self, encoder):
        encode_text_string = encoder.encode_text_string
        if encoder.realize_il:
            return encode_text_string(sjoin(self.generator))

        parts = (encode_text_string(text_string)
                 for text_string in self.generator)
        return b'\x7f' + bjoin(parts) + b'\xff'
Пример #6
0
    def __encode_cbor__(self, encoder):
        if encoder.realize_il:
            return encoder.encode_sorted_dict(tuple(self.generator),
                                              encoder.sort_method)

        encode_item = encoder.encode_item
        parts = (encode_item(key) + encode_item(kvalue)
                 for key, kvalue in self.generator)
        return b'\xbf' + bjoin(parts) + b'\xff'
Пример #7
0
    async def _read(n):
        nonlocal buff

        if n > len(buff):
            length = len(buff)
            parts = [buff]
            while length < n:
                part = await read()
                if not part:
                    raise UnexpectedEOFError(
                        f'need {n:,d} bytes but only {length:,d} available')
                length += len(part)
                parts.append(part)
            buff = bjoin(parts)

        result, buff = buff[:n], buff[n:]
        return result
Пример #8
0
 def encode_sorted_list(self, value):
     length = pack_cbor_length(len(value), 0x80)
     encode_item = self.encode_item
     encoded_items_gen = (encode_item(item) for item in value)
     return length + bjoin(sorted_items(encoded_items_gen,
                                        self.sort_method))
Пример #9
0
 def _make_list(self, length, encoded_items_gen):
     return pack_cbor_length(length, 0x80) + bjoin(encoded_items_gen)
Пример #10
0
 def __realize__(self, realize_one, item_gen, immutable):
     return bjoin(takewhile(lambda item: item is not Break, item_gen))