<s> import sys <EOL> import json <EOL> import multiprocessing <EOL> from time import sleep <EOL> from hodor . exceptions import * <EOL> from apiclient . errors import HttpError <EOL> from socket import error as socket_error <EOL> from hodor . cli import Context <EOL> def gme_exc_handler ( tries_remaining , exception , delay , args ) : <EOL> """<STR_LIT>""" <EOL> ctx = args [ <NUM_LIT:0> ] <EOL> if isinstance ( exception , HttpError ) : <EOL> if exception . resp . status == <NUM_LIT> : <EOL> print "<STR_LIT>" <EOL> if not isinstance ( ctx , Context ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> ctx . refresh_services ( ) <EOL> elif exception . resp . status == <NUM_LIT> : <EOL> raise NoContent ( ) <EOL> elif exception . resp . status in [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] : <EOL> content = json . loads ( exception . content ) <EOL> if content [ '<STR_LIT:error>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> raise QueryTooExpensive ( "<STR_LIT>" % ( content [ '<STR_LIT:error>' ] [ '<STR_LIT:message>' ] ) ) <EOL> elif content [ '<STR_LIT:error>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> print content <EOL> if content [ '<STR_LIT:error>' ] [ '<STR_LIT:message>' ] == "<STR_LIT>" : <EOL> raise BackendError ( "<STR_LIT>" % ( content [ '<STR_LIT:error>' ] [ '<STR_LIT:message>' ] ) ) <EOL> else : <EOL> pass <EOL> elif content [ '<STR_LIT:error>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> raise TableTooLarge ( "<STR_LIT>" % ( content [ '<STR_LIT:error>' ] [ '<STR_LIT:message>' ] ) ) <EOL> elif content [ '<STR_LIT:error>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> raise InternalServerError ( "<STR_LIT:%s>" % ( content [ '<STR_LIT:error>' ] [ '<STR_LIT:message>' ] ) ) <EOL> elif content [ '<STR_LIT:error>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> pass <EOL> elif exception . resp . status not in [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] : <EOL> raise exception <EOL> if isinstance ( ctx , Context ) : <EOL> ctx . log ( "<STR_LIT>" % ( exception , tries_remaining , delay ) ) <EOL> else : <EOL> if getattr ( exception , '<STR_LIT:content>' , None ) is not None : <EOL> content = json . loads ( exception . content ) <EOL> message = content [ '<STR_LIT:error>' ] [ '<STR_LIT:message>' ] + "<STR_LIT:U+0020>" + content [ '<STR_LIT:error>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> print >> sys . stderr , "<STR_LIT>" % ( message , exception . resp . status , tries_remaining , round ( delay , <NUM_LIT:2> ) ) <EOL> else : <EOL> raise exception <EOL> def example_exc_handler ( tries_remaining , exception , delay ) : <EOL> """<STR_LIT>""" <EOL> print >> sys . stderr , "<STR_LIT>" % ( exception , tries_remaining , delay ) <EOL> def retries ( max_tries , delay = <NUM_LIT:1> , backoff = <NUM_LIT> , exceptions = ( Exception , HttpError , socket_error ) , hook = gme_exc_handler ) : <EOL> """<STR_LIT>""" <EOL> def dec ( func ) : <EOL> def f2 ( * args , ** kwargs ) : <EOL> mydelay = delay <EOL> tries = range ( max_tries ) <EOL> tries . reverse ( ) <EOL> for tries_remaining in tries : <EOL> try : <EOL> return func ( * args , ** kwargs ) <EOL> except exceptions as e : <EOL> if tries_remaining > <NUM_LIT:0> : <EOL> if hook is not None : <EOL> hook ( tries_remaining , e , mydelay , args ) <EOL> sleep ( mydelay ) <EOL> mydelay = mydelay * backoff <EOL> else : <EOL> raise e <EOL> else : <EOL> break <EOL> return f2 <EOL> return dec </s>
