From c3573ad5d4c2523f9064c7a1fd049eb489f17a57 Mon Sep 17 00:00:00 2001 From: JustAnotherArchivist Date: Mon, 12 Oct 2020 12:28:35 +0000 Subject: [PATCH] Wrap some overlong lines --- irclog.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 7 deletions(-) diff --git a/irclog.py b/irclog.py index 85fa4c4..8470860 100644 --- a/irclog.py +++ b/irclog.py @@ -219,7 +219,13 @@ class Config(dict): raise InvalidConfig(f'Invalid channel {key!r} extrasearchchannels: refers to auth-required channel whose auth differs from this channel\'s') # Default values - finalObj = {'logging': {'level': 'INFO', 'format': '{asctime} {levelname} {name} {message}'}, 'storage': {'path': os.path.abspath(os.path.dirname(self._filename)), 'flushTime': 60}, 'irc': {'host': 'irc.hackint.org', 'port': 6697, 'ssl': 'yes', 'nick': 'irclogbot', 'real': 'I am an irclog bot.', 'certfile': None, 'certkeyfile': None}, 'web': {'host': '127.0.0.1', 'port': 8080}, 'channels': {}} + finalObj = { + 'logging': {'level': 'INFO', 'format': '{asctime} {levelname} {name} {message}'}, + 'storage': {'path': os.path.abspath(os.path.dirname(self._filename)), 'flushTime': 60}, + 'irc': {'host': 'irc.hackint.org', 'port': 6697, 'ssl': 'yes', 'nick': 'irclogbot', 'real': 'I am an irclog bot.', 'certfile': None, 'certkeyfile': None}, + 'web': {'host': '127.0.0.1', 'port': 8080}, + 'channels': {} + } # Default values for channels are already set above. # Merge in what was read from the config file and set keys on self @@ -444,7 +450,13 @@ class IRCClientProtocol(asyncio.Protocol): self.transport.close() # JOIN errors - elif line.command in (ircstates.numerics.ERR_TOOMANYCHANNELS, ircstates.numerics.ERR_CHANNELISFULL, ircstates.numerics.ERR_INVITEONLYCHAN, ircstates.numerics.ERR_BANNEDFROMCHAN, ircstates.numerics.ERR_BADCHANNELKEY): + elif line.command in ( + ircstates.numerics.ERR_TOOMANYCHANNELS, + ircstates.numerics.ERR_CHANNELISFULL, + ircstates.numerics.ERR_INVITEONLYCHAN, + ircstates.numerics.ERR_BANNEDFROMCHAN, + ircstates.numerics.ERR_BADCHANNELKEY, + ): self.logger.error(f'Failed to join channel: {message!r}, terminating connection') self.transport.close() @@ -566,7 +578,8 @@ class IRCClientProtocol(asyncio.Protocol): channel = line.params[1] yield 'TOPIC', channel, f'Topic of {channel}: {line.params[2]}' elif line.command == ircstates.numerics.RPL_TOPICWHOTIME: - yield 'TOPICWHO', line.params[1], f'Topic set by {irctokens.hostmask(line.params[2]).nickname} at {datetime.datetime.utcfromtimestamp(int(line.params[3])).replace(tzinfo = datetime.timezone.utc):%Y-%m-%d %H:%M:%SZ}' + date = datetime.datetime.utcfromtimestamp(int(line.params[3])).replace(tzinfo = datetime.timezone.utc) + yield 'TOPICWHO', line.params[1], f'Topic set by {irctokens.hostmask(line.params[2]).nickname} at {date:%Y-%m-%d %H:%M:%SZ}' elif line.command == ircstates.numerics.RPL_ENDOFNAMES: channel = line.params[1] users = self.server.channels[self.server.casefold(channel)].users @@ -636,7 +649,12 @@ class IRCClient: connectionClosedEvent.clear() try: self.logger.debug('Creating IRC connection') - self._transport, self._protocol = await loop.create_connection(lambda: IRCClientProtocol(self.messageQueue, connectionClosedEvent, loop, self.config, self.channels), self.config['irc']['host'], self.config['irc']['port'], ssl = self._get_ssl_context()) + self._transport, self._protocol = await loop.create_connection( + protocol_factory = lambda: IRCClientProtocol(self.messageQueue, connectionClosedEvent, loop, self.config, self.channels), + host = self.config['irc']['host'], + port = self.config['irc']['port'], + ssl = self._get_ssl_context(), + ) self.logger.debug('Starting send queue processing') asyncio.create_task(self._protocol.send_queue()) # Quits automatically on connectionClosedEvent self.logger.debug('Waiting for connection closure or SIGINT') @@ -881,7 +899,20 @@ class WebServer: #TODO Implement this in a better way... fn = date.strftime('%Y-%m.log') lines = list(self._raw_to_lines(self._file_iter_with_path(os.path.join(self.config['storage']['path'], request.match_info["path"], fn), request.match_info["path"]), filter = lambda path, ts, command, content: dateStart <= ts <= dateEnd)) - return aiohttp.web.Response(text = f'{html.escape(self._paths[request.match_info["path"]][0])} log for {date:%Y-%m-%d}{self.logStyleTag}Previous day Next day

' + self._render_log(lines) + '', content_type = 'text/html') + return aiohttp.web.Response( + text = ''.join([ + '', + f'{html.escape(self._paths[request.match_info["path"]][0])} log for {date:%Y-%m-%d}{self.logStyleTag}', + '', + f'Previous day ', + f'Next day', + '

', + self._render_log(lines), + '', + '', + ]), + content_type = 'text/html' + ) async def search(self, request): self.logger.info(f'Received request {id(request)} from {request.remote!r} for {request.path!r}') @@ -890,7 +921,20 @@ class WebServer: return aiohttp.web.HTTPNotFound() if 'q' not in request.query: - return aiohttp.web.Response(text = f'{html.escape(self._paths[request.match_info["path"]][0])} search
', content_type = 'text/html') + return aiohttp.web.Response( + text = ''.join([ + '' + f'{html.escape(self._paths[request.match_info["path"]][0])} search', + '', + '
', + '', + '', + '
', + '', + '' + ]), + content_type = 'text/html' + ) cmd = ['grep', '--fixed-strings', '--recursive', '--with-filename', '--null', '--line-number', request.query['q']] for path in itertools.chain((request.match_info['path'],), self._paths[request.match_info['path']][3]): @@ -899,7 +943,17 @@ class WebServer: #TODO Limit size and runtime stdout, _ = await proc.communicate() lines = self._raw_to_lines(self._stdout_with_path(stdout)) - return aiohttp.web.Response(text = f'{html.escape(self._paths[request.match_info["path"]][0])} search results for "{html.escape(request.query["q"])}"{self.logStyleTag}' + self._render_log(lines, withDate = True) + '', content_type = 'text/html') + return aiohttp.web.Response( + text = ''.join([ + '', + f'{html.escape(self._paths[request.match_info["path"]][0])} search results for "{html.escape(request.query["q"])}"{self.logStyleTag}', + '', + self._render_log(lines, withDate = True), + '', + '' + ]), + content_type = 'text/html' + ) def configure_logging(config):