wip event manager
This commit is contained in:
parent
47ada614fa
commit
e943a55626
@ -24,8 +24,8 @@ class ModuleEvent:
|
|||||||
"""Representation of a event initiated by a bot module"""
|
"""Representation of a event initiated by a bot module"""
|
||||||
|
|
||||||
def __init__(self, call=None, call_data=None, func=None, func_data=None,
|
def __init__(self, call=None, call_data=None, func=None, func_data=None,
|
||||||
cmp=None, cmp_data=None, interval=60, offset=0, times=1):
|
cmp=None, cmp_data=None, end_call=None, end_data=None,
|
||||||
|
interval=60, offset=0, times=1, max_attempt=-1):
|
||||||
"""Initialize the event
|
"""Initialize the event
|
||||||
|
|
||||||
Keyword arguments:
|
Keyword arguments:
|
||||||
@ -35,9 +35,12 @@ class ModuleEvent:
|
|||||||
func_data -- Argument(s) (single or dict) to pass as argument OR if no func, initial data to watch
|
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 -- 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
|
cmp_data -- Argument(s) (single or dict) to pass as argument OR if no cmp, data compared to previous
|
||||||
|
end_call -- Function called when times or max_attempt reach 0 (mainly for interaction with the event manager)
|
||||||
|
end_data -- Argument(s) (single or dict) to pass as argument
|
||||||
interval -- Time in seconds between each check (default: 60)
|
interval -- Time in seconds between each check (default: 60)
|
||||||
offset -- Time in seconds added to interval before the first check (default: 0)
|
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)
|
times -- Number of times the event has to be realized before being removed; -1 for no limit (default: 1)
|
||||||
|
max_attempt -- Maximum number of times the event will be checked
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# What have we to check?
|
# What have we to check?
|
||||||
@ -46,16 +49,17 @@ class ModuleEvent:
|
|||||||
|
|
||||||
# How detect a change?
|
# How detect a change?
|
||||||
self.cmp = cmp
|
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 callable(self.func):
|
||||||
if self.func_data is None:
|
if self.func_data is None:
|
||||||
self.cmp_data = self.func()
|
self.cmp_data = self.func()
|
||||||
elif isinstance(self.func_data, dict):
|
elif isinstance(self.func_data, dict):
|
||||||
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
|
||||||
|
|
||||||
# What should we call when?
|
# What should we call when?
|
||||||
self.call = call
|
self.call = call
|
||||||
@ -64,46 +68,29 @@ class ModuleEvent:
|
|||||||
else:
|
else:
|
||||||
self.call_data = func_data
|
self.call_data = func_data
|
||||||
|
|
||||||
# Store times
|
# Store time between each event
|
||||||
self.offset = timedelta(seconds=offset) # Time to wait before the first check
|
|
||||||
self.interval = timedelta(seconds=interval)
|
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
|
||||||
|
|
||||||
@property
|
# Cache the time of the next occurence
|
||||||
def current(self):
|
self.next_occur = datetime.now(timezone.utc) + timedelta(seconds=offset) + self.interval
|
||||||
"""Return the date of the near check"""
|
|
||||||
if self.times != 0:
|
|
||||||
if self._end is None:
|
|
||||||
self._end = datetime.now(timezone.utc) + self.offset + self.interval
|
|
||||||
return self._end
|
|
||||||
return None
|
|
||||||
|
|
||||||
@property
|
|
||||||
def next(self):
|
|
||||||
"""Return the date of the next check"""
|
|
||||||
if self.times != 0:
|
|
||||||
if self._end is None:
|
|
||||||
return self.current
|
|
||||||
elif self._end < datetime.now(timezone.utc):
|
|
||||||
self._end += self.interval
|
|
||||||
return self._end
|
|
||||||
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:
|
|
||||||
return self.current - datetime.now(timezone.utc)
|
return self.next_occur - datetime.now(timezone.utc)
|
||||||
return 99999 # TODO: 99999 is not a valid time to return
|
|
||||||
|
|
||||||
def check(self):
|
def check(self):
|
||||||
"""Run a check and realized the event if this is time"""
|
"""Run a check and realized the event if this is time"""
|
||||||
|
|
||||||
|
self.max_attempt -= 1
|
||||||
|
|
||||||
# Get initial data
|
# Get initial data
|
||||||
if self.func is None:
|
if not callable(self.func):
|
||||||
d_init = self.func_data
|
d_init = self.func_data
|
||||||
elif self.func_data is None:
|
elif self.func_data is None:
|
||||||
d_init = self.func()
|
d_init = self.func()
|
||||||
@ -113,7 +100,7 @@ class ModuleEvent:
|
|||||||
d_init = self.func(self.func_data)
|
d_init = self.func(self.func_data)
|
||||||
|
|
||||||
# then compare with current data
|
# then compare with current data
|
||||||
if self.cmp is None:
|
if not callable(self.cmp):
|
||||||
if self.cmp_data is None:
|
if self.cmp_data is None:
|
||||||
rlz = True
|
rlz = True
|
||||||
else:
|
else:
|
||||||
@ -138,3 +125,19 @@ class ModuleEvent:
|
|||||||
self.call(d_init, **self.call_data)
|
self.call(d_init, **self.call_data)
|
||||||
else:
|
else:
|
||||||
self.call(d_init, self.call_data)
|
self.call(d_init, self.call_data)
|
||||||
|
|
||||||
|
# Is it finished?
|
||||||
|
if self.times == 0 or self.max_attempt == 0:
|
||||||
|
if not callable(self.end_call):
|
||||||
|
pass # TODO: log a WARN here
|
||||||
|
else:
|
||||||
|
if self.end_data is None:
|
||||||
|
self.end_call()
|
||||||
|
elif isinstance(self.end_data, dict):
|
||||||
|
self.end_call(**self.end_data)
|
||||||
|
else:
|
||||||
|
self.end_call(self.end_data)
|
||||||
|
|
||||||
|
# Not finished, ready to next one!
|
||||||
|
else:
|
||||||
|
self.next_occur += self.interval
|
||||||
|
@ -51,7 +51,7 @@ class ModuleContext:
|
|||||||
self.config = ModuleState("module")
|
self.config = ModuleState("module")
|
||||||
|
|
||||||
self.hooks = list()
|
self.hooks = list()
|
||||||
self.events = list()
|
self.events = list() # Un eventManager, qui contient une liste globale et un thread global et quelques méthodes statique, mais chaque événement est exécuté dans son propre contexte :)
|
||||||
self.debug = context.verbosity > 0 if context is not None else False
|
self.debug = context.verbosity > 0 if context is not None else False
|
||||||
|
|
||||||
# Define some callbacks
|
# Define some callbacks
|
||||||
@ -79,7 +79,9 @@ class ModuleContext:
|
|||||||
def subtreat(msg):
|
def subtreat(msg):
|
||||||
yield from context.treater.treat_msg(msg)
|
yield from context.treater.treat_msg(msg)
|
||||||
def add_event(evt, eid=None):
|
def add_event(evt, eid=None):
|
||||||
return context.add_event(evt, eid, module_src=module)
|
eid = context.add_event(evt, eid, module_src=module)
|
||||||
|
self.events.append(eid)
|
||||||
|
return eid
|
||||||
def del_event(evt):
|
def del_event(evt):
|
||||||
return context.del_event(evt, module_src=module)
|
return context.del_event(evt, module_src=module)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user