def __init__(self, path): """Creates a new Chatlogger """ self.path = pathlib.Path(path) """the path to log chat to""" if not self.path.exists(): self.path.mkdir() if not self.path.is_dir(): psclient.log( f"E: Chatlogger(): logging directory is a file: {str(self.path.resolve())}" )
def formatData(self, data, isHTML=False): """Formats data to text Args: data (string of form userid|time|type|senderName|body): the data isHTML (bool, optional): Whether to format as HTML. Defaults to False. Returns: string: a human-readable version of the message """ splitData = data.split("|", 4) if len(splitData) == 5: userID, time, msgType, senderName, body = splitData elif len(splitData) == 3: userID, msgType, body = splitData time = "" senderName = userID else: psclient.log( f"DEBUG: unexpected number of data items (expected 5 or 3, got {str(len(splitData))}; data: f{data})" ) return "Unparseable message (bad format)" # TODO: figure out what to do about |html|, |raw|, etc try: time = f"[{str(datetime.utcfromtimestamp(int(time)).time())}] " if isHTML: time = f"<small>{html.escape(time)}</small>" except ValueError: time = "" body = body.strip().strip('\n') sender = senderName.strip() if isHTML: body = html.escape(body) sender = html.escape(sender) isAdmin = sender[:5] == '&' if sender else False htmlRankSet = set(psclient.ranksInOrder) htmlRankSet.discard('&') # '&' rank is already handled with isAdmin if sender and (isAdmin or sender[0] in htmlRankSet.union(set('+%@*#~'))): rank = sender[:5] if isAdmin else sender[0] sender = f"<small>{rank}</small><b>{sender[len(rank):]}</b>" if isHTML else rank + sender[ len(rank):] else: sender = f"<b>{sender}</b>" if msgType in ['chat', 'pm']: return f"{time}{sender}: {body}" if msgType == 'join': return f"{time}{sender} joined" if msgType == 'leave': return f"{time}{sender} left" return "Unparseable message"
def log(string: str) -> None: """Bootstrapped off of psclient.log(), but using our own loglevel Args: string (str): the string to log """ psclient.LOGLEVEL = config.loglevel return psclient.log(string)
def getFile(self, roomID, perms): """Returns a file object corresponding to the room's chatlog file. Args: roomID (string that is an ID): the room perms (string): the file perms (for example, 'r' or 'w') Returns: File: a file for the log file for that room and day """ roomFolderPath = self.path.joinpath(roomID) if not roomFolderPath.exists(): roomFolderPath.mkdir() if not roomFolderPath.is_dir(): return psclient.log( f"E: Chatlogger(): logging directory is a file: {str(roomFolderPath.resolve())}" ) filePath = roomFolderPath.joinpath(f"{str(datetime.now().date())}.txt") return filePath.open(perms)