From: Christian Heller Date: Sat, 22 Nov 2025 15:16:39 +0000 (+0100) Subject: Get rid of unnecessary MsgParseExpectation.idx_into_list. X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/todo?a=commitdiff_plain;h=e75f089f7cbc8934337a0d02608e4a6a85c3db4e;p=ircplom Get rid of unnecessary MsgParseExpectation.idx_into_list. --- diff --git a/src/ircplom/msg_parse_expectations.py b/src/ircplom/msg_parse_expectations.py index 26cf2e5..e01da27 100644 --- a/src/ircplom/msg_parse_expectations.py +++ b/src/ircplom/msg_parse_expectations.py @@ -36,7 +36,6 @@ class _MsgParseExpectation: verb: str, source: _MsgTokGuide, params: tuple[_MsgTokGuide, ...] = tuple(), - idx_into_list: int = -1, bonus_tasks: tuple[str, ...] = tuple() ) -> None: @@ -65,7 +64,6 @@ class _MsgParseExpectation: self.verb = verb self.source = _TokExpectation.from_(source) self.params = tuple(_TokExpectation.from_(param) for param in params) - self.idx_into_list = idx_into_list self.bonus_tasks = tuple(_Code.from_(item) for item in bonus_tasks) def parse_msg(self, @@ -76,15 +74,18 @@ class _MsgParseExpectation: into_nickuserhost: Callable ) -> Optional[dict[str, Any]]: 'Try parsing msg into informative result dictionary, or None on fail.' - cmp_params: list[str | tuple[str, ...]] - if self.idx_into_list < 0: - cmp_params = list(msg.params) - else: - idx_after = len(msg.params) + 1 - (len(self.params) - - self.idx_into_list) - cmp_params = (list(msg.params[:self.idx_into_list]) + - [msg.params[self.idx_into_list:idx_after]] + - list(msg.params[idx_after:])) + cmp_params: list[str | tuple[str, ...]] = [] + idx_after = 0 + for idx, param in enumerate(self.params): + if param == _MsgTok.LIST or (isinstance(param, tuple) + and param[0] == _MsgTok.LIST): + idx_after = len(msg.params) + 1 - (len(self.params) - idx) + cmp_params += [' '.join(msg.params[idx:idx_after])] + cmp_params += list(msg.params[idx_after:]) + break + cmp_params += msg.params[idx:idx + 1] + if (not idx_after) and len(cmp_params) != len(msg.params): + return None cmp_fields = tuple([msg.source] + cmp_params) ex_fields = tuple([self.source] + list(self.params)) if len(ex_fields) != len(cmp_fields): @@ -139,7 +140,7 @@ class _MsgParseExpectation: MSG_EXPECTATIONS: list[_MsgParseExpectation] = [ - # these we ignore except, where possible, for checking our nickname + # these we mostly ignore except, where possible, for checking our nickname _MsgParseExpectation( '001', # RPL_WELCOME @@ -247,9 +248,8 @@ MSG_EXPECTATIONS: list[_MsgParseExpectation] = [ '005', # RPL_ISUPPORT _MsgTok.SERVER, ((_MsgTok.NICKNAME, 'setattr_db.users.me:nick'), - (_MsgTok.ANY, 'do_db.set_isupport_from_rpl:isupport'), - _MsgTok.ANY), # comment - idx_into_list=1), + (_MsgTok.LIST, 'do_db.set_isupport_from_rpl:isupport'), + _MsgTok.ANY)), # comment _MsgParseExpectation( '372', # RPL_MOTD @@ -348,13 +348,13 @@ MSG_EXPECTATIONS: list[_MsgParseExpectation] = [ _MsgTok.SERVER, ('*', ('LS', ':subverb'), + ('*', ':tbc'), (_MsgTok.LIST, ':items'))), _MsgParseExpectation( 'CAP', _MsgTok.SERVER, ('*', ('LS', ':subverb'), - ('*', ':tbc'), (_MsgTok.LIST, ':items'))), _MsgParseExpectation( 'CAP', @@ -375,26 +375,26 @@ MSG_EXPECTATIONS: list[_MsgParseExpectation] = [ _MsgTok.SERVER, ('*', ('LIST', ':subverb'), + ('*', ':tbc'), (_MsgTok.LIST, ':items'))), _MsgParseExpectation( 'CAP', _MsgTok.SERVER, ('*', ('LIST', ':subverb'), - ('*', ':tbc'), (_MsgTok.LIST, ':items'))), _MsgParseExpectation( 'CAP', _MsgTok.SERVER, ((_MsgTok.NICKNAME, 'setattr_db.users.me:nick'), ('LIST', ':subverb'), + ('*', ':tbc'), (_MsgTok.LIST, ':items'))), _MsgParseExpectation( 'CAP', _MsgTok.SERVER, ((_MsgTok.NICKNAME, 'setattr_db.users.me:nick'), ('LIST', ':subverb'), - ('*', ':tbc'), (_MsgTok.LIST, ':items'))), # nickname management