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 """ 53 self.dataReceived(data) 54 # dataReceived handles errors unusually quietly! 55 if self.brokenPeer: 56 raise basic.NetstringParseError
57
58 - def stringReceived(self, string):
59 self.strings.append(string)
60