<s> import base64 <EOL> from copy import copy <EOL> import hashlib <EOL> from mimetypes import guess_type <EOL> import datetime <EOL> import threading <EOL> class CouchAttachmentsBuilder ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , original = None ) : <EOL> self . _dict = original or { } <EOL> @ staticmethod <EOL> def couch_style_digest ( data ) : <EOL> return '<STR_LIT>' . format ( base64 . b64encode ( hashlib . md5 ( data ) . digest ( ) ) ) <EOL> def no_change ( self , name , data ) : <EOL> return ( <EOL> self . _dict . get ( name ) and <EOL> self . _dict [ name ] . get ( '<STR_LIT>' ) == self . couch_style_digest ( data ) <EOL> ) <EOL> def add ( self , content , name = None , content_type = None ) : <EOL> if hasattr ( content , '<STR_LIT>' ) : <EOL> if hasattr ( content , '<STR_LIT>' ) : <EOL> content . seek ( <NUM_LIT:0> ) <EOL> data = content . read ( ) <EOL> else : <EOL> data = content <EOL> if isinstance ( data , unicode ) : <EOL> data = data . encode ( '<STR_LIT:utf-8>' ) <EOL> if content_type is None : <EOL> content_type = '<STR_LIT:;>' . join ( filter ( None , guess_type ( name ) ) ) <EOL> if self . no_change ( name , data ) : <EOL> self . _dict [ name ] . update ( { <EOL> '<STR_LIT>' : content_type , <EOL> } ) <EOL> else : <EOL> self . _dict [ name ] = { <EOL> '<STR_LIT:data>' : base64 . b64encode ( data ) , <EOL> '<STR_LIT>' : content_type , <EOL> } <EOL> def remove ( self , name ) : <EOL> """<STR_LIT>""" <EOL> del self . _dict [ name ] <EOL> def to_json ( self ) : <EOL> return copy ( self . _dict ) <EOL> class PaginateViewLogHandler ( object ) : <EOL> def log ( self , message ) : <EOL> pass <EOL> def view_starting ( self , db , view_name , kwargs , total_emitted ) : <EOL> self . log ( u'<STR_LIT>' . format ( <EOL> total_emitted , <EOL> total_emitted + kwargs [ '<STR_LIT>' ] - <NUM_LIT:1> ) <EOL> ) <EOL> startkey = kwargs . get ( '<STR_LIT>' ) <EOL> self . log ( u'<STR_LIT>' . format ( startkey , kwargs . get ( '<STR_LIT>' ) ) ) <EOL> def view_ending ( self , db , view_name , kwargs , total_emitted , time ) : <EOL> self . log ( '<STR_LIT>' . format ( time ) ) <EOL> def paginate_view ( db , view_name , chunk_size , <EOL> log_handler = PaginateViewLogHandler ( ) , ** view_kwargs ) : <EOL> """<STR_LIT>""" <EOL> if view_kwargs . get ( '<STR_LIT>' , True ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in view_kwargs : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in view_kwargs : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> view_kwargs [ '<STR_LIT>' ] = chunk_size <EOL> total_emitted = <NUM_LIT:0> <EOL> len_results = - <NUM_LIT:1> <EOL> while len_results : <EOL> log_handler . view_starting ( db , view_name , view_kwargs , total_emitted ) <EOL> start_time = datetime . datetime . utcnow ( ) <EOL> results = db . view ( view_name , ** view_kwargs ) <EOL> len_results = len ( results ) <EOL> for result in results : <EOL> yield result <EOL> total_emitted += len_results <EOL> log_handler . view_ending ( db , view_name , view_kwargs , total_emitted , <EOL> datetime . datetime . utcnow ( ) - start_time ) <EOL> if len_results : <EOL> view_kwargs [ '<STR_LIT>' ] = result [ '<STR_LIT:key>' ] <EOL> view_kwargs [ '<STR_LIT>' ] = result [ '<STR_LIT:id>' ] <EOL> view_kwargs [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> _override_db = threading . local ( ) <EOL> class OverrideDB ( object ) : <EOL> def __init__ ( self , document_class , database ) : <EOL> self . document_class = document_class <EOL> self . database = database <EOL> self . orig_database = None <EOL> self . orig_get_db = None <EOL> if not hasattr ( _override_db , '<STR_LIT>' ) : <EOL> _override_db . class_to_db = { } <EOL> def __enter__ ( self ) : <EOL> try : <EOL> self . orig_database = _override_db . class_to_db [ self . document_class ] <EOL> except KeyError : <EOL> self . orig_get_db = self . document_class . get_db <EOL> self . document_class . get_db = classmethod ( _get_db ) <EOL> _override_db . class_to_db [ self . document_class ] = self . database <EOL> def __exit__ ( self , exc_type , exc_val , exc_tb ) : <EOL> if self . orig_database is not None : <EOL> _override_db . class_to_db [ self . document_class ] = self . orig_database <EOL> else : <EOL> assert self . orig_get_db <EOL> del _override_db . class_to_db [ self . document_class ] <EOL> self . document_class . get_db = self . orig_get_db <EOL> def _get_db ( cls ) : <EOL> return _override_db . class_to_db [ cls ] </s>
