<s> import logging <EOL> from lampost . datastore . classes import set_dbo_class , get_dbo_class <EOL> from lampost . datastore . dbofield import DBOTField , DBOField <EOL> log = logging . getLogger ( __name__ ) <EOL> class CommonMeta ( type ) : <EOL> dbo_fields = { } <EOL> load_funcs = [ ] <EOL> class_providers = set ( ) <EOL> def __init__ ( cls , name , bases , new_attrs ) : <EOL> cls . _meta_init_attrs ( new_attrs ) <EOL> cls . _combine_base_fields ( bases ) <EOL> cls . _update_dbo_fields ( new_attrs ) <EOL> if getattr ( cls , '<STR_LIT>' , None ) : <EOL> cls . _template_init ( ) <EOL> cls . _update_actions ( new_attrs ) <EOL> if '<STR_LIT>' in new_attrs : <EOL> set_dbo_class ( cls . class_id , cls ) <EOL> elif '<STR_LIT>' in new_attrs : <EOL> cls . class_id = cls . dbo_key_type <EOL> set_dbo_class ( cls . class_id , cls ) <EOL> def add_dbo_fields ( cls , new_fields ) : <EOL> cls . _meta_init_attrs ( new_fields ) <EOL> cls . _update_dbo_fields ( new_fields ) <EOL> for name , dbo_field in new_fields . items ( ) : <EOL> setattr ( cls , name , dbo_field ) <EOL> def _meta_init_attrs ( cls , new_attrs ) : <EOL> for name , attr in new_attrs . items ( ) : <EOL> try : <EOL> attr . meta_init ( name ) <EOL> except AttributeError : <EOL> pass <EOL> def _combine_base_fields ( cls , bases ) : <EOL> cls . dbot_fields = { } <EOL> cls . dbo_fields = { } <EOL> cls . load_funcs = [ ] <EOL> cls . class_providers = set ( ) <EOL> for base in bases : <EOL> cls . dbo_fields . update ( { name : dbo_field for name , dbo_field in base . dbo_fields . items ( ) } ) <EOL> cls . load_funcs . extend ( base . load_funcs ) <EOL> cls . class_providers . update ( base . class_providers ) <EOL> cls . dbot_fields . update ( { name : dbot_field for name , dbot_field in base . dbot_fields . items ( ) } ) <EOL> def _update_dbo_fields ( cls , new_attrs ) : <EOL> for name , attr in new_attrs . items ( ) : <EOL> if hasattr ( attr , '<STR_LIT>' ) : <EOL> old_attr = cls . dbo_fields . get ( name ) <EOL> if old_attr != attr : <EOL> if old_attr and old_attr . default == attr . default : <EOL> log . warn ( "<STR_LIT>" , name , cls . __name__ ) <EOL> elif old_attr and old_attr . default : <EOL> log . info ( "<STR_LIT>" , name , cls . __name__ ) <EOL> cls . dbo_fields [ name ] = attr <EOL> elif isinstance ( attr , DBOTField ) : <EOL> cls . dbot_fields [ name ] = attr <EOL> load_func = new_attrs . get ( '<STR_LIT>' ) <EOL> if load_func : <EOL> cls . load_funcs . append ( load_func ) <EOL> def _template_init ( cls ) : <EOL> template_cls = get_dbo_class ( cls . template_id ) <EOL> old_class = getattr ( template_cls , '<STR_LIT>' , None ) <EOL> if old_class : <EOL> existing_fields = old_class . dbot_fields . values ( ) <EOL> log . info ( "<STR_LIT>" , old_class . __name__ , cls . __name__ , <EOL> cls . template_id ) <EOL> else : <EOL> log . info ( "<STR_LIT>" , cls . __name__ , cls . template_id ) <EOL> existing_fields = ( ) <EOL> new_dbo_fields = { name : DBOField ( * dbo_field . args , ** dbo_field . kwargs ) for name , dbo_field in <EOL> cls . dbot_fields . items ( ) if dbo_field not in existing_fields } <EOL> template_cls . add_dbo_fields ( new_dbo_fields ) <EOL> template_cls . instance_cls = cls <EOL> cls . template_cls = template_cls <EOL> def _update_actions ( cls , new_attrs ) : <EOL> cls . class_providers . update ( { func . __name__ for func in new_attrs . values ( ) if hasattr ( func , '<STR_LIT>' ) } ) <EOL> def call_mro ( self , func_name , * args , ** kwargs ) : <EOL> for cls in reversed ( self . __class__ . __mro__ ) : <EOL> try : <EOL> cls . __dict__ [ func_name ] ( self , * args , ** kwargs ) <EOL> except KeyError : <EOL> pass </s>
