From d338dc15c57e3aea12de78354da908b1457c5055 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Tue, 10 Jul 2012 05:05:52 -0400 Subject: [PATCH] Clean up command more (#30) --- earwigbot/commands/lag.py | 60 ++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/earwigbot/commands/lag.py b/earwigbot/commands/lag.py index c61cee6..86d5dd9 100644 --- a/earwigbot/commands/lag.py +++ b/earwigbot/commands/lag.py @@ -29,39 +29,41 @@ class Lag(Command): commands = ["lag", "replag", "maxlag"] def process(self, data): - if data.kwargs and "project" in data.kwargs and "lang" in data.kwargs: - project, lang = data.kwargs["project"], data.kwargs["lang"] - site = self.get_site(data, project, lang) - if not site: - return - elif data.args: - if len(data.args) > 1: - name = " ".join(data.args) - self.reply(data, "unknown site: \x0302{0}\x0F.".format(name)) - return - name = data.args[0] - if "." in name: - lang, project = name.split(".")[:2] - elif ":" in name: - project, lang = name.split(":")[:2] - else: - try: - site = self.bot.wiki.get_site(name) - except exceptions.SiteNotFoundError: - msg = "unknown site: \x0302{0}\x0F.".format(name) - self.reply(data, msg) - return - site = self.get_site(data, project, lang) - if not site: - return - else: - site = self.bot.wiki.get_site() + site = self.get_site(data) + if not site: + return - msg = "\x0302{0}\x0F: Toolserver replag is {1} seconds; database maxlag is {2} seconds" + msg = "\x0302{0}\x0F: Toolserver replag is {1} seconds; database maxlag is {2} seconds." msg = msg.format(site.name, site.get_replag(), site.get_maxlag()) self.reply(data, msg) - def get_site(self, data, project, lang): + def get_site(self): + if data.kwargs and "project" in data.kwargs and "lang" in data.kwargs: + project, lang = data.kwargs["project"], data.kwargs["lang"] + return self.get_site_from_proj_and_lang(data, project, lang) + + if not data.args: + return self.bot.wiki.get_site() + + if len(data.args) > 1: + name = " ".join(data.args) + self.reply(data, "unknown site: \x0302{0}\x0F.".format(name)) + return + name = data.args[0] + if "." in name: + lang, project = name.split(".")[:2] + elif ":" in name: + project, lang = name.split(":")[:2] + else: + try: + return self.bot.wiki.get_site(name) + except exceptions.SiteNotFoundError: + msg = "unknown site: \x0302{0}\x0F.".format(name) + self.reply(data, msg) + return + return self.get_site_from_proj_and_lang(data, project, lang) + + def get_site_from_proj_and_lang(self, data, project, lang): try: site = self.bot.wiki.get_site(project=project, lang=lang) except exceptions.SiteNotFoundError: