Skip to content

Commit

Permalink
[console] improve progress_message context (#12387)
Browse files Browse the repository at this point in the history
This allows statements executed within a ``progress_message`` context
to emit log messages without polluting the console output.

Co-authored-by: Bénédikt Tran <[email protected]>
  • Loading branch information
methane and picnixz authored May 22, 2024
1 parent 9cc0ea1 commit 48cbb43
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion sphinx/builders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ def write(
with progress_message(__('preparing documents')):
self.prepare_writing(docnames)

with progress_message(__('copying assets')):
with progress_message(__('copying assets'), nonl=False):
self.copy_assets()

if self.parallel_ok:
Expand Down
12 changes: 7 additions & 5 deletions sphinx/util/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,29 @@ class SkipProgressMessage(Exception):


class progress_message:
def __init__(self, message: str) -> None:
def __init__(self, message: str, *, nonl: bool = True) -> None:
self.message = message
self.nonl = nonl

def __enter__(self) -> None:
logger.info(bold(self.message + '... '), nonl=True)
logger.info(bold(self.message + '... '), nonl=self.nonl)

def __exit__(
self,
typ: type[BaseException] | None,
val: BaseException | None,
tb: TracebackType | None,
) -> bool:
prefix = "" if self.nonl else bold(self.message + ': ')
if isinstance(val, SkipProgressMessage):
logger.info(__('skipped'))
logger.info(prefix + __('skipped'))
if val.args:
logger.info(*val.args)
return True
elif val:
logger.info(__('failed'))
logger.info(prefix + __('failed'))
else:
logger.info(__('done'))
logger.info(prefix + __('done'))

return False

Expand Down

0 comments on commit 48cbb43

Please sign in to comment.