Skip to content

Commit

Permalink
refactor: Replace wrapLines() with Python's textwrap.wrap() (#1702)
Browse files Browse the repository at this point in the history
Close #1699

Thanks to Tom Hunter aka @cornpaffies for contribution.
  • Loading branch information
cornpaffies committed May 5, 2024
1 parent bd8a768 commit 4b27c01
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 47 deletions.
45 changes: 2 additions & 43 deletions common/logger.py
Expand Up @@ -57,9 +57,8 @@ def closelog():


def _do_syslog(message: str, level: int) -> str:
for line in wrapLine(message):
syslog.syslog(level, '{}{}: {}'.format(
SYSLOG_MESSAGE_PREFIX, _level_names[level], line))
syslog.syslog(level, '{}{}: {}'.format(
SYSLOG_MESSAGE_PREFIX, _level_names[level], message))


def error(msg, parent=None, traceDepth=0):
Expand Down Expand Up @@ -142,43 +141,3 @@ def _debugHeader(parent, traceDepth):

func = frame.f_code.co_name
return '[%s/%s:%s %s%s]' % (fmodule, fname, line, fclass, func)


# This function was moved from tools.py to here to solve a circular
# import dependency between "tools" and "logger".
def wrapLine(msg, size=950, delimiters='\t ', new_line_indicator = 'CONTINUE: '):
"""
Wrap line ``msg`` into multiple lines with each shorter than ``size``. Try
to break the line on ``delimiters``. New lines will start with
``new_line_indicator``.
Args:
msg (str): string that should get wrapped
size (int): maximum length of returned strings
delimiters (str): try to break ``msg`` on these characters
new_line_indicator (str): start new lines with this string
Yields:
str: lines with max ``size`` length
"""

# TODO Use "textwrap.wrap" instead (https://docs.python.org/3/library/textwrap.html)
# (which may change the output formatting and may affect unit tests then)
# To avoid duplicated argument values in calls this function could
# act as a wrapper-

if len(new_line_indicator) >= size - 1:
new_line_indicator = ''
while msg:
if len(msg) <= size:
yield(msg)
break
else:
line = ''
for look in range(size-1, size//2, -1):
if msg[look] in delimiters:
line, msg = msg[:look+1], new_line_indicator + msg[look+1:]
break
if not line:
line, msg = msg[:size], new_line_indicator + msg[size:]
yield(line)
7 changes: 3 additions & 4 deletions qt/qtsystrayicon.py
Expand Up @@ -20,6 +20,7 @@
import os
import subprocess
import signal
import textwrap

# TODO Is this really required? If the client is not configured for X11
# it may use Wayland or something else...
Expand Down Expand Up @@ -177,10 +178,8 @@ def updateInfo(self):
self.last_message = message
if self.decode:
message = (message[0], self.decode.log(message[1]))
self.menuStatusMessage.setText('\n'.join(logger.wrapLine(message[1], \
size = 80, \
delimiters = '', \
new_line_indicator = '') \
self.menuStatusMessage.setText('\n'.join(textwrap.wrap(message[1], \
width = 80) \
))
self.status_icon.setToolTip(message[1])

Expand Down

0 comments on commit 4b27c01

Please sign in to comment.