diff options
author | mathieui <mathieui@mathieui.net> | 2021-04-11 13:10:25 +0200 |
---|---|---|
committer | mathieui <mathieui@mathieui.net> | 2021-04-11 16:33:53 +0200 |
commit | 6d7921da55dd12dba63f6a4473893796ee61a2de (patch) | |
tree | 37a8ba3ab872563c9dc13dc108a3f0216d02b6f2 /poezio/logger.py | |
parent | a827743b8677ff9bb4ea63bbb1ac690fdfcb6457 (diff) | |
download | poezio-6d7921da55dd12dba63f6a4473893796ee61a2de.tar.gz poezio-6d7921da55dd12dba63f6a4473893796ee61a2de.tar.bz2 poezio-6d7921da55dd12dba63f6a4473893796ee61a2de.tar.xz poezio-6d7921da55dd12dba63f6a4473893796ee61a2de.zip |
internal: better typing & docstrings for logger/log loader
Diffstat (limited to 'poezio/logger.py')
-rw-r--r-- | poezio/logger.py | 52 |
1 files changed, 33 insertions, 19 deletions
diff --git a/poezio/logger.py b/poezio/logger.py index 7b6a5dc5..d30a78a3 100644 --- a/poezio/logger.py +++ b/poezio/logger.py @@ -11,7 +11,7 @@ conversations and roster changes import mmap import re -from typing import List, Dict, Optional, IO, Any, Union +from typing import List, Dict, Optional, IO, Any, Union, Generator from datetime import datetime from pathlib import Path @@ -19,6 +19,8 @@ from poezio import common from poezio.config import config from poezio.xhtml import clean_text from poezio.ui.types import Message, BaseMessage, LoggableTrait +from slixmpp import JID +from slixmpp.types import TypedDict, Literal import logging @@ -63,6 +65,16 @@ class LogMessage(LogItem): self.nick = nick +LogDict = TypedDict( + 'LogDict', + { + 'type': Literal['message', 'info'], 'txt': str, 'time': datetime, + 'history': bool, 'nickname': str + }, + total=False, +) + + def parse_log_line(msg: str, jid: str = '') -> Optional[LogItem]: """Parse a log line. @@ -111,33 +123,35 @@ class Logger: except Exception: pass - def get_file_path(self, jid: str) -> Path: + def get_file_path(self, jid: Union[str, JID]) -> Path: """Return the log path for a specific jid""" jidstr = str(jid).replace('/', '\\') return self.log_dir / jidstr - def fd_busy(self, jid: str) -> None: + def fd_busy(self, jid: Union[str, JID]) -> None: """Signal to the logger that this logfile is busy elsewhere. And that the messages should be queued to be logged later. :param jid: file name """ - self._busy_fds[jid] = True - self._buffered_fds[jid] = [] + jidstr = str(jid).replace('/', '\\') + self._busy_fds[jidstr] = True + self._buffered_fds[jidstr] = [] - def fd_available(self, jid: str) -> None: + def fd_available(self, jid: Union[str, JID]) -> None: """Signal to the logger that this logfile is no longer busy. And write messages to the end. :param jid: file name """ - if jid in self._busy_fds: - del self._busy_fds[jid] - if jid in self._buffered_fds: - msgs = ''.join(self._buffered_fds.pop(jid)) - if jid in self._fds: - self._fds[jid].close() - del self._fds[jid] + jidstr = str(jid).replace('/', '\\') + if jidstr in self._busy_fds: + del self._busy_fds[jidstr] + if jidstr in self._buffered_fds: + msgs = ''.join(self._buffered_fds.pop(jidstr)) + if jidstr in self._fds: + self._fds[jidstr].close() + del self._fds[jidstr] self.log_raw(jid, msgs) def close(self, jid: str) -> None: @@ -226,7 +240,7 @@ class Logger: return True return self.log_raw(jid, logged_msg) - def log_raw(self, jid: str, logged_msg: str, force: bool = False) -> bool: + def log_raw(self, jid: Union[str, JID], logged_msg: str, force: bool = False) -> bool: """Log a raw string. :param jid: filename @@ -334,7 +348,7 @@ def build_log_message(nick: str, return logged_msg + ''.join(' %s\n' % line for line in lines) -def last_message_in_archive(filepath: Path) -> Optional[Dict]: +def last_message_in_archive(filepath: Path) -> Optional[LogDict]: """Get the last message from the local archive. :param filepath: the log file path @@ -347,7 +361,7 @@ def last_message_in_archive(filepath: Path) -> Optional[Dict]: return last_msg -def iterate_messages_reverse(filepath: Path): +def iterate_messages_reverse(filepath: Path) -> Generator[LogDict, None, None]: """Get the latest messages from the log file, one at a time. :param fd: the file descriptor @@ -397,7 +411,7 @@ def _get_lines_from_fd(fd: IO[Any], nb: int = 10) -> List[str]: return lines -def parse_log_lines(lines: List[str], jid: str = '') -> List[Dict[str, Any]]: +def parse_log_lines(lines: List[str], jid: str = '') -> List[LogDict]: """ Parse raw log lines into poezio log objects @@ -420,11 +434,11 @@ def parse_log_lines(lines: List[str], jid: str = '') -> List[Dict[str, Any]]: log.debug('wrong log format? %s', log_item) continue message_lines = [] - message = { + message = LogDict({ 'history': True, 'time': common.get_local_time(log_item.time), 'type': 'message', - } + }) size = log_item.nb_lines if isinstance(log_item, LogInfo): message_lines.append(log_item.text) |