<s> """<STR_LIT>""" <EOL> from copy import deepcopy <EOL> from inspect import getsource <EOL> from itertools import groupby <EOL> from operator import attrgetter <EOL> from textwrap import dedent <EOL> from types import FunctionType <EOL> __all__ = [ '<STR_LIT>' ] <EOL> __docformat__ = '<STR_LIT>' <EOL> class ViewDefinition ( object ) : <EOL> r"""<STR_LIT>""" <EOL> def __init__ ( self , design , name , map_fun , reduce_fun = None , <EOL> language = '<STR_LIT>' , wrapper = None , options = None , <EOL> ** defaults ) : <EOL> """<STR_LIT>""" <EOL> if design . startswith ( '<STR_LIT>' ) : <EOL> design = design [ <NUM_LIT:8> : ] <EOL> self . design = design <EOL> self . name = name <EOL> if isinstance ( map_fun , FunctionType ) : <EOL> map_fun = _strip_decorators ( getsource ( map_fun ) . rstrip ( ) ) <EOL> self . map_fun = dedent ( map_fun . lstrip ( '<STR_LIT:\n>' ) ) <EOL> if isinstance ( reduce_fun , FunctionType ) : <EOL> reduce_fun = _strip_decorators ( getsource ( reduce_fun ) . rstrip ( ) ) <EOL> if reduce_fun : <EOL> reduce_fun = dedent ( reduce_fun . lstrip ( '<STR_LIT:\n>' ) ) <EOL> self . reduce_fun = reduce_fun <EOL> self . language = language <EOL> self . wrapper = wrapper <EOL> self . options = options <EOL> self . defaults = defaults <EOL> def __call__ ( self , db , ** options ) : <EOL> """<STR_LIT>""" <EOL> wrapper = options . pop ( '<STR_LIT>' , self . wrapper ) <EOL> merged_options = self . defaults . copy ( ) <EOL> merged_options . update ( options ) <EOL> return db . view ( '<STR_LIT:/>' . join ( [ self . design , self . name ] ) , <EOL> wrapper = wrapper , ** merged_options ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( type ( self ) . __name__ , '<STR_LIT:/>' . join ( [ <EOL> '<STR_LIT>' , self . design , '<STR_LIT>' , self . name <EOL> ] ) ) <EOL> def get_doc ( self , db ) : <EOL> """<STR_LIT>""" <EOL> return db . get ( '<STR_LIT>' % self . design ) <EOL> def sync ( self , db ) : <EOL> """<STR_LIT>""" <EOL> return type ( self ) . sync_many ( db , [ self ] ) <EOL> @ staticmethod <EOL> def sync_many ( db , views , remove_missing = False , callback = None ) : <EOL> """<STR_LIT>""" <EOL> docs = [ ] <EOL> views = sorted ( views , key = attrgetter ( '<STR_LIT>' ) ) <EOL> for design , views in groupby ( views , key = attrgetter ( '<STR_LIT>' ) ) : <EOL> doc_id = '<STR_LIT>' % design <EOL> doc = db . get ( doc_id , { '<STR_LIT>' : doc_id } ) <EOL> orig_doc = deepcopy ( doc ) <EOL> languages = set ( ) <EOL> missing = list ( doc . get ( '<STR_LIT>' , { } ) . keys ( ) ) <EOL> for view in views : <EOL> funcs = { '<STR_LIT>' : view . map_fun } <EOL> if view . reduce_fun : <EOL> funcs [ '<STR_LIT>' ] = view . reduce_fun <EOL> if view . options : <EOL> funcs [ '<STR_LIT>' ] = view . options <EOL> doc . setdefault ( '<STR_LIT>' , { } ) [ view . name ] = funcs <EOL> languages . add ( view . language ) <EOL> if view . name in missing : <EOL> missing . remove ( view . name ) <EOL> if remove_missing and missing : <EOL> for name in missing : <EOL> del doc [ '<STR_LIT>' ] [ name ] <EOL> elif missing and '<STR_LIT>' in doc : <EOL> languages . add ( doc [ '<STR_LIT>' ] ) <EOL> if len ( languages ) > <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' , list ( languages ) ) <EOL> doc [ '<STR_LIT>' ] = list ( languages ) [ <NUM_LIT:0> ] <EOL> if doc != orig_doc : <EOL> if callback is not None : <EOL> callback ( doc ) <EOL> docs . append ( doc ) <EOL> return db . update ( docs ) <EOL> def _strip_decorators ( code ) : <EOL> retval = [ ] <EOL> beginning = True <EOL> for line in code . splitlines ( ) : <EOL> if beginning and not line . isspace ( ) : <EOL> if line . lstrip ( ) . startswith ( '<STR_LIT:@>' ) : <EOL> continue <EOL> beginning = False <EOL> retval . append ( line ) <EOL> return '<STR_LIT:\n>' . join ( retval ) </s>
