<s> """<STR_LIT>""" <EOL> class MissingConfigError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Optional ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , default , description ) : <EOL> self . default = default <EOL> self . description = description <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . description , <EOL> repr ( self . default ) ) <EOL> def _is_optional ( substructure ) : <EOL> """<STR_LIT>""" <EOL> if type ( substructure ) == Optional : <EOL> return True <EOL> if type ( substructure ) is dict : <EOL> for value in substructure . values ( ) : <EOL> if not _is_optional ( value ) : <EOL> return False <EOL> return True <EOL> return False <EOL> def extract_substructure ( base , substructure ) : <EOL> """<STR_LIT>""" <EOL> if ( type ( substructure ) is not dict and <EOL> type ( base ) is not dict ) : <EOL> return base <EOL> if type ( base ) is not dict : <EOL> raise MissingConfigError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( repr ( base ) , repr ( substructure ) ) ) <EOL> if type ( substructure ) is not dict : <EOL> raise MissingConfigError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( repr ( base ) , repr ( substructure ) ) ) <EOL> try : <EOL> subdict = [ ] <EOL> for key , value in substructure . iteritems ( ) : <EOL> if type ( value ) is Optional : <EOL> base_val = base . get ( key , value . default ) <EOL> elif _is_optional ( value ) : <EOL> base_val = base . get ( key , { } ) <EOL> else : <EOL> base_val = base [ key ] <EOL> subdict . append ( ( key , extract_substructure ( base_val , value ) ) ) <EOL> return dict ( subdict ) <EOL> except KeyError as e : <EOL> raise MissingConfigError ( <EOL> "<STR_LIT>" . format ( e . args [ <NUM_LIT:0> ] ) ) </s>
