1
0
Fork 0

New class imodule : interface for modules

This commit is contained in:
Némunaire 2012-05-20 09:44:07 +02:00
parent 31be5808e5
commit adeb4959c8
3 changed files with 129 additions and 54 deletions

103
events.py
View File

@ -11,11 +11,9 @@ from xml.dom.minidom import parse
from xml.dom.minidom import parseString
from xml.dom.minidom import getDOMImplementation
filename = ""
EVENTS = {}
STREND = {}
threadManager = None
newStrendEvt = threading.Event()
import imodule
class Manager(threading.Thread):
def __init__(self, servers):
@ -44,16 +42,7 @@ class Manager(threading.Thread):
if closer is not None and closer.end is not None and closer.end > datetime.now():
timer.cancel()
def launch (servers):
global threadManager
stop()
threadManager = Manager(servers)
threadManager.start()
def stop ():
if threadManager is not None:
threadManager.stop = True
newStrendEvt.set()
class Strend:
def __init__(self, item):
@ -84,41 +73,61 @@ class Strend:
del STREND[self.name]
newStrendEvt.set()
def xmlparse(node):
"""Parse the given node and add events to the global list."""
for item in node.getElementsByTagName("strend"):
STREND[item.getAttribute("name")] = Strend(item)
for item in node.getElementsByTagName("event"):
if (item.hasAttribute("year")):
year = int(item.getAttribute("year"))
else:
year = 0
if (item.hasAttribute("month")):
month = int(item.getAttribute("month"))
else:
month = 0
if (item.hasAttribute("day")):
day = int(item.getAttribute("day"))
else:
day = 0
if (item.hasAttribute("hour")):
hour = int(item.getAttribute("hour"))
else:
hour = 0
if (item.hasAttribute("minute")):
minute = int(item.getAttribute("minute"))
else:
minute = 0
if (item.hasAttribute("second")):
second = int(item.getAttribute("second"))
else:
second = 0
if year == month == day == hour == minute == second == 0:
EVENTS[item.getAttribute("name")] = (None, item.getAttribute("before_after"), None)
else:
EVENTS[item.getAttribute("name")] = (datetime(year, month, day, hour, minute, second),item.getAttribute("msg_before"), item.getAttribute("msg_after"))
class Nemodule(imodule.ModuleBase):
filename = ""
events = dict()
strend = dict()
threadManager = None
newStrendEvt = threading.Event()
def launch (self, servers):
self.stop()
self.threadManager = Manager(servers)
self.threadManager.start()
def stop (self):
if self.threadManager is not None:
self.threadManager.stop = True
self.newStrendEvt.set()
def xmlparse(self, node):
"""Parse the given node and add events to the global list."""
for item in node.getElementsByTagName("strend"):
strend[item.getAttribute("name")] = Strend(item)
for item in node.getElementsByTagName("event"):
if (item.hasAttribute("year")):
year = int(item.getAttribute("year"))
else:
year = 0
if (item.hasAttribute("month")):
month = int(item.getAttribute("month"))
else:
month = 0
if (item.hasAttribute("day")):
day = int(item.getAttribute("day"))
else:
day = 0
if (item.hasAttribute("hour")):
hour = int(item.getAttribute("hour"))
else:
hour = 0
if (item.hasAttribute("minute")):
minute = int(item.getAttribute("minute"))
else:
minute = 0
if (item.hasAttribute("second")):
second = int(item.getAttribute("second"))
else:
second = 0
if year == month == day == hour == minute == second == 0:
events[item.getAttribute("name")] = (None, item.getAttribute("before_after"), None)
else:
events[item.getAttribute("name")] = (datetime(year, month, day, hour, minute, second),item.getAttribute("msg_before"), item.getAttribute("msg_after"))
def load_module(datas_path):

66
imodule.py Normal file
View File

@ -0,0 +1,66 @@
# coding=utf-8
class ModuleException(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class ModuleState:
def __init__(self, filename):
self.filename = filename
if not os.access(self.filename, os.R_OK):
with open(self.filename) as f:
for line in f:
print line
class ModuleBase:
def __init__(self, datas_path):
self.datas_path = datas_path
if self.module_name is None:
raise ModuleException("module_name not defined")
if self.conf is not None:
self.conf = self.datas_path + self.conf
if not os.access(self.conf, os.R_OK):
def print(self, message):
print ("[%s] %s" % (self.module_name, message))
def load(self):
return True
def save(self):
return True
def launch(self, servers):
return True
def stop(self):
return True
def help_tiny(self):
return None
def help_full(self):
return None
def parseanswer(self, msg):
return False
def parseask(self, msg):
return False
def parselisten(self, msg):
return False

14
qd.py
View File

@ -293,7 +293,7 @@ def parselisten (msg):
# if msg.channel == "#nemutest" and msg.sender not in DELAYED:
if msg.channel != "#nemutest" and msg.sender not in DELAYED:
if re.match("(42|quarante[- ]?deux).{,2}", msg.content.strip().lower()):
if re.match("^(42|quarante[- ]?deux).{,2}$", msg.content.strip().lower()):
if msg.time.minute == 10 and msg.time.second == 10 and msg.time.hour == 10:
getUser(msg.sender).playTen()
getUser(msg.sender).playGreat()
@ -304,7 +304,7 @@ def parselisten (msg):
else:
getUser(msg.sender).playBad()
if re.match("(23|vingt[ -]?trois).{,2}", msg.content.strip().lower()):
if re.match("^(23|vingt[ -]?trois).{,2}$", msg.content.strip().lower()):
if msg.time.minute == 23:
if msg.time.second == 0:
getUser(msg.sender).playGreat()
@ -312,7 +312,7 @@ def parselisten (msg):
else:
getUser(msg.sender).playBad()
if re.match("(10){3}.{,2}", msg.content.strip().lower()):
if re.match("^(10){3}.{,2}$", msg.content.strip().lower()):
if msg.time.minute == 10 and msg.time.hour == 10:
if msg.time.second == 10:
getUser(msg.sender).playGreat()
@ -320,13 +320,13 @@ def parselisten (msg):
else:
getUser(msg.sender).playBad()
if re.match("0?12345.{,2}", msg.content.strip().lower()):
if re.match("^0?12345.{,2}$", msg.content.strip().lower()):
if msg.time.hour == 1 and msg.time.minute == 23 and (msg.time.second == 45 or (msg.time.second == 46 and msg.time.microsecond < 330000)):
getUser(msg.sender).playSuite()
else:
getUser(msg.sender).playBad()
if re.match("[1l][e3]{2}[t7] ?time.{,2}", msg.content.strip().lower()):
if re.match("^[1l][e3]{2}[t7] ?time.{,2}$", msg.content.strip().lower()):
if msg.time.hour == 13 and msg.time.minute == 37:
if msg.time.second == 0:
getUser(msg.sender).playGreat()
@ -334,7 +334,7 @@ def parselisten (msg):
else:
getUser(msg.sender).playBad()
if re.match("(pi|3.14) ?time.{,2}", msg.content.strip().lower()):
if re.match("^(pi|3.14) ?time.{,2}$", msg.content.strip().lower()):
if msg.time.hour == 3 and msg.time.minute == 14:
if msg.time.second == 15 or msg.time.second == 16:
getUser(msg.sender).playGreat()
@ -342,7 +342,7 @@ def parselisten (msg):
else:
getUser(msg.sender).playBad()
if re.match("(404( ?time)?|time ?not ?found).{,2}", msg.content.strip().lower()):
if re.match("^(404( ?time)?|time ?not ?found).{,2}$", msg.content.strip().lower()):
if msg.time.hour == 4 and msg.time.minute == 4:
if msg.time.second == 0 or msg.time.second == 4:
getUser(msg.sender).playGreat()