Skip to content
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

Use red text when printing errors on command line #649

Merged
merged 14 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ Wasim Thabraze <[email protected]>
Varun Kamath <[email protected]>
Brian Rutledge <[email protected]>
Peter Stensmyr <[email protected]> (http://www.peterstensmyr.com)
Felipe Rocha Campos <[email protected]>
4 changes: 4 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ warn_return_any = True
no_implicit_reexport = True
strict_equality = True

[mypy-colorama]
; https://github.com/tartley/colorama/issues/206
ignore_missing_imports = True

bhrutledge marked this conversation as resolved.
Show resolved Hide resolved
[mypy-importlib_metadata]
; https://gitlab.com/python-devs/importlib_metadata/-/issues/10
ignore_missing_imports = True
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ install_requires=
importlib_metadata; python_version < "3.8"
keyring >= 15.1
rfc3986 >= 1.4.0
colorama >= 0.4.3
setup_requires =
setuptools_scm >= 1.15
include_package_data = True
Expand Down
13 changes: 13 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import sys

import colorama
import pretend

from twine import __main__ as dunder_main
Expand All @@ -20,4 +23,14 @@
def test_exception_handling(monkeypatch):
replaced_dispatch = pretend.raiser(exceptions.InvalidConfiguration("foo"))
monkeypatch.setattr(cli, "dispatch", replaced_dispatch)
assert (
bhrutledge marked this conversation as resolved.
Show resolved Hide resolved
dunder_main.main()
== colorama.Fore.RED + "InvalidConfiguration: foo" + colorama.Style.RESET_ALL
)


def test_no_color_exception(monkeypatch):
replaced_dispatch = pretend.raiser(exceptions.InvalidConfiguration("foo"))
monkeypatch.setattr(cli, "dispatch", replaced_dispatch)
monkeypatch.setattr(sys, "argv", ["upload", "--no-color"])
assert dunder_main.main() == "InvalidConfiguration: foo"
18 changes: 17 additions & 1 deletion twine/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import sys
from typing import Any

import colorama
import requests

from twine import cli
Expand All @@ -25,7 +27,21 @@ def main() -> Any:
try:
return cli.dispatch(sys.argv[1:])
except (exceptions.TwineException, requests.HTTPError) as exc:
return "{}: {}".format(exc.__class__.__name__, exc.args[0])
parser = argparse.ArgumentParser()
parser.add_argument(
"--no-color", default=False, required=False, action="store_true",
)
bhrutledge marked this conversation as resolved.
Show resolved Hide resolved

args, _ = parser.parse_known_args(sys.argv[1:])

pre_style, post_style = "", ""
if not args.no_color:
colorama.init()
pre_style, post_style = colorama.Fore.RED, colorama.Style.RESET_ALL

return "{}{}: {}{}".format(
callmecampos marked this conversation as resolved.
Show resolved Hide resolved
pre_style, exc.__class__.__name__, exc.args[0], post_style,
)
bhrutledge marked this conversation as resolved.
Show resolved Hide resolved


if __name__ == "__main__":
Expand Down
7 changes: 7 additions & 0 deletions twine/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,13 @@ def register_argparse_arguments(parser: argparse.ArgumentParser) -> None:
action="store_true",
help="Disable the progress bar.",
)
parser.add_argument(
"--no-color",
default=False,
required=False,
action="store_true",
help="Disable colored output.",
)
bhrutledge marked this conversation as resolved.
Show resolved Hide resolved

@classmethod
def from_argparse(cls, args: argparse.Namespace) -> "Settings":
Expand Down