Skip to content

Commit

Permalink
Merge pull request #644 from TotallyNotRobots/missing-mode-info
Browse files Browse the repository at this point in the history
Handle missing mode info for a mode
  • Loading branch information
linuxdaemon authored Mar 13, 2024
2 parents ebcd995 + a54946c commit 75399e0
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
10 changes: 8 additions & 2 deletions cloudbot/util/irc.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ class ModeChange:
info = attr.ib(type=ChannelMode)

@property
def is_status(self):
return self.info.type is ModeType.Status
def is_status(self) -> bool:
if not self.info:
return False

return self.info.type == ModeType.Status


@attr.s(hash=True)
Expand Down Expand Up @@ -78,6 +81,9 @@ def parse_mode_string(
adding = False
else:
mode_info = server_modes.get(c)
if not mode_info:
logger.warning("No ModeInfo found for %s", c)

if mode_info and mode_info.has_param(adding):
param = params.pop(0)
else:
Expand Down
3 changes: 2 additions & 1 deletion plugins/cryptocurrency.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
License:
GPL v3
"""

import inspect
import time
import warnings
Expand Down Expand Up @@ -660,7 +661,7 @@ def crypto_command(text, event):
def format_price(price: Union[int, float, Real]) -> str:
price = float(price)
if price < 1:
precision = max(2, min(10, -Decimal(str(price)).as_tuple().exponent))
precision = max(2, min(10, len(str(Decimal(str(price)))) - 2))
num_format = "{:01,.{}f}".format(price, precision)
else:
num_format = "{:,.2f}".format(price)
Expand Down
4 changes: 2 additions & 2 deletions plugins/minecraft_ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# TODO(linuxdaemon): Implement bedrock support
from mcstatus import JavaServer as MinecraftServer
from mcstatus.pinger import PingResponse
from mcstatus.status_response import JavaStatusResponse

from cloudbot import hook
from cloudbot.util import colors
Expand Down Expand Up @@ -48,7 +48,7 @@ def mcping(text):
return str(e)

try:
s = server.status() # type: PingResponse
s: JavaStatusResponse = server.status()
except socket.gaierror:
return "Invalid hostname"
except socket.timeout:
Expand Down
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
darker[flynt,isort] == 1.6.1
freezegun == 1.1.0
mypy == 0.910
mypy == 1.0.0
pre-commit == 3.3.3
pylint == 3.1.0
pytest == 6.2.5
pytest-asyncio == 0.20.3
pytest-cov == 4.1.0
pytest-random-order == 1.0.4
responses == 0.25.0
typing_extensions == 4.10.0
types-requests == 2.27.7
types-setuptools == 57.4.7
types-six == 1.16.2
Expand Down
30 changes: 30 additions & 0 deletions tests/core_tests/util_tests/test_irc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from cloudbot.util import irc


def test_mode_parse():
mode_info = {
"a": irc.ChannelMode(character="a", type=irc.ModeType.A),
"d": irc.StatusMode.make("^", "d", 3),
}
parsed = irc.parse_mode_string("+ad", ["foo", "bar"], mode_info)

assert parsed == [
irc.ModeChange("a", True, "foo", mode_info["a"]),
irc.ModeChange("d", True, "bar", mode_info["d"]),
]


def test_mode_parse_missing_mode():
mode_info = {
"a": irc.ChannelMode(character="a", type=irc.ModeType.A),
"d": irc.StatusMode.make("^", "d", 3),
}
parsed = irc.parse_mode_string("+adx", ["foo", "bar"], mode_info)

assert parsed == [
irc.ModeChange("a", True, "foo", mode_info["a"]),
irc.ModeChange("d", True, "bar", mode_info["d"]),
irc.ModeChange("x", True, None, None),
]

assert [m.is_status for m in parsed] == [False, True, False]

0 comments on commit 75399e0

Please sign in to comment.