<s> from conans . errors import ConanException <EOL> import yaml <EOL> from conans . model . values import Values <EOL> def bad_value_msg ( name , value , value_range ) : <EOL> return ( "<STR_LIT>" <EOL> % ( value , name , value_range ) ) <EOL> def undefined_field ( name , field , fields = None , value = None ) : <EOL> value_str = "<STR_LIT>" % value if value else "<STR_LIT>" <EOL> result = [ "<STR_LIT>" % ( name , field , value_str ) ] <EOL> result . append ( "<STR_LIT>" % ( name , fields or "<STR_LIT:none>" ) ) <EOL> return "<STR_LIT:\n>" . join ( result ) <EOL> def undefined_value ( name ) : <EOL> return "<STR_LIT>" % name <EOL> class ConfigItem ( object ) : <EOL> def __init__ ( self , definition , name , cls ) : <EOL> self . _name = name <EOL> self . _value = None <EOL> self . _cls = cls <EOL> self . _definition = { } <EOL> if isinstance ( definition , dict ) : <EOL> for k , v in definition . items ( ) : <EOL> k = str ( k ) <EOL> self . _definition [ k ] = cls ( v , name , k ) <EOL> else : <EOL> self . _definition = sorted ( [ str ( v ) for v in definition ] ) <EOL> def copy ( self ) : <EOL> """<STR_LIT>""" <EOL> cls = type ( self ) <EOL> result = cls ( { } , name = self . _name , cls = self . _cls ) <EOL> result . _value = self . _value <EOL> if self . is_final : <EOL> result . _definition = self . _definition [ : ] <EOL> else : <EOL> result . _definition = { k : v . copy ( ) for k , v in self . _definition . items ( ) } <EOL> return result <EOL> @ property <EOL> def is_final ( self ) : <EOL> return not isinstance ( self . _definition , dict ) <EOL> def __bool__ ( self ) : <EOL> if not self . _value : <EOL> return False <EOL> return self . _value . lower ( ) not in [ "<STR_LIT:false>" , "<STR_LIT:none>" , "<STR_LIT:0>" , "<STR_LIT>" ] <EOL> def __nonzero__ ( self ) : <EOL> return self . __bool__ ( ) <EOL> def __str__ ( self ) : <EOL> return self . _value <EOL> def __eq__ ( self , other ) : <EOL> if other is None : <EOL> return self . _value is None <EOL> other = str ( other ) <EOL> if other not in self . values_range : <EOL> raise ConanException ( bad_value_msg ( self . _name , other , self . values_range ) ) <EOL> return other == self . __str__ ( ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __delattr__ ( self , item ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _get_child ( self . _value ) . remove ( item ) <EOL> except : <EOL> pass <EOL> def remove ( self , values ) : <EOL> if not isinstance ( values , ( list , tuple , set ) ) : <EOL> values = [ values ] <EOL> for v in values : <EOL> v = str ( v ) <EOL> if isinstance ( self . _definition , dict ) : <EOL> self . _definition . pop ( v , None ) <EOL> else : <EOL> if v in self . _definition : <EOL> self . _definition . remove ( v ) <EOL> if self . _value == v : <EOL> raise ConanException ( bad_value_msg ( self . _name , v , self . values_range ) ) <EOL> def _get_child ( self , item ) : <EOL> if not isinstance ( self . _definition , dict ) : <EOL> raise ConanException ( undefined_field ( self . _name , item , None , self . _value ) ) <EOL> if self . _value is None : <EOL> raise ConanException ( undefined_value ( self . _name ) ) <EOL> return self . _definition [ self . _value ] <EOL> def __getattr__ ( self , item ) : <EOL> item = str ( item ) <EOL> sub_config_dict = self . _get_child ( item ) <EOL> return getattr ( sub_config_dict , item ) <EOL> def __setattr__ ( self , item , value ) : <EOL> if item [ <NUM_LIT:0> ] == "<STR_LIT:_>" or item . startswith ( "<STR_LIT:value>" ) : <EOL> return super ( ConfigItem , self ) . __setattr__ ( item , value ) <EOL> item = str ( item ) <EOL> sub_config_dict = self . _get_child ( item ) <EOL> return setattr ( sub_config_dict , item , value ) <EOL> def __getitem__ ( self , value ) : <EOL> value = str ( value ) <EOL> try : <EOL> return self . _definition [ value ] <EOL> except : <EOL> raise ConanException ( bad_value_msg ( self . _name , value , self . values_range ) ) <EOL> @ property <EOL> def value ( self ) : <EOL> return self . _value <EOL> @ value . setter <EOL> def value ( self , v ) : <EOL> v = str ( v ) <EOL> if v not in self . _definition : <EOL> raise ConanException ( bad_value_msg ( self . _name , v , self . values_range ) ) <EOL> self . _value = v <EOL> @ property <EOL> def values_range ( self ) : <EOL> try : <EOL> return sorted ( list ( self . _definition . keys ( ) ) ) <EOL> except : <EOL> return self . _definition <EOL> @ property <EOL> def values_list ( self ) : <EOL> if self . _value is None : <EOL> return [ ] <EOL> result = [ ] <EOL> partial_name = "<STR_LIT:.>" . join ( self . _name . split ( "<STR_LIT:.>" ) [ <NUM_LIT:1> : ] ) <EOL> result . append ( ( partial_name , self . _value ) ) <EOL> if isinstance ( self . _definition , dict ) : <EOL> sub_config_dict = self . _definition [ self . _value ] <EOL> result . extend ( sub_config_dict . values_list ) <EOL> return result <EOL> def validate ( self ) : <EOL> if self . _value is None : <EOL> raise ConanException ( undefined_value ( self . _name ) ) <EOL> if isinstance ( self . _definition , dict ) : <EOL> self . _definition [ self . _value ] . validate ( ) <EOL> class ConfigDict ( object ) : <EOL> def __init__ ( self , definition , name , parent_value = None ) : <EOL> self . _name = name <EOL> self . _parent_value = parent_value <EOL> cls = type ( self ) <EOL> self . _data = { str ( k ) : ConfigItem ( v , "<STR_LIT>" % ( name , k ) , cls ) <EOL> for k , v in definition . items ( ) } <EOL> def copy ( self ) : <EOL> """<STR_LIT>""" <EOL> cls = type ( self ) <EOL> result = cls ( { } , name = self . _name , parent_value = self . _parent_value ) <EOL> for k , v in self . _data . items ( ) : <EOL> result . _data [ k ] = v . copy ( ) <EOL> return result <EOL> @ classmethod <EOL> def loads ( cls , text ) : <EOL> name = cls . __name__ . lower ( ) <EOL> if name == "<STR_LIT>" : <EOL> name = "<STR_LIT>" <EOL> return cls ( yaml . load ( text ) or { } ) <EOL> def validate ( self ) : <EOL> for field in self . fields : <EOL> child = self . _data [ field ] <EOL> child . validate ( ) <EOL> @ property <EOL> def fields ( self ) : <EOL> return sorted ( list ( self . _data . keys ( ) ) ) <EOL> def remove ( self , item ) : <EOL> if not isinstance ( item , ( list , tuple , set ) ) : <EOL> item = [ item ] <EOL> for it in item : <EOL> it = str ( it ) <EOL> self . _data . pop ( it , None ) <EOL> def clear ( self ) : <EOL> self . _data = { } <EOL> def _check_field ( self , field ) : <EOL> if field not in self . _data : <EOL> raise ConanException ( undefined_field ( self . _name , field , self . fields , <EOL> self . _parent_value ) ) <EOL> def __getattr__ ( self , field ) : <EOL> assert field [ <NUM_LIT:0> ] != "<STR_LIT:_>" , "<STR_LIT>" % field <EOL> self . _check_field ( field ) <EOL> return self . _data [ field ] <EOL> def __setattr__ ( self , field , value ) : <EOL> if field [ <NUM_LIT:0> ] == "<STR_LIT:_>" or field . startswith ( "<STR_LIT>" ) : <EOL> return super ( ConfigDict , self ) . __setattr__ ( field , value ) <EOL> self . _check_field ( field ) <EOL> self . _data [ field ] . value = value <EOL> @ property <EOL> def values ( self ) : <EOL> return Values . from_list ( self . values_list ) <EOL> @ property <EOL> def values_list ( self ) : <EOL> result = [ ] <EOL> for field in self . fields : <EOL> config_item = self . _data [ field ] <EOL> result . extend ( config_item . values_list ) <EOL> return result <EOL> def items ( self ) : <EOL> return self . values_list <EOL> def iteritems ( self ) : <EOL> return self . values_list <EOL> @ values_list . setter <EOL> def values_list ( self , vals ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( vals , list ) , vals <EOL> for ( name , value ) in vals : <EOL> list_settings = name . split ( "<STR_LIT:.>" ) <EOL> attr = self <EOL> for setting in list_settings [ : - <NUM_LIT:1> ] : <EOL> attr = getattr ( attr , setting ) <EOL> setattr ( attr , list_settings [ - <NUM_LIT:1> ] , str ( value ) ) <EOL> @ values . setter <EOL> def values ( self , vals ) : <EOL> assert isinstance ( vals , Values ) <EOL> self . values_list = vals . as_list ( ) </s>
