1 # Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
2 # Copyright (C) 2013 YAMAMOTO Takashi <yamamoto at valinux co jp>
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 from abc import ABCMeta, abstractmethod
22 @six.add_metaclass(ABCMeta)
23 class StreamParser(object):
24 """Streaming parser base class.
26 An instance of a subclass of this class is used to extract messages
27 from a raw byte stream.
29 It's designed to be used for data read from a transport which doesn't
30 preserve message boundaries. A typical example of such a transport
34 class TooSmallException(Exception):
40 def parse(self, data):
41 """Tries to extract messages from a raw byte stream.
43 The data argument would be python bytes newly read from the input
46 Returns an ordered list of extracted messages.
47 It can be an empty list.
49 The rest of data which doesn't produce a complete message is
50 kept internally and will be used when more data is come.
51 I.e. next time this method is called again.
57 msg, self._q = self.try_parse(self._q)
58 except self.TooSmallException:
64 def try_parse(self, q):
65 """Try to extract a message from the given bytes.
67 This is an override point for subclasses.
69 This method tries to extract a message from bytes given by the
72 Raises TooSmallException if the given data is not enough to
73 extract a complete message but there's still a chance to extract
74 a message if more data is come later.