-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move MetaWarning classes into new cli module #142
Open
Pennycook
wants to merge
5
commits into
intel:main
Choose a base branch
from
Pennycook:cli
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
997b69f
Move MetaWarning classes into new cli module
Pennycook 93c6ae4
Tweak cli interfaces to make them easier to test
Pennycook 4358d8e
Add tests for Formatter class
Pennycook 0f2d5b9
Add tests for MetaWarning class
Pennycook 2caf0a7
Add tests for WarningAggregator class
Pennycook File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (C) 2019-2024 Intel Corporation | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
import logging | ||
import re | ||
|
||
|
||
class Formatter(logging.Formatter): | ||
""" | ||
A Formatter that formats LogRecords using a format inspired by compilers | ||
like gcc/clang, with optional colors. | ||
""" | ||
|
||
def __init__(self, *, colors: bool = False): | ||
""" | ||
Initialize this Formatter. | ||
|
||
Parameters | ||
---------- | ||
colors: bool, default: False | ||
Whether to colorize the output. | ||
""" | ||
self.colors = colors | ||
|
||
def format(self, record: logging.LogRecord) -> str: | ||
""" | ||
Format the specified record. | ||
|
||
Parameters | ||
---------- | ||
record: logging.LogRecord | ||
The record to format. | ||
|
||
Returns | ||
------- | ||
str | ||
The formatted output string. | ||
""" | ||
msg = record.msg | ||
level = record.levelname.lower() | ||
|
||
# Display info messages with no special formatting. | ||
if level == "info": | ||
return f"{msg}" | ||
|
||
# Drop colors if requested. | ||
if not self.colors: | ||
return f"{level}: {msg}" | ||
|
||
# Otherwise, use ASCII codes to improve readability. | ||
BOLD = "\033[1m" | ||
DEFAULT = "\033[39m" | ||
YELLOW = "\033[93m" | ||
RED = "\033[91m" | ||
RESET = "\033[0m" | ||
|
||
if level == "warning": | ||
color = YELLOW | ||
elif level == "error": | ||
color = RED | ||
else: | ||
color = DEFAULT | ||
return f"{BOLD}{color}{level}{RESET}: {msg}" | ||
|
||
|
||
class MetaWarning: | ||
""" | ||
A MetaWarning is used to represent multiple warnings, and to provide | ||
suggested actions to the user. | ||
""" | ||
|
||
def __init__(self, regex: str, msg: str): | ||
""" | ||
Initialize a new MetaWarning. | ||
|
||
Parameters | ||
---------- | ||
regex: str | ||
A regular expression used to identify constituent warnings. | ||
If any warning matches `regex`, this MetaWarning will trigger. | ||
|
||
msg: str | ||
The message to display when this MetaWarning is triggered. | ||
""" | ||
self.regex = re.compile(regex) | ||
self.msg = msg | ||
self._count = 0 | ||
|
||
def inspect(self, record: logging.LogRecord) -> bool: | ||
""" | ||
Inspect a LogRecord to determine if it matches this MetaWarning. | ||
|
||
Parameters | ||
---------- | ||
record: logging.LogRecord | ||
The LogRecord to inspect. | ||
|
||
Returns | ||
------- | ||
bool | ||
True if `record` matches this MetaWarning and False otherwise. | ||
""" | ||
if self.regex.search(record.msg): | ||
self._count += 1 | ||
return True | ||
return False | ||
|
||
def warn(self, logger: logging.Logger): | ||
""" | ||
Produce the warning associated with this MetaWarning. | ||
|
||
Parameters | ||
---------- | ||
log: logging.Logger | ||
The Logger that should be used to generate the MetaWarning. | ||
""" | ||
if self._count == 0: | ||
return | ||
logger.warning(self.msg.format(self._count)) | ||
|
||
|
||
class WarningAggregator(logging.Filter): | ||
""" | ||
A WarningAggregator is a logging.Filter that inspects warnings to generate | ||
meta-warnings and statistics. It does not perform any filtering, but uses | ||
the logging.Filter mechanism as a hook to automatically inspect every | ||
warning passing through a logger. | ||
""" | ||
|
||
def __init__(self): | ||
self.meta_warnings = [ | ||
MetaWarning(".", "{} warnings generated during preprocessing."), | ||
MetaWarning( | ||
"user include", | ||
"{} user include files could not be found.\n" | ||
+ " These could contain important macros and includes.\n" | ||
+ " Suggested solutions:\n" | ||
+ " - Check that the file(s) exist in the code base.\n" | ||
+ " - Check the include paths in the compilation database.\n" | ||
+ " - Check if the include(s) should have used '<>'.", | ||
), | ||
MetaWarning( | ||
"system include", | ||
"{} system include files could not be found.\n" | ||
+ " These could define important feature macros.\n" | ||
+ " Suggested solutions:\n" | ||
+ " - Check that the file(s) exist on your system.\n" | ||
+ " - Use .cbi/config to define system include paths.\n" | ||
+ " - Use .cbi/config to define important macros.", | ||
), | ||
] | ||
|
||
def filter(self, record: logging.LogRecord) -> bool: | ||
""" | ||
Inspect the specified LogRecord, attempting to match it against each | ||
possible MetaWarning. | ||
|
||
Parameters | ||
---------- | ||
record: logging.LogRecord | ||
The record to inspect. | ||
|
||
Returns | ||
------- | ||
bool | ||
True, to prevent any warnings from being filtered. | ||
""" | ||
if record.levelno == logging.WARNING: | ||
for meta_warning in self.meta_warnings: | ||
meta_warning.inspect(record) | ||
return True | ||
|
||
def warn(self, logger: logging.Logger): | ||
""" | ||
Produce the warning associated with any MetaWarning(s) that were | ||
matched by this WarningAggregator. | ||
|
||
Parameters | ||
---------- | ||
logger: logging.Logger | ||
The Logger that should be used to generate the MetaWarning(s). | ||
""" | ||
for meta_warning in self.meta_warnings: | ||
meta_warning.warn(logger) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Copyright (C) 2019-2024 Intel Corporation | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
import logging | ||
import unittest | ||
|
||
from codebasin.cli import Formatter | ||
|
||
|
||
class TestFormatter(unittest.TestCase): | ||
""" | ||
Test Formatter class. | ||
""" | ||
|
||
def setUp(self): | ||
logging.disable() | ||
|
||
def test_constructor(self): | ||
"""Check constructor arguments""" | ||
self.assertTrue(Formatter(colors=True).colors) | ||
self.assertFalse(Formatter(colors=False).colors) | ||
self.assertFalse(Formatter().colors) | ||
|
||
def test_format(self): | ||
"""Check output format""" | ||
levels = ["DEBUG", "INFO", "WARNING", "ERROR"] | ||
colors = ["\033[39m", "\033[39m", "\033[93m", "\033[91m"] | ||
for colorize in [True, False]: | ||
for levelname, color in zip(levels, colors): | ||
formatter = Formatter(colors=colorize) | ||
with self.subTest( | ||
colorize=colorize, | ||
levelname=levelname, | ||
color=color, | ||
): | ||
record = logging.makeLogRecord( | ||
{ | ||
"msg": "Testing", | ||
"levelname": levelname, | ||
}, | ||
) | ||
msg = record.msg | ||
level = record.levelname.lower() | ||
output = formatter.format(record) | ||
if level == "info": | ||
expected = msg | ||
elif colorize: | ||
expected = f"\033[1m{color}{level}\033[0m: {msg}" | ||
else: | ||
expected = f"{level}: {msg}" | ||
self.assertEqual(output, expected) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if it's just me, but the naming of this module doesn't seem to really fit its contents?
It's called
cli.py
but it contains logging stuff, and if we're moving things around, would it make sense to name it something that maps better? (log_utils.py
?)Otherwise, refactors and new tests look good to me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I can see the confusion. I'll explain my logic, and you can tell me if it makes sense 😆.
The thinking behind this "cli" module was that it would contain things that were only useful for the user-facing command-line interface, but which might be shared across commands (e.g., the
Formatter
is currently used by bothcodebasin
andcbicov
).I was basically looking for a way to separate things that we use to build the user-facing CLI from the modules and interfaces that we (eventually) want people to be able to use when importing the
codebasin
package into another script.Maybe it would be better to have this as something like a
cli
package, containing alogging.py
? Or an_internal
package containing alogging.py
? I'm definitely open to suggestions here.