Package buildbot :: Package util :: Module netstrings
[frames] | no frames]

Source Code for Module buildbot.util.netstrings

 1  # This file is part of Buildbot.  Buildbot is free software: you can 
 2  # redistribute it and/or modify it under the terms of the GNU General Public 
 3  # License as published by the Free Software Foundation, version 2. 
 4  # 
 5  # This program is distributed in the hope that it will be useful, but WITHOUT 
 6  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 7  # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
 8  # details. 
 9  # 
10  # You should have received a copy of the GNU General Public License along with 
11  # this program; if not, write to the Free Software Foundation, Inc., 51 
12  # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
13  # 
14  # Copyright Buildbot Team Members 
15   
16  from twisted.protocols import basic 
17  from zope.interface import implements 
18  from twisted.internet.interfaces import IAddress, ITransport 
19   
20 -class NullAddress(object):
21 "an address for NullTransport" 22 implements(IAddress)
23
24 -class NullTransport(object):
25 "a do-nothing transport to make NetstringReceiver happy" 26 implements(ITransport)
27 - def write(self, data): raise NotImplementedError
28 - def writeSequence(self, data): raise NotImplementedError
29 - def loseConnection(self): pass
30 - def getPeer(self):
31 return NullAddress
32 - def getHost(self):
33 return NullAddress
34
35 -class NetstringParser(basic.NetstringReceiver):
36 """ 37 Adapts the Twisted netstring support (which assumes it is on a socket) to 38 work on simple strings, too. Call the C{feed} method with arbitrary blocks 39 of data, and override the C{stringReceived} method to get called for each 40 embedded netstring. The default implementation collects the netstrings in 41 the list C{self.strings}. 42 """ 43
44 - def __init__(self):
45 # most of the complexity here is stubbing out the transport code so 46 # that Twisted-10.2.0 and higher believes that this is a valid protocol 47 self.makeConnection(NullTransport()) 48 self.strings = []
49
50 - def feed(self, data):
51 """ 52 Add arbitrariliy-sized C{data} to the incoming-data buffer. Any 53 complete netstrings will trigger a call to the stringReceived method. 54 55 Note that this method (and the Twisted class it is based on) cannot 56 detect a trailing partial netstring at EOF - the data will be silently 57 ignored. 58 59 @raise C{twisted.protocols.basic.NetstringParseError}: if invalid 60 """ 61 self.dataReceived(data) 62 # dataReceived handles errors unusually quietly! 63 if self.brokenPeer: 64 raise basic.NetstringParseError
65
66 - def stringReceived(self, string):
67 self.strings.append(string)
68