Skip to content

Commit

Permalink
Restore version resolution. Fixes #174.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Sep 8, 2020
1 parent a2e8405 commit c7e6023
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
12 changes: 12 additions & 0 deletions irc/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import contextlib

try:
from importlib import metadata # type: ignore
except ImportError:
import importlib_metadata as metadata # type: ignore


def _get_version():
with contextlib.suppress(Exception):
return metadata.version('irc')
return 'unknown'
5 changes: 3 additions & 2 deletions irc/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,13 @@ def disconnect(self, msg="I'll be back!"):
"""
self.connection.disconnect(msg)

def get_version(self):
@staticmethod
def get_version():
"""Returns the bot version.
Used when answering a CTCP VERSION request.
"""
return "Python irc.bot ({version})".format(version=irc.client.VERSION_STRING)
return f"Python irc.bot ({irc._get_version()})"

def jump_server(self, msg="Changing servers"):
"""Connect to a new server, possibly disconnecting from the current.
Expand Down
5 changes: 2 additions & 3 deletions irc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@
import irc.client
from . import events

SRV_WELCOME = "Welcome to {__name__} v{irc.client.VERSION}.".format(**locals())

log = logging.getLogger(__name__)


Expand Down Expand Up @@ -219,11 +217,12 @@ def handle_nick(self, params):
# and MOTD.
self.nick = nick
self.server.clients[nick] = self
msg = f"Welcome to {__name__} v{irc._get_version()}."
response = ':%s %s %s :%s' % (
self.server.servername,
events.codes['welcome'],
self.nick,
SRV_WELCOME,
msg,
)
self.send_queue.append(response)
response = ':%s 376 %s :End of MOTD command.' % (
Expand Down
4 changes: 4 additions & 0 deletions irc/tests/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,7 @@ def test_reconnects_are_stable(self, disconnecting_server):
bot.reactor.process_once()
time.sleep(0.01)
assert len(bot.reactor.scheduler.queue) <= 1


def test_version():
assert isinstance(irc.bot.SingleServerIRCBot.get_version(), str)
6 changes: 1 addition & 5 deletions irc/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@


def test_version():
assert 'VERSION' in vars(irc.client)
assert 'VERSION_STRING' in vars(irc.client)
assert isinstance(irc.client.VERSION, tuple)
assert irc.client.VERSION, "No VERSION detected."
assert isinstance(irc.client.VERSION_STRING, str)
assert isinstance(irc._get_version(), str)


@mock.patch('irc.connection.socket')
Expand Down

0 comments on commit c7e6023

Please sign in to comment.