<s> """<STR_LIT>""" <EOL> class DatabaseConfigurator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , settings , defaults = { } ) : <EOL> self . settings = settings <EOL> self . defaults = defaults <EOL> def __iter__ ( self ) : <EOL> for alias in self . settings . iterkeys ( ) : <EOL> config = self . get_value ( alias ) <EOL> shard_info = config . get ( '<STR_LIT>' , { } ) <EOL> host_list = config . get ( '<STR_LIT>' ) <EOL> if not shard_info and not host_list : <EOL> yield alias , config <EOL> continue <EOL> hosts = { } <EOL> if host_list : <EOL> for num , host_config in host_list . iteritems ( ) : <EOL> for key , value in config . iteritems ( ) : <EOL> if key in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> continue <EOL> host_config . setdefault ( key , value ) <EOL> hosts [ num ] = host_config <EOL> else : <EOL> hosts [ <NUM_LIT:0> ] = config <EOL> assert not set ( hosts . iterkeys ( ) ) . difference ( set ( xrange ( len ( hosts ) ) ) ) , '<STR_LIT>' <EOL> cluster_size = shard_info . get ( '<STR_LIT:size>' ) or len ( hosts ) <EOL> assert cluster_size > <NUM_LIT:0> , '<STR_LIT>' <EOL> for num in xrange ( cluster_size ) : <EOL> shard = '<STR_LIT>' % ( alias , num ) <EOL> host_num = num % len ( hosts ) <EOL> shard_n_config = hosts [ host_num ] . copy ( ) <EOL> if not shard_n_config . get ( '<STR_LIT>' ) : <EOL> shard_n_config [ '<STR_LIT>' ] = alias <EOL> yield shard , shard_n_config <EOL> yield alias , config <EOL> def get_parent_value ( self , key ) : <EOL> defaults = self . defaults . copy ( ) <EOL> if '<STR_LIT:.>' in key : <EOL> parts = key . split ( '<STR_LIT:.>' ) <EOL> for subkey in ( '<STR_LIT:.>' . join ( parts [ : ( idx + <NUM_LIT:1> ) ] ) for idx in xrange ( len ( parts ) - <NUM_LIT:1> ) ) : <EOL> if subkey in self . settings : <EOL> defaults . update ( self . settings [ subkey ] ) <EOL> return defaults <EOL> def get_value ( self , key ) : <EOL> defaults = self . get_parent_value ( key ) <EOL> defaults . update ( self . settings [ key ] ) <EOL> return defaults <EOL> def to_dict ( self ) : <EOL> return dict ( self ) <EOL> def wraps ( func ) : <EOL> """<STR_LIT>""" <EOL> def wrapped ( wrapper ) : <EOL> actual = getattr ( func , '<STR_LIT>' , func ) <EOL> for attr in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if hasattr ( actual , attr ) : <EOL> setattr ( wrapper , attr , getattr ( actual , attr ) ) <EOL> wrapper . __wraps__ = actual <EOL> return wrapper <EOL> return wrapped </s>
