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 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -26,3 +26,4 @@ Wasim Thabraze <wasim@thabraze.me>
Varun Kamath <varunkamath18@gmail.com>
Brian Rutledge <bhrutledge@gmail.com>
Peter Stensmyr <peter.stensmyr@gmail.com> (http://www.peterstensmyr.com)
Felipe Rocha Campos <felipecampos@google.com>
4 changes: 4 additions & 0 deletions mypy.ini
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
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
6 changes: 5 additions & 1 deletion tests/test_main.py
Expand Up @@ -10,6 +10,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import colorama
import pretend

from twine import __main__ as dunder_main
Expand All @@ -20,4 +21,7 @@
def test_exception_handling(monkeypatch):
replaced_dispatch = pretend.raiser(exceptions.InvalidConfiguration("foo"))
monkeypatch.setattr(cli, "dispatch", replaced_dispatch)
assert dunder_main.main() == "InvalidConfiguration: foo"
assert (
bhrutledge marked this conversation as resolved.
Show resolved Hide resolved
dunder_main.main()
== colorama.Fore.RED + "InvalidConfiguration: foo" + colorama.Style.RESET_ALL
)
15 changes: 14 additions & 1 deletion twine/__main__.py
Expand Up @@ -20,12 +20,25 @@
from twine import cli
from twine import exceptions

try:
import colorama
except Exception:
callmecampos marked this conversation as resolved.
Show resolved Hide resolved
colorama = None


def main() -> Any:
try:
if colorama:
colorama.init()
return cli.dispatch(sys.argv[1:])
except (exceptions.TwineException, requests.HTTPError) as exc:
return "{}: {}".format(exc.__class__.__name__, exc.args[0])
pre_style, post_style = "", ""
if colorama:
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