fix: poezio_logs script was broken

also add type hints and pep8ify
This commit is contained in:
mathieui 2021-03-17 18:21:21 +01:00
parent 5b6ec15f73
commit 1de60fb213

View file

@ -3,12 +3,11 @@
A simple script to parse and output logs from a poezio logfile A simple script to parse and output logs from a poezio logfile
""" """
from poezio.logger import LogInfo, LogMessage, parse_log_line
from functools import singledispatch
from poezio import poopt
import argparse import argparse
import datetime from functools import singledispatch
import sys from typing import List, Optional, IO
from poezio import poopt
from poezio.logger import LogInfo, LogMessage, parse_log_line
INFO_COLOR = '\033[35;2m' INFO_COLOR = '\033[35;2m'
NICK_COLOR = '\033[36;2m' NICK_COLOR = '\033[36;2m'
@ -18,12 +17,17 @@ TIME_COLOR = '\033[33;2m'
SHOW_INFO = True SHOW_INFO = True
SHOW_TIME = True SHOW_TIME = True
@singledispatch @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') time = log_object.time.strftime('%Y-%m-%d %H:%M:%S')
nick = log_object.nick 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 pad = ' ' * offset
if additional_lines: if additional_lines:
@ -32,17 +36,29 @@ def print_log(log_object, additional_lines=None):
more = '' more = ''
if SHOW_TIME: 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: 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)) @print_log.register(type(None))
def _(log_object, additional_lines=None): def print_log_none(log_object, additional_lines=None):
return return
@print_log.register(LogInfo) @print_log.register(LogInfo)
def _(log_object, additional_lines=None): def print_log_loginfo(log_object: LogInfo,
if not SHOW_INFO: return additional_lines: Optional[List[str]] = None):
if not SHOW_INFO:
return
time = log_object.time.strftime('%Y-%m-%d %H:%M:%S') + ' ' time = log_object.time.strftime('%Y-%m-%d %H:%M:%S') + ' '
offset = (poopt.wcswidth(time) + 1) if SHOW_TIME else 0 offset = (poopt.wcswidth(time) + 1) if SHOW_TIME else 0
@ -54,13 +70,17 @@ def _(log_object, additional_lines=None):
more = '' more = ''
if SHOW_TIME: 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: 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 in_text = False
more_lines = [] more_lines: List[str] = []
current_log = None current_log = None
for line in fd: for line in fd:
if in_text and not line.startswith(' '): if in_text and not line.startswith(' '):
@ -71,10 +91,11 @@ def parse_messages(fd):
elif in_text: elif in_text:
more_lines.append(line[1:]) more_lines.append(line[1:])
continue continue
current_log = parse_log_line(line) current_log = parse_log_line(line, fd.name)
in_text = True in_text = True
print_log(current_log, more_lines) print_log(current_log, more_lines)
if __name__ == '__main__': if __name__ == '__main__':
parser = argparse.ArgumentParser('poezio_logs', description=""" parser = argparse.ArgumentParser('poezio_logs', description="""
Show the logs stored in poezio format in a more human-readable way. Show the logs stored in poezio format in a more human-readable way.