<s> from __future__ import print_function <EOL> import json <EOL> import logging <EOL> import re <EOL> import requests <EOL> import sys <EOL> import threading <EOL> import time <EOL> import traceback <EOL> from contextlib import contextmanager <EOL> from bayeslite . version import __version__ <EOL> class BqlLogger ( object ) : <EOL> '''<STR_LIT>''' <EOL> def info ( self , msg_format , * values ) : <EOL> '''<STR_LIT>''' <EOL> if len ( values ) > <NUM_LIT:0> : <EOL> msg_format = msg_format % values <EOL> print ( msg_format ) <EOL> def warn ( self , msg_format , * values ) : <EOL> '''<STR_LIT>''' <EOL> if len ( values ) > <NUM_LIT:0> : <EOL> msg_format = msg_format % values <EOL> print ( msg_format , file = sys . stderr ) <EOL> def plot ( self , _suggested_name , figure ) : <EOL> '''<STR_LIT>''' <EOL> if ( hasattr ( figure , '<STR_LIT>' ) ) : <EOL> figure . show ( ) <EOL> else : <EOL> print ( repr ( figure ) ) <EOL> def result ( self , msg_format , * values ) : <EOL> '''<STR_LIT>''' <EOL> if len ( values ) > <NUM_LIT:0> : <EOL> msg_format = msg_format % values <EOL> print ( msg_format ) <EOL> def debug ( self , _msg_format , * _values ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def exception ( self , msg_format , * values ) : <EOL> '''<STR_LIT>''' <EOL> self . warn ( '<STR_LIT>' + msg_format , * values ) <EOL> exc_type , exc_value , exc_traceback = sys . exc_info ( ) <EOL> if exc_type : <EOL> lines = traceback . format_exception ( exc_type , exc_value , exc_traceback ) <EOL> self . warn ( '<STR_LIT:\n>' . join ( lines ) ) <EOL> @ classmethod <EOL> def format_escape ( str ) : <EOL> str = re . sub ( r"<STR_LIT>" , r"<STR_LIT>" , str ) <EOL> str = re . sub ( r"<STR_LIT>" , r"<STR_LIT>" , str ) <EOL> return str <EOL> class DebugLogger ( BqlLogger ) : <EOL> def debug ( self , msg_format , * values ) : <EOL> self . warn ( '<STR_LIT>' + msg_format % values ) <EOL> exc_type , exc_value , exc_traceback = sys . exc_info ( ) <EOL> if exc_type : <EOL> lines = traceback . format_exception ( exc_type , exc_value , exc_traceback ) <EOL> self . warn ( '<STR_LIT:\n>' . join ( lines ) ) <EOL> class QuietLogger ( BqlLogger ) : <EOL> def info ( self , _msg_format , * _values ) : <EOL> pass <EOL> def plot ( self , _suggested_name , _figure ) : <EOL> pass <EOL> def warn ( self , _msg_format , * _values ) : <EOL> pass <EOL> class SilentLogger ( QuietLogger ) : <EOL> def plot ( self , _suggested_name , _figure ) : <EOL> pass <EOL> def result ( self , _msg , * _values ) : <EOL> pass <EOL> def debug ( self , _msg , * _values ) : <EOL> pass <EOL> def exception ( self , _msg , * _values ) : <EOL> pass <EOL> class LoggingLogger ( BqlLogger ) : <EOL> def info ( self , msg_format , * values ) : <EOL> logging . info ( msg_format , * values ) <EOL> def warn ( self , msg_format , * values ) : <EOL> logging . warning ( msg_format , * values , file = sys . stderr ) <EOL> def plot ( self , suggested_name , figure ) : <EOL> figure . savefig ( suggested_name + "<STR_LIT>" ) <EOL> def debug ( self , * args , ** kwargs ) : <EOL> logging . debug ( * args , ** kwargs ) <EOL> def exception ( self , * args , ** kwargs ) : <EOL> logging . exception ( * args , ** kwargs ) <EOL> class CaptureLogger ( BqlLogger ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , verbose = False ) : <EOL> self . calls = [ ] <EOL> self . _verbose = verbose <EOL> self . _next_capturelogger_results = [ ] <EOL> def set_next_capturelogger_results ( self , results ) : <EOL> """<STR_LIT>""" <EOL> self . _next_capturelogger_results = results <EOL> def next_capturelogger_result ( self ) : <EOL> if self . _next_capturelogger_results : <EOL> return self . _next_capturelogger_results . pop ( <NUM_LIT:0> ) <EOL> else : <EOL> return None <EOL> def info ( self , msg_format , * values ) : <EOL> if self . _verbose : <EOL> print ( '<STR_LIT>' + msg_format , * values ) <EOL> self . calls . append ( ( '<STR_LIT:info>' , msg_format , values ) ) <EOL> def warn ( self , msg_format , * values ) : <EOL> if self . _verbose : <EOL> print ( '<STR_LIT>' + msg_format , * values ) <EOL> self . calls . append ( ( '<STR_LIT>' , msg_format , values ) ) <EOL> def plot ( self , suggested_name , figure ) : <EOL> if self . _verbose : <EOL> if ( hasattr ( figure , '<STR_LIT>' ) ) : <EOL> figure . show ( ) <EOL> else : <EOL> print ( repr ( figure ) ) <EOL> self . calls . append ( ( '<STR_LIT>' , suggested_name , figure ) ) <EOL> def debug ( self , * args , ** kwargs ) : <EOL> if self . _verbose : <EOL> print ( "<STR_LIT>" + repr ( args ) + repr ( kwargs ) ) <EOL> self . calls . append ( ( '<STR_LIT>' , args , kwargs ) ) <EOL> def exception ( self , * args , ** kwargs ) : <EOL> self . calls . append ( ( '<STR_LIT>' , args , kwargs , sys . exc_info ( ) ) ) <EOL> def result ( self , msg_format , * values ) : <EOL> if self . _verbose : <EOL> print ( '<STR_LIT>' + msg_format , * values ) <EOL> self . calls . append ( ( '<STR_LIT:result>' , msg_format , values ) ) <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> if self . _verbose : <EOL> print ( "<STR_LIT>" + repr ( args ) + repr ( kwargs ) ) <EOL> self . calls . append ( ( '<STR_LIT>' , args , kwargs ) ) <EOL> return self . next_capturelogger_result ( ) <EOL> def __getattr__ ( self , name ) : <EOL> def _capture ( * args , ** kwargs ) : <EOL> self . calls . append ( ( name , args , kwargs ) ) <EOL> return self . next_capturelogger_result ( ) <EOL> return _capture <EOL> PROBCOMP_URL = '<STR_LIT>' <EOL> class CallHomeStatusLogger ( BqlLogger ) : <EOL> def __init__ ( self , url = PROBCOMP_URL , post = None ) : <EOL> if post is None : <EOL> post = requests . post <EOL> self . _post = post <EOL> self . _url = url <EOL> def info ( self , json_msg , * unused_values ) : <EOL> self . _send ( json_msg ) <EOL> def warn ( self , json_msg , * unused_values ) : <EOL> self . _send ( json_msg ) <EOL> def _send ( self , json_string ) : <EOL> data = { '<STR_LIT>' : json_string , <EOL> '<STR_LIT>' : '<STR_LIT>' % ( __version__ , ) <EOL> } <EOL> t = threading . Thread ( target = self . _post , <EOL> args = ( self . _url , ) , <EOL> kwargs = { '<STR_LIT:data>' : data } ) <EOL> t . start ( ) <EOL> class NpPdEncoder ( json . JSONEncoder ) : <EOL> def default ( self , obj ) : <EOL> try : <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> if '<STR_LIT>' in obj . to_json . __code__ . co_varnames : <EOL> return obj . to_json ( default_handler = NpPdEncoder ) <EOL> else : <EOL> return obj . to_json ( ) <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> return obj . toJSON ( ) <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> return obj . to_dict ( ) <EOL> if isinstance ( obj , complex ) : <EOL> return { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : x . real , '<STR_LIT>' : x . imag } <EOL> return json . JSONEncoder . default ( self , obj ) <EOL> except Exception as e : <EOL> return repr ( e ) <EOL> def query_info_to_json ( session_id , logtype , query , bindings , <EOL> start_time , error , end_time ) : <EOL> session = { '<STR_LIT>' : [ [ session_id , logtype , ( query , bindings ) , <EOL> start_time , error , end_time ] ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT:type>' , '<STR_LIT:data>' , <EOL> '<STR_LIT>' , '<STR_LIT:error>' , '<STR_LIT>' ] , <EOL> '<STR_LIT:version>' : __version__ , <EOL> } <EOL> return json . dumps ( session , cls = NpPdEncoder ) <EOL> @ contextmanager <EOL> def logged_query ( query_string = None , bindings = ( ) , name = None , logger = None ) : <EOL> if query_string is None : <EOL> query_string = "<STR_LIT>" <EOL> if logger is None : <EOL> logger = CallHomeStatusLogger ( ) <EOL> if name is None or name is False : <EOL> yield <EOL> else : <EOL> start_time = time . time ( ) <EOL> try : <EOL> yield <EOL> except : <EOL> logger . warn ( query_info_to_json ( <EOL> name , '<STR_LIT>' , query_string , bindings , <EOL> start_time , traceback . format_exc ( ) , time . time ( ) ) ) <EOL> raise <EOL> logger . info ( query_info_to_json ( <EOL> name , '<STR_LIT>' , query_string , bindings , <EOL> start_time , None , time . time ( ) ) ) </s>
