<s> """<STR_LIT>""" <EOL> from twisted . python import log <EOL> LOG_SYSTEM = '<STR_LIT>' <EOL> DEFAULT_MAX_COST = <NUM_LIT:5> <EOL> class LinkVector : <EOL> def __init__ ( self , local_networks , blacklist_networks = None , max_cost = DEFAULT_MAX_COST ) : <EOL> self . local_networks = local_networks <EOL> self . blacklist_networks = blacklist_networks if not blacklist_networks is None else [ ] <EOL> self . max_cost = max_cost <EOL> self . vectors = { } <EOL> self . _shortest_paths = { } <EOL> self . subscribers = [ ] <EOL> def callOnUpdate ( self , f ) : <EOL> self . subscribers . append ( f ) <EOL> def updated ( self ) : <EOL> for f in self . subscribers : <EOL> f ( ) <EOL> def updateVector ( self , port , vectors ) : <EOL> if port in self . vectors : <EOL> self . vectors [ port ] . update ( vectors ) <EOL> else : <EOL> self . vectors [ port ] = vectors <EOL> self . _calculateVectors ( ) <EOL> self . updated ( ) <EOL> def deleteVector ( self , port ) : <EOL> try : <EOL> self . vectors . pop ( port ) <EOL> self . _calculateVectors ( ) <EOL> except KeyError : <EOL> log . msg ( '<STR_LIT>' % port ) <EOL> def _calculateVectors ( self ) : <EOL> log . msg ( '<STR_LIT>' , debug = True , system = LOG_SYSTEM ) <EOL> paths = { } <EOL> for port , vectors in self . vectors . items ( ) : <EOL> for network , cost in vectors . items ( ) : <EOL> if network in self . local_networks : <EOL> continue <EOL> if network in self . blacklist_networks : <EOL> log . msg ( '<STR_LIT>' % network , system = LOG_SYSTEM ) <EOL> continue <EOL> if cost > self . max_cost : <EOL> log . msg ( '<STR_LIT>' % ( network , cost , self . max_cost ) , system = LOG_SYSTEM ) <EOL> continue <EOL> if not network in paths : <EOL> paths [ network ] = ( port , cost ) <EOL> log . msg ( '<STR_LIT>' % ( network , port , cost ) , debug = True , system = LOG_SYSTEM ) <EOL> elif cost < paths [ network ] [ <NUM_LIT:1> ] : <EOL> paths [ network ] = ( port , cost ) <EOL> log . msg ( '<STR_LIT>' % ( network , port , cost ) , debug = True , system = LOG_SYSTEM ) <EOL> self . _shortest_paths = paths <EOL> def vector ( self , network ) : <EOL> try : <EOL> port , cost = self . _shortest_paths [ network ] <EOL> return port <EOL> except KeyError : <EOL> return None <EOL> def listVectors ( self ) : <EOL> return { network : cost for ( network , ( _ , cost ) ) in self . _shortest_paths . items ( ) } </s>
