From 5bec50744c0a03394aa47810961861b4c0686959 Mon Sep 17 00:00:00 2001 From: nemunaire Date: Fri, 25 Jul 2014 18:02:30 +0200 Subject: [PATCH] Dusting some modules --- modules/man.py | 2 +- modules/sleepytime.py | 4 +- modules/spell/__init__.py | 2 +- modules/velib.py | 25 ++++------- modules/watchWebsite/__init__.py | 77 +++++++++++++------------------- 5 files changed, 43 insertions(+), 67 deletions(-) diff --git a/modules/man.py b/modules/man.py index 00edc8e..e12b84a 100644 --- a/modules/man.py +++ b/modules/man.py @@ -13,7 +13,7 @@ def load(context): def help_tiny (): """Line inserted in the response to the command !help""" - return "Read man on IRC" + return "Read manual pages on IRC" def help_full (): return "!man [0-9] /what/: gives informations about /what/." diff --git a/modules/sleepytime.py b/modules/sleepytime.py index b53a2e5..a97d509 100644 --- a/modules/sleepytime.py +++ b/modules/sleepytime.py @@ -37,7 +37,7 @@ def cmd_sleep(msg): g.append(f[i+1].strftime("%H:%M")) return Response(msg.sender, "You should try to fall asleep at one of the following" - " times: %s" % ', '.join(g), msg.channel) + " times: %s" % ', '.join(g), channel=msg.channel) # Just get awake times else: @@ -49,4 +49,4 @@ def cmd_sleep(msg): return Response(msg.sender, "If you head to bed right now, you should try to wake" " up at one of the following times: %s" % - ', '.join(g), msg.channel) + ', '.join(g), channel=msg.channel) diff --git a/modules/spell/__init__.py b/modules/spell/__init__.py index ebaa7c8..07f69ff 100644 --- a/modules/spell/__init__.py +++ b/modules/spell/__init__.py @@ -26,7 +26,7 @@ def load(context): def cmd_spell(msg): if len(msg.cmds) < 2: - return Response(msg.sender, "indique une orthographe approximative du mot dont tu veux vérifier l'orthographe.", msg.channel, msg.nick) + raise IRCException("indique une orthographe approximative du mot dont tu veux vérifier l'orthographe.") lang = "fr" strRes = list() diff --git a/modules/velib.py b/modules/velib.py index 8385476..f001778 100644 --- a/modules/velib.py +++ b/modules/velib.py @@ -57,21 +57,17 @@ def print_station_status(msg, station): (available, free) = station_status(station) if available is not None and free is not None: return Response(msg.sender, - "%s: à la station %s : %d vélib et %d points d'attache" - " disponibles." % (msg.nick, station, available, free), - msg.channel) - else: - return Response(msg.sender, - "%s: station %s inconnue." % (msg.nick, station), - msg.channel) + "à la station %s : %d vélib et %d points d'attache" + " disponibles." % (station, available, free), + channel=msg.channel, nick=msg.nick) + raise IRCException("station %s inconnue." % station) def ask_stations(msg): """Hook entry from !velib""" global DATAS if len(msg.cmds) > 5: - return Response(msg.sender, - "Demande-moi moins de stations à la fois.", - msg.channel, nick=msg.nick) + raise IRCException("demande-moi moins de stations à la fois.") + elif len(msg.cmds) > 1: for station in msg.cmds[1:]: if re.match("^[0-9]{4,5}$", station): @@ -79,10 +75,7 @@ def ask_stations(msg): elif station in DATAS.index: return print_station_status(msg, DATAS.index[station]["number"]) else: - return Response(msg.sender, - "numéro de station invalide.", - msg.channel, nick=msg.nick) + raise IRCException("numéro de station invalide.") + else: - return Response(msg.sender, - "Pour quelle station ?", - msg.channel, nick=msg.nick) + raise IRCException("pour quelle station ?") diff --git a/modules/watchWebsite/__init__.py b/modules/watchWebsite/__init__.py index 1f69158..ba56450 100644 --- a/modules/watchWebsite/__init__.py +++ b/modules/watchWebsite/__init__.py @@ -32,18 +32,11 @@ def load(context): print("No alert defined for this site: " + site["url"]) #DATAS.delChild(site) -def unload(context): - """Unregister watched website""" - # Useless in 3.3? -# for site in DATAS.getNodes("watch"): -# context.del_event(site["evt_id"]) - pass - def getPageContent(url): """Returns the content of the given url""" print_debug("Get page %s" % url) try: - raw = urlopen(url, timeout=15) + raw = urlopen(url, timeout=10) return raw.read().decode() except: return None @@ -60,8 +53,7 @@ def start_watching(site): def del_site(msg): if len(msg.cmds) <= 1: - return Response(msg.sender, "quel site dois-je arrêter de surveiller ?", - msg.channel, msg.nick) + raise IRCException("quel site dois-je arrêter de surveiller ?") url = msg.cmds[1] @@ -71,53 +63,44 @@ def del_site(msg): for a in site.getNodes("alert"): if a["channel"] == msg.channel: if (msg.sender == a["sender"] or msg.is_owner): - site.delChild(a) - if not site.hasNode("alert"): - del_event(site["_evt_id"]) - DATAS.delChild(site) - save() - return Response(msg.sender, - "je ne surveille désormais plus cette URL.", - channel=msg.channel, nick=msg.nick) - else: - return Response(msg.sender, - "Vous ne pouvez pas supprimer cette URL.", - channel=msg.channel, nick=msg.nick) - return Response(msg.sender, - "je ne surveillais pas cette URL, impossible de la supprimer.", - channel=msg.channel, nick=msg.nick) - return Response(msg.sender, "je ne surveillais pas cette URL pour vous.", - channel=msg.channel, nick=msg.nick) + raise IRCException("vous ne pouvez pas supprimer cette URL.") + site.delChild(a) + if not site.hasNode("alert"): + del_event(site["_evt_id"]) + DATAS.delChild(site) + save() + return Response(msg.sender, + "je ne surveille désormais plus cette URL.", + channel=msg.channel, nick=msg.nick) + raise IRCException("je ne surveillais pas cette URL !") def add_site(msg, diffType="diff"): print (diffType) if len(msg.cmds) <= 1: - return Response(msg.sender, "quel site dois-je surveiller ?", - msg.channel, msg.nick) + raise IRCException("quel site dois-je surveiller ?") url = msg.cmds[1] o = urlparse(url, "http") - if o.netloc != "": - alert = ModuleState("alert") - alert["sender"] = msg.sender - alert["server"] = msg.server - alert["channel"] = msg.channel - alert["message"] = "%s a changé !" % url + if o.netloc == "": + raise IRCException("je ne peux pas surveiller cette URL") - if url not in DATAS.index: - watch = ModuleState("watch") - watch["type"] = diffType - watch["url"] = url - watch["time"] = 123 - DATAS.addChild(watch) - watch.addChild(alert) - start_watching(watch) - else: - DATAS.index[url].addChild(alert) + alert = ModuleState("alert") + alert["sender"] = msg.sender + alert["server"] = msg.server + alert["channel"] = msg.channel + alert["message"] = "%s a changé !" % url + + if url not in DATAS.index: + watch = ModuleState("watch") + watch["type"] = diffType + watch["url"] = url + watch["time"] = 123 + DATAS.addChild(watch) + watch.addChild(alert) + start_watching(watch) else: - return Response(msg.sender, "je ne peux pas surveiller cette URL", - channel=msg.channel, nick=msg.nick) + DATAS.index[url].addChild(alert) save() return Response(msg.sender, channel=msg.channel, nick=msg.nick,