Add a way to listen a module function for changes, ...

This commit is contained in:
Némunaire 2012-08-14 06:56:28 +02:00
parent 433f0977bf
commit 69f375e9f1
2 changed files with 71 additions and 1 deletions

7
bot.py
View File

@ -16,6 +16,7 @@
# 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/>.
import event
import hooks
from server import Server
@ -31,6 +32,8 @@ class Bot:
self.datas_path = './datas/'
self.hooks = hooks.MessagesHook()
self.events = list()
def addServer(self, node, nick, owner, realname):
"""Add a new server to the context"""
@ -87,7 +90,6 @@ class Bot:
if verb: print ("Save and unload all modules...")
k = list(self.modules.keys())
for mod in k:
print (mod)
self.unload_module(mod, verb)
if verb: print ("Close all servers connection...")
@ -105,6 +107,9 @@ def reload():
import prompt.builtins
imp.reload(prompt.builtins)
import event
imp.reload(event)
import hooks
imp.reload(hooks)

65
event.py Normal file
View File

@ -0,0 +1,65 @@
# -*- coding: utf-8 -*-
# Nemubot is a modulable IRC bot, built around XML configuration files.
# Copyright (C) 2012 Mercier Pierre-Olivier
#
# 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
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# 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/>.
import threading
class ModuleEvent:
def __init__(self, func, func_data, check, cmp_data, intervalle=60,
call=None, call_data=None, times=1):
# What have we to check?
self.func = func
self.func_data = func_data
# How detect a change?
self.check = check
if cmp_data is not None:
self.cmp_data = cmp_data
else:
self.cmp_data = self.func(self.func_data)
self.intervalle = intervalle
# What should we call when
self.call = call
if call_data is not None:
self.call_data = call_data
else:
self.call_data = func_data
# How many times do this event?
self.times = times
def launch_check(self):
d = self.func(self.func_data)
#print ("do test with", d, self.cmp_data)
if self.check(d, self.cmp_data):
self.call(self.call_data)
self.times -= 1
self.run()
def run(self):
if self.times != 0:
#print ("run timer")
self.timer = threading.Timer(self.intervalle, self.launch_check)
self.timer.start()
#else:
#print ("no more times")
def stop(self):
self.timer.cancel()