From f0ad6e128dc310255f68987ec5d928f1d4fbd4c9 Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Sat, 25 Aug 2012 13:36:43 -0400 Subject: [PATCH] More compact URL parsing. --- irker.py | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/irker.py b/irker.py index 182dabc..f14d2a3 100755 --- a/irker.py +++ b/irker.py @@ -8,7 +8,7 @@ and relays to IRC channels. Requires Python 2.6. """ -import os, sys, json, irclib, exceptions, getopt +import os, sys, json, irclib, exceptions, getopt, urlparse import threading, Queue class SessionException(exceptions.Exception): @@ -26,21 +26,14 @@ class Session(): self.thread = threading.Thread(target=self.dequeue) self.thread.daemon = True self.thread.start() - # The channel specification - if not url.startswith("irc://") or url.count("/") != 3: - raise SessionException("ill-formed IRC URL") - else: - url = url[6:] - parts = url.split(":", 1) - if len(parts) == 2: - try: - self.port = int(parts[1]) - except ValuError: - raise SessionException("invalid port number") - else: - self.port = 6667 - (self.servername, self.channel) = parts[0].split("/", 1) # Client setup + parsed = urlparse.urlparse(url) + host, sep, port = parsed.netloc.partition(':') + if not port: + port = 6667 + self.servername = host + self.channel = parsed.path.lstrip('/') + self.port = int(port) #self.ircserver.connect(self.servername, self.port, self.name()) # Also must join the channel. Session.count += 1 -- 2.26.2