<s> from flask import request <EOL> from flask_acl . globals import current_acl_manager <EOL> def parse_predicate ( input ) : <EOL> if isinstance ( input , basestring ) : <EOL> negate = input . startswith ( '<STR_LIT:!>' ) <EOL> if negate : <EOL> input = input [ <NUM_LIT:1> : ] <EOL> predicate = current_acl_manager . predicates . get ( input ) <EOL> if not predicate : <EOL> raise ValueError ( '<STR_LIT>' % input ) <EOL> if negate : <EOL> predicate = Not ( predicate ) <EOL> return predicate <EOL> if isinstance ( input , ( tuple , list ) ) : <EOL> return And ( parse_predicate ( x ) for x in input ) <EOL> return input <EOL> class Any ( object ) : <EOL> def __call__ ( self , ** kw ) : <EOL> return True <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' <EOL> class Not ( object ) : <EOL> def __init__ ( self , predicate ) : <EOL> self . predicate = predicate <EOL> def __call__ ( self , ** kw ) : <EOL> return not self . predicate ( ** kw ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . predicate <EOL> class And ( object ) : <EOL> op = all <EOL> def __init__ ( self , * predicates ) : <EOL> self . predicates = predicates <EOL> def __call__ ( self , ** kw ) : <EOL> return self . op ( x ( ** kw ) for x in self . predicates ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . op . __name__ . upper ( ) , '<STR_LIT:U+002CU+0020>' . join ( repr ( x ) for x in self . predicates ) ) <EOL> class Or ( And ) : <EOL> op = any <EOL> class Authenticated ( object ) : <EOL> def __call__ ( self , user , ** kw ) : <EOL> return user . is_authenticated ( ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' <EOL> class Active ( object ) : <EOL> def __call__ ( self , user , ** kw ) : <EOL> return user . is_active ( ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' <EOL> class Anonymous ( object ) : <EOL> def __call__ ( self , user , ** kw ) : <EOL> return user . is_anonymous ( ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' <EOL> class Local ( object ) : <EOL> def __call__ ( self , ** kw ) : <EOL> return request . remote_addr in ( '<STR_LIT:127.0.0.1>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' <EOL> Remote = lambda : Not ( Local ( ) ) <EOL> default_predicates = { <EOL> '<STR_LIT>' : Active ( ) , <EOL> '<STR_LIT>' : Anonymous ( ) , <EOL> '<STR_LIT>' : Authenticated ( ) , <EOL> '<STR_LIT>' : Local ( ) , <EOL> '<STR_LIT>' : Remote ( ) , <EOL> '<STR_LIT>' : Any ( ) , <EOL> } </s>
