Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
UdderWeb Team
committed
Jan 26, 2012
1 parent
2ce137b
commit 6101115
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from Hook import * | ||
parser = None | ||
from Logging import LogFile | ||
log = LogFile("Alarm") | ||
import time | ||
import datetime | ||
parseStrings = [ | ||
"%m/%d", | ||
"%m/%d/%y", | ||
"%m/%d/%y %H:%M:%S", | ||
"%m/%d/%y %h:%M:%S %p" | ||
] | ||
def backupParse(s): | ||
global parseStrings | ||
for fmt in parseStrings: | ||
try: | ||
return datetime.datetime.strptime(s,fmt) | ||
except:pass | ||
return None | ||
|
||
try: | ||
import parsedatetime.parsedatetime as pdt | ||
import parsedatetime.parsedatetime_consts as pdc | ||
c = pdc.Constants() | ||
p = pdt.Calendar(c) | ||
parser = lambda s:datetime.datetime(*p.parse(s)[0][:6]) | ||
log.debug("Using parsedatetime") | ||
except: | ||
parser = backupParse | ||
log.debug("Using backup parser") | ||
|
||
@requires("IRCArgs") | ||
class Alarm: | ||
alarms = [] | ||
@dedicated() | ||
def ded(self,response): | ||
time.sleep(1) | ||
if len(self.alarms)==0: | ||
return | ||
log.debug("Alarms exist. Checking time.") | ||
now = datetime.datetime.now() | ||
curAlarm = self.alarms[0] | ||
log.debug("Next alarm:",curAlarm) | ||
log.debug(curAlarm[0] > now) | ||
while curAlarm[0] < now: | ||
yield response.msg(curAlarm[1],curAlarm[2]) | ||
self.alarms.pop(0) | ||
curAlarm = self.alarms[0] | ||
|
||
|
||
@bindFunction(message="!alarm (?P<when>.*):(?P<what>.*)") | ||
def sched(self,response,target,when,what): | ||
log.debug("Scheduler triggered.") | ||
dt = parser(when) | ||
if not dt: | ||
log.warning("Invalid time.",when) | ||
return response.msg(target,"Sorry, I couldn't figure out when \"%s\" is."%when) | ||
log.debug("Scheduled alarm:",when,target,what) | ||
self.alarms.append((dt,target,what)) | ||
self.alarms.sort() | ||
|
||
|
||
|
||
|