Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isupport: maintain PREFIX ordering #2200

Merged
merged 1 commit into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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