"""Implementation of UTTP (Untyped Tree Transfer Protocol) in Python."""

# $Id: uttp.py 87017 2019-07-16 18:31:28Z satskyse $

# Tell pylint not to obsess about old-style class definitions
# pylint: disable=C1001


import sys

PY3 = sys.version_info[ 0 ] == 3


class Reader:
    """Parse input buffer and sequentially return a stream of UTTP tokens."""

    class FormatError(Exception):
        """Exception raised for unexpected characters in the input stream."""
        pass

    CHUNK_PART = 0
    CHUNK = 1
    CONTROL_SYMBOL = 2
    NUMBER = 3
    END_OF_BUFFER = 4

    __CONTROL_CHARS = 0
    __CHUNK_LENGTH = 1
    __CHUNK = 2

    def __init__(self, offset=0):
        """Initialize the state machine of this object."""
        self.__state = Reader.__CONTROL_CHARS
        self.__offset = offset
        self.__buf = ''
        self.__buf_offset = 0
        self.__token = ''
        self.__length_acc = 0
        self.__chunk_continued = False

    def reset_offset(self, offset=0):
        """Set the current offset in the input stream to the new value."""
        self.__offset = offset

    def set_new_buf(self, buf):
        """Start processing of the next chunk of data."""
        self.__buf = buf
        self.__buf_offset = 0

    def next_event(self):
        """Parse the input buffer until a parsing event occurs."""
        if self.__buf_offset == len(self.__buf):
            return Reader.END_OF_BUFFER
        if self.__state == Reader.__CONTROL_CHARS:
            # At least one character will be consumed in this block.
            self.__offset += 1

            # Slicing is used to make it compatible between python 2 and
            # python 3. In case of python 2 the buf is a string while in case
            # of python 3 it is a bytes array
            next_char = self.__buf[self.__buf_offset : self.__buf_offset + 1]
            self.__buf_offset += 1

            # All non-digit characters are considered control symbols.
            if not next_char.isdigit():
                self.__token = next_char
                return Reader.CONTROL_SYMBOL

            # The current character is a digit, which is the first
            # character of the next chunk length. Proceed with reading
            # the chunk length.
            self.__state = Reader.__CHUNK_LENGTH
            self.__length_acc = int(next_char)
            if self.__buf_offset == len(self.__buf):
                return Reader.END_OF_BUFFER
            return self.__continue_reading_chunk_length()
        if self.__state == Reader.__CHUNK_LENGTH:
            return self.__continue_reading_chunk_length()
        return self.__continue_reading_chunk()

    def read_raw_data(self, data_size):
        """Read a block of fixed size data. Return a "parsing event"."""
        if self.__state != Reader.__CONTROL_CHARS:
            raise Reader.FormatError('invalid reader state')

        self.__length_acc = data_size
        self.__state = Reader.__CHUNK

        if self.__buf_offset == len(self.__buf):
            return Reader.END_OF_BUFFER

        return self.__continue_reading_chunk()

    def get_chunk(self):
        """Return the chunk (part) if next_event() was CHUNK(_PART)."""
        return self.__token

    def get_control_symbol(self):
        """Return the control symbol if next_event() was CONTROL_SYMBOL."""
        return self.__token

    def get_number(self):
        """Return the number if next_event() was NUMBER."""
        return self.__length_acc

    def get_offset(self):
        """Return the offset of the current character in the input stream."""
        return self.__offset

    def __continue_reading_chunk_length(self):
        """The current state is __CHUNK_LENGTH, proceed with parsing."""
        # Slicing is used to make it compatible between python 2 and
        # python 3. In case of python 2 the buf is a string while in case
        # of python 3 it is a bytes array
        next_char = self.__buf[self.__buf_offset : self.__buf_offset + 1]
        while next_char.isdigit():
            self.__length_acc = self.__length_acc * 10 + int(next_char)
            self.__offset += 1
            self.__buf_offset += 1
            if self.__buf_offset == len(self.__buf):
                return Reader.END_OF_BUFFER
            next_char = self.__buf[self.__buf_offset : self.__buf_offset + 1]

        self.__offset += 1
        self.__buf_offset += 1

        # For python 3 ...
        if type(next_char) == bytes:
            next_char = next_char.decode()

        if next_char == '+':
            self.__chunk_continued = True
        elif next_char == ' ':
            self.__chunk_continued = False
        else:
            self.__state = Reader.__CONTROL_CHARS
            if next_char == '=':
                return Reader.NUMBER
            elif next_char == '-':
                self.__length_acc = -self.__length_acc
                return Reader.NUMBER
            else:
                self.__token = next_char
                raise Reader.FormatError('invalid character (' +
                                         repr(next_char) + ') '
                                         'after chunk length ' +
                                         str(self.__length_acc))

        self.__state = Reader.__CHUNK
        if self.__buf_offset == len(self.__buf):
            return Reader.END_OF_BUFFER

        return self.__continue_reading_chunk()

    def __continue_reading_chunk(self):
        """The current state is __CHUNK, proceed with reading the chunk."""
        chunk_end = self.__buf_offset + self.__length_acc
        if chunk_end <= len(self.__buf):
            self.__token = self.__buf[self.__buf_offset:chunk_end]
            self.__offset += self.__length_acc
            self.__buf_offset = chunk_end
            # The last part of the chunk has been read --
            # get back to reading control symbols.
            self.__state = Reader.__CONTROL_CHARS
            if self.__chunk_continued:
                return Reader.CHUNK_PART
            return Reader.CHUNK
        else:
            self.__token = self.__buf[self.__buf_offset:]
            self.__offset += len(self.__token)
            self.__length_acc -= len(self.__token)
            self.__buf_offset = len(self.__buf)
            return Reader.CHUNK_PART

class Writer:
    """Serialize series of chunks of data for sending over binary streams."""
    def __init__(self, min_buf_size):
        if PY3:
            self.__buf = b''
        else:
            self.__buf = ''
        self.__min_buf_size = min_buf_size

    def send_control_symbol(self, symbol):
        """Pack a control symbol into the internal buffer. Control
        symbol can be any single byte character except digits.
        Return a buffer to send to the output stream in case of overflow."""
        if PY3:
            if type(symbol) != bytes:
                symbol = symbol.encode()

        if len(self.__buf) < self.__min_buf_size:
            self.__buf += symbol
            return None

        buf = self.__buf
        self.__buf = symbol
        return buf

    def send_chunk(self, chunk, to_be_continued=False):
        """Copy a chunk of data to the internal buffer. Return a buffer
        to send to the output stream in case of overflow."""
        if PY3:
            if type(chunk) != bytes:
                chunk = chunk.encode()

        chunk_length = str(len(chunk))
        if PY3:
            chunk_length = chunk_length.encode()
        self.__buf += chunk_length


        if to_be_continued:
            if PY3:
                self.__buf += b'+'
            else:
                self.__buf += '+'
        else:
            if PY3:
                self.__buf += b' '
            else:
                self.__buf += ' '

        if len(self.__buf) + len(chunk) <= self.__min_buf_size:
            self.__buf += chunk
            return None

        buf = self.__buf
        self.__buf = chunk
        return buf

    def send_raw_data(self, data):
        """Send a block of fixed size data. Return a buffer
        to send to the output stream in case of overflow."""
        if PY3:
            if type(data) != bytes:
                data = data.encode()

        if len(self.__buf) + len(data) <= self.__min_buf_size:
            self.__buf += data
            return None

        buf = self.__buf
        self.__buf = data
        return buf

    def send_number(self, number):
        """Pack a number into the internal buffer. Return a buffer
        to send to the output stream in case of overflow."""
        if number >= 0:
            number = str(number) + '='
        else:
            number = str(-number) + '-'

        if PY3:
            number = number.encode()

        if len(self.__buf) + len(number) <= self.__min_buf_size:
            self.__buf += number
            return None

        buf = self.__buf
        self.__buf = number
        return buf

    def flush_buf(self):
        """Return the contents of the internal buffer and reset the buffer."""
        buf = self.__buf
        if PY3:
            self.__buf = b''
        else:
            self.__buf = ''
        return buf
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274

-