Fix events (the first event was always skiped)

This commit is contained in:
Némunaire 2012-09-01 15:49:30 +02:00
parent d394ff5784
commit 387ecc7d5c
3 changed files with 49 additions and 29 deletions

56
bot.py
View File

@ -50,7 +50,7 @@ class Bot:
self.hooks = hooks.MessagesHook(self) self.hooks = hooks.MessagesHook(self)
# Other known bots, making a bots network # Other known bots, making a bots network
self.network = dict() self.network = dict()
self.hooks_cache = dict() self.hooks_cache = dict()
# Messages to be treated # Messages to be treated
@ -61,24 +61,33 @@ class Bot:
def add_event(self, evt): def add_event(self, evt):
"""Register an event and return its identifiant for futur update""" """Register an event and return its identifiant for futur update"""
# Find ID # Find an ID
now = datetime.now() now = datetime.now()
evt.id = "%d%c%d%d%c%d%d%c%d" % (now.year, ID_letters[now.microsecond % 52], now.month, now.day, ID_letters[now.microsecond % 42], now.hour, now.minute, ID_letters[now.microsecond % 32], now.second) evt.id = "%d%c%d%d%c%d%d%c%d" % (now.year, ID_letters[now.microsecond % 52],
now.month, now.day, ID_letters[now.microsecond % 42],
now.hour, now.minute, ID_letters[now.microsecond % 32],
now.second)
# Add the event in place # Add the event in place
t = evt.current t = evt.current
i = 0 i = -1
for i in range(0, len(self.events)): for i in range(0, len(self.events)):
if self.events[i].current > t: if self.events[i].current > t:
i -= 1
break break
self.events.insert(i, evt) self.events.insert(i + 1, evt)
if i == 0: if i == -1:
self.update_timer() self.update_timer()
return evt.id return evt.id
def del_event(self, id): def del_event(self, id):
"""Find and remove an event from list""" """Find and remove an event from list"""
if len(self.events) > 0 and id == self.events[0].id:
self.events.remove(self.events[0])
self.update_timer()
return True
for evt in self.events: for evt in self.events:
if evt.id == id: if evt.id == id:
self.events.remove(evt) self.events.remove(evt)
@ -157,7 +166,7 @@ class Bot:
if name in self.modules: if name in self.modules:
self.modules[name].save() self.modules[name].save()
if hasattr(self.modules[name], "unload"): if hasattr(self.modules[name], "unload"):
self.modules[name].unload() self.modules[name].unload(self)
# Remove registered hooks # Remove registered hooks
for (s, h) in self.modules[name].REGISTERED_HOOKS: for (s, h) in self.modules[name].REGISTERED_HOOKS:
self.hooks.del_hook(s, h) self.hooks.del_hook(s, h)
@ -384,8 +393,14 @@ def hotswap(bak):
def reload(): def reload():
import imp import imp
import prompt.builtins import channel
imp.reload(prompt.builtins) imp.reload(channel)
import consumer
imp.reload(consumer)
import DCC
imp.reload(DCC)
import event import event
imp.reload(event) imp.reload(event)
@ -393,22 +408,19 @@ def reload():
import hooks import hooks
imp.reload(hooks) imp.reload(hooks)
import xmlparser
imp.reload(xmlparser)
import xmlparser.node
imp.reload(xmlparser.node)
import importer import importer
imp.reload(importer) imp.reload(importer)
import message
imp.reload(message)
import prompt.builtins
imp.reload(prompt.builtins)
import server import server
imp.reload(server) imp.reload(server)
import channel import xmlparser
imp.reload(channel) imp.reload(xmlparser)
import xmlparser.node
import DCC imp.reload(xmlparser.node)
imp.reload(DCC)
import message
imp.reload(message)

View File

@ -37,6 +37,8 @@ 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 self.offset = timedelta(seconds=offset) # Time to wait before the first check
self.intervalle = timedelta(seconds=intervalle) self.intervalle = timedelta(seconds=intervalle)
@ -92,7 +94,10 @@ class ModuleEvent:
#print ("do test with", d, self.cmp_data) #print ("do test with", d, self.cmp_data)
if self.check is None: if self.check is None:
r = True if self.cmp_data is None:
r = True
else:
r = d != self.cmp_data
elif self.cmp_data is None: elif self.cmp_data is None:
r = self.check(d) r = self.check(d)
elif isinstance(self.cmp_data, dict): elif isinstance(self.cmp_data, dict):
@ -103,8 +108,11 @@ class ModuleEvent:
if r: if r:
self.times -= 1 self.times -= 1
if self.call_data is None: if self.call_data is None:
self.call() if d is None:
self.call()
else:
self.call(d)
elif isinstance(self.call_data, dict): elif isinstance(self.call_data, dict):
self.call(**self.call_data) self.call(d, **self.call_data)
else: else:
self.call(self.call_data) self.call(d, self.call_data)

View File

@ -147,7 +147,7 @@ class ModuleLoader(SourceLoader):
module.name = fullname module.name = fullname
module.print = lambda msg: print("[%s] %s"%(module.name, msg)) module.print = lambda msg: print("[%s] %s"%(module.name, msg))
module.print_debug = lambda msg: mod_print_dbg(module, msg) module.print_debug = lambda msg: mod_print_dbg(module, msg)
module.send_response = lambda srv, res: mod_send_response(context, srv, res) module.send_response = lambda srv, res: mod_send_response(self.context, srv, res)
if not hasattr(module, "NODATA"): if not hasattr(module, "NODATA"):
module.DATAS = xmlparser.parse_file(self.context.datas_path module.DATAS = xmlparser.parse_file(self.context.datas_path
@ -236,7 +236,7 @@ def mod_print_dbg(mod, msg):
def mod_save(mod, datas_path): def mod_save(mod, datas_path):
mod.DATAS.save(datas_path + "/" + mod.name + ".xml") mod.DATAS.save(datas_path + "/" + mod.name + ".xml")
mod.print ("Saving!") mod.print_debug("Saving!")
def mod_has_access(mod, config, msg): def mod_has_access(mod, config, msg):
if config is not None and config.hasNode("channel"): if config is not None and config.hasNode("channel"):
@ -249,4 +249,4 @@ def mod_has_access(mod, config, msg):
return True return True
def mod_send_response(context, server, res): def mod_send_response(context, server, res):
context.servers[server].send_response(res) context.servers[server].send_response(res, None)