class countDown(commandHandler): def __init__(self): Cmd.__init__(self) self.__conflog() self.re_thing=re.compile(r"\"(.+?)\"") self.database=DoDataBase() self.handler("show") def check_update(self): for thing,date in self.database.items(): date,update_days = (date.split(':',1)+[None])[:2] if update_days and self.toTime('',date)[0] == 0: self.update_date(thing, datetime.datetime(*map(int,date.split('-'))), int(update_days)) # 传入date是一个datetime对象,并非xxxx-xx-xx def update_date(self,thing,date,update_days): while True: date=date+datetime.timedelta(days=update_days) if (date - datetime.datetime.now()).days + 1 >=0: break date=date.strftime("%Y-%m-%d") self.database.update(thing, date + ':' + str(update_days)) def __conflog(self): log_file="/var/log/countdown.log" self.logger=logging.getLogger("countDown") handler=logging.FileHandler(log_file) formatter=logging.Formatter("%(asctime)s %(message)s") handler.setFormatter(formatter) self.logger.addHandler(handler) self.logger.setLevel(logging.INFO) def do_show(self,line): self.check_update() self.listItem={} things=[self.toTime(x,y.split(":")[0])[1] for x,y in self.database.items()] for i,thing in enumerate(things): print "(%d) %s "%(i+1,thing) self.listItem[str(i+1)]=self.re_thing.findall(thing)[0] def toTime(self,thing,time): date=datetime.datetime(*map(int,time.split('-'))) nowDate=datetime.datetime.now() dates=(date-nowDate).days + 1 if dates>=0: line=1,"离\"%s\"还有 %d 天"%(thing,dates) else: line=0,"\"%s\"已经过去了 %d 天"%(thing,abs(dates)) return line def do_end(self,line): sys.exit(0)
def __init__(self): Cmd.__init__(self) self.__conflog() self.re_thing=re.compile(r"\"(.+?)\"") self.database=DoDataBase() self.handler("show")
def __init__(self): Cmd.__init__(self) self.__conflog() self.re_thing = re.compile(r"\"(.+?)\"") self.database = DoDataBase() self.handler("show")
class countDown(commandHandler): def __init__(self): Cmd.__init__(self) self.__conflog() self.re_thing = re.compile(r"\"(.+?)\"") self.database = DoDataBase() self.handler("show") def check_update(self): for thing, date in self.database.items(): date, update_days = (date.split(':', 1) + [None])[:2] if update_days and self.toTime('', date)[0] == 0: self.update_date(thing, datetime.datetime(*map(int, date.split('-'))), int(update_days)) # 传入date是一个datetime对象,并非xxxx-xx-xx def update_date(self, thing, date, update_days): while True: date = date + datetime.timedelta(days=update_days) if (date - datetime.datetime.now()).days + 1 >= 0: break date = date.strftime("%Y-%m-%d") self.database.update(thing, date + ':' + str(update_days)) def __conflog(self): log_file = "/var/log/countdown.log" self.logger = logging.getLogger("countDown") handler = logging.FileHandler(log_file) formatter = logging.Formatter("%(asctime)s %(message)s") handler.setFormatter(formatter) self.logger.addHandler(handler) self.logger.setLevel(logging.INFO) def do_show(self, line): self.check_update() self.listItem = {} things = [ self.toTime(x, y.split(":")[0])[1] for x, y in self.database.items() ] for i, thing in enumerate(things): print "(%d) %s " % (i + 1, thing) self.listItem[str(i + 1)] = self.re_thing.findall(thing)[0] def toTime(self, thing, time): date = datetime.datetime(*map(int, time.split('-'))) nowDate = datetime.datetime.now() dates = (date - nowDate).days + 1 if dates >= 0: line = 1, "离\"%s\"还有 %d 天" % (thing, dates) else: line = 0, "\"%s\"已经过去了 %d 天" % (thing, abs(dates)) return line def do_end(self, line): sys.exit(0)