def text_to_absolute_time(self): import time dtime = datetime.today() self.assertEquals(utils.text_to_absolute_time("3:32:08 AM"), datetime.replace(hour=3, minute=32, second=8)) self.assertEquals(utils.text_to_absolute_time("3:32:08 p.m."), datetime.replace(hour=15, minute=32, second=8)) self.assertEquals(utils.text_to_absolute_time("5:34 pm"), datetime.replace(hour=17, minute=34, second=0))
def execute(self, **kwargs): """Schedule a command to be run at a later time. Keyword arguments: command -- the command (in natural language) to be run in -- a relative time offset (hours, minutes, and/or seconds) at -- an absolute time to run the command """ expire = 0 if "command" not in kwargs: raise plugin.UnsuccessfulExecution("No command provided.") if "in" in kwargs: try: idx = kwargs["in"].rfind(" ") wait = kwargs["in"][:idx] scale = kwargs["in"][idx + 1:] except ValueError: raise plugin.UnsuccessfulExecution("Error parsing time.") try: expire = int(wait) except ValueError: try: expire = utils.text_to_int(wait) except: raise plugin.UnsuccessfulExecution("Error parsing time.") if scale.startswith("h"): expire = datetime.datetime.now() + datetime.timedelta( hours=expire) elif scale.startswith("m"): expire = datetime.datetime.now() + datetime.timedelta( minutes=expire) elif scale.startswith("s"): expire = datetime.datetime.now() + datetime.timedelta( seconds=expire) else: raise plugin.UnsuccessfulExecution( "Could not determine time scale (h/m/s).") elif "at" in kwargs: expire = utils.text_to_absolute_time(wait) if not expire: raise plugin.UnsuccessfulExecution("Error parsing time.") else: raise plugin.UnsuccessfulExecution( 'Command could not be scheduled. Must specify "in" or "at"') cmd, namespace, args = self.registrar.find_best_service( kwargs["command"]) if cmd is None: raise plugin.UnsuccessfulExecution("Provided text could not be " "converted into a command.") self.queue.put((expire, (cmd, namespace, args))) self.event.set() return 'Command scheduled.'
def execute(self, **kwargs): """Schedule a command to be run at a later time. Keyword arguments: command -- the command (in natural language) to be run in -- a relative time offset (hours, minutes, and/or seconds) at -- an absolute time to run the command """ expire = 0 if "command" not in kwargs: raise plugin.UnsuccessfulExecution("No command provided.") if "in" in kwargs: try: idx = kwargs["in"].rfind(" ") wait = kwargs["in"][:idx] scale = kwargs["in"][idx + 1:] except ValueError: raise plugin.UnsuccessfulExecution("Error parsing time.") try: expire = int(wait) except ValueError: try: expire = utils.text_to_int(wait) except: raise plugin.UnsuccessfulExecution("Error parsing time.") if scale.startswith("h"): expire = datetime.datetime.now() + datetime.timedelta(hours=expire) elif scale.startswith("m"): expire = datetime.datetime.now() + datetime.timedelta(minutes=expire) elif scale.startswith("s"): expire = datetime.datetime.now() + datetime.timedelta(seconds=expire) else: raise plugin.UnsuccessfulExecution( "Could not determine time scale (h/m/s).") elif "at" in kwargs: expire = utils.text_to_absolute_time(wait) if not expire: raise plugin.UnsuccessfulExecution("Error parsing time.") else: raise plugin.UnsuccessfulExecution( 'Command could not be scheduled. Must specify "in" or "at"') cmd, namespace, args = self.registrar.find_best_service(kwargs["command"]) if cmd is None: raise plugin.UnsuccessfulExecution("Provided text could not be " "converted into a command.") self.queue.put((expire, (cmd, namespace, args))) self.event.set() return 'Command scheduled.'