Skip to content

Commit

Permalink
Format logs from poetry-core
Browse files Browse the repository at this point in the history
  • Loading branch information
sdispater committed Mar 27, 2020
1 parent 7457f16 commit c16580a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 4 deletions.
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
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
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

0 comments on commit c16580a

Please sign in to comment.