<s> from couchdbkit . exceptions import DocTypeError <EOL> def schema_map ( schema , dynamic_properties ) : <EOL> if hasattr ( schema , "<STR_LIT>" ) and hasattr ( schema , '<STR_LIT>' ) : <EOL> schema = { schema . _doc_type : schema } <EOL> elif isinstance ( schema , list ) : <EOL> schema = dict ( ( s . _doc_type , s ) for s in schema ) <EOL> if dynamic_properties is not None : <EOL> for name , cls in schema . items ( ) : <EOL> if cls . _allow_dynamic_properties != dynamic_properties : <EOL> schema [ name ] = type ( cls . __name__ , ( cls , ) , { <EOL> '<STR_LIT>' : dynamic_properties , <EOL> } ) <EOL> return schema <EOL> def doctype_attr_of ( classes ) : <EOL> doc_type_attrs = set ( cls . _doc_type_attr for cls in classes ) <EOL> assert len ( doc_type_attrs ) == <NUM_LIT:1> , "<STR_LIT>" <EOL> return doc_type_attrs . pop ( ) <EOL> def get_multi_wrapper ( classes ) : <EOL> doctype_attr = doctype_attr_of ( classes . values ( ) ) <EOL> def wrap ( doc ) : <EOL> doc_type = doc . get ( doctype_attr ) <EOL> try : <EOL> cls = classes [ doc_type ] <EOL> except KeyError : <EOL> raise DocTypeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( doc_type ) <EOL> ) <EOL> return cls . wrap ( doc ) <EOL> return wrap <EOL> def schema_wrapper ( schema , dynamic_properties = None ) : <EOL> if hasattr ( schema , "<STR_LIT>" ) and hasattr ( schema , '<STR_LIT>' ) and not dynamic_properties : <EOL> return schema . wrap <EOL> mapping = schema_map ( schema , dynamic_properties ) <EOL> return get_multi_wrapper ( mapping ) <EOL> def maybe_schema_wrapper ( schema , params ) : <EOL> dynamic_properties = params . pop ( '<STR_LIT>' , None ) <EOL> return schema_wrapper ( schema , dynamic_properties ) </s>
