Skip to content

Commit

Permalink
Improve UI (#2230)
Browse files Browse the repository at this point in the history
* Upgrade cleo and clikit to the latest version

* Tweak colors

* Format logs from poetry-core
  • Loading branch information
sdispater authored Mar 27, 2020
1 parent 424fdbf commit 063d19a
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 36 deletions.
33 changes: 25 additions & 8 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion poetry/console/commands/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ class BuildCommand(EnvCommand):
option("format", "f", "Limit the format to either sdist or wheel.", flag=False)
]

loggers = [
"poetry.core.masonry.builders.sdist",
"poetry.core.masonry.builders.wheel",
]

def handle(self):
from poetry.core.masonry import Builder

Expand All @@ -21,7 +26,7 @@ def handle(self):

package = self.poetry.package
self.line(
"Building <c1>{}</c1> (<b>{}</b>)".format(
"Building <c1>{}</c1> (<c2>{}</c2>)".format(
package.pretty_name, package.version
)
)
Expand Down
2 changes: 1 addition & 1 deletion poetry/console/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def handle(self):
return 0

self.line(
" - Installing <c1>{}</c1> (<b>{}</b>)".format(
" - Installing <c1>{}</c1> (<c2>{}</c2>)".format(
self.poetry.package.pretty_name, self.poetry.package.pretty_version
)
)
Expand Down
5 changes: 3 additions & 2 deletions poetry/console/config/application_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ def configure(self):
super(ApplicationConfig, self).configure()

self.add_style(Style("c1").fg("cyan"))
self.add_style(Style("c2").fg("green"))
self.add_style(Style("info").fg("blue"))
self.add_style(Style("comment").fg("green"))
self.add_style(Style("error").fg("red").bold())
self.add_style(Style("warning").fg("yellow"))
self.add_style(Style("debug").fg("black").bold())
self.add_style(Style("warning").fg("yellow").bold())
self.add_style(Style("debug").fg("default").dark())

self.add_event_listener(PRE_HANDLE, self.register_command_loggers)
self.add_event_listener(PRE_HANDLE, self.set_env)
Expand Down
7 changes: 7 additions & 0 deletions poetry/console/logging/formatters/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from .builder_formatter import BuilderLogFormatter


FORMATTERS = {
"poetry.core.masonry.builders.sdist": BuilderLogFormatter(),
"poetry.core.masonry.builders.wheel": BuilderLogFormatter(),
}
13 changes: 13 additions & 0 deletions poetry/console/logging/formatters/builder_formatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import re

from .formatter import Formatter


class BuilderLogFormatter(Formatter):
def format(self, msg): # type: (str) -> str
if msg.startswith(" - Building ") or msg.startswith(" - Built "):
msg = re.sub(r" - (Buil(?:t|ing)) (.+)", " - \\1 <c2>\\2</c2>", msg)
elif msg.startswith(" - Adding: "):
msg = re.sub(r" - Adding: (.+)", " - Adding: <b>\\1</b>", msg)

return msg
6 changes: 6 additions & 0 deletions poetry/console/logging/formatters/formatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import logging


class Formatter(object):
def format(self, record): # type: (logging.LogRecord) -> str
raise NotImplementedError()
6 changes: 5 additions & 1 deletion poetry/console/logging/io_formatter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging

from .formatters import FORMATTERS


class IOFormatter(logging.Formatter):

Expand All @@ -15,7 +17,9 @@ def format(self, record):
level = record.levelname.lower()
msg = record.msg

if level in self._colors:
if record.name in FORMATTERS:
msg = FORMATTERS[record.name].format(msg)
elif level in self._colors:
msg = "<{}>{}</>".format(self._colors[level], msg)

return msg
Expand Down
12 changes: 6 additions & 6 deletions poetry/installation/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def _execute_install(self, operation): # type: (Install) -> None
if operation.skipped:
if self.is_verbose() and (self._execute_operations or self.is_dry_run()):
self._io.write_line(
" - Skipping <c1>{}</c1> (<b>{}</b>) {}".format(
" - Skipping <c1>{}</c1> (<c2>{}</c2>) {}".format(
operation.package.pretty_name,
operation.package.full_pretty_version,
operation.skip_reason,
Expand All @@ -316,7 +316,7 @@ def _execute_install(self, operation): # type: (Install) -> None

if self._execute_operations or self.is_dry_run():
self._io.write_line(
" - Installing <c1>{}</c1> (<b>{}</b>)".format(
" - Installing <c1>{}</c1> (<c2>{}</c2>)".format(
operation.package.pretty_name, operation.package.full_pretty_version
)
)
Expand All @@ -333,7 +333,7 @@ def _execute_update(self, operation): # type: (Update) -> None
if operation.skipped:
if self.is_verbose() and (self._execute_operations or self.is_dry_run()):
self._io.write_line(
" - Skipping <c1>{}</c1> (<b>{}</b>) {}".format(
" - Skipping <c1>{}</c1> (<c2>{}</c2>) {}".format(
target.pretty_name,
target.full_pretty_version,
operation.skip_reason,
Expand All @@ -344,7 +344,7 @@ def _execute_update(self, operation): # type: (Update) -> None

if self._execute_operations or self.is_dry_run():
self._io.write_line(
" - Updating <c1>{}</c1> (<b>{}</b> -> <b>{}</b>)".format(
" - Updating <c1>{}</c1> (<c2>{}</c2> -> <c2>{}</c2>)".format(
target.pretty_name,
source.full_pretty_version,
target.full_pretty_version,
Expand All @@ -360,7 +360,7 @@ def _execute_uninstall(self, operation): # type: (Uninstall) -> None
if operation.skipped:
if self.is_verbose() and (self._execute_operations or self.is_dry_run()):
self._io.write_line(
" - Not removing <c1>{}</c1> (<b>{}</b>) {}".format(
" - Not removing <c1>{}</c1> (<c2>{}</c2>) {}".format(
operation.package.pretty_name,
operation.package.full_pretty_version,
operation.skip_reason,
Expand All @@ -371,7 +371,7 @@ def _execute_uninstall(self, operation): # type: (Uninstall) -> None

if self._execute_operations or self.is_dry_run():
self._io.write_line(
" - Removing <c1>{}</c1> (<b>{}</b>)".format(
" - Removing <c1>{}</c1> (<c2>{}</c2>)".format(
operation.package.pretty_name, operation.package.full_pretty_version
)
)
Expand Down
4 changes: 2 additions & 2 deletions poetry/publishing/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def files(self):
def publish(self, repository_name, username, password, cert=None, client_cert=None):
if repository_name:
self._io.write_line(
"Publishing <c1>{}</c1> (<b>{}</b>) "
"Publishing <c1>{}</c1> (<c2>{}</c2>) "
"to <info>{}</info>".format(
self._package.pretty_name,
self._package.pretty_version,
Expand All @@ -38,7 +38,7 @@ def publish(self, repository_name, username, password, cert=None, client_cert=No
)
else:
self._io.write_line(
"Publishing <c1>{}</c1> (<b>{}</b>) "
"Publishing <c1>{}</c1> (<c2>{}</c2>) "
"to <info>PyPI</info>".format(
self._package.pretty_name, self._package.pretty_version
)
Expand Down
20 changes: 9 additions & 11 deletions poetry/puzzle/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,31 +731,31 @@ def debug(self, message, depth=0):

message = (
"<fg=blue>fact</>: <c1>{}</c1>{} "
"depends on <c1>{}</c1> (<b>{}</b>)".format(
"depends on <c1>{}</c1> (<c2>{}</c2>)".format(
name, version, m.group(2), m.group(3)
)
)
elif " is " in message:
message = re.sub(
"fact: (.+) is (.+)",
"<fg=blue>fact</>: <c1>\\1</c1> is <b>\\2</b>",
"<fg=blue>fact</>: <c1>\\1</c1> is <c2>\\2</c2>",
message,
)
else:
message = re.sub(
r"(?<=: )(.+?) \((.+?)\)", "<c1>\\1</c1> (<b>\\2</b>)", message
r"(?<=: )(.+?) \((.+?)\)", "<c1>\\1</c1> (<c2>\\2</c2>)", message
)
message = "<fg=blue>fact</>: {}".format(message.split("fact: ")[1])
elif message.startswith("selecting "):
message = re.sub(
r"selecting (.+?) \((.+?)\)",
"<fg=blue>selecting</> <c1>\\1</c1> (<b>\\2</b>)",
"<fg=blue>selecting</> <c1>\\1</c1> (<c2>\\2</c2>)",
message,
)
elif message.startswith("derived:"):
m = re.match(r"derived: (.+?) \((.+?)\)$", message)
if m:
message = "<fg=blue>derived</>: <c1>{}</c1> (<b>{}</b>)".format(
message = "<fg=blue>derived</>: <c1>{}</c1> (<c2>{}</c2>)".format(
m.group(1), m.group(2)
)
else:
Expand All @@ -768,14 +768,14 @@ def debug(self, message, depth=0):
m2 = re.match(r"(.+?) \((.+?)\)", m.group(1))
if m2:
name = m2.group(1)
version = " (<b>{}</b>)".format(m2.group(2))
version = " (<c2>{}</c2>)".format(m2.group(2))
else:
name = m.group(1)
version = ""

message = (
"<fg=red;options=bold>conflict</>: <c1>{}</c1>{} "
"depends on <c1>{}</c1> (<b>{}</b>)".format(
"depends on <c1>{}</c1> (<c2>{}</c2>)".format(
name, version, m.group(2), m.group(3)
)
)
Expand All @@ -791,7 +791,7 @@ def debug(self, message, depth=0):
debug_info = (
"\n".join(
[
"<comment>{}:</> {}".format(str(depth).rjust(4), s)
"<debug>{}:</debug> {}".format(str(depth).rjust(4), s)
for s in debug_info.split("\n")
]
)
Expand All @@ -806,9 +806,7 @@ def progress(self):
self._io.write_line("Resolving dependencies...")
yield
else:
indicator = Indicator(
self._io, "{message} <fg=black;options=bold>({elapsed:2s})</>"
)
indicator = Indicator(self._io, "{message} <debug>({elapsed:2s})</debug>")

with indicator.auto(
"<info>Resolving dependencies...</info>",
Expand Down
2 changes: 1 addition & 1 deletion poetry/repositories/pypi_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,4 +503,4 @@ def _download(self, url, dest): # type: (str, str) -> None
f.write(chunk)

def _log(self, msg, level="info"):
getattr(logger, level)("<comment>{}:</comment> {}".format(self._name, msg))
getattr(logger, level)("<debug>{}:</debug> {}".format(self._name, msg))
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ classifiers = [
[tool.poetry.dependencies]
python = "~2.7 || ^3.5"
poetry-core = "^1.0.0a5"
cleo = "^0.7.6"
clikit = "^0.4.3"
cleo = "^0.8.0"
clikit = "^0.5.1"
requests = "^2.18"
cachy = "^0.3.0"
requests-toolbelt = "^0.8.0"
Expand Down
35 changes: 34 additions & 1 deletion tests/console/commands/test_publish.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import pytest

from poetry.utils._compat import PY36
from poetry.utils._compat import Path


@pytest.mark.skipif(
not PY36, reason="Improved error rendering is only available on Python >=3.6"
)
def test_publish_returns_non_zero_code_for_upload_errors(app, app_tester, http):
http.register_uri(
http.POST, "https://upload.pypi.org/legacy/", status=400, body="Bad Request"
Expand All @@ -14,7 +20,34 @@ def test_publish_returns_non_zero_code_for_upload_errors(app, app_tester, http):
Publishing simple-project (1.2.3) to PyPI
[UploadError]
UploadError
HTTP Error 400: Bad Request
"""

assert expected in app_tester.io.fetch_output()


@pytest.mark.skipif(
PY36, reason="Improved error rendering is not available on Python <3.6"
)
def test_publish_returns_non_zero_code_for_upload_errors_older_python(
app, app_tester, http
):
http.register_uri(
http.POST, "https://upload.pypi.org/legacy/", status=400, body="Bad Request"
)

exit_code = app_tester.execute("publish --username foo --password bar")

assert 1 == exit_code

expected = """
Publishing simple-project (1.2.3) to PyPI
UploadError
HTTP Error 400: Bad Request
"""

Expand Down

0 comments on commit 063d19a

Please sign in to comment.