Skip to content

Commit

Permalink
Support --no-color
Browse files Browse the repository at this point in the history
  • Loading branch information
bhrutledge committed Jan 23, 2022
1 parent 4bba7fb commit dbdf31a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 26 deletions.
3 changes: 1 addition & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ def test_no_color_exception(monkeypatch, capsys):

captured = capsys.readouterr()

# Removing trailing whitespace on lines wrapped by Rich; trying to test it was ugly.
# TODO: Assert no color
# Removing trailing whitespace on wrapped lines; trying to test it was ugly.
assert [line.rstrip() for line in captured.out.splitlines()] == [
"ERROR InvalidDistribution: Cannot find file (or expand pattern):",
" 'missing.whl'",
Expand Down
26 changes: 3 additions & 23 deletions twine/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,16 @@
from typing import Any

import requests
import rich.console
import rich.highlighter
import rich.logging
import rich.theme

from twine import cli
from twine import exceptions


def main() -> Any:
# Ensure that all log messages are displayed.
# Color will be configured during cli.dispatch() after argparse.
root_logger = logging.getLogger("twine")
root_logger.addHandler(
rich.logging.RichHandler(
console=rich.console.Console(
theme=rich.theme.Theme(
{
"logging.level.debug": "green",
"logging.level.info": "blue",
"logging.level.warning": "yellow",
"logging.level.error": "red",
"logging.level.critical": "reverse red",
}
),
force_terminal=True,
),
show_time=False,
show_path=False,
highlighter=rich.highlighter.NullHighlighter(),
)
)
root_logger.addHandler(logging.StreamHandler(sys.stdout))

try:
error = cli.dispatch(sys.argv[1:])
Expand Down
42 changes: 41 additions & 1 deletion twine/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import logging
from typing import Any, List, Tuple

import importlib_metadata
import rich.console
import rich.highlighter
import rich.logging
import rich.theme
from packaging import requirements

import twine
Expand All @@ -34,10 +39,44 @@ def dep_versions() -> str:
)


def configure_logging() -> None:
root_logger = logging.getLogger("twine")

# Overwrite basic configuration in main()
# TODO: Use dictConfig() instead?
for handler in root_logger.handlers:
root_logger.removeHandler(handler)

root_logger.addHandler(
rich.logging.RichHandler(
# TODO: Maybe make console a module attribute to facilitate testing and
# using Rich's other functionality.
console=rich.console.Console(
# TODO: Set this if FORCE_COLOR or PY_COLORS in os.environ
force_terminal=True,
no_color=getattr(args, "no_color", False),
theme=rich.theme.Theme(
{
"logging.level.debug": "green",
"logging.level.info": "blue",
"logging.level.warning": "yellow",
"logging.level.error": "red",
"logging.level.critical": "reverse red",
}
),
),
show_time=False,
show_path=False,
highlighter=rich.highlighter.NullHighlighter(),
)
)


def dispatch(argv: List[str]) -> Any:
registered_commands = importlib_metadata.entry_points(
group="twine.registered_commands"
)

parser = argparse.ArgumentParser(prog="twine")
parser.add_argument(
"--version",
Expand All @@ -60,9 +99,10 @@ def dispatch(argv: List[str]) -> Any:
help=argparse.SUPPRESS,
nargs=argparse.REMAINDER,
)

parser.parse_args(argv, namespace=args)

configure_logging()

main = registered_commands[args.command].load()

return main(args.args)

0 comments on commit dbdf31a

Please sign in to comment.