-
-
Notifications
You must be signed in to change notification settings - Fork 43
Add verbose option to install and uninstall #60
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
878eafb
Add verbose option to install
ryanking13 7ab09e5
Add logging test
ryanking13 83c5027
Add verbose option to uninstall
ryanking13 804814b
Update logging messages
ryanking13 765e86d
Add tests
ryanking13 8ec0496
Fix mock_add_wheel
ryanking13 2a0cd45
Use logger for warning messages
ryanking13 833d607
Fix logging messages
ryanking13 401a878
Fix duplicated specifier
ryanking13 5b782af
Update changelog
ryanking13 c9760c2
Use logger.warning instaed of logger.warn
ryanking13 ecd8280
Fix tests
ryanking13 e6fcdcc
Add docstring to add_wheel
ryanking13 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,103 @@ | ||
| import contextlib | ||
| import logging | ||
| import sys | ||
| from collections.abc import Generator | ||
| from typing import Any | ||
|
|
||
| _logger: logging.Logger | None = None | ||
| _indentation: int = 0 | ||
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def indent_log(num: int = 2) -> Generator[None, None, None]: | ||
| """ | ||
| A context manager which will cause the log output to be indented for any | ||
| log messages emitted inside it. | ||
| """ | ||
| global _indentation | ||
|
|
||
| _indentation += num | ||
| try: | ||
| yield | ||
| finally: | ||
| _indentation -= num | ||
|
|
||
|
|
||
| # borrowed from pip._internal.utils.logging | ||
| class IndentingFormatter(logging.Formatter): | ||
| default_time_format = "%Y-%m-%dT%H:%M:%S" | ||
|
|
||
| def __init__( | ||
| self, | ||
| *args: Any, | ||
| add_timestamp: bool = False, | ||
| **kwargs: Any, | ||
| ) -> None: | ||
| """ | ||
| A logging.Formatter that obeys the indent_log() context manager. | ||
| :param add_timestamp: A bool indicating output lines should be prefixed | ||
| with their record's timestamp. | ||
| """ | ||
| self.add_timestamp = add_timestamp | ||
| super().__init__(*args, **kwargs) | ||
|
|
||
| def get_message_start(self, formatted: str, levelno: int) -> str: | ||
| """ | ||
| Return the start of the formatted log message (not counting the | ||
| prefix to add to each line). | ||
| """ | ||
| if levelno < logging.WARNING: | ||
| return "" | ||
| if levelno < logging.ERROR: | ||
| return "WARNING: " | ||
|
|
||
| return "ERROR: " | ||
|
|
||
| def format(self, record: logging.LogRecord) -> str: | ||
| """ | ||
| Calls the standard formatter, but will indent all of the log message | ||
| lines by our current indentation level. | ||
| """ | ||
| global _indentation | ||
|
|
||
| formatted = super().format(record) | ||
| message_start = self.get_message_start(formatted, record.levelno) | ||
| formatted = message_start + formatted | ||
|
|
||
| prefix = "" | ||
| if self.add_timestamp: | ||
| prefix = f"{self.formatTime(record)} " | ||
| prefix += " " * _indentation | ||
| formatted = "".join([prefix + line for line in formatted.splitlines(True)]) | ||
| return formatted | ||
|
|
||
|
|
||
| def _set_formatter_once() -> None: | ||
| global _logger | ||
|
|
||
| if _logger is not None: | ||
| return | ||
|
|
||
| _logger = logging.getLogger("micropip") | ||
|
|
||
| ch = logging.StreamHandler(sys.stdout) | ||
| ch.setLevel(logging.NOTSET) | ||
| ch.setFormatter(IndentingFormatter()) | ||
|
|
||
| _logger.addHandler(ch) | ||
|
|
||
|
|
||
| def setup_logging(verbosity: int | bool) -> logging.Logger: | ||
| _set_formatter_once() | ||
|
|
||
| if verbosity >= 2: | ||
| level_number = logging.DEBUG | ||
| elif verbosity == 1: # True == 1 | ||
| level_number = logging.INFO | ||
| else: | ||
| level_number = logging.WARNING | ||
|
|
||
| assert _logger | ||
| _logger.setLevel(level_number) | ||
|
|
||
| return _logger |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.