Pick events class from v4

This commit is contained in:
nemunaire 2014-09-23 22:27:53 +02:00
parent 04eccbe250
commit acded35e1a
6 changed files with 82 additions and 49 deletions

View File

@ -205,11 +205,17 @@ class EventConsumer:
def run(self, context): def run(self, context):
try: try:
self.evt.launch_check() self.evt.check()
except: except:
logger.exception("Error during event end") logger.exception("Error during event end")
# Reappend the event in the queue if it has next iteration
if self.evt.next is not None: if self.evt.next is not None:
context.add_event(self.evt, self.evt.id) context.add_event(self.evt, eid=self.evt.id)
# Or remove reference of this event
elif hasattr(self.evt, "module_src") and self.evt.module_src is not None:
self.evt.module_src.REGISTERED_EVENTS.remove(self.evt.id)

103
event.py
View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Nemubot is a modulable IRC bot, built around XML configuration files. # Nemubot is a smart and modulable IM bot.
# Copyright (C) 2012 Mercier Pierre-Olivier # Copyright (C) 2012-2014 nemunaire
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by # it under the terms of the GNU Affero General Public License as published by
@ -16,18 +16,36 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from datetime import datetime from datetime import datetime, timedelta
from datetime import timedelta
class ModuleEvent: class ModuleEvent:
def __init__(self, func=None, func_data=None, check=None, cmp_data=None,
intervalle=60, offset=0, call=None, call_data=None, times=1): """Representation of a event initiated by a bot module"""
def __init__(self, call=None, call_data=None, func=None, func_data=None,
cmp=None, cmp_data=None, interval=60, offset=0, times=1):
"""Initialize the event
Keyword arguments:
call -- Function to call when the event is realized
call_data -- Argument(s) (single or dict) to pass as argument
func -- Function called to check
func_data -- Argument(s) (single or dict) to pass as argument OR if no func, initial data to watch
cmp -- Boolean function called to check changes
cmp_data -- Argument(s) (single or dict) to pass as argument OR if no cmp, data compared to previous
interval -- Time in seconds between each check (default: 60)
offset -- Time in seconds added to interval before the first check (default: 0)
times -- Number of times the event has to be realized before being removed; -1 for no limit (default: 1)
"""
# What have we to check? # What have we to check?
self.func = func self.func = func
self.func_data = func_data self.func_data = func_data
# How detect a change? # How detect a change?
self.check = check self.cmp = cmp
self.cmp_data = None
if cmp_data is not None: if cmp_data is not None:
self.cmp_data = cmp_data self.cmp_data = cmp_data
elif self.func is not None: elif self.func is not None:
@ -37,20 +55,19 @@ class ModuleEvent:
self.cmp_data = self.func(**self.func_data) self.cmp_data = self.func(**self.func_data)
else: else:
self.cmp_data = self.func(self.func_data) self.cmp_data = self.func(self.func_data)
else:
self.cmp_data = None
self.offset = timedelta(seconds=offset) # Time to wait before the first check # What should we call when?
self.intervalle = timedelta(seconds=intervalle)
self.end = None
# What should we call when
self.call = call self.call = call
if call_data is not None: if call_data is not None:
self.call_data = call_data self.call_data = call_data
else: else:
self.call_data = func_data self.call_data = func_data
# Store times
self.offset = timedelta(seconds=offset) # Time to wait before the first check
self.interval = timedelta(seconds=interval)
self._end = None # Cache
# How many times do this event? # How many times do this event?
self.times = times self.times = times
@ -59,60 +76,68 @@ class ModuleEvent:
def current(self): def current(self):
"""Return the date of the near check""" """Return the date of the near check"""
if self.times != 0: if self.times != 0:
if self.end is None: if self._end is None:
self.end = datetime.now() + self.offset + self.intervalle self._end = datetime.now() + self.offset + self.interval
return self.end return self._end
return None return None
@property @property
def next(self): def next(self):
"""Return the date of the next check""" """Return the date of the next check"""
if self.times != 0: if self.times != 0:
if self.end is None: if self._end is None:
return self.current return self.current
elif self.end < datetime.now(): elif self._end < datetime.now():
self.end += self.intervalle self._end += self.interval
return self.end return self._end
return None return None
@property @property
def time_left(self): def time_left(self):
"""Return the time left before/after the near check""" """Return the time left before/after the near check"""
if self.current is not None: if self.current is not None:
return self.current - datetime.now() return self.current - datetime.now()
return 99999 return 99999 #TODO: 99999 is not a valid time to return
def launch_check(self):
def check(self):
"""Run a check and realized the event if this is time"""
# Get initial data
if self.func is None: if self.func is None:
d = self.func_data d_init = self.func_data
elif self.func_data is None: elif self.func_data is None:
d = self.func() d_init = self.func()
elif isinstance(self.func_data, dict): elif isinstance(self.func_data, dict):
d = self.func(**self.func_data) d_init = self.func(**self.func_data)
else: else:
d = self.func(self.func_data) d_init = self.func(self.func_data)
#print ("do test with", d, self.cmp_data)
if self.check is None: # then compare with current data
if self.cmp is None:
if self.cmp_data is None: if self.cmp_data is None:
r = True rlz = True
else: else:
r = d != self.cmp_data rlz = (d_init != self.cmp_data)
elif self.cmp_data is None: elif self.cmp_data is None:
r = self.check(d) rlz = self.cmp(d_init)
elif isinstance(self.cmp_data, dict): elif isinstance(self.cmp_data, dict):
r = self.check(d, **self.cmp_data) rlz = self.cmp(d_init, **self.cmp_data)
else: else:
r = self.check(d, self.cmp_data) rlz = self.cmp(d_init, self.cmp_data)
if r: if rlz:
self.times -= 1 self.times -= 1
# Call attended function
if self.call_data is None: if self.call_data is None:
if d is None: if d_init is None:
self.call() self.call()
else: else:
self.call(d) self.call(d_init)
elif isinstance(self.call_data, dict): elif isinstance(self.call_data, dict):
self.call(d, **self.call_data) self.call(d_init, **self.call_data)
else: else:
self.call(d, self.call_data) self.call(d_init, self.call_data)

View File

@ -14,7 +14,7 @@ yrn = datetime.today().year + 1
def load(context): def load(context):
d = datetime(yrn, 1, 1, 0, 0, 0) - datetime.now() d = datetime(yrn, 1, 1, 0, 0, 0) - datetime.now()
add_event(ModuleEvent(intervalle=0, offset=d.total_seconds(), call=bonneannee)) add_event(ModuleEvent(interval=0, offset=d.total_seconds(), call=bonneannee))
def bonneannee(): def bonneannee():
txt = "Bonne année %d !" % datetime.today().year txt = "Bonne année %d !" % datetime.today().year

View File

@ -29,7 +29,7 @@ def load(context):
for evt in DATAS.index.keys(): for evt in DATAS.index.keys():
if DATAS.index[evt].hasAttribute("end"): if DATAS.index[evt].hasAttribute("end"):
event = ModuleEvent(call=fini, call_data=dict(strend=DATAS.index[evt])) event = ModuleEvent(call=fini, call_data=dict(strend=DATAS.index[evt]))
event.end = DATAS.index[evt].getDate("end") event._end = DATAS.index[evt].getDate("end")
idt = context.add_event(event) idt = context.add_event(event)
if idt is not None: if idt is not None:
DATAS.index[evt]["id"] = idt DATAS.index[evt]["id"] = idt
@ -101,7 +101,7 @@ def start_countdown(msg):
strnd["end"] = datetime(now.year, now.month, now.day, hou, minu, sec) strnd["end"] = datetime(now.year, now.month, now.day, hou, minu, sec)
else: else:
strnd["end"] = datetime(now.year, now.month, now.day + 1, hou, minu, sec) strnd["end"] = datetime(now.year, now.month, now.day + 1, hou, minu, sec)
evt.end = strnd.getDate("end") evt._end = strnd.getDate("end")
strnd["id"] = add_event(evt) strnd["id"] = add_event(evt)
except: except:
DATAS.delChild(strnd) DATAS.delChild(strnd)
@ -122,8 +122,10 @@ def start_countdown(msg):
strnd["end"] += timedelta(days=int(t)*365) strnd["end"] += timedelta(days=int(t)*365)
else: else:
strnd["end"] += timedelta(seconds=int(t)) strnd["end"] += timedelta(seconds=int(t))
evt.end = strnd.getDate("end") evt._end = strnd.getDate("end")
strnd["id"] = add_event(evt) eid = add_event(evt)
if eid is not None:
strnd["id"] = eid
save() save()
if "end" in strnd: if "end" in strnd:

View File

@ -38,7 +38,7 @@ def start_watching(site, offset=0):
print_debug("Add event for site: %s" % o.netloc) print_debug("Add event for site: %s" % o.netloc)
evt = ModuleEvent(func=lambda url: w3m(url), cmp_data=site["lastcontent"], evt = ModuleEvent(func=lambda url: w3m(url), cmp_data=site["lastcontent"],
func_data=site["url"], offset=offset, func_data=site["url"], offset=offset,
intervalle=site.getInt("time"), interval=site.getInt("time"),
call=alert_change, call_data=site) call=alert_change, call_data=site)
site["_evt_id"] = add_event(evt) site["_evt_id"] = add_event(evt)

View File

@ -16,7 +16,7 @@ API_URL="http://worldcup.sfg.io/%s"
def load(context): def load(context):
from event import ModuleEvent from event import ModuleEvent
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, intervalle=30)) 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))
def help_full (): def help_full ():
@ -66,7 +66,7 @@ def cmd_watch(msg):
def current_match_new_action(match_str, osef): def current_match_new_action(match_str, osef):
global DATAS global DATAS
add_event(ModuleEvent(func=lambda url: urlopen(url).read().decode(), func_data=API_URL % "matches/current?by_date=DESC", call=current_match_new_action, intervalle=30)) add_event(ModuleEvent(func=lambda url: urlopen(url).read().decode(), func_data=API_URL % "matches/current?by_date=DESC", call=current_match_new_action, interval=30))
matches = json.loads(match_str) matches = json.loads(match_str)