<s> import sublime <EOL> import threading <EOL> import logging <EOL> import types <EOL> class Registry ( object ) : <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> def __init__ ( self ) : <EOL> self . negotiators = { } <EOL> self . sessionsByUserByNegotiator = { } <EOL> self . sessionsByViewId = { } <EOL> def buildNegotiatorKey ( self , protocol , config ) : <EOL> return '<STR_LIT>' % ( protocol , config [ '<STR_LIT:username>' ] , config [ '<STR_LIT:host>' ] , config [ '<STR_LIT:port>' ] ) <EOL> def addOrUpdateNegotiator ( self , protocol , config , constructorsByProtocol ) : <EOL> """<STR_LIT>""" <EOL> errorMsgs = [ '<STR_LIT>' ] <EOL> if not '<STR_LIT:username>' in config : <EOL> errorMsgs . append ( '<STR_LIT>' % protocol ) <EOL> self . logger . warning ( errorMsgs [ - <NUM_LIT:1> ] ) <EOL> elif not '<STR_LIT:host>' in config : <EOL> errorMsgs . append ( '<STR_LIT>' % protocol ) <EOL> self . logger . warning ( errorMsgs [ - <NUM_LIT:1> ] ) <EOL> elif not '<STR_LIT:port>' in config : <EOL> errorMsgs . append ( '<STR_LIT>' % protocol ) <EOL> self . logger . warning ( errorMsgs [ - <NUM_LIT:1> ] ) <EOL> negotiatorKey = self . buildNegotiatorKey ( protocol , config ) <EOL> updated = self . hasNegotiator ( negotiatorKey ) <EOL> added = not updated <EOL> if updated : <EOL> updated = ( self . getNegotiator ( negotiatorKey ) . getConfig ( ) != config ) <EOL> if updated : <EOL> self . logger . debug ( '<STR_LIT>' + negotiatorKey ) <EOL> elif added : <EOL> self . logger . debug ( '<STR_LIT>' + negotiatorKey ) <EOL> else : <EOL> self . logger . debug ( '<STR_LIT>' + negotiatorKey ) <EOL> if updated : <EOL> self . removeNegotiator ( negotiatorKey ) <EOL> self . negotiators [ negotiatorKey ] = constructorsByProtocol [ protocol ] ( negotiatorKey , config ) <EOL> if len ( errorMsgs ) > <NUM_LIT:1> : <EOL> sublime . error_message ( errorMsgs . join ( '<STR_LIT:\n>' ) ) <EOL> return ( negotiatorKey , updated ) <EOL> def hasNegotiator ( self , protocol , config ) : <EOL> negotiatorKey = self . buildNegotiatorKey ( protocol , config ) <EOL> return self . hasNegotiator ( negotiatorKey ) <EOL> def hasNegotiator ( self , negotiatorKey ) : <EOL> return self . negotiators . has_key ( negotiatorKey ) <EOL> def removeNegotiator ( self , protocol , config ) : <EOL> negotiatorKey = self . buildNegotiatorKey ( protocol , config ) <EOL> self . removeNegotiator ( negotiatorKey ) <EOL> def removeNegotiator ( self , negotiatorKey ) : <EOL> oldNegotiator = self . negotiators . pop ( negotiatorKey ) <EOL> if oldNegotiator . isConnected ( ) : <EOL> oldNegotiator . disconnect ( ) <EOL> def listNegotiatorKeys ( self ) : <EOL> return self . negotiators . keys ( ) <EOL> def listNegotiators ( self ) : <EOL> return self . negotiators . values ( ) ; <EOL> def listNegotiatorEntries ( self ) : <EOL> return self . negotiators . items ( ) <EOL> def iterNegotiators ( self ) : <EOL> return self . negotiators . itervalues ( ) <EOL> def iterNegotiatorEntries ( self ) : <EOL> return self . negotiators . iteritems ( ) <EOL> def getNegotiator ( self , protocol , config ) : <EOL> negotiatorKey = self . buildNegotiatorKey ( protocol , config ) <EOL> return self . getNegotiator ( negotiatorKey ) <EOL> def getNegotiator ( self , negotiatorKey ) : <EOL> return self . negotiators [ negotiatorKey ] <EOL> def registerSession ( self , session ) : <EOL> negotiatorKey = session . getParentNegotiatorKey ( ) <EOL> peerUser = session . sharingWithUser <EOL> if negotiatorKey in self . sessionsByUserByNegotiator : <EOL> if peerUser in self . sessionsByUserByNegotiator [ negotiatorKey ] : <EOL> existingSessions = self . sessionsByUserByNegotiator [ negotiatorKey ] [ peerUser ] <EOL> if session in existingSessions : <EOL> if hasattr ( session , '<STR_LIT>' ) and session . view : <EOL> self . logger . warn ( '<STR_LIT>' % ( session . view . file_name ( ) , peerUser ) ) <EOL> else : <EOL> self . logger . debug ( '<STR_LIT>' % peerUser ) <EOL> else : <EOL> existingSessions . add ( session ) <EOL> else : <EOL> self . sessionsByUserByNegotiator [ negotiatorKey ] = { peerUser : set ( [ session ] ) } <EOL> else : <EOL> self . sessionsByUserByNegotiator [ negotiatorKey ] = { peerUser : set ( [ session ] ) } <EOL> def registerSessionByView ( self , view , session ) : <EOL> if view . id ( ) in self . sessionsByViewId : <EOL> self . logger . warn ( '<STR_LIT>' % ( view . file_name ( ) , session . str ( ) ) ) <EOL> else : <EOL> self . sessionsByViewId [ view . id ( ) ] = session <EOL> def hasSession ( self , negotiatorKey , peerUser ) : <EOL> return negotiatorKey in self . sessionsByUserByNegotiator and peerUser in self . sessionsByUserByNegotiator [ negotiatorKey ] <EOL> def getSessionsByNegotiatorAndPeer ( self , negotiatorKey , peerUser ) : <EOL> sessions = None <EOL> if negotiatorKey in self . sessionsByUserByNegotiator : <EOL> sessions = self . sessionsByUserByNegotiator [ negotiatorKey ] . get ( peerUser ) <EOL> return sessions <EOL> def getSessionByView ( self , view ) : <EOL> return self . sessionsByViewId . get ( view . id ( ) ) <EOL> def listSessions ( self ) : <EOL> sessions = [ ] <EOL> for sessionsByUser in self . sessionsByUserByNegotiator . itervalues ( ) : <EOL> for userSessions in sessionsByUser . itervalues ( ) : <EOL> sessions += userSessions <EOL> return sessions <EOL> def removeSession ( self , session ) : <EOL> """<STR_LIT>""" <EOL> negotiatorKey = session . getParentNegotiatorKey ( ) <EOL> peerUser = session . sharingWithUser <EOL> if negotiatorKey in self . sessionsByUserByNegotiator : <EOL> sessionsByUser = self . sessionsByUserByNegotiator [ negotiatorKey ] <EOL> if peerUser in sessionsByUser : <EOL> sessionsByUser [ peerUser ] . discard ( session ) <EOL> if session in self . sessionsByViewId . values ( ) : <EOL> for viewId , registeredSession in self . sessionsByViewId . items ( ) : <EOL> if session == registeredSession : <EOL> self . sessionsByViewId . pop ( viewId , None ) <EOL> import sys <EOL> import sub_collab <EOL> if '<STR_LIT>' not in sys . modules or isinstance ( sys . modules [ '<STR_LIT>' ] , types . ModuleType ) : <EOL> Registry . logger . debug ( '<STR_LIT>' ) <EOL> registry = Registry ( ) <EOL> sub_collab . registry = registry <EOL> sys . modules [ '<STR_LIT>' ] = registry </s>
