<s> import re <EOL> from twisted . internet import protocol <EOL> from twisted . protocols . basic import LineReceiver <EOL> def sanitizeMetric ( name ) : <EOL> """<STR_LIT>""" <EOL> name = re . sub ( '<STR_LIT>' , '<STR_LIT:_>' , name ) <EOL> name = re . sub ( '<STR_LIT>' , '<STR_LIT:->' , name ) <EOL> name = re . sub ( '<STR_LIT>' , '<STR_LIT>' , name ) <EOL> return name . encode ( '<STR_LIT:ascii>' ) <EOL> def sanitizeMetricElement ( element ) : <EOL> """<STR_LIT>""" <EOL> return sanitizeMetric ( element . replace ( '<STR_LIT:.>' , '<STR_LIT:_>' ) ) <EOL> class GraphiteLineProtocol ( LineReceiver ) : <EOL> delimiter = '<STR_LIT:\n>' <EOL> service = None <EOL> def connectionMade ( self ) : <EOL> self . service . protocol = self <EOL> def connectionLost ( self , reason ) : <EOL> self . service . protocol = None <EOL> def lineReceived ( self , line ) : <EOL> pass <EOL> def sendMetric ( self , path , value , timestamp ) : <EOL> """<STR_LIT>""" <EOL> self . sendLine ( '<STR_LIT>' % ( path , value , timestamp ) ) <EOL> class GraphiteClientFactory ( protocol . ReconnectingClientFactory ) : <EOL> protocol = GraphiteLineProtocol <EOL> def __init__ ( self , service ) : <EOL> self . service = service <EOL> def buildProtocol ( self , addr ) : <EOL> self . resetDelay ( ) <EOL> p = self . protocol ( ) <EOL> p . service = self . service <EOL> return p <EOL> class FakeGraphiteProtocol ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . output = { } <EOL> def sendMetric ( self , path , value , timestamp ) : <EOL> self . output [ path ] = ( value , timestamp ) </s>
