From: Eric S. Raymond Date: Sat, 25 Aug 2012 17:36:43 +0000 (-0400) Subject: More compact URL parsing. X-Git-Tag: 1.0~132 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=f0ad6e128dc310255f68987ec5d928f1d4fbd4c9;p=irker.git More compact URL parsing. --- 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