Skip to content

Commit

Permalink
isupport: maintain PREFIX ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
half-duplex committed Oct 24, 2021
1 parent b8fec20 commit 0848282
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
13 changes: 10 additions & 3 deletions sopel/irc/isupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
# Licensed under the Eiffel Forum License 2.
from __future__ import generator_stop

from collections import OrderedDict
import functools
import itertools
import re
from typing import Dict


def _optional(parser, default=None):
Expand Down Expand Up @@ -97,7 +99,7 @@ def _parse_prefix(value):
if len(modes) != len(prefixes):
raise ValueError('Mode list does not match for PREFIX: %r' % value)

return tuple(sorted(zip(modes, prefixes)))
return tuple(zip(modes, prefixes))


ISUPPORT_PARSERS = {
Expand Down Expand Up @@ -328,7 +330,7 @@ def MAXLIST(self):
return dict(self['MAXLIST'])

@property
def PREFIX(self):
def PREFIX(self) -> Dict[str, str]:
"""Expose ``PREFIX`` as a dict, if advertised by the server.
This exposes information about the modes and nick prefixes used for
Expand All @@ -343,6 +345,8 @@ def PREFIX(self):
'v': '+',
}
Entries are in order of descending privilege.
This attribute is not available if the server does not provide the
right information, and accessing it will raise an
:exc:`AttributeError`.
Expand All @@ -355,7 +359,10 @@ def PREFIX(self):
if 'PREFIX' not in self:
raise AttributeError('PREFIX')

return dict(self['PREFIX'])
# This can use a normal dict once we drop python 3.6, as 3.7 promises
# `dict` maintains insertion order. Since `OrderedDict` subclasses
# `dict`, we'll not promise to always return the former.
return OrderedDict(self['PREFIX'])

@property
def TARGMAX(self):
Expand Down
25 changes: 25 additions & 0 deletions test/irc/test_irc_isupport.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Tests for core ``sopel.irc.isupport``"""
from __future__ import generator_stop

from collections import OrderedDict

import pytest

from sopel.irc import isupport
Expand Down Expand Up @@ -500,6 +502,29 @@ def test_parse_parameter_prefix_invalid_format():
isupport.parse_parameter('PREFIX=(o)@+')


def test_parse_parameter_prefix_order_parser():
"""Ensure PREFIX order is maintained through parser.
https://modern.ircdocs.horse/#prefix-parameter
"""
key, value = isupport.parse_parameter('PREFIX=(qov)~@+')

assert value == (('q', '~'), ('o', '@'), ('v', '+'))


def test_parse_parameter_prefix_order_property():
"""Ensure PREFIX order is maintained in property."""
instance = isupport.ISupport()

key, value = isupport.parse_parameter('PREFIX=(qov)~@+')
new = instance.apply(
prefix=value,
)

assert new.PREFIX == OrderedDict((('q', '~'), ('o', '@'), ('v', '+')))
assert tuple(new.PREFIX.keys()) == ('q', 'o', 'v')


def test_parse_parameter_targmax():
key, value = isupport.parse_parameter('TARGMAX=PRIVMSG:3')

Expand Down

0 comments on commit 0848282

Please sign in to comment.