Skip to content

Commit 962ec65

Browse files
committed
simplifying debug colour choice
1 parent 7e8da3e commit 962ec65

1 file changed

Lines changed: 18 additions & 16 deletions

File tree

devtools/debug.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@
1515
CWD = Path('.').resolve()
1616

1717

18-
def env_true(var_name, alt='FALSE'):
19-
return os.getenv(var_name, alt).upper() in {'1', 'TRUE'}
18+
def env_true(var_name, alt=None):
19+
env = os.getenv(var_name, None)
20+
if env:
21+
return env.upper() in {'1', 'TRUE'}
22+
else:
23+
return alt
2024

2125

2226
pformat = PrettyFormat(
2327
indent_step=int(os.getenv('PY_DEVTOOLS_INDENT', 4)),
2428
simple_cutoff=int(os.getenv('PY_DEVTOOLS_SIMPLE_CUTOFF', 10)),
2529
width=int(os.getenv('PY_DEVTOOLS_SIMPLE_TERM_WIDTH', 120)),
26-
yield_from_generators=env_true('PY_DEVTOOLS_YIELD_FROM_GEN', 'TRUE'),
30+
yield_from_generators=env_true('PY_DEVTOOLS_YIELD_FROM_GEN', True),
2731
)
2832

2933

@@ -38,17 +42,17 @@ def __init__(self, value, *, name=None, **extra):
3842
self.extra.append(('len', len(value)))
3943
self.extra += [(k, v) for k, v in extra.items() if v is not None]
4044

41-
def str(self, colour=False, highlight=False) -> str:
45+
def str(self, highlight=False) -> str:
4246
s = ''
4347
if self.name:
44-
s = sformat(self.name, sformat.blue, apply=colour) + ': '
48+
s = sformat(self.name, sformat.blue, apply=highlight) + ': '
4549
s += pformat(self.value, indent=4, highlight=highlight)
4650
suffix = (
4751
' ({0.value.__class__.__name__}) {1}'
4852
.format(self, ' '.join('{}={}'.format(k, v) for k, v in self.extra))
4953
.rstrip(' ') # trailing space if extra is empty
5054
)
51-
s += sformat(suffix, sformat.dim, apply=colour)
55+
s += sformat(suffix, sformat.dim, apply=highlight)
5256
return s
5357

5458
def __str__(self) -> str:
@@ -68,16 +72,16 @@ def __init__(self, *, filename: str, lineno: int, frame: str, arguments: List[De
6872
self.frame = frame
6973
self.arguments = arguments
7074

71-
def str(self, colour=False, highlight=False) -> str:
72-
if colour:
75+
def str(self, highlight=False) -> str:
76+
if highlight:
7377
prefix = '{}:{} {}'.format(
7478
sformat(self.filename, sformat.magenta),
7579
sformat(self.lineno, sformat.green),
7680
sformat(self.frame, sformat.green, sformat.italic)
7781
)
7882
else:
7983
prefix = '{0.filename}:{0.lineno} {0.frame}'.format(self)
80-
return prefix + '\n ' + '\n '.join(a.str(colour, highlight) for a in self.arguments)
84+
return prefix + '\n ' + '\n '.join(a.str(highlight) for a in self.arguments)
8185

8286
def __str__(self) -> str:
8387
return self.str()
@@ -97,26 +101,24 @@ class Debug:
97101

98102
def __init__(self, *,
99103
warnings: Optional[bool]=None,
100-
colour: Optional[bool]=None,
101104
highlight: Optional[bool]=None,
102105
frame_context_length: int=50):
103-
self._show_warnings = self._env_bool(warnings, 'PY_DEVTOOLS_WARNINGS')
104-
self._colour = self._env_bool(colour, 'PY_DEVTOOLS_COLOUR')
105-
self._highlight = self._env_bool(highlight, 'PY_DEVTOOLS_HIGHLIGHT')
106+
self._show_warnings = self._env_bool(warnings, 'PY_DEVTOOLS_WARNINGS', True)
107+
self._highlight = self._env_bool(highlight, 'PY_DEVTOOLS_HIGHLIGHT', None)
106108
# 50 lines should be enough to make sure we always get the entire function definition
107109
self._frame_context_length = frame_context_length
108110

109111
@classmethod
110-
def _env_bool(cls, value, env_name, env_default='TRUE'):
112+
def _env_bool(cls, value, env_name, env_default):
111113
if value is None:
112114
return env_true(env_name, env_default)
113115
else:
114116
return value
115117

116118
def __call__(self, *args, file_=None, flush_=True, **kwargs) -> None:
117119
d_out = self._process(args, kwargs, r'debug *\(')
118-
colours_possible = isatty(file_)
119-
s = d_out.str(self._colour and colours_possible, self._highlight and colours_possible)
120+
highlight = isatty(file_) if self._highlight is None else self._highlight
121+
s = d_out.str(highlight)
120122
print(s, file=file_, flush=flush_)
121123

122124
def format(self, *args, **kwargs) -> DebugOutput:

0 commit comments

Comments
 (0)