<s> class YadisServiceManager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , starting_url , yadis_url , services , session_key ) : <EOL> self . starting_url = starting_url <EOL> self . yadis_url = yadis_url <EOL> self . services = list ( services ) <EOL> self . session_key = session_key <EOL> self . _current = None <EOL> def __len__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . services ) <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _current = self . services . pop ( <NUM_LIT:0> ) <EOL> except IndexError : <EOL> raise StopIteration <EOL> else : <EOL> return self . _current <EOL> def current ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _current <EOL> def forURL ( self , url ) : <EOL> return url in [ self . starting_url , self . yadis_url ] <EOL> def started ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _current is not None <EOL> def store ( self , session ) : <EOL> """<STR_LIT>""" <EOL> session [ self . session_key ] = self <EOL> class Discovery ( object ) : <EOL> """<STR_LIT>""" <EOL> DEFAULT_SUFFIX = '<STR_LIT>' <EOL> PREFIX = '<STR_LIT>' <EOL> def __init__ ( self , session , url , session_key_suffix = None ) : <EOL> """<STR_LIT>""" <EOL> self . session = session <EOL> self . url = url <EOL> if session_key_suffix is None : <EOL> session_key_suffix = self . DEFAULT_SUFFIX <EOL> self . session_key_suffix = session_key_suffix <EOL> def getNextService ( self , discover ) : <EOL> """<STR_LIT>""" <EOL> manager = self . getManager ( ) <EOL> if manager is not None and not manager : <EOL> self . destroyManager ( ) <EOL> if not manager : <EOL> yadis_url , services = discover ( self . url ) <EOL> manager = self . createManager ( services , yadis_url ) <EOL> if manager : <EOL> service = manager . next ( ) <EOL> manager . store ( self . session ) <EOL> else : <EOL> service = None <EOL> return service <EOL> def cleanup ( self , force = False ) : <EOL> """<STR_LIT>""" <EOL> manager = self . getManager ( force = force ) <EOL> if manager is not None : <EOL> service = manager . current ( ) <EOL> self . destroyManager ( force = force ) <EOL> else : <EOL> service = None <EOL> return service <EOL> def getSessionKey ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . PREFIX + self . session_key_suffix <EOL> def getManager ( self , force = False ) : <EOL> """<STR_LIT>""" <EOL> manager = self . session . get ( self . getSessionKey ( ) ) <EOL> if ( manager is not None and ( manager . forURL ( self . url ) or force ) ) : <EOL> return manager <EOL> else : <EOL> return None <EOL> def createManager ( self , services , yadis_url = None ) : <EOL> """<STR_LIT>""" <EOL> key = self . getSessionKey ( ) <EOL> if self . getManager ( ) : <EOL> raise KeyError ( '<STR_LIT>' % <EOL> ( key , self . url ) ) <EOL> if not services : <EOL> return None <EOL> manager = YadisServiceManager ( self . url , yadis_url , services , key ) <EOL> manager . store ( self . session ) <EOL> return manager <EOL> def destroyManager ( self , force = False ) : <EOL> """<STR_LIT>""" <EOL> if self . getManager ( force = force ) is not None : <EOL> key = self . getSessionKey ( ) <EOL> del self . session [ key ] </s>
