55try :
66 import colorful
77except ImportError :
8+
89 class colorful :
10+
911 def __getattr__ (self , attr ):
1012 return lambda st : st
13+
1114 colorful = colorful ()
1215from .utils import make_unicode
1316
1417__print = print
18+
19+
1520def _print (* args , ** kwargs ):
1621 flush = False
1722 if 'flush' in kwargs :
@@ -21,22 +26,28 @@ def _print(*args, **kwargs):
2126 if flush :
2227 kwargs .get ('file' , sys .stdout ).flush ()
2328
29+
2430def _join_dict (a , b ):
2531 """join two dict"""
2632 c = a .copy ()
2733 for k , v in b .items ():
2834 c [k ] = v
2935 return c
3036
37+
3138_log_funcs = {}
3239_log_lock = Lock ()
40+
41+
3342def log (funcname , * args , ** kwargs ):
3443 """log with log function specified by ``funcname``"""
3544 _log_lock .acquire ()
36- rv = _log_funcs .get (funcname , lambda * args , ** kwargs : None )(* args , ** kwargs )
45+ rv = _log_funcs .get (funcname , lambda * args , ** kwargs : None )(* args ,
46+ ** kwargs )
3747 _log_lock .release ()
3848 return rv
3949
50+
4051"""5 log levels
41521. debug: debug info
42532. info: common info
@@ -51,6 +62,7 @@ def log(funcname, *args, **kwargs):
5162warn = partial (log , 'warn' )
5263error = partial (log , 'error' )
5364
65+
5466def register_logfunc (funcname , func ):
5567 """register logfunc
5668 str funcname -> name of logfunc
@@ -64,17 +76,28 @@ def register_logfunc(funcname, func):
6476 except KeyError :
6577 pass
6678
67- _nb_print = lambda * args , ** kwargs : _print (* args , ** _join_dict (kwargs , {'flush' : True }))
68- _nb_print_e = lambda * args , ** kwargs : _print (* args , ** _join_dict (kwargs , {'file' : sys .stderr , 'flush' : True }))
69- _cl_print = lambda color , * args , ** kwargs : _nb_print (* [color (make_unicode (item )) for item in args ], ** kwargs ) if sys .stdout .isatty () else _nb_print (* args , ** kwargs )
70- _cl_print_e = lambda color , * args , ** kwargs : _nb_print_e (* [color (make_unicode (item )) for item in args ], ** kwargs ) if sys .stderr .isatty () else _nb_print_e (* args , ** kwargs )
79+
80+ _nb_print = lambda * args , ** kwargs : _print (
81+ * args , ** _join_dict (kwargs , {'flush' : True }))
82+ _nb_print_e = lambda * args , ** kwargs : _print (
83+ * args , ** _join_dict (kwargs , {
84+ 'file' : sys .stderr ,
85+ 'flush' : True
86+ }))
87+ _cl_print = lambda color , * args , ** kwargs : _nb_print (
88+ * [color (make_unicode (item )) for item in args ], ** kwargs
89+ ) if sys .stdout .isatty () else _nb_print (* args , ** kwargs )
90+ _cl_print_e = lambda color , * args , ** kwargs : _nb_print_e (
91+ * [color (make_unicode (item )) for item in args ], ** kwargs
92+ ) if sys .stderr .isatty () else _nb_print_e (* args , ** kwargs )
7193
7294_default_debug = partial (_cl_print , colorful .cyan )
7395_default_info = partial (_cl_print , colorful .blue )
7496_default_print = _nb_print
7597_default_warn = partial (_cl_print_e , colorful .yellow )
7698_default_error = partial (_cl_print_e , colorful .red )
7799
100+
78101def set_quiet ():
79102 """set log mode to "quiet" """
80103 register_logfunc ('debug' , None )
@@ -83,6 +106,7 @@ def set_quiet():
83106 register_logfunc ('warn' , None )
84107 register_logfunc ('error' , _default_error )
85108
109+
86110def set_normal ():
87111 """set log mode to "normal" """
88112 register_logfunc ('debug' , None )
@@ -91,6 +115,7 @@ def set_normal():
91115 register_logfunc ('warn' , _default_warn )
92116 register_logfunc ('error' , _default_error )
93117
118+
94119def set_verbose ():
95120 """set log mode to "verbose" """
96121 register_logfunc ('debug' , _default_debug )
@@ -99,4 +124,5 @@ def set_verbose():
99124 register_logfunc ('warn' , _default_warn )
100125 register_logfunc ('error' , _default_error )
101126
127+
102128set_normal ()
0 commit comments