<s> """<STR_LIT>""" <EOL> from datetime import timedelta <EOL> from itertools import product <EOL> from flask import make_response , request , current_app <EOL> from functools import update_wrapper , wraps <EOL> from security_monkey . datastore import Account <EOL> from security_monkey . exceptions import BotoConnectionIssue <EOL> from functools import wraps <EOL> import boto <EOL> import botocore <EOL> import time <EOL> def crossdomain ( allowed_origins = None , methods = None , headers = None , <EOL> max_age = <NUM_LIT> , attach_to_all = True , <EOL> automatic_options = True ) : <EOL> """<STR_LIT>""" <EOL> if methods is not None : <EOL> methods = '<STR_LIT:U+002CU+0020>' . join ( sorted ( x . upper ( ) for x in methods ) ) <EOL> if headers is not None and not isinstance ( headers , basestring ) : <EOL> headers = '<STR_LIT:U+002CU+0020>' . join ( x . upper ( ) for x in headers ) <EOL> if not isinstance ( allowed_origins , basestring ) : <EOL> allowed_origins = '<STR_LIT:U+002CU+0020>' . join ( allowed_origins ) <EOL> if isinstance ( max_age , timedelta ) : <EOL> max_age = max_age . total_seconds ( ) <EOL> def get_origin ( allowed_origins ) : <EOL> origin = request . headers . get ( "<STR_LIT>" , None ) <EOL> if origin and current_app . config . get ( '<STR_LIT>' , False ) : <EOL> return origin <EOL> if origin and origin in allowed_origins : <EOL> return origin <EOL> return None <EOL> def get_methods ( ) : <EOL> if methods is not None : <EOL> return methods <EOL> options_resp = current_app . make_default_options_response ( ) <EOL> return options_resp . headers . get ( '<STR_LIT>' , '<STR_LIT:GET>' ) <EOL> def decorator ( f ) : <EOL> def wrapped_function ( * args , ** kwargs ) : <EOL> if automatic_options and request . method == '<STR_LIT>' : <EOL> resp = current_app . make_default_options_response ( ) <EOL> else : <EOL> resp = make_response ( f ( * args , ** kwargs ) ) <EOL> if not attach_to_all and request . method != '<STR_LIT>' : <EOL> return resp <EOL> h = resp . headers <EOL> h [ '<STR_LIT>' ] = get_origin ( allowed_origins ) <EOL> h [ '<STR_LIT>' ] = get_methods ( ) <EOL> h [ '<STR_LIT>' ] = str ( max_age ) <EOL> h [ '<STR_LIT>' ] = '<STR_LIT:true>' <EOL> h [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> return resp <EOL> f . provide_automatic_options = False <EOL> return update_wrapper ( wrapped_function , f ) <EOL> return decorator <EOL> def record_exception ( ) : <EOL> def decorator ( f ) : <EOL> @ wraps ( f ) <EOL> def decorated_function ( * args , ** kwargs ) : <EOL> try : <EOL> return f ( * args , ** kwargs ) <EOL> except Exception as e : <EOL> index = kwargs . get ( '<STR_LIT:index>' ) <EOL> account = kwargs . get ( '<STR_LIT>' ) <EOL> region = kwargs . get ( '<STR_LIT>' ) or kwargs . get ( '<STR_LIT>' ) <EOL> name = kwargs . get ( '<STR_LIT:name>' ) <EOL> exception_map = kwargs . get ( '<STR_LIT>' ) <EOL> exc = BotoConnectionIssue ( str ( e ) , index , account , name ) <EOL> if name : <EOL> exception_map [ ( index , account , region , name ) ] = exc <EOL> elif region : <EOL> exception_map [ ( index , account , region ) ] = exc <EOL> elif account : <EOL> exception_map [ ( index , account ) ] = exc <EOL> else : <EOL> exception_map [ ( index , ) ] = exc <EOL> return decorated_function <EOL> return decorator <EOL> def iter_account_region ( index = None , accounts = None , regions = None ) : <EOL> regions = regions or [ '<STR_LIT>' ] <EOL> def decorator ( f ) : <EOL> @ wraps ( f ) <EOL> def decorated_function ( * args , ** kwargs ) : <EOL> item_list = [ ] ; exception_map = { } <EOL> for account_name , region in product ( accounts , regions ) : <EOL> account = Account . query . filter ( Account . name == account_name ) . first ( ) <EOL> if not account : <EOL> print "<STR_LIT>" , account_name <EOL> return <EOL> kwargs [ '<STR_LIT:index>' ] = index <EOL> kwargs [ '<STR_LIT>' ] = account . name <EOL> kwargs [ '<STR_LIT>' ] = account . number <EOL> kwargs [ '<STR_LIT>' ] = region <EOL> kwargs [ '<STR_LIT>' ] = account . role_name or '<STR_LIT>' <EOL> itm , exc = f ( * args , ** kwargs ) <EOL> item_list . extend ( itm ) <EOL> exception_map . update ( exc ) <EOL> return item_list , exception_map <EOL> return decorated_function <EOL> return decorator </s>
