<s> """<STR_LIT>""" <EOL> import datetime <EOL> import json <EOL> import time <EOL> import urllib <EOL> from flask import _request_ctx_stack , g <EOL> __version__ = '<STR_LIT>' <EOL> __author__ = '<STR_LIT>' <EOL> __license__ = '<STR_LIT>' <EOL> class TrackUsage ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , app = None , storage = None ) : <EOL> """<STR_LIT>""" <EOL> self . _exclude_views = set ( ) <EOL> self . _include_views = set ( ) <EOL> if app is not None and storage is not None : <EOL> self . init_app ( app , storage ) <EOL> def init_app ( self , app , storage ) : <EOL> """<STR_LIT>""" <EOL> self . app = app <EOL> self . _storage = storage <EOL> self . _use_freegeoip = app . config . get ( <EOL> '<STR_LIT>' , False ) <EOL> self . _freegeoip_endpoint = app . config . get ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . _type = app . config . get ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> if self . _type not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise NotImplementedError ( <EOL> '<STR_LIT>' ) <EOL> app . before_request ( self . before_request ) <EOL> app . after_request ( self . after_request ) <EOL> def before_request ( self ) : <EOL> """<STR_LIT>""" <EOL> ctx = _request_ctx_stack . top <EOL> view_func = self . app . view_functions . get ( ctx . request . endpoint ) <EOL> if self . _type == '<STR_LIT>' : <EOL> if view_func in self . _exclude_views : <EOL> return <EOL> elif self . _type == '<STR_LIT>' : <EOL> if view_func not in self . _include_views : <EOL> return <EOL> else : <EOL> raise NotImplementedError ( <EOL> '<STR_LIT>' ) <EOL> g . start_time = datetime . datetime . utcnow ( ) <EOL> def after_request ( self , response ) : <EOL> """<STR_LIT>""" <EOL> ctx = _request_ctx_stack . top <EOL> view_func = self . app . view_functions . get ( ctx . request . endpoint ) <EOL> if self . _type == '<STR_LIT>' : <EOL> if view_func in self . _exclude_views : <EOL> return response <EOL> elif self . _type == '<STR_LIT>' : <EOL> if view_func not in self . _include_views : <EOL> return response <EOL> else : <EOL> raise NotImplementedError ( <EOL> '<STR_LIT>' ) <EOL> now = datetime . datetime . utcnow ( ) <EOL> speed = None <EOL> try : <EOL> speed = ( now - g . start_time ) . total_seconds ( ) <EOL> except : <EOL> speed_result = ( now - g . start_time ) <EOL> speed = float ( "<STR_LIT>" % ( <EOL> speed_result . seconds , speed_result . microseconds ) ) <EOL> data = { <EOL> '<STR_LIT:url>' : ctx . request . url , <EOL> '<STR_LIT>' : ctx . request . user_agent , <EOL> '<STR_LIT>' : ctx . request . blueprint , <EOL> '<STR_LIT>' : ctx . request . view_args , <EOL> '<STR_LIT:status>' : response . status_code , <EOL> '<STR_LIT>' : ctx . request . remote_addr , <EOL> '<STR_LIT>' : ctx . request . headers . get ( <EOL> '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : bool ( ctx . request . authorization ) , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT:path>' : ctx . request . path , <EOL> '<STR_LIT>' : float ( speed ) , <EOL> '<STR_LIT:date>' : int ( time . mktime ( now . timetuple ( ) ) ) <EOL> } <EOL> if self . _use_freegeoip : <EOL> ip_info = json . loads ( urllib . urlopen ( <EOL> self . _freegeoip_endpoint + urllib . quote_plus ( <EOL> remote_addr ) ) . read ( ) ) <EOL> data [ '<STR_LIT>' ] = ip_info <EOL> self . _storage ( data ) <EOL> return response <EOL> def exclude ( self , view ) : <EOL> """<STR_LIT>""" <EOL> self . _exclude_views . add ( view ) <EOL> def include ( self , view ) : <EOL> """<STR_LIT>""" <EOL> self . _include_views . add ( view ) <EOL> def _modify_blueprint ( self , blueprint , include_type ) : <EOL> """<STR_LIT>""" <EOL> blueprint . before_request = self . before_request <EOL> blueprint . after_request = self . after_request <EOL> class DeferredViewGrabber ( object ) : <EOL> views = [ ] <EOL> def add_url_rule ( self , rule , endpoint , view_func , ** options ) : <EOL> self . views . append ( view_func ) <EOL> dvg = DeferredViewGrabber ( ) <EOL> [ defered ( dvg ) for defered in blueprint . deferred_functions ] <EOL> for view in dvg . views : <EOL> if include_type . lower ( ) == '<STR_LIT>' : <EOL> self . _include_views . add ( view ) <EOL> elif include_type . lower ( ) == '<STR_LIT>' : <EOL> self . _exclude_views . add ( view ) <EOL> else : <EOL> raise NotImplementedError ( <EOL> '<STR_LIT>' ) <EOL> return blueprint <EOL> def include_blueprint ( self , blueprint ) : <EOL> """<STR_LIT>""" <EOL> self . _modify_blueprint ( blueprint , '<STR_LIT>' ) <EOL> def exclude_blueprint ( self , blueprint ) : <EOL> """<STR_LIT>""" <EOL> self . _modify_blueprint ( blueprint , '<STR_LIT>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from flask import Flask <EOL> app = Flask ( __name__ ) <EOL> app . config [ '<STR_LIT>' ] = False <EOL> app . config [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> from flask_track_usage . storage . printer import PrintStorage <EOL> t = TrackUsage ( app , PrintStorage ( ) ) <EOL> @ t . include <EOL> @ app . route ( '<STR_LIT:/>' ) <EOL> def index ( ) : <EOL> return "<STR_LIT>" <EOL> app . run ( debug = True ) </s>
