1212
1313__all__ = ['Debug' , 'debug' ]
1414CWD = Path ('.' ).resolve ()
15- pformat = PrettyFormat ()
1615
1716
1817def env_true (var_name , alt = 'FALSE' ):
1918 return os .getenv (var_name , alt ).upper () in {'1' , 'TRUE' }
2019
2120
21+ pformat = PrettyFormat (
22+ indent_step = int (os .getenv ('PY_DEVTOOLS_INDENT' , 4 )),
23+ simple_cutoff = int (os .getenv ('PY_DEVTOOLS_SIMPLE_CUTOFF' , 10 )),
24+ width = int (os .getenv ('PY_DEVTOOLS_SIMPLE_TERM_WIDTH' , 80 )),
25+ yield_from_generators = env_true ('PY_DEVTOOLS_YIELD_FROM_GEN' , 'TRUE' ),
26+ )
27+
28+
2229class DebugArgument :
2330 __slots__ = 'value' , 'name' , 'extra'
2431
@@ -30,17 +37,17 @@ def __init__(self, value, *, name=None, **extra):
3037 self .extra .append (('len' , len (value )))
3138 self .extra += [(k , v ) for k , v in extra .items () if v is not None ]
3239
33- def str (self , colours = False ) -> str :
40+ def str (self , colour = False , highlight = False ) -> str :
3441 s = ''
3542 if self .name :
36- s = sformat (self .name , sformat .blue , apply = colours ) + ': '
37- s += pformat (self .value , indent = 2 )
43+ s = sformat (self .name , sformat .blue , apply = colour ) + ': '
44+ s += pformat (self .value , indent = 2 , highlight = highlight )
3845 suffix = (
3946 ' ({0.value.__class__.__name__}) {1}'
4047 .format (self , ' ' .join ('{}={}' .format (k , v ) for k , v in self .extra ))
4148 .rstrip (' ' ) # trailing space if extra is empty
4249 )
43- s += sformat (suffix , sformat .dim , apply = colours )
50+ s += sformat (suffix , sformat .dim , apply = colour )
4451 return s
4552
4653 def __str__ (self ) -> str :
@@ -60,16 +67,16 @@ def __init__(self, *, filename: str, lineno: int, frame: str, arguments: List[De
6067 self .frame = frame
6168 self .arguments = arguments
6269
63- def str (self , colours = False ) -> str :
64- if colours :
70+ def str (self , colour = False , highlight = False ) -> str :
71+ if colour :
6572 prefix = '{}:{} {}\n ' .format (
6673 sformat (self .filename , sformat .magenta ),
6774 sformat (self .lineno , sformat .green ),
6875 sformat (self .frame , sformat .green , sformat .italic )
6976 )
7077 else :
7178 prefix = '{0.filename}:{0.lineno} {0.frame}\n ' .format (self )
72- return prefix + '\n ' .join (a .str (colours ) for a in self .arguments )
79+ return prefix + '\n ' .join (a .str (colour , highlight ) for a in self .arguments )
7380
7481 def __str__ (self ) -> str :
7582 return self .str ()
@@ -89,10 +96,12 @@ class Debug:
8996
9097 def __init__ (self , * ,
9198 warnings : Optional [bool ]= None ,
92- colours : Optional [bool ]= None ,
99+ colour : Optional [bool ]= None ,
100+ highlight : Optional [bool ]= None ,
93101 frame_context_length : int = 50 ):
94102 self ._warnings = self ._env_bool (warnings , 'PY_DEVTOOLS_WARNINGS' )
95- self ._colours = self ._env_bool (colours , 'PY_DEVTOOLS_COLOURS' )
103+ self ._colour = self ._env_bool (colour , 'PY_DEVTOOLS_COLOUR' )
104+ self ._highlight = self ._env_bool (highlight , 'PY_DEVTOOLS_HIGHLIGHT' )
96105 # 50 lines should be enough to make sure we always get the entire function definition
97106 self ._frame_context_length = frame_context_length
98107
@@ -106,7 +115,7 @@ def _env_bool(cls, value, env_name, env_default='TRUE'):
106115 def __call__ (self , * args , file_ = None , flush_ = True , ** kwargs ) -> None :
107116 d_out = self ._process (args , kwargs , r'debug *\(' )
108117 colours_possible = isatty (file_ )
109- s = d_out .str (self ._colours and colours_possible )
118+ s = d_out .str (self ._colour and colours_possible , self . _highlight and colours_possible )
110119 print (s , file = file_ , flush = flush_ )
111120
112121 def format (self , * args , ** kwargs ) -> DebugOutput :
0 commit comments