Skip to content

Commit 663a87a

Browse files
committed
Tweak cli interfaces to make them easier to test
Signed-off-by: John Pennycook <john.pennycook@intel.com>
1 parent 3cc0770 commit 663a87a

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

codebasin/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def _main():
231231
# Temporarily override log_level to ensure they are visible.
232232
stdout_handler.setLevel(logging.WARNING)
233233
print("")
234-
aggregator.warn()
234+
aggregator.warn(log)
235235
stdout_handler.setLevel(log_level)
236236

237237
# Count lines for platforms

codebasin/cli.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import logging
66
import re
77

8-
log = logging.getLogger(__name__)
9-
108

119
class Formatter(logging.Formatter):
1210
"""
@@ -89,25 +87,37 @@ def __init__(self, regex: str, msg: str):
8987
self.msg = msg
9088
self._count = 0
9189

92-
def inspect(self, record: logging.LogRecord):
90+
def inspect(self, record: logging.LogRecord) -> bool:
9391
"""
9492
Inspect a LogRecord to determine if it matches this MetaWarning.
9593
9694
Parameters
9795
----------
9896
record: logging.LogRecord
9997
The LogRecord to inspect.
98+
99+
Returns
100+
-------
101+
bool
102+
True if `record` matches this MetaWarning and False otherwise.
100103
"""
101104
if self.regex.search(record.msg):
102105
self._count += 1
106+
return True
107+
return False
103108

104-
def warn(self):
109+
def warn(self, logger: logging.Logger):
105110
"""
106111
Produce the warning associated with this MetaWarning.
112+
113+
Parameters
114+
----------
115+
log: logging.Logger
116+
The Logger that should be used to generate the MetaWarning.
107117
"""
108118
if self._count == 0:
109119
return
110-
log.warning(self.msg.format(self._count))
120+
logger.warning(self.msg.format(self._count))
111121

112122

113123
class WarningAggregator(logging.Filter):
@@ -161,10 +171,15 @@ def filter(self, record: logging.LogRecord) -> bool:
161171
meta_warning.inspect(record)
162172
return True
163173

164-
def warn(self):
174+
def warn(self, logger: logging.Logger):
165175
"""
166176
Produce the warning associated with any MetaWarning(s) that were
167177
matched by this WarningAggregator.
178+
179+
Parameters
180+
----------
181+
logger: logging.Logger
182+
The Logger that should be used to generate the MetaWarning(s).
168183
"""
169184
for meta_warning in self.meta_warnings:
170-
meta_warning.warn()
185+
meta_warning.warn(logger)

0 commit comments

Comments
 (0)