<s> """<STR_LIT>""" <EOL> from functools import wraps <EOL> import logging <EOL> from flask import abort , current_app , url_for <EOL> from flask import redirect as _redirect <EOL> from flask . signals import Namespace <EOL> __version__ = '<STR_LIT>' <EOL> log = logging . getLogger ( u'<STR_LIT>' ) <EOL> RAISE_ERROR_ON_MISSING_FEATURES = u'<STR_LIT>' <EOL> FEATURE_FLAGS_CONFIG = u'<STR_LIT>' <EOL> EXTENSION_NAME = "<STR_LIT>" <EOL> class StopCheckingFeatureFlags ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class NoFeatureFlagFound ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> _ns = Namespace ( ) <EOL> missing_feature = _ns . signal ( '<STR_LIT>' ) <EOL> def AppConfigFlagHandler ( feature = None ) : <EOL> """<STR_LIT>""" <EOL> if not current_app : <EOL> log . warn ( u"<STR_LIT>" . format ( feature = feature ) ) <EOL> return False <EOL> try : <EOL> return current_app . config [ FEATURE_FLAGS_CONFIG ] [ feature ] <EOL> except ( AttributeError , KeyError ) : <EOL> raise NoFeatureFlagFound ( ) <EOL> class FeatureFlag ( object ) : <EOL> JINJA_TEST_NAME = u'<STR_LIT>' <EOL> def __init__ ( self , app = None ) : <EOL> if app is not None : <EOL> self . init_app ( app ) <EOL> self . handlers = [ AppConfigFlagHandler ] <EOL> def init_app ( self , app ) : <EOL> """<STR_LIT>""" <EOL> app . config . setdefault ( FEATURE_FLAGS_CONFIG , { } ) <EOL> app . config . setdefault ( RAISE_ERROR_ON_MISSING_FEATURES , False ) <EOL> if hasattr ( app , "<STR_LIT>" ) : <EOL> app . add_template_test ( self . check , name = self . JINJA_TEST_NAME ) <EOL> else : <EOL> app . jinja_env . tests [ self . JINJA_TEST_NAME ] = self . check <EOL> if not hasattr ( app , '<STR_LIT>' ) : <EOL> app . extensions = { } <EOL> app . extensions [ EXTENSION_NAME ] = self <EOL> def clear_handlers ( self ) : <EOL> """<STR_LIT>""" <EOL> self . handlers = [ ] <EOL> def add_handler ( self , function ) : <EOL> """<STR_LIT>""" <EOL> self . handlers . append ( function ) <EOL> def remove_handler ( self , function ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . handlers . remove ( function ) <EOL> except ValueError : <EOL> pass <EOL> def check ( self , feature ) : <EOL> """<STR_LIT>""" <EOL> found = False <EOL> for handler in self . handlers : <EOL> try : <EOL> if handler ( feature ) : <EOL> return True <EOL> except StopCheckingFeatureFlags : <EOL> return False <EOL> except NoFeatureFlagFound : <EOL> pass <EOL> else : <EOL> found = True <EOL> if not found : <EOL> message = u"<STR_LIT>" . format ( feature = feature ) <EOL> if current_app . debug and current_app . config . get ( RAISE_ERROR_ON_MISSING_FEATURES , False ) : <EOL> raise KeyError ( message ) <EOL> else : <EOL> log . info ( message ) <EOL> missing_feature . send ( self , feature = feature ) <EOL> return False <EOL> def is_active ( feature ) : <EOL> """<STR_LIT>""" <EOL> if current_app : <EOL> feature_flagger = current_app . extensions . get ( EXTENSION_NAME ) <EOL> if feature_flagger : <EOL> return feature_flagger . check ( feature ) <EOL> else : <EOL> raise AssertionError ( "<STR_LIT>" ) <EOL> else : <EOL> log . warn ( u"<STR_LIT>" . format ( feature = feature ) ) <EOL> return False <EOL> def is_active_feature ( feature , redirect_to = None , redirect = None ) : <EOL> """<STR_LIT>""" <EOL> def _is_active_feature ( func ) : <EOL> @ wraps ( func ) <EOL> def wrapped ( * args , ** kwargs ) : <EOL> if not is_active ( feature ) : <EOL> url = redirect_to <EOL> if redirect : <EOL> url = url_for ( redirect ) <EOL> if url : <EOL> log . debug ( u'<STR_LIT>' . format ( feature = feature , url = url ) ) <EOL> return _redirect ( url , code = <NUM_LIT> ) <EOL> else : <EOL> log . debug ( u'<STR_LIT>' . format ( feature = feature ) ) <EOL> abort ( <NUM_LIT> ) <EOL> return func ( * args , ** kwargs ) <EOL> return wrapped <EOL> return _is_active_feature <EOL> class NullHandler ( logging . Handler ) : <EOL> def emit ( self , record ) : <EOL> pass <EOL> log . addHandler ( NullHandler ( ) ) </s>
