#NEWLINE# def fit_to_schema( doc , schema ) : #NEWLINE# #INDENT# def log( msg ) : #NEWLINE# #INDENT# raise SchemaMismatchException ( ( 'doc-schema mismatch: %s (%s)' % ( msg , doc ) ) ) #NEWLINE# #UNINDENT# if ( schema is None ) : #NEWLINE# #INDENT# if doc : #NEWLINE# #INDENT# log ( ( '%s is not null' % doc ) ) #NEWLINE# #UNINDENT# return None #NEWLINE# #UNINDENT# if isinstance ( schema , list ) : #NEWLINE# #INDENT# if ( not doc ) : #NEWLINE# #INDENT# doc = [ ] #NEWLINE# #UNINDENT# if ( not isinstance ( doc , list ) ) : #NEWLINE# #INDENT# doc = [ doc ] #NEWLINE# #UNINDENT# answ = [ ] #NEWLINE# ( schema_ , ) = schema #NEWLINE# for doc_ in doc : #NEWLINE# #INDENT# answ . append ( fit_to_schema ( doc_ , schema_ ) ) #NEWLINE# #UNINDENT# return answ #NEWLINE# #UNINDENT# if isinstance ( doc_ , dict ) : #NEWLINE# #INDENT# if ( not doc ) : #NEWLINE# #INDENT# doc = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# if ( not isinstance ( doc , dict ) ) : #NEWLINE# #INDENT# doc = { #NEWLINE# #INDENT# '' : doc , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# doc_keys = set ( doc . keys ( ) ) #NEWLINE# schema_keys = set ( schema . keys ( ) ) #NEWLINE# if ( doc_keys - schema_keys ) : #NEWLINE# #INDENT# log ( ( "doc has keys not in schema: '%s'" % "', '" . join ( ( doc_keys - schema_keys ) ) ) ) #NEWLINE# #UNINDENT# answ = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for key in schema : #NEWLINE# #INDENT# if doc . has_key ( key ) : #NEWLINE# #INDENT# answ [ key ] = fit_to_schema ( doc . get ( key ) , schema [ key ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# answ [ key ] = render_never_was ( schema [ key ] ) #NEWLINE# #UNINDENT# #UNINDENT# return answ #NEWLINE# #UNINDENT# if ( schema == 'string' ) : #NEWLINE# #INDENT# if ( not doc ) : #NEWLINE# #INDENT# doc = '' #NEWLINE# #UNINDENT# if ( not isinstance ( doc , basestring ) ) : #NEWLINE# #INDENT# doc = unicode ( doc ) #NEWLINE# #UNINDENT# return doc