events: ModuleEvent don't store function argument anymore

This commit is contained in:
nemunaire 2018-12-30 00:42:21 +01:00
commit b349d22370
4 changed files with 23 additions and 66 deletions

View file

@ -1,5 +1,6 @@
"""Alert on changes on websites"""
from functools import partial
import logging
from random import randint
import urllib.parse
@ -209,15 +210,14 @@ def start_watching(site, offset=0):
offset -- offset time to delay the launch of the first check
"""
o = urlparse(getNormalizedURL(site["url"]), "http")
#print_debug("Add %s event for site: %s" % (site["type"], o.netloc))
#o = urlparse(getNormalizedURL(site["url"]), "http")
#print("Add %s event for site: %s" % (site["type"], o.netloc))
try:
evt = ModuleEvent(func=fwatch,
cmp_data=site["lastcontent"],
func_data=site["url"], offset=offset,
interval=site.getInt("time"),
call=alert_change, call_data=site)
evt = ModuleEvent(func=partial(fwatch, url=site["url"]),
cmp=site["lastcontent"],
offset=offset, interval=site.getInt("time"),
call=partial(alert_change, site=site))
site["_evt_id"] = add_event(evt)
except IMException:
logger.exception("Unable to watch %s", site["url"])

View file

@ -4,6 +4,7 @@
import email
from email.utils import mktime_tz, parseaddr, parsedate_tz
from functools import partial
from nntplib import NNTP, decode_header
import re
import time
@ -89,7 +90,7 @@ def _indexServer(**kwargs):
return "{user}:{password}@{host}:{port}".format(**kwargs)
def _newevt(**args):
context.add_event(ModuleEvent(call=_fini, call_data=args, interval=42))
context.add_event(ModuleEvent(call=partial(_fini, **args), interval=42))
def _fini(to_server, to_channel, lastcheck, group, server):
print("fini called")

View file

@ -3,6 +3,7 @@
"""The 2014,2018 football worldcup module"""
from datetime import datetime, timezone
from functools import partial
import json
import re
from urllib.parse import quote
@ -21,7 +22,7 @@ from nemubot.module.more import Response
API_URL="http://worldcup.sfg.io/%s"
def load(context):
context.add_event(ModuleEvent(func=lambda url: urlopen(url, timeout=10).read().decode(), func_data=API_URL % "matches/current?by_date=DESC", call=current_match_new_action, interval=30))
context.add_event(ModuleEvent(func=partial(lambda url: urlopen(url, timeout=10).read().decode(), API_URL % "matches/current?by_date=DESC"), call=current_match_new_action, interval=30))
def help_full ():
@ -65,10 +66,10 @@ def cmd_watch(msg):
context.save()
raise IMException("This channel will not anymore receives world cup events.")
def current_match_new_action(matches, osef):
def current_match_new_action(matches):
def cmp(om, nm):
return len(nm) and (len(om) == 0 or len(nm[0]["home_team_events"]) != len(om[0]["home_team_events"]) or len(nm[0]["away_team_events"]) != len(om[0]["away_team_events"]))
context.add_event(ModuleEvent(func=lambda url: json.loads(urlopen(url).read().decode()), func_data=API_URL % "matches/current?by_date=DESC", cmp=cmp, call=current_match_new_action, interval=30))
context.add_event(ModuleEvent(func=partial(lambda url: json.loads(urlopen(url).read().decode()), API_URL % "matches/current?by_date=DESC"), cmp=partial(cmp, matches), call=current_match_new_action, interval=30))
for match in matches:
if is_valid(match):