diff options
author | mathieui <mathieui@mathieui.net> | 2021-03-17 18:21:21 +0100 |
---|---|---|
committer | mathieui <mathieui@mathieui.net> | 2021-03-17 18:21:21 +0100 |
commit | 1de60fb213ee6fc1a845fffcbf91d360729353ce (patch) | |
tree | ef689ca533910be14a1fa99fe12382e1efa7d0c2 | |
parent | 5b6ec15f73a5085e5c83e5a5eb591d65de67c73d (diff) | |
download | poezio-1de60fb213ee6fc1a845fffcbf91d360729353ce.tar.gz poezio-1de60fb213ee6fc1a845fffcbf91d360729353ce.tar.bz2 poezio-1de60fb213ee6fc1a845fffcbf91d360729353ce.tar.xz poezio-1de60fb213ee6fc1a845fffcbf91d360729353ce.zip |
fix: poezio_logs script was broken
also add type hints and pep8ify
-rwxr-xr-x | scripts/poezio_logs | 53 |
1 files changed, 37 insertions, 16 deletions
diff --git a/scripts/poezio_logs b/scripts/poezio_logs index c97b304f..b2374a6d 100755 --- a/scripts/poezio_logs +++ b/scripts/poezio_logs @@ -3,12 +3,11 @@ A simple script to parse and output logs from a poezio logfile """ -from poezio.logger import LogInfo, LogMessage, parse_log_line +import argparse from functools import singledispatch +from typing import List, Optional, IO from poezio import poopt -import argparse -import datetime -import sys +from poezio.logger import LogInfo, LogMessage, parse_log_line INFO_COLOR = '\033[35;2m' NICK_COLOR = '\033[36;2m' @@ -18,12 +17,17 @@ TIME_COLOR = '\033[33;2m' SHOW_INFO = True SHOW_TIME = True + @singledispatch -def print_log(log_object, additional_lines=None): +def print_log(log_object: LogMessage, + additional_lines: Optional[List[str]] = None): time = log_object.time.strftime('%Y-%m-%d %H:%M:%S') nick = log_object.nick - offset = ((poopt.wcswidth(time) +1) if SHOW_TIME else 0) + 2 + poopt.wcswidth(nick) + offset = (( + (poopt.wcswidth(time) + 1) if SHOW_TIME else 0) + + 2 + poopt.wcswidth(nick) + ) pad = ' ' * offset if additional_lines: @@ -32,17 +36,29 @@ def print_log(log_object, additional_lines=None): more = '' if SHOW_TIME: - print(('%s%s%s %s%s%s> %s\n' % (TIME_COLOR, time, NO_COLOR, NICK_COLOR, nick, NO_COLOR, log_object.text)) + more, end='') + print( + f'{TIME_COLOR}{time}{NO_COLOR} {NICK_COLOR}{nick}{NO_COLOR}> ' + f'{log_object.text}\n' + more, + end='', + ) else: - print(('%s%s%s> %s\n' % (NICK_COLOR, nick, NO_COLOR, log_object.text)) + more, end='') + print( + f'{NICK_COLOR}{nick}{NO_COLOR}> ' + f'{log_object.text}\n' + more, + end='', + ) + @print_log.register(type(None)) -def _(log_object, additional_lines=None): +def print_log_none(log_object, additional_lines=None): return + @print_log.register(LogInfo) -def _(log_object, additional_lines=None): - if not SHOW_INFO: return +def print_log_loginfo(log_object: LogInfo, + additional_lines: Optional[List[str]] = None): + if not SHOW_INFO: + return time = log_object.time.strftime('%Y-%m-%d %H:%M:%S') + ' ' offset = (poopt.wcswidth(time) + 1) if SHOW_TIME else 0 @@ -54,13 +70,17 @@ def _(log_object, additional_lines=None): more = '' if SHOW_TIME: - print(('%s%s%s %s%s\n' % (TIME_COLOR, time, NO_COLOR, INFO_COLOR, log_object.text)) + more, end='') + print( + f'{TIME_COLOR}{time}{NO_COLOR}{log_object.text}\n' + more, + end='' + ) else: - print(('%s%s\n' % (INFO_COLOR, log_object.text)) + more, end='') + print(f'{INFO_COLOR}{log_object.text}\n' + more, end='') -def parse_messages(fd): + +def parse_messages(fd: IO[str]): in_text = False - more_lines = [] + more_lines: List[str] = [] current_log = None for line in fd: if in_text and not line.startswith(' '): @@ -71,10 +91,11 @@ def parse_messages(fd): elif in_text: more_lines.append(line[1:]) continue - current_log = parse_log_line(line) + current_log = parse_log_line(line, fd.name) in_text = True print_log(current_log, more_lines) + if __name__ == '__main__': parser = argparse.ArgumentParser('poezio_logs', description=""" Show the logs stored in poezio format in a more human-readable way. |