<s> from inflection import tableize <EOL> from . errors import AlreadyRegisteredError <EOL> import remodel . models <EOL> from . registry import index_registry <EOL> from . related import ( HasOneDescriptor , BelongsToDescriptor , HasManyDescriptor , <EOL> HasAndBelongsToManyDescriptor ) <EOL> class FieldHandlerBase ( type ) : <EOL> def __new__ ( cls , name , bases , dct ) : <EOL> if not all ( isinstance ( dct [ rel_type ] , tuple ) for rel_type in remodel . models . REL_TYPES ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> model = dct . pop ( '<STR_LIT>' ) <EOL> dct [ '<STR_LIT>' ] , dct [ '<STR_LIT>' ] = set ( ) , set ( ) <EOL> for rel in dct . pop ( '<STR_LIT>' ) : <EOL> if isinstance ( rel , tuple ) : <EOL> other , field , lkey , rkey = rel <EOL> else : <EOL> other = rel <EOL> field , lkey , rkey = other . lower ( ) , '<STR_LIT:id>' , '<STR_LIT>' % model . lower ( ) <EOL> dct [ field ] = HasOneDescriptor ( other , lkey , rkey ) <EOL> dct [ '<STR_LIT>' ] . add ( field ) <EOL> index_registry . register ( other , rkey ) <EOL> for rel in dct . pop ( '<STR_LIT>' ) : <EOL> if isinstance ( rel , tuple ) : <EOL> other , field , lkey , rkey = rel <EOL> else : <EOL> other = rel <EOL> field , lkey , rkey = other . lower ( ) , '<STR_LIT>' % other . lower ( ) , '<STR_LIT:id>' <EOL> dct [ field ] = BelongsToDescriptor ( other , lkey , rkey ) <EOL> dct [ '<STR_LIT>' ] . add ( field ) <EOL> dct [ '<STR_LIT>' ] . add ( lkey ) <EOL> index_registry . register ( model , lkey ) <EOL> for rel in dct . pop ( '<STR_LIT>' ) : <EOL> if isinstance ( rel , tuple ) : <EOL> other , field , lkey , rkey = rel <EOL> else : <EOL> other = rel <EOL> field , lkey , rkey = tableize ( other ) , '<STR_LIT:id>' , '<STR_LIT>' % model . lower ( ) <EOL> dct [ field ] = HasManyDescriptor ( other , lkey , rkey ) <EOL> dct [ '<STR_LIT>' ] . add ( field ) <EOL> index_registry . register ( other , rkey ) <EOL> for rel in dct . pop ( '<STR_LIT>' ) : <EOL> if isinstance ( rel , tuple ) : <EOL> other , field , lkey , rkey = rel <EOL> else : <EOL> other = rel <EOL> field , lkey , rkey = tableize ( other ) , '<STR_LIT:id>' , '<STR_LIT:id>' <EOL> join_model = '<STR_LIT:_>' + '<STR_LIT>' . join ( sorted ( [ model , other ] ) ) <EOL> try : <EOL> remodel . models . ModelBase ( join_model , ( remodel . models . Model , ) , { } ) <EOL> except AlreadyRegisteredError : <EOL> pass <EOL> mlkey , mrkey = '<STR_LIT>' % model . lower ( ) , '<STR_LIT>' % other . lower ( ) <EOL> dct [ field ] = HasAndBelongsToManyDescriptor ( other , lkey , rkey , join_model , mlkey , mrkey ) <EOL> dct [ '<STR_LIT>' ] . add ( field ) <EOL> index_registry . register ( join_model , mlkey ) <EOL> index_registry . register ( join_model , mrkey ) <EOL> return super ( FieldHandlerBase , cls ) . __new__ ( cls , name , bases , dct ) <EOL> class FieldHandler ( object ) : <EOL> def __getattribute__ ( self , name ) : <EOL> if name in super ( FieldHandler , self ) . __getattribute__ ( '<STR_LIT>' ) : <EOL> raise AttributeError ( '<STR_LIT>' % name ) <EOL> return super ( FieldHandler , self ) . __getattribute__ ( name ) <EOL> def __setattr__ ( self , name , value ) : <EOL> if name in self . restricted : <EOL> raise AttributeError ( '<STR_LIT>' % name ) <EOL> super ( FieldHandler , self ) . __setattr__ ( name , value ) <EOL> def __delattr__ ( self , name ) : <EOL> if name in self . restricted : <EOL> raise AttributeError ( '<STR_LIT>' % name ) <EOL> super ( FieldHandler , self ) . __delattr__ ( name ) <EOL> def as_dict ( self ) : <EOL> return { field : self . __dict__ [ field ] for field in self . __dict__ <EOL> if not field . startswith ( '<STR_LIT:_>' ) } </s>
