<s> import operator <EOL> import threading <EOL> import six <EOL> from flower import core <EOL> from flower . local import local <EOL> _local = local ( ) <EOL> class Registry ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> __shared_state__ = dict ( <EOL> _registered_names = { } , <EOL> _by_ref = { } <EOL> ) <EOL> def __init__ ( self ) : <EOL> self . __dict__ = self . __shared_state__ <EOL> self . _lock = threading . RLock ( ) <EOL> def register ( self , name , ref ) : <EOL> """<STR_LIT>""" <EOL> with self . _lock : <EOL> if name in self . _registered_names : <EOL> if self . _registered_names [ name ] == ref : <EOL> return <EOL> raise KeyError ( "<STR_LIT>" ) <EOL> self . _registered_names [ name ] = ref <EOL> if not ref in self . _by_ref : <EOL> self . _by_ref [ ref ] = [ name ] <EOL> else : <EOL> self . _by_ref [ ref ] . append ( name ) <EOL> def unregister ( self , ref_or_name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if isinstance ( ref_or_name , six . string_types ) : <EOL> with self . _lock : <EOL> ref = self . _registered_names [ ref_or_name ] <EOL> names = self . _by_ref [ ref ] <EOL> del names [ operator . indexOf ( names , ref_or_name ) ] <EOL> del self . _registered_names [ ref_or_name ] <EOL> else : <EOL> with self . _lock : <EOL> names = self . _by_ref [ ref_or_name ] <EOL> for name in names : <EOL> del self . _registered_names [ name ] <EOL> except ( KeyError , IndexError ) : <EOL> pass <EOL> def registered ( self , ref = None ) : <EOL> """<STR_LIT>""" <EOL> print ( type ( core . getcurrent ( ) ) ) <EOL> if ref is None : <EOL> try : <EOL> ref = core . getcurrent ( ) . ref <EOL> except AttributeError : <EOL> return [ ] <EOL> print ( ref ) <EOL> print ( self . _by_ref ) <EOL> if ref not in self . _by_ref : <EOL> return [ ] <EOL> print ( self . _by_ref [ ref ] ) <EOL> return sorted ( self . _by_ref [ ref ] ) <EOL> def by_name ( self , name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _registered_names [ name ] <EOL> except KeyError : <EOL> return None <EOL> def __getitem__ ( self , ref_or_name ) : <EOL> if isinstance ( ref_or_name , six . string_types ) : <EOL> return self . by_name ( ref_or_name ) <EOL> else : <EOL> return self . registered ( ref_or_name ) <EOL> def __delitem__ ( self , ref_or_name ) : <EOL> self . unregister ( ref_or_name ) <EOL> def __contains__ ( self , ref_or_name ) : <EOL> with self . _lock : <EOL> if isinstance ( ref_or_name , six . string_types ) : <EOL> return ref_or_name in self . _registered_names <EOL> else : <EOL> return ref_or_name in self . _by_ref <EOL> def __iter__ ( self ) : <EOL> return iter ( self . _registered_name . items ( ) ) <EOL> registry = Registry ( ) <EOL> register = registry . register <EOL> unregister = registry . unregister <EOL> registered = registry . registered </s>
