77from textwrap import dedent
88from typing import Generator , List , Optional , Tuple
99
10+ from .ansi import isatty , sformat
11+ from .prettier import PrettyFormat
12+
1013__all__ = ['Debug' , 'debug' ]
1114CWD = Path ('.' ).resolve ()
15+ pformat = PrettyFormat ()
1216
1317
1418def env_true (var_name , alt = 'FALSE' ):
@@ -26,18 +30,13 @@ def __init__(self, value, *, name=None, **extra):
2630 self .extra .append (('len' , len (value )))
2731 self .extra += [(k , v ) for k , v in extra .items () if v is not None ]
2832
29- def value_str (self ):
30- if isinstance (self .value , str ):
31- return '"{}"' .format (self .value )
32- return str (self .value )
33-
3433 def __str__ (self ) -> str :
3534 template = '{value} ({self.value.__class__.__name__}) {extra}'
3635 if self .name :
3736 template = '{self.name} = ' + template
3837 return template .format (
3938 self = self ,
40- value = self .value_str ( ),
39+ value = pformat ( self .value , indent = 2 ),
4140 extra = ' ' .join ('{}={}' .format (k , v ) for k , v in self .extra )
4241 ).rstrip (' ' ) # trailing space if extra is empty
4342
@@ -55,9 +54,19 @@ def __init__(self, *, filename: str, lineno: int, frame: str, arguments: List[De
5554 self .frame = frame
5655 self .arguments = arguments
5756
57+ def str (self , colours = False ) -> str :
58+ if colours :
59+ prefix = '{}:{} {}\n ' .format (
60+ sformat (self .filename , sformat .magenta ),
61+ sformat (self .lineno , sformat .green ),
62+ sformat (self .frame , sformat .green , sformat .italic )
63+ )
64+ else :
65+ prefix = '{0.filename}:{0.lineno} {0.frame}\n ' .format (self )
66+ return prefix + '\n ' .join (str (a ) for a in self .arguments )
67+
5868 def __str__ (self ) -> str :
59- template = '{s.filename}:{s.lineno} {s.frame}\n {a}'
60- return template .format (s = self , a = '\n ' .join (str (a ) for a in self .arguments ))
69+ return self .str ()
6170
6271 def __repr__ (self ) -> str :
6372 arguments = ' ' .join (str (a ) for a in self .arguments )
@@ -88,13 +97,15 @@ def _env_bool(cls, value, env_name, env_default='TRUE'):
8897 else :
8998 return value
9099
91- def __call__ (self , * args , ** kwargs ):
92- print (self ._process (args , kwargs , r'debug *\(' ), flush = True )
100+ def __call__ (self , * args , file_ = None , flush_ = True , ** kwargs ) -> None :
101+ d_out = self ._process (args , kwargs , r'debug *\(' )
102+ s = d_out .str (isatty (file_ ))
103+ print (s , file = file_ , flush = flush_ )
93104
94- def format (self , * args , ** kwargs ):
105+ def format (self , * args , ** kwargs ) -> DebugOutput :
95106 return self ._process (args , kwargs , r'debug.format *\(' )
96107
97- def _process (self , args , kwargs , func_regex ):
108+ def _process (self , args , kwargs , func_regex ) -> DebugOutput :
98109 curframe = inspect .currentframe ()
99110 frames = inspect .getouterframes (curframe , context = self ._frame_context_length )
100111 # BEWARE: this must be call by a method which in turn is called "directly" for the frame to be correct
0 commit comments