<s> """<STR_LIT>""" <EOL> from inspect import getmro <EOL> from importlib import import_module <EOL> class Database ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , db_driver , ** kwrds ) : <EOL> mod = import_module ( '<STR_LIT>' + db_driver , __package__ ) <EOL> Backend = getattr ( mod , "<STR_LIT>" ) <EOL> self . backend = Backend ( ** kwrds ) <EOL> def ensure_table ( self , cls ) : <EOL> return self . backend . ensure_table ( cls ) <EOL> def find_one ( self , cls , id ) : <EOL> return self . backend . find_one ( cls , id ) <EOL> def find_all ( self , cls ) : <EOL> return self . backend . find_all ( cls ) <EOL> def find_by_index ( self , cls , index_name , value ) : <EOL> return self . backend . find_by_index ( cls , index_name , value ) <EOL> def save ( self , obj ) : <EOL> self . backend . save ( obj ) <EOL> class _DatabaseMapping ( object ) : <EOL> def __init__ ( self ) : <EOL> self . clear_mappings ( ) <EOL> def add_mapping ( self , cls , db ) : <EOL> self . mapping [ cls ] = db <EOL> def get_mapping ( self , cls , no_mapping_ok = False ) : <EOL> db = None <EOL> for candidate_cls in getmro ( cls ) : <EOL> db = self . mapping . get ( candidate_cls , None ) <EOL> if db is not None : <EOL> break <EOL> if db is None : <EOL> db = self . default_database <EOL> if db is None : <EOL> if no_mapping_ok : <EOL> return None <EOL> raise ValueError ( "<STR_LIT>" % repr ( cls ) ) <EOL> return db <EOL> def clear_mappings ( self ) : <EOL> self . default_database = None <EOL> self . mapping = { } <EOL> _database_mapping = _DatabaseMapping ( ) <EOL> def default_database ( db ) : <EOL> """<STR_LIT>""" <EOL> _database_mapping . default_database = db <EOL> def class_database ( cls , db ) : <EOL> """<STR_LIT>""" <EOL> _database_mapping . add_mapping ( cls , db ) <EOL> def clear_database_config ( ) : <EOL> """<STR_LIT>""" <EOL> _database_mapping . clear_mappings ( ) <EOL> def get_mapping ( cls , no_mapping_ok = False ) : <EOL> """<STR_LIT>""" <EOL> return _database_mapping . get_mapping ( cls , no_mapping_ok ) </s>
