<s> """<STR_LIT>""" <EOL> import re <EOL> import logging <EOL> import collections <EOL> from datetime import timedelta <EOL> from six import string_types <EOL> from flask import request , current_app <EOL> try : <EOL> from flask import _app_ctx_stack as stack <EOL> except ImportError : <EOL> from flask import _request_ctx_stack as stack <EOL> if not hasattr ( logging , '<STR_LIT>' ) : <EOL> class NullHandler ( logging . Handler ) : <EOL> def emit ( self , record ) : <EOL> pass <EOL> logging . NullHandler = NullHandler <EOL> ACL_ORIGIN = '<STR_LIT>' <EOL> ACL_METHODS = '<STR_LIT>' <EOL> ACL_ALLOW_HEADERS = '<STR_LIT>' <EOL> ACL_EXPOSE_HEADERS = '<STR_LIT>' <EOL> ACL_CREDENTIALS = '<STR_LIT>' <EOL> ACL_MAX_AGE = '<STR_LIT>' <EOL> ACL_REQUEST_METHOD = '<STR_LIT>' <EOL> ACL_REQUEST_HEADERS = '<STR_LIT>' <EOL> ALL_METHODS = [ '<STR_LIT:GET>' , '<STR_LIT>' , '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> CONFIG_OPTIONS = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> FLASK_CORS_EVALUATED = '<STR_LIT>' <EOL> RegexObject = type ( re . compile ( '<STR_LIT>' ) ) <EOL> DEFAULT_OPTIONS = dict ( origins = '<STR_LIT:*>' , <EOL> methods = ALL_METHODS , <EOL> allow_headers = '<STR_LIT:*>' , <EOL> automatic_options = True , <EOL> send_wildcard = False , <EOL> vary_header = True , <EOL> supports_credentials = False , <EOL> resources = r'<STR_LIT>' , <EOL> intercept_exceptions = True ) <EOL> def parse_resources ( resources ) : <EOL> if isinstance ( resources , dict ) : <EOL> resources = [ ( re_fix ( k ) , v ) for k , v in resources . items ( ) ] <EOL> def pattern_length ( pair ) : <EOL> maybe_regex , _ = pair <EOL> return len ( get_regexp_pattern ( maybe_regex ) ) <EOL> return sorted ( resources , <EOL> key = pattern_length , <EOL> reverse = True ) <EOL> elif isinstance ( resources , string_types ) : <EOL> return [ ( re_fix ( resources ) , { } ) ] <EOL> elif isinstance ( resources , collections . Iterable ) : <EOL> return [ ( re_fix ( r ) , { } ) for r in resources ] <EOL> elif isinstance ( resources , RegexObject ) : <EOL> return [ ( re_fix ( resources ) , { } ) ] <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def get_regexp_pattern ( regexp ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return regexp . pattern <EOL> except AttributeError : <EOL> return str ( regexp ) <EOL> def get_cors_origin ( options , request_origin ) : <EOL> origins = options . get ( '<STR_LIT>' ) <EOL> wildcard = r'<STR_LIT>' in origins <EOL> if request_origin : <EOL> debugLog ( "<STR_LIT>" , request_origin ) <EOL> if wildcard and options . get ( '<STR_LIT>' ) : <EOL> debugLog ( "<STR_LIT>" ) <EOL> return '<STR_LIT:*>' <EOL> elif try_match_any ( request_origin , origins ) : <EOL> debugLog ( "<STR_LIT>" ) <EOL> return request_origin <EOL> else : <EOL> debugLog ( "<STR_LIT>" , <EOL> map ( get_regexp_pattern , origins ) ) <EOL> return None <EOL> else : <EOL> debugLog ( "<STR_LIT>" ) <EOL> return None <EOL> def get_allow_headers ( options , acl_request_headers ) : <EOL> if acl_request_headers : <EOL> request_headers = [ h . strip ( ) for h in acl_request_headers . split ( '<STR_LIT:U+002C>' ) ] <EOL> matching_headers = filter ( <EOL> lambda h : try_match_any ( h , options . get ( '<STR_LIT>' ) ) , <EOL> request_headers <EOL> ) <EOL> return '<STR_LIT:U+002CU+0020>' . join ( sorted ( matching_headers ) ) <EOL> return None <EOL> def get_cors_headers ( options , request_headers , request_method , response_headers ) : <EOL> origin_to_set = get_cors_origin ( options , request_headers . get ( '<STR_LIT>' ) ) <EOL> headers = { } <EOL> if origin_to_set is None : <EOL> return headers <EOL> infoLog ( "<STR_LIT>" , <EOL> request_headers . get ( '<STR_LIT>' ) , ACL_ORIGIN , origin_to_set ) <EOL> headers [ ACL_ORIGIN ] = origin_to_set <EOL> headers [ ACL_EXPOSE_HEADERS ] = options . get ( '<STR_LIT>' ) <EOL> if options . get ( '<STR_LIT>' ) : <EOL> headers [ ACL_CREDENTIALS ] = '<STR_LIT:true>' <EOL> if request_method == '<STR_LIT>' : <EOL> acl_request_method = request_headers . get ( ACL_REQUEST_METHOD , '<STR_LIT>' ) . upper ( ) <EOL> if acl_request_method and acl_request_method in options . get ( '<STR_LIT>' ) : <EOL> headers [ ACL_ALLOW_HEADERS ] = get_allow_headers ( options , request_headers . get ( ACL_REQUEST_HEADERS ) ) <EOL> headers [ ACL_MAX_AGE ] = options . get ( '<STR_LIT>' ) <EOL> headers [ ACL_METHODS ] = options . get ( '<STR_LIT>' ) <EOL> else : <EOL> infoLog ( "<STR_LIT>" , <EOL> acl_request_method , options . get ( '<STR_LIT>' ) ) <EOL> if options . get ( '<STR_LIT>' ) : <EOL> if headers [ ACL_ORIGIN ] != '<STR_LIT:*>' and len ( options . get ( '<STR_LIT>' ) ) > <NUM_LIT:1> : <EOL> vary = [ '<STR_LIT>' , response_headers . get ( '<STR_LIT>' ) ] <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT:U+002CU+0020>' . join ( v for v in vary if v is not None ) <EOL> return dict ( ( k , v ) for k , v in headers . items ( ) if v ) <EOL> def set_cors_headers ( resp , options ) : <EOL> '''<STR_LIT>''' <EOL> if hasattr ( resp , FLASK_CORS_EVALUATED ) : <EOL> debugLog ( '<STR_LIT>' ) <EOL> return resp <EOL> headers_to_set = get_cors_headers ( options , <EOL> request . headers , <EOL> request . method , <EOL> resp . headers ) <EOL> debugLog ( '<STR_LIT>' , str ( headers_to_set ) ) <EOL> for k , v in headers_to_set . items ( ) : <EOL> resp . headers [ k ] = v <EOL> return resp <EOL> def re_fix ( reg ) : <EOL> '''<STR_LIT>''' <EOL> return r'<STR_LIT>' if reg == r'<STR_LIT:*>' else reg <EOL> def try_match_any ( inst , patterns ) : <EOL> return any ( try_match ( inst , pattern ) for pattern in patterns ) <EOL> def try_match ( request_origin , pattern ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> if isinstance ( pattern , RegexObject ) : <EOL> return re . match ( pattern , request_origin ) <EOL> else : <EOL> return re . match ( pattern , request_origin , flags = re . IGNORECASE ) <EOL> except : <EOL> return request_origin == pattern <EOL> def get_cors_options ( appInstance , * dicts ) : <EOL> '''<STR_LIT>''' <EOL> options = DEFAULT_OPTIONS . copy ( ) <EOL> options . update ( get_app_kwarg_dict ( appInstance ) ) <EOL> if dicts : <EOL> for d in dicts : <EOL> options . update ( d ) <EOL> return serialize_options ( options ) <EOL> def get_app_kwarg_dict ( appInstance = None ) : <EOL> '''<STR_LIT>''' <EOL> app = ( appInstance or current_app ) <EOL> return dict ( <EOL> ( k . lower ( ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) , app . config . get ( k ) ) <EOL> for k in CONFIG_OPTIONS <EOL> if app . config . get ( k ) is not None <EOL> ) <EOL> def flexible_str ( obj ) : <EOL> '''<STR_LIT>''' <EOL> if ( not isinstance ( obj , string_types ) <EOL> and isinstance ( obj , collections . Iterable ) ) : <EOL> return '<STR_LIT:U+002CU+0020>' . join ( str ( item ) for item in sorted ( obj ) ) <EOL> else : <EOL> return str ( obj ) <EOL> def serialize_option ( options_dict , key , upper = False ) : <EOL> if key in options_dict : <EOL> value = flexible_str ( options_dict [ key ] ) <EOL> options_dict [ key ] = value . upper ( ) if upper else value <EOL> def ensure_iterable ( inst ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( inst , string_types ) : <EOL> return [ inst ] <EOL> elif not isinstance ( inst , collections . Iterable ) : <EOL> return [ inst ] <EOL> else : <EOL> return inst <EOL> def sanitize_regex_param ( param ) : <EOL> return [ re_fix ( x ) for x in ensure_iterable ( param ) ] <EOL> def serialize_options ( opts ) : <EOL> '''<STR_LIT>''' <EOL> options = ( opts or { } ) . copy ( ) <EOL> options [ '<STR_LIT>' ] = sanitize_regex_param ( options . get ( '<STR_LIT>' ) ) <EOL> options [ '<STR_LIT>' ] = sanitize_regex_param ( options . get ( '<STR_LIT>' ) ) <EOL> if r'<STR_LIT>' in options [ '<STR_LIT>' ] and options [ '<STR_LIT>' ] and options [ '<STR_LIT>' ] : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> serialize_option ( options , '<STR_LIT>' ) <EOL> serialize_option ( options , '<STR_LIT>' , upper = True ) <EOL> if isinstance ( options . get ( '<STR_LIT>' ) , timedelta ) : <EOL> options [ '<STR_LIT>' ] = str ( int ( options [ '<STR_LIT>' ] . total_seconds ( ) ) ) <EOL> return options <EOL> def getLogger ( app = None ) : <EOL> '''<STR_LIT>''' <EOL> if stack . top is not None : <EOL> return logging . getLogger ( "<STR_LIT>" % current_app . logger_name ) <EOL> elif app is not None : <EOL> return logging . getLogger ( "<STR_LIT>" % app . logger_name ) <EOL> else : <EOL> return logging . getLogger ( "<STR_LIT>" ) <EOL> def debugLog ( * args , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> getLogger ( ) . debug ( * args , ** kwargs ) <EOL> def infoLog ( * args , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> getLogger ( ) . info ( * args , ** kwargs ) </s>
